-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModels.py
203 lines (176 loc) · 8.21 KB
/
Models.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
'''
A file to hold the models struture.
The model with the bests results is in loadModel08() function
'''
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
def loadModel01(nb_classes, input_shape):
model = Sequential()
model.add(Convolution2D(6, 3, 3, activation='relu', input_shape=input_shape, bias=True))
model.add(Convolution2D(12, 3, 3, activation='relu', bias=True))
model.add(Convolution2D(24, 3, 3, activation='relu', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(50, activation='relu', bias=True))
model.add(Dropout(0.5))
model.add(Dense(25, activation='relu', bias=True))
model.add(Dropout(0.5))
model.add(Dense(nb_classes, activation='softmax'))
return model, 1
def loadModel02(nb_classes, input_shape):
model = Sequential()
model.add(Convolution2D(6, 3, 3, activation='relu', input_shape=input_shape, bias=True))
model.add(Convolution2D(12, 3, 3, activation='relu', bias=True))
model.add(Convolution2D(24, 3, 3, activation='relu', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(50, activation='relu', bias=True))
model.add(Dropout(0.5))
model.add(Dense(25, activation='relu', bias=True))
model.add(Dropout(0.5))
model.add(Dense(nb_classes, activation='softmax'))
return model, 2
def loadModel03(nb_classes, input_shape):
model = Sequential()
model.add(Convolution2D(100, 5, 5, activation='tanh', input_shape=input_shape, bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(100, 4, 4, activation='tanh', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(300, activation='tanh'))
model.add(Dense(100, activation='tanh'))
model.add(Dense(nb_classes, activation='softmax'))
return model, 3
def loadModel04(nb_classes, input_shape):
model = Sequential()
model.add(Convolution2D(5, 3, 3, border_mode='same', activation='tanh', input_shape=input_shape, bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Convolution2D(5, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(150, activation='tanh'))
model.add(Dropout(0.25))
model.add(Dense(50, activation='tanh'))
model.add(Dropout(0.25))
model.add(Dense(nb_classes, activation='softmax'))
return model, 4
def loadModel05(nb_classes, input_shape):
model = Sequential()
model.add(Convolution2D(100, 3, 3, border_mode='same', activation='tanh', input_shape=input_shape, bias=True))
model.add(Convolution2D(100, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(Convolution2D(100, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Convolution2D(100, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(Convolution2D(100, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(150, activation='tanh'))
model.add(Dropout(0.25))
model.add(Dense(50, activation='tanh'))
model.add(Dropout(0.25))
model.add(Dense(nb_classes, activation='softmax'))
return model, 5
def loadModel07(nb_classes, input_shape):
model = Sequential()
model.add(Convolution2D(100, 3, 3, border_mode='same', activation='tanh', input_shape=input_shape, bias=True))
model.add(Convolution2D(100, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(Convolution2D(100, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Convolution2D(100, 3, 3, border_mode='same', activation='tanh', input_shape=input_shape, bias=True))
model.add(Convolution2D(100, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(Convolution2D(100, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(150, activation='tanh'))
model.add(Dropout(0.25))
model.add(Dense(50, activation='tanh'))
model.add(Dropout(0.25))
model.add(Dense(nb_classes, activation='softmax'))
return model, 7
def loadModel08(nb_classes, input_shape):
model = Sequential()
model.add(Convolution2D(15, 3, 3, border_mode='same', activation='tanh', input_shape=input_shape, bias=True))
model.add(Convolution2D(30, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Convolution2D(15, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(Convolution2D(30, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Convolution2D(15, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(Convolution2D(30, 3, 3, border_mode='same', activation='tanh', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(500, activation='tanh'))
model.add(Dropout(0.25))
model.add(Dense(250, activation='tanh'))
model.add(Dropout(0.25))
model.add(Dense(nb_classes, activation='softmax'))
return model, 8
def loadModel09(nb_classes, input_shape):
model = Sequential()
model.add(Convolution2D(32, 28, 28, border_mode='same', activation='tanh', input_shape=input_shape, bias=True))
model.add(Convolution2D(64, 28, 28, border_mode='same', activation='tanh', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(64, 14, 14, border_mode='same', activation='tanh', bias=True))
model.add(Convolution2D(128, 14, 14, border_mode='same', activation='tanh', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(128, 10, 10, border_mode='same', activation='tanh', bias=True))
model.add(Convolution2D(128, 10, 10, border_mode='same', activation='tanh', bias=True))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(128, 5, 5, border_mode='same', activation='tanh', bias=True))
model.add(Convolution2D(128, 5, 5, border_mode='same', activation='tanh', bias=True))
model.add(Flatten())
model.add(Dense(500, activation='tanh'))
model.add(Dropout(0.25))
model.add(Dense(150, activation='tanh'))
model.add(Dense(nb_classes, activation='softmax'))
return model, 9
def compileSGD(model):
sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
return model
'''
Train a model with data augmentation
'''
def trainWithImageAugmentation(model, batch_size, nb_epoch, X_train, Y_train, X_test, Y_test):
print("DATA AUGMENTATION ON")
# Define data preparation
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=True, # randomly flip images
vertical_flip=False) # randomly flip images
datagen.fit(X_train)
for X_batch, y_batch in datagen.flow(X_train, Y_train, batch_size=9):
for i in range(0, 9):
pyplot.subplot(330+1+i)
img = X_batch[i]
pyplot.imshow(img.reshape(100, 100), cmap=pyplot.get_cmap('gray'))
pyplot.show()
break
# Fits the model
hist = model.fit_generator(datagen.flow(X_train, Y_train,
batch_size=batch_size),
samples_per_epoch=X_train.shape[0],
nb_epoch=nb_epoch,
validation_data=(X_test, Y_test))
return model, hist