[For recording] Keras image system Part 2: Make judgment by CNN using your own data set

Thanks.

The previous article was about reading images with Keras and creating a dataset.

https://qiita.com/ku_a_i/items/7f71933f6b71cf30a3a8

This time, I will try to use that dataset to actually make a judgment by CNN. It is assumed that the folder structure is as follows.

python


'''
picture -╷- train -╷- Apple(Classification 1) -╷- **.png #For learning
         ╎         ╎               ╎  (Many images ... omitted)
         ╎         ╎               ╵- **.png
         ╎         ╵- Mango(Classification 2) - (Abbreviation)
         ╎         
         ╵-  val  -╷- Apple(Classification 1) - (Abbreviation) #For learning verification
                   ╵- Mango(Classification 2) - (Abbreviation)
'''

0. Last review. Creating a dataset

python


from keras.preprocessing.image import ImageDataGenerator

#Setting
classes = ['Apple', 'Mango']#If you want to learn to classify apples and mangoes
train_data_dir = './picture/train'#Specify the parent folder for classification(Apple,Upper folder of Mango)
val_data_dir = './picture/val'
img_height = 100
img_width = 100
batch_size = 16

#Create training data
#Divide by 255 and scale
train_datagen = ImageDataGenerator(rescale = 1.0 / 255) #You can also set padding, but this time it will be omitted.

#Set the learning generator.
#Roughly speaking, generator is every time(Here for each batch size)To generate an image
train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size = (img_height, img_width), #No dimensions required
    color_mode = 'rgb', #If gray,'grayscale'Enter
    classes = classes, 
    class_mode = 'binary', #Since there are two, it is binary. If there are 3 or more'categorical'
    batch_size = batch_size,
    shuffle = True)


#Create validation data
val_datagen = ImageDataGenerator(rescale = 1.0 / 255)

#Set the validation generator.
val_generator = val_datagen.flow_from_directory(
    val_data_dir,
    target_size = (img_height, img_width),
    color_mode = 'rgb',
    classes = classes, 
    class_mode = 'binary',
    batch_size = batch_size,
    shuffle = True)

Now that we have a dataset, let's create a CNN to insert this data.

1. Creating a CNN

There are several types of CNN. Simply put ① Design from 0 by yourself and create a layer (2) Use a model that someone in the world has already tuned for learning.

These two are roughly classified. There are already many easy-to-understand articles on Qiita for both ① and ②, so this time I will try it with ②. I would like to tell you that CNN learning using Keras generator is the first.

python


#Load VGG16

from keras.applications.vgg16 import VGG16
from keras.optimizers import Adam
from keras.layers import Input, Dense, Flatten, Dropout
from keras.models import Sequential
from keras.models import Model

input_tensor = Input(shape=(img_width, img_height, 3))

#The road part of VGG. FC layer is not required, so include_top=Set to False
vgg16 = VGG16(include_top=False, weights='imagenet', input_tensor=input_tensor)

#Design the FC layer that you no longer need
model = Sequential()
model.add(Flatten(input_shape=vgg16.output_shape[1:]))
model.add(Dense(256, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

#Create a model by combining VGG16 and your own FC layer
vgg16_model = Model(input=vgg16.input, output=model(vgg16.output))

#Freeze the layer just before the last conv layer(Freeze: Omit from learning)
for layer in vgg16_model.layers[:15]:
    layer.trainble=False
    
#compile
vgg16_model.compile(loss='binary_crossentropy',
                 optimizer=Adam(lr=1e-4),
                 metrics=['accuracy'])

This completes the design of the CNN used for learning. All you have to do is learn. Before that, let's take a look at the big picture of CNN

python


vgg16_model.summary()

キャプチャ.PNG

It's quite luxurious, but don't worry about it. ..

By the way, the part I made is a sequential part, but there are no details. Let's check it below

python


model.summary()

キャプチャ4.PNG

Now all layers are clear.

2. Let's learn with CNN

Now that I have a complete picture, it's time to start learning.

python


#Enter the total number of images *.Convenient because you can see the total number of sheets with n
steps_per_epoch = train_generator.n
validation_steps = val_generator.n
#Setting the number of Epoch. Since there is Early Stopping described later, you can set it larger.
nb_epoch = 1000

#Callback settings
from keras.callbacks import EarlyStopping,ModelCheckpoint
es = EarlyStopping(monitor='val_loss', patience=10, verbose=1)#Only patience numbers val_Automatic stop if loss does not update
cp = ModelCheckpoint(filepath = './model.hdf5', save_best_only=True)#save_best_Save model when updating loss with only

#CNN learning started. fit_Use generator.
history = vgg16_model.fit_generator(
    train_generator,
    steps_per_epoch = steps_per_epoch // batch_size,
    epochs = nb_epoch,
    verbose=1,
    validation_data=val_generator,
    validation_steps = validation_steps // batch_size,
    callbacks=[es, cp])

After learning, let's plot and display the state of learning

python


acc = histry.history['acc']
val_acc = histry.history['val_acc']
loss = histry.history['loss']
val_loss = histry.history['val_loss']

epochs = range(1, len(acc) + 1)

plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()

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()

キャプチャ1.PNG

3. Let's judge using the trained model

And let's judge another image with the completed learning model Since I learned how to load an image last time, let's load it with load_img and judge one image.

python


import numpy as np
from keras.preprocessing.image import load_img,img_to_array

img_shape = (100, 100, 3) 
img = load_img('B.jpg', grayscale=False, target_size=img_shape)

#Convert to numpy array type for inclusion in neural network
x_test = img_to_array(img) / 255

#With reshape to put in CNN(n, w, h, c)I'll make it into a mold
#Since a small number from 0 to 1 is output, np.I'll change the output to a class label with round(0or1)
print(np.round(vgg16_model.predict(x_test.reshape(1,100,100,3))))

You should see 0 or 1 as a result. Since you can check each classification with print (train_generator.class_indices) If the learning is successful, 0 should be Apple this time and 1 should be Mango.

Well, this time I will make it like this. How was that? I don't think it was that difficult. Next time, I'm thinking of doing image restoration with an autoencoder or classifying versions of 3 or more.

In addition, since we prioritized the release, we will look back on it later and supplement / correct any strange or insufficient explanations. ..

Recommended Posts

[For recording] Keras image system Part 2: Make judgment by CNN using your own data set
[For recording] Keras image system Part 1: How to create your own data set?
Let's make an image recognition model with your own data and play!
Make your own PC for deep learning
[Machine learning] Create a machine learning model by performing transfer learning with your own data set
Make predictions using regression on actual data ~ part1