Skip to content

Instantly share code, notes, and snippets.

@is1394
Forked from bellbind/curve_example.py
Created September 1, 2017 02:56
Show Gist options
  • Save is1394/f0431d9976870cc11247149227420ef2 to your computer and use it in GitHub Desktop.
Save is1394/f0431d9976870cc11247149227420ef2 to your computer and use it in GitHub Desktop.
[python3][keras][tensorflow] number recognition with mnist dataset
# [define network model]
from keras.models import Sequential
from keras.layers import Activation, Dense, Dropout, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
model = Sequential()
# count of convolution filter=32, row of filter=3, col of filter=3,
# note: input_shape format is different by the backend: tensorflow or theano
# tensorflow: (rows, cols, channels) # number of color channels
model.add(Convolution2D(32, 3, 3, input_shape=(28, 28, 1)))
model.add(Activation("relu"))
model.add(Convolution2D(64, 3, 3))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation("softmax"))
model.compile(
optimizer="sgd", loss="categorical_crossentropy", metrics=['accuracy'])
print(model.to_yaml()) # show model
# [prepare dataset]
from keras.datasets import mnist
from keras.utils import np_utils
# mnist dataset as monoral image and its number
# x: 0-255 of 28x28, y: 0-9, train: 60000 x and y, test: 10000 x and y
(x_train_raw, y_train_raw), (x_test_raw, y_test_raw) = mnist.load_data()
# convert (samples, rows, cols) to (samples, rows, cols, channels)
x_train = x_train_raw.reshape(*x_train_raw.shape, 1).astype("float32") / 255
x_test = x_test_raw.reshape(*x_test_raw.shape, 1).astype("float32") / 255
# convert 0-9 data to 10 categorical array: e.g. 2 => [0.0.1,0.0, 0,0,0,0,0]
y_train = np_utils.to_categorical(y_train_raw, 10)
y_test = np_utils.to_categorical(y_test_raw, 10)
# [train by train dataset] wait an hour
history = model.fit(x_train, y_train, validation_data=(x_test, y_test))
# [check by test dataset]
score = model.evaluate(x_test, y_test)
print("[score] {}, accuracy: {}".format(*score))
# [predict by single data]
result = model.predict(x_test[0:1]) # numpy array of a single data
print("[result] {} may be {}".format(result[0], result[0].argmax()))
print("[answer] {} as {}".format(y_test[0], y_test_raw[0]))
#############################################################################
# [setup keras]
# $ python3 -m venv keras
# $ ./keras/bin/pip install six
# $ ./keras/bin/pip install https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.1-py3-none-any.whl
# $ ./keras/bin/pip install keras
# $ ./keras/bin/pip install h5py # for save and load model weight
#
# $ ./keras/bin/python3 mnist_example.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment