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
(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.
· 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.
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?
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.
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
I turned 30 epoch on the CPU. Even if you don't use GPU, it's fast if it's about 4 layers.
loss
accuracy
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!
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.
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