-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAlexNet.py
More file actions
314 lines (255 loc) · 13.9 KB
/
AlexNet.py
File metadata and controls
314 lines (255 loc) · 13.9 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten, Activation, Conv2D, MaxPooling2D, Dropout, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from bitstring import BitArray
from typing import List
import plots
import config
#REF UNDERSTAND: http://www.lapix.ufsc.br/ensino/visao/visao-computacionaldeep-learning/deep-learningreconhecimento-de-imagens/
#ARQUITECHTURE CONFIGURATION: https://www.learnopencv.com/number-of-parameters-and-tensor-sizes-in-convolutional-neural-network/
def alexNet(particleDimensions, x_train, x_test, y_train, y_test):
try:
#CONVERT FLOAT DATA --> IF I USE PSO TO OPTIMIZE CNN MODEL
particleDimensions = [int(particleDimensions[i]) for i in range(len(particleDimensions))]
model = Sequential()
model.add(Conv2D(filters=particleDimensions[0], kernel_size=(3,3), padding='same', #zero padding --> default 96 neurons
input_shape=(x_train.shape[1], x_train.shape[2], x_train.shape[3])))
model.add(Activation('relu'))
#model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2), padding='same'))
model.add(Dropout(0.25))
model.add(Conv2D(filters=particleDimensions[1], kernel_size=(3,3), padding='same')) #default 256 neurons
model.add(Activation('relu'))
#model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=particleDimensions[2], kernel_size=(3,3), padding='same')) #default 384 neurons
model.add(Activation('relu'))
model.add(Conv2D(filters=particleDimensions[2], kernel_size=(3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(particleDimensions[3])) #default 2048
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(particleDimensions[4])) #default 2048
model.add(Activation('relu'))
model.add(Dropout(0.5))
# model.add(Dense(particleDimensions[5])) #default 1000
# model.add(Activation('relu'))
# model.add(Dropout(0.4))
model.add(Dense(4))
model.add(Activation('softmax'))
model.summary()
#OPTIMIZER
opt = keras.optimizers.RMSprop(learning_rate=0.0001, decay=1e-6)
# COMPILE MODEL
model.compile(optimizer=opt, loss='categorical_crossentropy',
metrics=['accuracy']) # CROSSENTROPY BECAUSE IT'S MORE ADEQUATED TO MULTI-CLASS PROBLEMS
#DATA AUGMENTATION
# image_gen = 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
# zca_epsilon=1e-06, # epsilon for ZCA whitening
# rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)
# # randomly shift images horizontally (fraction of total width)
# width_shift_range=0.1,
# # randomly shift images vertically (fraction of total height)
# height_shift_range=0.1,
# shear_range=0., # set range for random shear
# zoom_range=0., # set range for random zoom
# channel_shift_range=0., # set range for random channel shifts
# # set mode for filling points outside the input boundaries
# fill_mode='nearest',
# cval=0., # value used for fill_mode = "constant"
# horizontal_flip=True, # randomly flip images
# vertical_flip=False, # randomly flip images
# # set rescaling factor (applied before any other transformation)
# rescale=None,
# # set function that will be applied on each input
# preprocessing_function=None,
# # image data format, either "channels_first" or "channels_last"
# data_format=None,
# # fraction of images reserved for validation (strictly between 0 and 1)
# validation_split=0.0)
#
# image_gen.fit(x_train)
#
# history = model.fit_generator(image_gen.flow(
# x=x_train,
# y=y_train,
# batch_size=64),
# epochs=30,
# validation_data=(x_test, y_test),
# workers=6,
# shuffle=True,
# )
history = model.fit(
x=x_train,
y=y_train,
batch_size=32,
epochs=55,
validation_data=(x_test, y_test),
workers=4,
shuffle=True,
)
scores = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
loss = 1.5 * ((1.0 - (1.0 / (particleDimensions[0]+particleDimensions[1]+particleDimensions[2])))
+ (1.0 - (1.0 / (particleDimensions[3]+particleDimensions[4])))) + 5.0 * (1.0 - scores[1])
plots.plotTrainValLoss(history)
plots.plotTrainValAcc(history)
#SAVE MODEL TO FILE --> IN ORDER TO KEP TRAINING MODEL AFTER THAT
#model.save(config.SAVED_MODEL_FILE2)
#del model
return loss
except:
raise
def alexNetAugmentation(particleDimensions, x_train, x_test, y_train, y_test, loadModel):
try:
#CONVERT FLOAT DATA --> IF I USE PSO TO OPTIMIZE CNN MODEL
particleDimensions = [int(particleDimensions[i]) for i in range(len(particleDimensions))]
#OPTIMIZER
opt = keras.optimizers.RMSprop(learning_rate=0.0001, decay=1e-6)
# COMPILE MODEL
loadModel.compile(optimizer=opt, loss='categorical_crossentropy',
metrics=['accuracy']) # CROSSENTROPY BECAUSE IT'S MORE ADEQUATED TO MULTI-CLASS PROBLEMS
#DATA AUGMENTATION
image_gen = ImageDataGenerator(
width_shift_range=0.1,
height_shift_range=0.1,
rotation_range=10,
zoom_range=0.1, # set range for random zoom
horizontal_flip=True,)
#validation_split=0.30 # randomly flip images
#featurewise_center=True
image_gen.fit(x_train)
#REF: https://stackoverflow.com/questions/53808335/data-augmentation-in-validation
#https: // stackoverflow.com / questions / 41174546 / keras - data - augmentation - parameters
train_generator = image_gen.flow(x_train, y_train, batch_size=64)
#validation_generator = image_gen.flow(x_test, y_test, batch_size=32)
history = loadModel.fit_generator(
generator=train_generator,
validation_data=(x_test, y_test),
epochs=60,
#use_multiprocessing=True,
steps_per_epoch=x_train.shape[0]/64,
validation_steps=x_test.shape[0]/16,
workers=4,
shuffle=True,
)
scores = loadModel.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
loss = 1.5 * ((1.0 - (1.0 / (particleDimensions[0]+particleDimensions[1]+particleDimensions[2])))
+ (1.0 - (1.0 / (particleDimensions[4]+particleDimensions[5])))) + 5.0 * (1.0 - scores[1])
plots.plotTrainValLoss(history)
plots.plotTrainValAcc(history)
return loss
except:
raise
def alexNetForGA(ga_solution, x_train, x_test, y_train, y_test):
try:
conv1 = BitArray(ga_solution[0:6]).uint
conv2 = BitArray(ga_solution[6:13]).uint
conv3 = BitArray(ga_solution[13:21]).uint
dense1 = BitArray(ga_solution[21:29]).uint
dense2 = BitArray(ga_solution[29:35]).uint
#dense3 = BitArray(ga_solution[47:57]).uint
if conv1 == 0 or conv2 ==0 or conv3 == 0 or dense1 == 0 or dense2 == 0: #if any of the values is 0, it immediately penalises the individual
return 100,
model = Sequential()
model.add(Conv2D(filters=conv1, kernel_size=(3,3), padding='same', strides=2, #zero padding --> default 96 neurons
input_shape=(x_train.shape[1], x_train.shape[2], x_train.shape[3])))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), padding='same'))
model.add(Dropout(0.25))
model.add(Conv2D(filters=conv2, kernel_size=(3,3), padding='same')) #default 256 neurons
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=conv3, kernel_size=(3,3), padding='same')) #default 384 neurons
model.add(Activation('relu'))
model.add(Conv2D(filters=conv3, kernel_size=(3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(dense1)) #default 4096
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(dense2)) #default 2048
model.add(Activation('relu'))
model.add(Dropout(0.5))
# model.add(Dense(dense3)) #default 1000
# model.add(Activation('relu'))
# model.add(Dropout(0.3))
model.add(Dense(4))
model.add(Activation('softmax'))
model.summary()
#OPTIMIZER
opt = keras.optimizers.RMSprop(learning_rate=0.0001, decay=1e-6)
# COMPILE MODEL
model.compile(optimizer=opt, loss='categorical_crossentropy',
metrics=['accuracy']) # CROSSENTROPY BECAUSE IT'S MORE ADEQUATED TO MULTI-CLASS PROBLEMS
#DATA AUGMENTATION
# image_gen = 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
# zca_epsilon=1e-06, # epsilon for ZCA whitening
# rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)
# # randomly shift images horizontally (fraction of total width)
# width_shift_range=0.1,
# # randomly shift images vertically (fraction of total height)
# height_shift_range=0.1,
# shear_range=0., # set range for random shear
# zoom_range=0., # set range for random zoom
# channel_shift_range=0., # set range for random channel shifts
# # set mode for filling points outside the input boundaries
# fill_mode='nearest',
# cval=0., # value used for fill_mode = "constant"
# horizontal_flip=True, # randomly flip images
# vertical_flip=False, # randomly flip images
# # set rescaling factor (applied before any other transformation)
# rescale=None,
# # set function that will be applied on each input
# preprocessing_function=None,
# # image data format, either "channels_first" or "channels_last"
# data_format=None,
# # fraction of images reserved for validation (strictly between 0 and 1)
# validation_split=0.0)
#
# image_gen.fit(x_train)
#
# model.fit_generator(image_gen.flow(
# x=x_train,
# y=y_train,
# batch_size=32),
# epochs=5,
# validation_data=(x_test, y_test),
# workers=6,
# shuffle=True)
history = model.fit(
x=x_train,
y=y_train,
batch_size=32,
epochs=30,
validation_data=(x_test, y_test),
workers=4,
shuffle=True,
)
scores = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
loss = 1.5 * ((1.0 - (1.0 / (conv1+conv2+conv3)))
+ (1.0 - (1.0 / (dense1+dense2)))) + 5.0 * (1.0 - scores[1])
return loss,
except:
raise