Last time Create an image recognition program that distinguishes monkeys, wild boars and crows using the created training data
https://keras.io/ja/
Keras is a high-level neural network library written in Python that can be run on TensorFlow, CNTK, and Theano.
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.utils import np_utils
import keras
import numpy as np
The general flow is as follows:
classes = ["monkey", "boar", "crow"]
num_classes = len(classes)
image_size = 50
def main():
X_train, X_test, y_train, y_test = np.load("./animal.npy", allow_pickle=True)
X_train = X_train.astype("float") / 256
X_test = X_test.astype("float") / 256
y_train = np_utils.to_categorical(y_train, num_classes)
y_test = np_utils.to_categorical(y_test, num_classes)
model = model_train(X_train, y_train)
model_eval(model, X_test, y_test)
-The reason why X_train
and X_test
are divided by 256 is normalization (the data is in the range of 0 to 1).
-To_categorical
has an array of[0, 1, 2]
[[1, 0, 0]
[0, 1, 0]
[0, 0, 1]]
So, convert to an array that contains 1 only in the index of the correct answer and 0 in other places.
Refer to the 34th line and after of the following code Reference: https://github.com/keras-team/keras/blob/master/examples/cifar10_cnn.py
def model_train(X, y):
model = Sequential()
model.add(Conv2D(32, (3,3), padding='same', input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3,3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(3))
model.add(Activation('softmax'))
opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
model.compile(loss='categorical_crossentropy',
optimizer=opt, metrics=['accuracy'])
model.fit(X, y, batch_size=32, epochs=100)
#Save model
model.save("./animal_cnn.h5")
return model
model = Sequential()
Create a model object
model.add(Conv2D(...))
Add a layer
model.add(Activation('relu'))
Add a layer that uses "ReLU (Rectified Linear Unit)" as the activation function
model.add(MaxPooling2D(pool_size=(2,2)))
Add a layer for max pooling (dividing the input into predetermined areas and outputting the maximum value of each area)
model.add(Dropout(0.25))
Discard 25% (what?) To eliminate data bias
opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
Declaration of optimization. With the method of ʻoptimizers.rmsprop`, the learning rate is 0.0001, and the rate of lowering the learning rate is 0.000001 each time.
model.compile()
Compiling the model.
model.fit(X, y, batch_size=32, epochs=100)
Train with X
and y
.
batch_size
is the number of data used in one training.
ʻEpochs` How many sets of training do you do?
ʻVerify the model created by the evaluate` method.
def model_eval(model, X, y):
scores = model.evaluate(X, y, verbose=1)
print('test Loss: ', scores[0])
print('test Accuracy: ', scores[1])
Recommended Posts