Last active
April 30, 2020 21:06
-
-
Save karanjakhar/5d3b7d757cb6e834586fdec31e4dcd41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.datasets import mnist | |
from keras.utils import to_categorical | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D | |
(x_train, y_train), (x_test, y_test) = mnist.load_data() | |
# save input image dimensions | |
img_rows, img_cols = 28, 28 | |
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) | |
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) | |
x_train = x_train/255 | |
x_test = x_test/255 | |
num_classes = 10 | |
y_train = to_categorical(y_train, num_classes) | |
y_test = to_categorical(y_test, num_classes) | |
model = Sequential() | |
model.add(Conv2D(32, kernel_size=(3, 3), | |
activation='relu', | |
input_shape=(img_rows, img_cols, 1))) | |
model.add(Conv2D(64, (3, 3), activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Dropout(0.25)) | |
model.add(Flatten()) | |
model.add(Dense(128, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(num_classes, activation='softmax')) | |
model.compile(loss='categorical_crossentropy', | |
optimizer='adam', | |
metrics=['accuracy']) | |
batch_size = 128 | |
epochs = 10 | |
model.fit(x_train, y_train, | |
batch_size=batch_size, | |
epochs=epochs, | |
verbose=1, | |
validation_data=(x_test, y_test)) | |
score = model.evaluate(x_test, y_test, verbose=0) | |
print('Test loss:', score[0]) | |
print('Test accuracy:', score[1]) | |
model.save("test_model.h5") | |
# load the model | |
from keras.models import load_model | |
model = load_model("test_model.h5") | |
# predict digit | |
prediction = model.predict(gray) | |
print(prediction.argmax()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment