-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLearnToPayAttention.py
576 lines (502 loc) · 32.6 KB
/
LearnToPayAttention.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import tensorflow as tf
import keras
import numpy as np
from keras import backend as K
from keras.models import Model
from keras.engine.topology import Layer
from keras.layers import Input
from keras.layers.core import Dense, Lambda, Activation, Flatten, Reshape
from keras.layers.convolutional import Conv2D
from keras.layers.merge import Concatenate, Add
from keras.layers.pooling import MaxPooling2D, AveragePooling2D
from keras.layers.normalization import BatchNormalization
from keras.callbacks import Callback, LearningRateScheduler, ModelCheckpoint, LambdaCallback, TensorBoard, EarlyStopping, ReduceLROnPlateau
from keras.optimizers import SGD
import winsound
import os
class StandardVGG:
def __init__(self):
inp = Input(shape=(32, 32, 3))
regularizer = keras.regularizers.l2(0.0005)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1', kernel_regularizer=regularizer)(inp)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2', kernel_regularizer=regularizer)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
# Block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1', kernel_regularizer=regularizer)(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2', kernel_regularizer=regularizer)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
# Block 3
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1', kernel_regularizer=regularizer)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2', kernel_regularizer=regularizer)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3', kernel_regularizer=regularizer)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
# Block 4
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1', kernel_regularizer=regularizer)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2', kernel_regularizer=regularizer)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3', kernel_regularizer=regularizer)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
# Block 5
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1', kernel_regularizer=regularizer)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2', kernel_regularizer=regularizer)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3', kernel_regularizer=regularizer)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
x = Flatten(name='flatten')(x)
x = Dense(4096, activation='relu', name='fc1', kernel_regularizer=regularizer)(x)
x = Dense(4096, activation='relu', name='fc2', kernel_regularizer=regularizer)(x)
x = Dense(10, activation='softmax', name='predictions', kernel_regularizer=regularizer)(x)
self.model = Model(inp, x, name='vgg16')
self.name = "VGG16"
self.datasetname="cifar16"
print("Generated "+self.name)
optimizer=SGD(lr=1, momentum=0.9, decay=0.0000001)
loss='categorical_crossentropy'
metrics=['accuracy']
self.model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
def StandardFit(self, datasetname=None, X=[], Y=[], beep=False):
Y = keras.utils.to_categorical(Y,self.outputclasses)
if datasetname==None:
datasetname=self.datasetname
scheduler = LearningRateScaler(25, 0.5)
startingepoch = 0
pastepochs = list(map(int, [x.replace(".hdf5", "").replace(self.name+"-"+datasetname, "").replace(" ", "") for x in os.listdir("weights") if self.name+"-"+datasetname in x]))
if len(pastepochs):
if max(pastepochs) == 300:
print("Found completely trained weights for "+self.name+"-"+datasetname)
return
self.model.load_weights("weights/"+self.name+"-"+datasetname+" "+str(max(pastepochs))+".hdf5")
startingepoch = max(pastepochs)
tboardcb = TensorBoard(log_dir='./logs', histogram_freq=0, batch_size=3, write_graph=True, write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)
checkpoint = ModelCheckpoint("weights/"+self.name+"-"+datasetname+" {epoch}.hdf5", save_weights_only=True)
epochprint = LambdaCallback(on_epoch_end=lambda epoch, logs: print("Passed epoch "+str(epoch)))
callbackslist = [scheduler, checkpoint, epochprint, tboardcb]
if beep:
callbackslist.append(Beeper(1))
self.model.fit(X, Y, 128, 300, callbacks=callbackslist, initial_epoch=startingepoch,shuffle=True)
pastepochs = list(map(int, [x.replace(".hdf5", "").replace(self.name+"-"+datasetname, "").replace(" ", "") for x in os.listdir("weights") if self.name+"-"+datasetname in x]))
if max(pastepochs) > 290:
for filenum in range(1,297): #delete most of the lower weight files
try:
os.remove("weights/"+self.name+"-"+datasetname+" "+str(filenum)+".hdf5")
except OSError:
pass
return self.model
class AttentionVGG:
def VGGBlock(self, x, regularizer = None, batchnorm = False):
if batchnorm:
x = Conv2D(64, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv1')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(64, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv2')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(128, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv3')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(128, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv4')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(256, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv5')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(256, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv6')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(256, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv7')(x)
x = BatchNormalization()(x)
local1 = Activation('relu')(x) # batch*x*y*channel
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(x)
x = Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv8')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv9')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv10')(x)
x = BatchNormalization()(x)
local2 = Activation('relu')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2')(local2)
x = Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv11')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv12')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv13')(x)
x = BatchNormalization()(x)
local3 = Activation('relu')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool3')(local3)
x = Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv14')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool4')(x)
x = Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizer, name='conv15')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool5')(x)
x = Flatten(name='pregflatten')(x)
g = Dense(512, activation='relu', kernel_regularizer=regularizer, name='globalg')(x) # batch*512
return (g, local1, local2, local3)
else:
x = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv1')(x)
x = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv2')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv3')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv4')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv5')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv6')(x)
local1 = Conv2D(256, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv7')(x) # batch*x*y*channel
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(local1)
x = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv8')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv9')(x)
local2 = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv10')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2')(local2)
x = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv11')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv12')(x)
local3 = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv13')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool3')(local3)
x = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv14')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool4')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_regularizer=regularizer, name='conv15')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='pool5')(x)
x = Flatten(name='pregflatten')(x)
g = Dense(512, activation='relu', kernel_regularizer=regularizer, name='globalg')(x) # batch*512
return (g, local1, local2, local3)
def __init__(self, att='att3', gmode='concat', compatibilityfunction='pc', datasetname="cifar100", height=32, width=32, channels=3, outputclasses=10, batchnorm=True, batchnormalizeinput=True, weight_decay=0.0005, optimizer=SGD(lr=0.01, momentum=0.9, decay=0.0000001), loss='categorical_crossentropy', metrics=['accuracy']):
inp = Input(shape=(height, width, channels))
input = inp
if batchnormalizeinput:
input = BatchNormalization()(input)
regularizer = keras.regularizers.l2(weight_decay)
self.datasetname = datasetname
self.outputclasses=outputclasses
(g, local1, local2, local3) = self.VGGBlock(input,regularizer,batchnorm)
l1 = Dense(512, kernel_regularizer=regularizer, name='l1connectordense')(local1) # batch*x*y*512
c1 = ParametrisedCompatibility(kernel_regularizer=regularizer, name='cpc1')([l1, g]) # batch*x*y
if compatibilityfunction == 'dp':
c1 = Lambda(lambda lam: K.squeeze(K.map_fn(lambda xy: K.dot(xy[0], xy[1]), elems=(lam[0], K.expand_dims(lam[1], -1)), dtype='float32'), 3), name='cdp1')([l1, g]) # batch*x*y
flatc1 = Flatten(name='flatc1')(c1) # batch*xy
a1 = Activation('softmax', name='softmax1')(flatc1) # batch*xy
reshaped1 = Reshape((-1,512), name='reshape1')(l1) # batch*xy*512.
g1 = Lambda(lambda lam: K.squeeze(K.batch_dot(K.expand_dims(lam[0], 1), lam[1]), 1), name='g1')([a1, reshaped1]) # batch*512.
l2 = local2
c2 = ParametrisedCompatibility(kernel_regularizer=regularizer, name='cpc2')([l2, g])
if compatibilityfunction == 'dp':
c2 = Lambda(lambda lam: K.squeeze(K.map_fn(lambda xy: K.dot(xy[0], xy[1]), elems=(lam[0], K.expand_dims(lam[1], -1)), dtype='float32'), 3), name='cdp2')([l2, g])
flatc2 = Flatten(name='flatc2')(c2)
a2 = Activation('softmax', name='softmax2')(flatc2)
reshaped2 = Reshape((-1,512), name='reshape2')(l2)
g2 = Lambda(lambda lam: K.squeeze(K.batch_dot(K.expand_dims(lam[0], 1), lam[1]), 1), name='g2')([a2, reshaped2])
l3 = local3
c3 = ParametrisedCompatibility(kernel_regularizer=regularizer, name='cpc3')([l3, g])
if compatibilityfunction == 'dp':
c3 = Lambda(lambda lam: K.squeeze(K.map_fn(lambda xy: K.dot(xy[0], xy[1]), elems=(lam[0], K.expand_dims(lam[1], -1)), dtype='float32'), 3), name='cdp3')([l3, g])
flatc3 = Flatten(name='flatc3')(c3)
a3 = Activation('softmax', name='softmax3')(flatc3)
reshaped3 = Reshape((-1,512), name='reshape3')(l3)
g3 = Lambda(lambda lam: K.squeeze(K.batch_dot(K.expand_dims(lam[0], 1), lam[1]), 1), name='g3')([a3, reshaped3])
out = ''
if gmode == 'concat':
glist = [g3]
if att == 'att2':
glist.append(g2)
if att == 'att3':
glist.append(g2)
glist.append(g1)
predictedG = g3
if att != 'att1' and att != 'att':
predictedG = Concatenate(axis=1, name='ConcatG')(glist)
x = Dense(outputclasses, kernel_regularizer=regularizer, name=str(outputclasses)+'ConcatG')(predictedG)
out = Activation("softmax", name='concatsoftmaxout')(x)
else:
gd3 = Dense(outputclasses, activation='softmax', name=str(outputclasses)+'indepsoftmaxg3')(g3)
if att == 'att' or att == 'att1':
out = gd3
elif att == 'att2':
gd2 = Dense(outputclasses, activation='softmax', kernel_regularizer=regularizer, name=str(outputclasses)+'indepsoftmaxg2')(g2)
out = Add(name='addg3g2')([gd3, gd2])
out = Lambda(lambda lam: lam/2, name='2average')(out)
else:
gd2 = Dense(outputclasses, activation='softmax', kernel_regularizer=regularizer, name=str(outputclasses)+'indepsoftmaxg2')(g2)
gd1 = Dense(outputclasses, activation='softmax', kernel_regularizer=regularizer, name=str(outputclasses)+'indepsoftmaxg2')(g1)
out = Add(name='addg3g2g1')([gd1, gd2, gd3])
out = Lambda(lambda lam: lam/3, name='3average')(out)
model = Model(inputs=inp, outputs=out)
model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
name = ("(VGG-"+att+")-"+gmode+"-"+compatibilityfunction).replace('att)', 'att1)')
print("Generated "+name)
self.name = name
self.model = model
def StandardFit(self, datasetname=None, X=[], Y=[], transfer=False, beep=False, initial_lr=0.01, min_delta=None, patience=7, validation_data=None, lrplateaufactor=None, lrplateaupatience=4):
Y = keras.utils.to_categorical(Y,self.outputclasses)
if datasetname==None:
datasetname=self.datasetname
if os.path.isfile("weights/"+self.name+"-"+datasetname+" early.hdf5"):
print("Found early-stopped weights for "+self.name+"-"+datasetname)
return
scheduler = LearningRateScaler(25, 0.5, initial_lr)
startingepoch = 0
pastepochs = list(map(int, [x.replace(".hdf5", "").replace(self.name+"-"+datasetname, "").replace(" ", "") for x in os.listdir("weights") if (self.name+"-"+datasetname in x) & ("early" not in x)]))
if len(pastepochs):
if max(pastepochs) == 300:
print("Found completely trained weights for "+self.name+"-"+datasetname)
return
self.model.load_weights("weights/"+self.name+"-"+datasetname+" "+str(max(pastepochs))+".hdf5")
startingepoch = max(pastepochs)
elif transfer:
if os.path.isfile("weights/"+self.name+"-"+datasetname+" early.hdf5"):
self.model.load_weights("weights/"+self.name+"-"+datasetname+" early.hdf5", by_name=True)
else:
self.model.load_weights("weights/"+self.name+"-cifar100 300.hdf5", by_name=True)
scheduler = LearningRateScheduler(transfer_schedule)
tboardcb = TensorBoard(log_dir='./logs', histogram_freq=0, batch_size=3, write_graph=True, write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)
checkpoint = ModelCheckpoint("weights/"+self.name+"-"+datasetname+" {epoch}.hdf5", save_weights_only=True)
epochprint = LambdaCallback(on_epoch_end=lambda epoch, logs: print("Passed epoch "+str(epoch)))
callbackslist = [scheduler, checkpoint, epochprint, tboardcb]
if beep:
callbackslist.append(Beeper(1))
if validation_data == None:
self.model.fit(X, Y, 128, 300, callbacks=callbackslist, initial_epoch=startingepoch,shuffle=True)
else:
if min_delta != None:
callbackslist.append(EarlyStopping(monitor='val_acc', min_delta=min_delta, patience=patience))
if lrplateaufactor != None:
callbackslist.append(ReduceLROnPlateau(monitor='loss', factor = lrplateaufactor, patience = lrplateaupatience))
self.model.fit(X, Y, 128, 300, callbacks=callbackslist, initial_epoch=startingepoch,shuffle=True,validation_data=(validation_data[0], keras.utils.to_categorical(validation_data[1],self.outputclasses)))
self.model.save_weights("weights/"+self.name+"-"+datasetname+" early.hdf5")
pastepochs = list(map(int, [x.replace(".hdf5", "").replace(self.name+"-"+datasetname, "").replace(" ", "") for x in os.listdir("weights") if (self.name+"-"+datasetname in x) & ("early" not in x)]))
if max(pastepochs) > 290:
for filenum in range(1,297): #delete most of the lower weight files
try:
os.remove("weights/"+self.name+"-"+datasetname+" "+str(filenum)+".hdf5")
except OSError:
pass
return self.model
def transfer_schedule(epoch):
if epoch < 30:
return 0.1
if epoch < 60:
return 0.2
if epoch < 90:
return 0.4
if epoch < 120:
return 0.2
if epoch < 150:
return 0.1
if epoch < 180:
return 0.05
if epoch < 210:
return 0.025
if epoch < 240:
return 0.0125
if epoch < 270:
return 0.00625
return 0.003125
class AttentionRN:
def __init__(self, att='att2', gmode='concat', compatibilityfunction='pc', datasetname="cifar10", height=32, width=32, channels=3, outputclasses=100, weight_decay=0.0005, optimizer=SGD(lr=0.01, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy']):
inp = Input(shape=(height, width, channels)) #batch*x*y*3
regularizer = keras.regularizers.l2(weight_decay)
self.datasetname = datasetname
self.outputclasses=outputclasses
x = BatchNormalization()(inp)
#block1, out batch*(x)*(y)*16
x = Conv2D(16, (3, 3), padding='same', kernel_regularizer=regularizer, name='block1conv1')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(16, (3, 3), padding='same', kernel_regularizer=regularizer, name='block1conv2')(x) #batch*x*y*16
x = BatchNormalization()(x)
#block2, out batch*(x/2)*(y/2)*64
for i in range(0,18):
identity = x
if i == 0:
identity=Conv2D(64,(2,2), padding='same', kernel_regularizer=regularizer, name='block2dimchangeconv')(identity)
x = Conv2D(16, (1, 1), padding='same', kernel_regularizer=regularizer, name='block2resblock'+str((i+1))+'conv1')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(16, (3, 3), padding='same', kernel_regularizer=regularizer, name='block2resblock'+str((i+1))+'conv2')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(64, (1, 1), padding='same', kernel_regularizer=regularizer, name='block2resblock'+str((i+1))+'conv3')(x)
x = BatchNormalization()(x)
x = Add()([identity,x])
x = Activation('relu')(x)
l1 = x #16 filters, 32x32 resolution
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2pool')(x)
#block3, out batch*(x/4)*(y/4)*128
for i in range(0,18):
identity = x
if i == 0:
identity=Conv2D(128, (2,2), padding='same', kernel_regularizer=regularizer, name='block3dimchangeconv')(identity)
x = Conv2D(32, (1, 1), padding='same', kernel_regularizer=regularizer, name='block3resblock'+str((i+1))+'conv1')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(32, (3, 3), padding='same', kernel_regularizer=regularizer, name='block3resblock'+str((i+1))+'conv2')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(128, (1, 1), padding='same', kernel_regularizer=regularizer, name='block3resblock'+str((i+1))+'conv3')(x)
x = BatchNormalization()(x)
x = Add()([identity,x])
x = Activation('relu')(x)
l2 = x #256 filters, 16x16 resolution
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3pool')(x)
#block4, out batch*(x/4)*(y/4)*256
for i in range(0,18):
identity = x
if i == 0:
identity=Conv2D(256, (2,2), padding='same', kernel_regularizer=regularizer, name='block4dimchangeconv')(identity)
x = Conv2D(64, (1, 1), padding='same', kernel_regularizer=regularizer, name='block4resblock'+str((i+1))+'conv1')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(64, (3, 3), padding='same', kernel_regularizer=regularizer, name='block4resblock'+str((i+1))+'conv2')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(256, (1, 1), padding='same', kernel_regularizer=regularizer, name='block4resblock'+str((i+1))+'conv3')(x)
x = BatchNormalization()(x)
x = Add()([identity,x])
x = Activation('relu')(x)
l3 = x #512 filters, 8x8 resolution
x = Conv2D(256, (3, 3), padding='same', kernel_regularizer=regularizer, name='outconv')(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2,2), strides=(2,2), name="gpool")(x)
gbase = Flatten(name='pregflatten')(x)
g64 = Dense(64, kernel_regularizer=regularizer, name='globalg64')(gbase)
g128 = Dense(128, kernel_regularizer=regularizer, name='globalg128')(gbase)
g256 = Dense(256, kernel_regularizer=regularizer, name='globalg256')(gbase)
c1 = ParametrisedCompatibility(kernel_regularizer=regularizer, name='cpc1')([l1, g64]) # batch*x*y
if compatibilityfunction == 'dp':
c1 = Lambda(lambda lam: K.squeeze(K.map_fn(lambda xy: K.dot(xy[0], xy[1]), elems=(lam[0], K.expand_dims(lam[1], -1)), dtype='float32'), 3), name='cdp1')([l1, g64]) # batch*x*y
flatc1 = Flatten(name='flatc1')(c1) # batch*xy
a1 = Activation('softmax', name='softmax1')(flatc1) # batch*xy
reshaped1 = Reshape((-1,64), name='reshape1')(l1) # batch*xy*256.
g1 = Lambda(lambda lam: K.squeeze(K.batch_dot(K.expand_dims(lam[0], 1), lam[1]), 1), name='g1')([a1, reshaped1]) # batch*256.
c2 = ParametrisedCompatibility(kernel_regularizer=regularizer, name='cpc2')([l2, g128])
if compatibilityfunction == 'dp':
c2 = Lambda(lambda lam: K.squeeze(K.map_fn(lambda xy: K.dot(xy[0], xy[1]), elems=(lam[0], K.expand_dims(lam[1], -1)), dtype='float32'), 3), name='cdp2')([l2, g128])
flatc2 = Flatten(name='flatc2')(c2)
a2 = Activation('softmax', name='softmax2')(flatc2)
reshaped2 = Reshape((-1,128), name='reshape2')(l2)
g2 = Lambda(lambda lam: K.squeeze(K.batch_dot(K.expand_dims(lam[0], 1), lam[1]), 1), name='g2')([a2, reshaped2])
c3 = ParametrisedCompatibility(kernel_regularizer=regularizer, name='cpc3')([l3, g256])
if compatibilityfunction == 'dp':
c3 = Lambda(lambda lam: K.squeeze(K.map_fn(lambda xy: K.dot(xy[0], xy[1]), elems=(lam[0], K.expand_dims(lam[1], -1)), dtype='float32'), 3), name='cdp3')([l3, g256])
flatc3 = Flatten(name='flatc3')(c3)
a3 = Activation('softmax', name='softmax3')(flatc3)
reshaped3 = Reshape((-1,256), name='reshape3')(l3)
g3 = Lambda(lambda lam: K.squeeze(K.batch_dot(K.expand_dims(lam[0], 1), lam[1]), 1), name='g3')([a3, reshaped3])
out = ''
if gmode == 'concat':
glist = [g3]
if att == 'att2':
glist.append(g2)
if att == 'att3':
glist.append(g2)
glist.append(g1)
predictedG = g3
if att != 'att1' and att != 'att':
predictedG = Concatenate(axis=1, name='ConcatG')(glist)
x = Dense(outputclasses, kernel_regularizer=regularizer, name=str(outputclasses)+'ConcatG')(predictedG)
out = Activation("softmax", name='concatsoftmaxout')(x)
else:
gd3 = Dense(outputclasses, activation='softmax', name=str(outputclasses)+'indepsoftmaxg3')(g3)
if att == 'att' or att == 'att1':
out = gd3
elif att == 'att2':
gd2 = Dense(outputclasses, activation='softmax', kernel_regularizer=regularizer, name=str(outputclasses)+'indepsoftmaxg2')(g2)
out = Add(name='addg3g2')([gd3, gd2])
out = Lambda(lambda lam: lam/2, name='2average')(out)
else:
gd2 = Dense(outputclasses, activation='softmax', kernel_regularizer=regularizer, name=str(outputclasses)+'indepsoftmaxg2')(g2)
gd1 = Dense(outputclasses, activation='softmax', kernel_regularizer=regularizer, name=str(outputclasses)+'indepsoftmaxg2')(g1)
out = Add(name='addg3g2g1')([gd1, gd2, gd3])
out = Lambda(lambda lam: lam/3, name='3average')(out)
model = Model(inputs=inp, outputs=out)
model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
name = ("(RN-"+att+")-"+gmode+"-"+compatibilityfunction).replace('att)', 'att1)')
print("Generated "+name)
self.name = name
self.model = model
def StandardFit(self, datasetname=None, X=[], Y=[], beep=False, initial_lr=0.01, min_delta=None, patience=3, validation_data=None, lrplateaufactor=None, lrplateaupatience=4):
Y = keras.utils.to_categorical(Y,self.outputclasses)
if datasetname==None:
datasetname=self.datasetname
if os.path.isfile("weights/"+self.name+"-"+datasetname+" early.hdf5"):
print("Found early-stopped weights for "+self.name+"-"+datasetname)
return
scheduler = LearningRateScaler([60, 120, 160], 0.2, initial_lr)
startingepoch = 0
pastepochs = list(map(int, [x.replace(".hdf5", "").replace(self.name+"-"+datasetname, "").replace(" ", "") for x in os.listdir("weights") if (self.name+"-"+datasetname in x) & ("early" not in x)]))
if pastepochs:
if max(pastepochs) == 200:
print("Found completely trained weights for "+self.name+"-"+datasetname)
return
self.model.load_weights("weights/"+self.name+"-"+datasetname+" "+str(max(pastepochs))+".hdf5")
startingepoch = max(pastepochs)
tboardcb = TensorBoard(log_dir='./logs', histogram_freq=0, batch_size=3, write_graph=True, write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)
checkpoint = ModelCheckpoint("weights/"+self.name+"-"+datasetname+" {epoch}.hdf5", save_weights_only=True)
epochprint = LambdaCallback(on_epoch_end=lambda epoch, logs: print("Passed epoch "+str(epoch)))
callbackslist = [scheduler, checkpoint, epochprint, tboardcb]
if beep:
callbackslist.append(Beeper(1))
if validation_data == None:
self.model.fit(X, Y, 64, 200, callbacks=callbackslist, initial_epoch=startingepoch,shuffle=True)
else:
if min_delta != None:
callbackslist.append(EarlyStopping(monitor='val_acc', min_delta=min_delta, patience=patience))
if lrplateaufactor != None:
callbackslist.append(ReduceLROnPlateau(monitor='acc', factor = lrplateaufactor, patience = lrplateaupatience))
self.model.fit(X, Y, 64, 200, callbacks=callbackslist, initial_epoch=startingepoch,shuffle=True,validation_data=(validation_data[0], keras.utils.to_categorical(validation_data[1],self.outputclasses)))
self.model.save_weights("weights/"+self.name+"-"+datasetname+" early.hdf5")
pastepochs = list(map(int, [x.replace(".hdf5", "").replace(self.name+"-"+datasetname, "").replace(" ", "") for x in os.listdir("weights") if (self.name+"-"+datasetname in x) & ("early" not in x)]))
if max(pastepochs) > 190:
for filenum in range(1,197):
try:
os.remove("weights/"+self.name+"-"+datasetname+" "+str(filenum)+".hdf5")
except OSError:
pass
return self.model
class ParametrisedCompatibility(Layer):
def __init__(self, kernel_regularizer=None, **kwargs):
super(ParametrisedCompatibility, self).__init__(**kwargs)
self.regularizer = kernel_regularizer
def build(self, input_shape):
self.u = self.add_weight(name='u', shape=(input_shape[0][3], 1), initializer='uniform', regularizer=self.regularizer, trainable=True)
super(ParametrisedCompatibility, self).build(input_shape)
def call(self, x): # add l and g. Dot the sum with u.
return K.dot(K.map_fn(lambda lam: (lam[0]+lam[1]),elems=(x),dtype='float32'), self.u)
def compute_output_shape(self, input_shape):
return (input_shape[0][0], input_shape[0][1], input_shape[0][2])
class LearningRateScaler(Callback):
def __init__(self, epochs, multiplier, initial_lr=None):
self.multiplier = multiplier
self.epochs = epochs
self.initial_lr = initial_lr
self.startingepoch = True
def on_train_begin(self, logs=None):
if self.initial_lr == None:
self.initial_lr = K.get_value(self.model.optimizer.lr)
print("Initial lr="+str(self.initial_lr))
def on_epoch_begin(self, epoch, logs=None):
if not hasattr(self.model.optimizer, 'lr'):
raise ValueError('Optimizer must have a "lr" attribute.')
print("Current lr: " + str(K.get_value(self.model.optimizer.lr)))
lr = self.initial_lr
if (epoch > 0 and epoch % self.epochs == 0) or self.startingepoch:
for i in range(0, epoch // self.epochs):
lr = lr * self.multiplier
K.set_value(self.model.optimizer.lr, lr)
print("Updated learning rate to "+str(lr))
self.startingepoch = False
elif isinstance(self.epochs, list):
if epoch>0 and epoch in self.epochs.sort():
for i in range(0, self.epochs.sort().index(epoch)+1):
lr = lr * self.multiplier
K.set_value(self.model.optimizer.lr, lr)
print("Updated learning rate to "+str(lr))
def on_epoch_end(self, epoch, logs=None):
startingepoch = False
class Beeper(Callback):
def __init__(self, batches):
self.batches = batches
def on_batch_end(self, batch, logs={}):
if batch > 0 and batch % self.batches == 0:
winsound.Beep(440,150)
if __name__ == "__main__":
testmodel = StandardVGG()