Notes on building CNNs with Keras and classifying them using the CIFAR-10 dataset
from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPool2D
from keras.optimizers import Adam
from keras.layers.core import Dense, Activation, Dropout, Flatten
from keras.utils import plot_model
from keras.callbacks import TensorBoard
from keras.datasets import cifar10
from keras.utils import np_utils
(X_train, y_train),(X_test, y_test) = cifar10.load_data()
Is stored.
Normalize each pixel value in the range 0-1
#Type conversion to float
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
#Normalize each pixel value
X_train /= 255.0
X_test /= 255.0
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
Convert to one-hot representation for nb_classes classes with np_utils.to_categorical.
Since there are 10 classes this time, we set nb_classes = 10
.
Actually define the model,
#Model definition
model = Sequential()
model.add(Conv2D(32,3,input_shape=(32,32,3)))
model.add(Activation('relu'))
model.add(Conv2D(32,3))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(64,3))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(1.0))
model.add(Dense(nb_classes, activation='softmax'))
adam = Adam(lr=1e-4)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=["accuracy"])
Define a model object with model.Sequential (). It is possible to add elements with model.add (). The first model.add () adds a 32x32 convolution layer (kernel 3x3), and since the input image is a 32x32 RGB image, the input layer is `ʻinput_shape (32,32,3). It can be defined by ``. Use Activation () to add the activation function, and Dense () to add the fully connected layer. Set model.compile () to obtain Adam as the optimization method, cross entropy as the error function, and accuracy as the calculation.
history = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_split=0.1)
You can learn with model.fit (). This time, we set nb_epoch = 100
to set the number of epochs to 100.
varidation_split is a parameter that uses some percentage of the training data for the test. This time, 10% is used for the test.
Visualize your data with Keras
#Plot the model
plot_model(model, to_file='./model2.png')
Since the trained model can be saved as a file, the model configuration data can be saved as a JSON file and the model weight data can be saved as an HDF5 file.
json_string = model.to_json()
open(os.path.join('./', 'cnn_model.json'), 'w').write(json_string)
model.save_weights(os.path.join('./', 'cnn_model_weight.hdf5'))
Since we are using Tensorflow for the backend this time, we can also use Tensorboar below. [Keras / TensorFlow] Using TensorBoard with Keras
CNN was constructed with Keras, and 10 categories were classified with CIFAR-10. From here, I will try various things.
Source Code https://github.com/sasayabaku/Machine-Learning/blob/master/Example_CNN/cifar10.ipynb
Keras Documentation Deep Learning starting with Keras
Recommended Posts