765 I tried to identify the three professional families by CNN (with Chainer 2.0.0)

At the deep learning study session in the laboratory, I decided to try something, so I tried to identify the original three families of Idolmaster by CNN.

The result is like this

out4.png (I borrowed this image from http://idolmaster.jp/images/event/9th/goods/img_goods37.jpg. If there is a problem, I will delete it.)

I thought about it and it took about 10 hours. I am grateful to the code published by various people.

environment

· MacOS X El Capitan ・ Python 3.5.2 ・ OpenCV 3.1.0 ・ Anaconda3-4.2.0 ・ Chainer 2.0.0 ・ PIL Such

If you put OpenCV in conda and put Chainer or something in it, it's easy.

data set

Download google image with python3 I borrowed this person's code and collected about 100 images of each of the three people.

I also collected other images for other purposes.

"100 sheets? Little!" You might think, but this time it worked. Is it because the features are easy to understand?

Face cutout

Estimate who's face using OpenCV (Eigenface, Fisherface, LBPH) Based on this code, I cut it out and saved it.

The size is 32x32.

The change is lbpcascade_animeface.xml for anime face detection by OpenCV The "lbpcascade_animeface.xml" distributed in is used for the feature detector and the color can be saved.

kao.py


#!/usr/bin/python
# -*- coding: utf-8 -*-

import cv2, os
import numpy as np
from PIL import Image

#The original image
train_path = 'Original image folder path'

#Anime face feature classifier
cascadePath = "lbpcascade_animeface.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)

#Get the image in the specified path
def get_images_and_labels(path):
    #Array to store images
    images = []
    #Array to store labels
    labels = []
    #Array to store file names
    files = []
    i = 0
    for f in os.listdir(path):
        #Image path
        image_path = os.path.join(path, f)
        #Load image
        image_pil = Image.open(image_path)
        #Stored in an array of NumPy
        image = np.array(image_pil, 'uint8')
        #Detect faces with an animated face feature classifier
        faces = faceCascade.detectMultiScale(image)
        #Processing of detected face image
        for (x, y, w, h) in faces:
            #Resize face to 32x32 size
            roi = cv2.resize(image[y: y + h, x: x + w], (32, 32), interpolation=cv2.INTER_LINEAR)
            #Store images in an array
            images.append(roi)
            #Store filenames in an array
            files.append(f)
            save_path = './Export folder path/' + str(i) + '.jpg'
            #If you save it as it is, it will be bluish (not RGB)
            cv2.imwrite(save_path, roi[:, :, ::-1].copy())
            print(i)

            i+=1

    return images

images = get_images_and_labels(train_path)

#End processing
cv2.destroyAllWindows()

It worked without padding the data.

Learning

python: Use chainer to recognize Bakemonogatari characters! ~ Part5 Multi-value classification by major characters (applicable to unknown data) ~ Based on this article, I rewrote it to run with chainer 2.0.0.

The point is

    train = tuple_dataset.TupleDataset(X_train, y_train)
    test = tuple_dataset.TupleDataset(X_test, y_test)

    train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
    test_iter = chainer.iterators.SerialIterator(test, args.batchsize,
                                                 repeat=False, shuffle=False)

    # Set up a trainer
    updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu)
    trainer = training.Trainer(updater, (args.epoch, 'epoch'), out="output")

    # Evaluate the model with the test dataset for each epoch
    trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu))

By replacing the "learning and testing" of the original code, trainer and updater run. later

model = L.Classifier(clf_bake())

I changed the layer to L.Convolution2D, and replaced def forward with def call.

The model looks like this when visualized

cg.png

Learning results

I turned 30 epoch on the CPU. Even if you don't use GPU, it's fast if it's about 4 layers. スクリーンショット 2017-07-02 13.45.10.png

loss loss.png

accuracy accuracy.png

Even though there are about 100 data sets, 85% of val / accuracy is quite amazing. I was surprised when I turned it for the first time.

CNN is amazing!

Unknown data estimation

Most of them also use python: chainer to recognize Bakemonogatari characters! ~ Part5 Multi-value classification by main characters (applicable to unknown data) ~ is based on the article.

The point is to load the model

model = L.Classifier(clf_bake())

chainer.serializers.load_npz("output/model_snapshot_500", model) 

Using Classifier like

The return value of the recognition function

def recognition(image, faces):

(Omission)

return model.predictor(face_images) , image

Is to be.

Impressions

It's fun because you can see the results immediately. It's fun to prepare your own data set and try it.

Let's try more classes this time.

Recommended Posts

765 I tried to identify the three professional families by CNN (with Chainer 2.0.0)
I tried to learn the sin function with chainer
I tried to identify the language using CNN + Melspectogram
I tried to save the data with discord
I tried to learn the angle from sin and cos with chainer
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to approximate the sin function using chainer
I tried to solve the problem with Python Vol.1
I tried to analyze the whole novel "Weathering with You" ☔️
I tried to find the average of the sequence with TensorFlow
I tried to notify the train delay information with LINE Notify
I tried to classify MNIST by GNN (with PyTorch geometric)
I tried to implement ListNet of rank learning with Chainer
I tried to implement Harry Potter sort hat with CNN
I tried to divide the file into folders with Python
I tried to approximate the sin function using chainer (re-challenge)
I tried to make Othello AI that I learned 7.2 million hands by deep learning with Chainer
I tried to move the ball
I tried to predict the sales of game software with VARISTA by referring to the article of Codexa
I tried to estimate the interval.
I tried to describe the traffic in real time with WebSocket
I tried to solve the ant book beginner's edition with python
I tried to automate the watering of the planter with Raspberry Pi
I tried to visualize the Beverage Preference Dataset by tensor decomposition.
I tried to process the image in "sketch style" with OpenCV
I tried to get started with Bitcoin Systre on the weekend
I tried to implement sentence classification by Self Attention with PyTorch
I tried to summarize the commands used by beginner engineers today
I tried to predict by letting RNN learn the sine wave
I tried to process the image in "pencil style" with OpenCV
I tried to expand the size of the logical volume with LVM
I tried running the DNN part of OpenPose with Chainer CPU
I tried to solve the shift scheduling problem by various methods
I tried to improve the efficiency of daily work with Python
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 1
Stock price plummeted with "new corona"? I tried to get the Nikkei Stock Average by web scraping
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 2
I tried to implement Autoencoder with TensorFlow
I tried to summarize the umask command
I tried to visualize AutoEncoder with TensorFlow
I tried to recognize the wake word
I tried to get started with Hy
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
I tried to implement CVAE with PyTorch
I tried playing with the image with Pillow
I tried to solve TSP with QAOA
I tried CNN fine tuning with Resnet
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
I tried moving the image to the specified folder by right-clicking and left-clicking
I tried to communicate with a remote server by Socket communication with Python.
I tried to express sadness and joy with the stable marriage problem.
I tried to get the authentication code of Qiita API with Python.
I tried to automatically extract the movements of PES players with software
I tried to find the optimal path of the dreamland by (quantum) annealing
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
I tried to verify and analyze the acceleration of Python by Cython
I tried to analyze the negativeness of Nono Morikubo. [Compare with Posipa]