From 7d053270c97f2030500cee90f8c1a0b8cf1d5f64 Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Wed, 27 Feb 2019 16:57:50 +0000 Subject: Add lenet model --- lenet.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 lenet.py diff --git a/lenet.py b/lenet.py new file mode 100644 index 0000000..f85bc6d --- /dev/null +++ b/lenet.py @@ -0,0 +1,72 @@ +from __future__ import print_function +import tensorflow.keras as keras +from tensorflow.keras.datasets import mnist +from tensorflow.keras.models import Sequential +from tensorflow.keras.layers import Dense, Dropout, Flatten +from tensorflow.keras.layers import Conv2D, MaxPooling2D, AveragePooling2D +from tensorflow.keras import backend as K +from tensorflow.keras import optimizers + +batch_size = 128 +num_classes = 10 + +def get_lenet(): + model = keras.Sequential() + + model.add(Conv2D(filters=6, kernel_size=(3, 3), activation='relu', input_shape=(28,28,1))) + model.add(AveragePooling2D()) + + model.add(Conv2D(filters=16, kernel_size=(3, 3), activation='relu')) + model.add(AveragePooling2D()) + + model.add(Flatten()) + + model.add(Dense(units=120, activation='relu')) + + model.add(Dense(units=84, activation='relu')) + + model.add(Dense(units=10, activation = 'softmax')) + return model + + +# input image dimensions +img_rows, img_cols = 28, 28 + +# the data, split between train and test sets +(x_train, y_train), (x_test, y_test) = mnist.load_data() + +if K.image_data_format() == 'channels_first': + 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 +print('x_train shape:', x_train.shape) +print(x_train.shape[0], 'train samples') +print(x_test.shape[0], 'test samples') + +# convert class vectors to binary class matrices +y_train = keras.utils.to_categorical(y_train, num_classes) +y_test = keras.utils.to_categorical(y_test, num_classes) + +model = get_lenet() + +sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) +model.compile(loss='mean_squared_error', optimizer=sgd) + +model.fit(x_train, y_train, + batch_size=batch_size, + epochs=1, + verbose=1) + +y_pred = model.predict(x_test) + +print(y_pred.shape) +print(y_test.shape) -- cgit v1.2.3-54-g00ecf