Aidemy 2020/10/4
Hello, it is Yope! I am a liberal arts student, but I was interested in the possibilities of AI, so I went to the AI-specialized school "Aidemy" to study. I would like to share the knowledge gained here with you, and I am summarizing it on Qiita. I am very happy that many people have read the previous summary article. Thank you! This time, we will post a memo for gender recognition. Nice to meet you.
・ Collect data and cleanse data. -Data collection is not built-in by Keras, but downloads and collects images online. -For data cleansing, use OpenCV that handles images.
-The channel of the image data of men and women handled this time is in the order of "b, g, r", but in OpenCV that handles images, the data must be in the order of "r, g, b", so cv2.split It is necessary to divide the data into three with cv2.merge and arrange the order of the data array with cv2.merge.
img = cv2.imread('./6100_gender_recognition_data/male/Aaron_Eckhart_0001.jpg')
b,g,r=cv2.split(img)
img=cv2.merge([r,g,b])
-Use __cv2.resize (image, (vertical, horizontal)) __. After saving with cv2.imwrite (), get the reduced image with plt.imread as a figure and output it with plt.imshow ().
import cv2
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread("./6100_gender_recognition_data/male/Aaron_Eckhart_0001.jpg ")
my_img=cv2.resize(img, (50, 50))
cv2.imwrite("resize.img",my_img)
img=plt.imread("resize.img")
plt.imshow(img)
plt.show()
-When changing from color (red, green, blue) to monochrome (white, black), set cv2.COLOR_RGB2GRAY in the second argument of __cv2.cvtColor (image) __. This is often done when working with images. -When outputting the converted image with plt.show (), the first argument will be recognized as "green" if it is left as it is, so it is necessary to describe it as __plt.gray () __.
・ Create a model using CNN (+ Keras + TensorFlow). In addition, transfer learning is performed using VGG16.
-(Review) Create a Sequential () instance and add a layer with add. ・ (Review) Definition of each layer -Fully connected layer: __Dense (number of units, input_dim = number of dimensions of data) __ ・ Convolutional layer: __Conv2D (filters, kernel_size, stripes, padding) __ -Pooling layer: __MaxPooling2D (pool_size, stripes, padding) __ ・ Flattening layer: __Flatten () __ -Activation function: model.add (Activation ("sigmoid"))
・ (Review) Compile with compile, learn with fit, predict with np.argmax (model.predict ()). ・ __Model.compile (optimizer, loss, metrics) __ ・ __Model.fit (learning data, teacher label, batch_size, epochs) __
・ (Review) Transfer learning using VGG16
#Input shape settings
input_tensor = Input(shape=(32, 32, 3))
#VGG model settings
vgg16 = VGG16(include_top=False,weights='imagenet',input_tensor=input_tensor)
#Combining VGG model with new layer
model = Model(inputs=vgg16.input, outputs=top_model(vgg16.output))
#Fixed number of layers and weights for VGG model
for layer in model.layers[:15]:
layer.trainable = False
Recommended Posts