Wednesday, June 7, 2017

Convolutional neural network by keras

Make convolutional neural network model for mnist in keras

Overview

Convolutional neural network is one of the best solutions about image classification. In keras, it is relatively easy to make model.



Code I used

from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten
from keras.utils import to_categorical
from keras import backend as K
import keras

# get data
(x_train, y_train),(x_test, y_test) = mnist.load_data()


# decide the size of image
W = 28
H = 28

# resize
if K.image_data_format() == 'channels_first':
     x_train = x_train.reshape(x_train.shape[0], 1, H, W)
     x_test = x_test.reshape(x_test.shape[0], 1, H, W)
     input_shape = (1, H, W)
else:
     x_train = x_train.reshape(x_train.shape[0], H, W, 1)
     x_test = x_test.reshape(x_test.shape[0], H, W, 1)
     input_shape = (H, W, 1)

# make teacher hot-encoded
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

# set model
model = Sequential()
model.add(Conv2D(10, (3,3), activation='relu', input_shape=(28,28,1)))
model.add(Conv2D(32, (3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

# training
model.fit(x_train, y_train, batch_size=256, epochs=20, validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print(score)
The code above is to make convolutinal neural network(CNN) model to classify mnist(digit number image data).

Point

data

This time the data is mnist, one of the most popular image data for classification.
# get data
(x_train, y_train),(x_test, y_test) = mnist.load_data()


# decide the size of image
W = 28
H = 28

# resize
if K.image_data_format() == 'channels_first':
     x_train = x_train.reshape(x_train.shape[0], 1, H, W)
     x_test = x_test.reshape(x_test.shape[0], 1, H, W)
     input_shape = (1, H, W)
else:
     x_train = x_train.reshape(x_train.shape[0], H, W, 1)
     x_test = x_test.reshape(x_test.shape[0], H, W, 1)
     input_shape = (H, W, 1)
The procedure is simple.
* get images
* separate them into train and test set(and into explaining variables and teacher)
* resize the image

model

Convolutional neural network are basically composed with convolutional layers and pooling layers. By add() method, keras adds those layers to model.
It is not necessary to use exactly same number of convolutional and pooling layer but after one or some convolutional layers, you need to add at least one pooling layer.
# set model
model = Sequential()
model.add(Conv2D(10, (3,3), activation='relu', input_shape=(28,28,1)))
model.add(Conv2D(32, (3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])
By convolutional and pooling layers, the features are extracted from 2-dimension data(meaning images). Flatten() makes the 2-dimension features 1-dimension.
Those features go to one hidden layer and output layer.
By this code, the accuracy is more or less 0.98.
Written with StackEdit.