Skip to content

Instantly share code, notes, and snippets.

@seibert
Created March 12, 2018 12:05
Show Gist options
  • Save seibert/75bda277e5104a0ea4be692db45c0d36 to your computer and use it in GitHub Desktop.
Save seibert/75bda277e5104a0ea4be692db45c0d36 to your computer and use it in GitHub Desktop.
import os, sys
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
batch_size = 128
nb_classes = 10
nb_epoch = 20
img_rows, img_cols = 28, 28
nb_filters = 64
pool_size = (2, 2)
kernel_size = (3, 3)
(X_train, y_train), (X_test, y_test) = mnist.load_data()
if K.image_dim_ordering() == 'th':
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
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)
input_shape = (img_rows, img_cols, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
model.add(Conv2D(nb_filters,
kernel_size=kernel_size,
padding='same',
input_shape=input_shape))
model.add(Activation('relu'))
model.add(Conv2D(nb_filters, kernel_size=kernel_size))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.25))
model.add(Conv2D(nb_filters*2, kernel_size=kernel_size, padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(nb_filters*2, kernel_size=kernel_size))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.25))
model.add(Conv2D(nb_filters*4, kernel_size=kernel_size, padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(nb_filters*4, kernel_size=kernel_size))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
print('Starting fit...', file=sys.stderr)
history = model.fit(X_train, Y_train, batch_size=batch_size, epochs=nb_epoch,
verbose=2, validation_data=(X_test, Y_test))
print('Fit complete.', file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment