aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVasil Zlatanov <v@skozl.com>2019-02-27 16:57:50 +0000
committerVasil Zlatanov <v@skozl.com>2019-02-27 16:57:50 +0000
commit7d053270c97f2030500cee90f8c1a0b8cf1d5f64 (patch)
tree73e48c2503b3086786b6b218f4a24880653a9935
parent589c0af3eadc6cf93dd51f1fc1e08e4a93e53e20 (diff)
downloade4-gan-7d053270c97f2030500cee90f8c1a0b8cf1d5f64.tar.gz
e4-gan-7d053270c97f2030500cee90f8c1a0b8cf1d5f64.tar.bz2
e4-gan-7d053270c97f2030500cee90f8c1a0b8cf1d5f64.zip
Add lenet model
-rw-r--r--lenet.py72
1 files changed, 72 insertions, 0 deletions
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)