Previous articles ( Scraping , Image processing with OpenCV ) introduced how to create your own dataset. In this article, I would like to introduce how to create a machine learning model that uses the created data set to perform transfer learning and judge Japanese and foreigners.
Prepare the data set required to create a machine learning model ← Contents up to the previous time ↓ ---------- What to do in this article from here ---------- ↓ Create a machine learning model using transfer learning. ↓ Use machine learning models to determine photos of Japanese and foreigners.
In a nutshell, transfer learning is a model learning method used to improve the performance of machine learning models in a short period of time. In general, the performance (accuracy) of a machine learning model is better if the layer of the machine learning model is deep and wide. However, building such a deep and wide machine learning model from scratch requires a huge amount of time and data. Therefore, transfer learning is to use the part other than the fully connected layer of the existing high-performance machine learning model (VGG16 etc.) as the feature extraction layer, and then build and train the part of the fully connected layer by yourself. .. As a result, compared to building a machine learning model from scratch, transfer learning requires only learning the fully connected layer, so it is possible to build a machine learning model with good performance in a short time. Source: "What is transfer learning? How do you do the expected" transfer learning "in deep learning?
The source code used this time is shown below.
cnn.py
from keras.layers import Dense, Dropout, Flatten, Activation
from keras.layers import Conv2D, MaxPooling2D, Input, BatchNormalization
from keras.models import Sequential, load_model, Model
from keras.applications.vgg16 import VGG16
from keras.optimizers import SGD
from keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
epochs = 10
#Plot accuracy and loss for each epoch on a graph
def show_graph(history):
# Setting Parameters
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
# 1) Accracy Plt
plt.plot(epochs, acc, 'bo', label='training acc')
plt.plot(epochs, val_acc, 'b', label='validation acc')
plt.title('Training and Validation acc')
plt.legend()
plt.figure()
# 2) Loss Plt
plt.plot(epochs, loss, 'bo', label='training loss')
plt.plot(epochs, val_loss, 'b', label='validation loss')
plt.title('Training and Validation loss')
plt.legend()
plt.show()
#Get data from a dataset
(X_train, y_train, X_test, y_test) = np.load('Dataset PATH')
X_train = np.array(X_train)
X_train = X_train.astype('float32')
X_train /= 255
y_train = np.array(y_train)
X_test = np.array(X_test)
X_test = X_test.astype('float32')
X_test /= 255
y_test = np.array(y_test)
datagen = image.ImageDataGenerator(
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2
)
datagen.fit(X_train)
input_tensor = Input(shape=(64, 64, 3))
#Read VGG16 data
vgg16 = VGG16(include_top=False, weights='imagenet', input_tensor=input_tensor)
top_model = Sequential()
top_model.add(Flatten(input_shape=vgg16.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(BatchNormalization())
top_model.add(Dropout(0.5))
top_model.add(Dense(2, activation='softmax'))
#vgg16 and top_Concatenate models
model = Model(inputs=vgg16.input, outputs=top_model(vgg16.output))
#Fix the weights up to the 19th layer using the for statement
for layer in model.layers[:19]:
layer.trainable = False
model.summary()
model.compile(loss='categorical_crossentropy',
optimizer=SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])
history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=32),
steps_per_epoch=len(X_train)/32, epochs=epochs, validation_data=(X_test, y_test))
#Evaluation of accuracy
scores = model.evaluate(X_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
The data acquired from the dataset this time is (64 * 64) color image data and its label. X_train and X_test are image data, and y_train and y_test are labels. Looking at the contents of X_train,
print(X_train.shape)
print(X_train[0])
(1547, 64, 64, 3)
[[[ 36 40 50]
[ 40 46 59]
[ 57 64 82]
...
[114 120 124]
[161 155 152]
[141 118 109]]
...
[[203 146 115]
[210 154 123]
[182 128 95]
...
[249 250 248]
[251 243 241]
[228 212 213]]]
It looks like this. This means that X_train contains 1547 (64 * 64) color images. The value stored in X_train is an 8-bit unsigned integer type from 0 to 255. I divide this by 255 to make it a number that fits between 0 and 1 to reduce the learning cost.
If you check the y_train data in the same way,
print(y_train.shape)
print(y_train[0])
(1547, 2)
[0 1]
You can see that the same number of one-hot-vectors as X_train are stored.
Then load the VGG16 model. Please refer to Keras Applications documentation for loading the VGG16 model. This time, 3 arguments are set. include_top indicates whether or not to include the three fully connected layers on the output layer side of the network. Since we are doing transfer learning this time, we do not need the fully connected layer of VGG16. Therefore, it is set to False. weights determine the weight of VGG16. If it is None, it will be random, and if it is'imagenet', it will be the trained weight. input_tensor specifies the size of the input image.
top_model is the part of the fully connected layer that sticks after VGG16. VGG16 and top_model are integrated into a model called model. And this time, I want to use the weight of VGG16 as it is learned in imagenet, so I am writing the following code.
#Fix the weights up to the 19th layer using the for statement
for layer in model.layers[:19]:
layer.trainable = False
Even if you say that you build a machine learning model by transfer learning, if the specifications of the personal computer you are using are low, the number of data is large, or the number of epochs is large, learning will take time. In such a case, let's create a machine learning model using Google Colabratory. Google Colabratory is a jupyter notebook environment provided by Google. The biggest advantage of using this service is that it can be processed at high speed using the GPU. I think that the processing time will differ by about 10 times depending on the presence or absence of GPU. So, below are the steps required to execute the source code written above in Google Colaboratory.
First open Google Colaboratory, give it a name, and then change the settings to use the GPU. To enable the GPU, click the "Edit-> Notebook Settings" button just below the file name in the upper left. Then, the following screen will appear, so select "GPU" there. The notebook is now ready for GPU. Then type the following code to change the version of numpy.
pip install numpy==1.16.1
The reason for writing this code is to prevent errors when reading the dataset in the source code above. Then upload the dataset on your notebook. To upload, link Google Colaboratory and load the uploaded file to Google Drive. To make it work, type the following code.
from google.colab import drive
drive.mount('/content/gdrive')
When you enter the code, the area where you enter the URL and authentication code will appear. When you click the link and log in to Google Drive, the password for authentication will be displayed. Copy it and enter the authentication code to use the data on Google Drive. The directory structure on the notebook is (./gdrive/MyDrive/). The file saved in my Google Drive is saved there.
This time, we created a machine learning model by performing transfer learning using our own data set. Even an amateur can easily write code in machine learning, but there is a possibility that the understanding of the written code will be weakened by that amount, so I think it is important to look back on the code I wrote in this way. I will. Next time, I will use the machine learning model created this time to create a web application that distinguishes the faces of Japanese and foreigners.
・ Keras Documentatino-Application ・ How to use Google Colabratory ・ I tried using the free GPU environment of Google Colaboratory ・ What is transfer learning? How do you do the expected "transfer learning" in deep learning?
Recommended Posts