-
Notifications
You must be signed in to change notification settings - Fork 13
/
mnist.py
219 lines (158 loc) · 5.95 KB
/
mnist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from keras.datasets import mnist
from keras.models import Sequential, model_from_json
from keras.layers import Dense, Dropout, Activation, Flatten, Input
from keras.layers import Convolution2D, MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import np_utils
import argparse
import numpy as np
from tensorflow.python.platform import flags
FLAGS = flags.FLAGS
def set_mnist_flags():
try:
flags.DEFINE_integer('BATCH_SIZE', 64, 'Size of training batches')
except argparse.ArgumentError:
pass
flags.DEFINE_integer('NUM_CLASSES', 10, 'Number of classification classes')
flags.DEFINE_integer('IMAGE_ROWS', 28, 'Input row dimension')
flags.DEFINE_integer('IMAGE_COLS', 28, 'Input column dimension')
flags.DEFINE_integer('NUM_CHANNELS', 1, 'Input depth dimension')
def data_mnist(one_hot=True):
"""
Preprocess MNIST dataset
"""
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
y_train = y_train
X_train = X_train.reshape(X_train.shape[0],
FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS)
X_test = X_test.reshape(X_test.shape[0],
FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS)
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')
print "Loaded MNIST test data."
if one_hot:
# convert class vectors to binary class matrices
y_train = np_utils.to_categorical(y_train, FLAGS.NUM_CLASSES).astype(np.float32)
y_test = np_utils.to_categorical(y_test, FLAGS.NUM_CLASSES).astype(np.float32)
return X_train, y_train, X_test, y_test
def modelA():
model = Sequential()
model.add(Conv2D(64, (5, 5),
padding='valid'))
model.add(Activation('relu'))
model.add(Conv2D(64, (5, 5)))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(FLAGS.NUM_CLASSES))
return model
def modelB():
model = Sequential()
model.add(Dropout(0.2, input_shape=(FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS)))
model.add(Convolution2D(64, 8, 8,
subsample=(2, 2),
border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(128, 6, 6,
subsample=(2, 2),
border_mode='valid'))
model.add(Activation('relu'))
model.add(Convolution2D(128, 5, 5,
subsample=(1, 1)))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(FLAGS.NUM_CLASSES))
return model
def modelC():
model = Sequential()
model.add(Convolution2D(128, 3, 3,
border_mode='valid',
input_shape=(FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS)))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(FLAGS.NUM_CLASSES))
return model
def modelD():
model = Sequential()
model.add(Flatten(input_shape=(FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS)))
model.add(Dense(300, init='he_normal', activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(300, init='he_normal', activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(300, init='he_normal', activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(300, init='he_normal', activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(FLAGS.NUM_CLASSES))
return model
def modelE():
model = Sequential()
model.add(Flatten(input_shape=(FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS)))
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(FLAGS.NUM_CLASSES))
return model
def modelF():
model = Sequential()
model.add(Convolution2D(32, 3, 3,
border_mode='valid',
input_shape=(FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(FLAGS.NUM_CLASSES))
return model
def model_mnist(type=1):
"""
Defines MNIST model using Keras sequential model
"""
models = [modelA, modelB, modelC, modelD, modelE, modelF]
return models[type]()
def data_gen_mnist(X_train):
datagen = ImageDataGenerator()
datagen.fit(X_train)
return datagen
def load_model(model_path, type=1):
try:
with open(model_path+'.json', 'r') as f:
json_string = f.read()
model = model_from_json(json_string)
except IOError:
model = model_mnist(type=type)
model.load_weights(model_path)
return model