-
Notifications
You must be signed in to change notification settings - Fork 0
/
zzxModel.py
345 lines (287 loc) · 11.2 KB
/
zzxModel.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = {
'name': 'Zhaoxi Zhang',
'Email': 'zhaoxi_zhang@163.com',
'QQ': '809536596',
'Created': ''
}
import numpy as np
import time
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential, Model, load_model
from tensorflow.keras.layers import Dense, Dropout, Flatten, Activation, Input, AveragePooling2D
from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization, ReLU, Reshape, Conv2DTranspose
from tensorflow.keras import regularizers
from tensorflow.keras.optimizers import Adam, SGD
from tensorflow.keras.regularizers import l2
from tensorflow.keras.utils import get_custom_objects
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.layers import LeakyReLU
import zzxDataset
import os
import zzxFunc
@tf.custom_gradient
def floor_func(y):
# y=tf.nn.relu(y)
def backward(dy):
return dy
# return tf.maximum(tf.floor(y*10)/10,tf.zeros_like(y)), backward
return tf.floor(y * 10) / 10, backward
def floor_relu(y):
return floor_func(tf.nn.relu(y))
# return floor_func(tf.nn.relu(y+0.1*tf.sin(10*y)))
@tf.custom_gradient
def step_func(y):
def backward(dy):
return dy
return tf.cast(y>tf.zeros_like(y),dtype='float32')+tf.nn.relu(y), backward
def step_relu(y):
return step_func(tf.nn.relu(y))
get_custom_objects().update({'step_relu': Activation(step_relu)})
get_custom_objects().update({'floor_relu': Activation(floor_relu)})
def lr_schedule(epoch):
"""Learning Rate Schedule
Learning rate is scheduled to be reduced after 80, 120, 160, 180 epochs.
Called automatically every epoch as part of callbacks during training.
# Arguments
epoch (int): The number of epochs
# Returns
lr (float32): learning rate
"""
lr = 1e-1
if epoch > 180:
lr *= 1e-4
elif epoch > 160:
lr *= 1e-3
elif epoch > 120:
lr *= 1e-2
elif epoch > 80:
lr *= 1e-1
# elif epoch > 10:
# lr *= 1e-1
print('Learning rate: ', lr)
return lr
class zzxModel():
def __init__(
self,
dataset,
batch_size=32,
epochs=200,
act_func='relu',
kernel_size=(3,3),
custom_act_flag=False,
build_dir=True,
learning_rate=1e-3,
opt=None
):
self.dataset=dataset
self.custom_act_flag=custom_act_flag
self.act_func=act_func
self.batch_size = batch_size # orig paper trained all networks with batch_size=128
self.epochs = epochs
self.num_classes=self.dataset.num_classes
self.input_shape=dataset.input_shape
self.learning_rate=learning_rate
self.kernel_size=kernel_size
self.opt=opt
self.loss=None
self.metrics=None
self.model=None
self.importDataset()
if build_dir:
self.buildFolder()
def setModel(
self,
model_path=None,
weight_path=None
):
if not (model_path is None):
self.loadModel(model_path=model_path)
else:
self.setArchitecture()
self.model.summary()
if not (weight_path is None):
self.loadWeights(weights_path=weight_path)
def setArchitecture(self):
return None
def importDataset(self):
(self.x_train,self.y_train),(self.x_test,self.y_test)=self.dataset.getData()
def fitModel(self, opt=Adam):
self.model.compile(
loss=self.loss,
optimizer=opt,
metrics=self.metrics
)
lr_scheduler = LearningRateScheduler(lr_schedule)
# lr_reducer = ReduceLROnPlateau(
# factor=np.sqrt(0.1),
# cooldown=0,
# patience=5,
# min_lr=0.5e-6
# )
# callbacks = [lr_scheduler]#lr_reducer,
self.model.fit(
self.x_train,
self.y_train,
batch_size=self.batch_size,
epochs=self.epochs,
validation_data=(self.x_test, self.y_test),
shuffle=True,
# callbacks=callbacks
)
def saveModel(self,modelName):
self.model.save(self.saveModelPath+'/'+modelName)
def saveModelWeights(self,weightName):
self.model.save_weights(self.saveModelPath+'/'+weightName)
def evaluateModel(self, x_test=None, y_test=None):
try:
if x_test==None:
x_test=self.x_test
y_test=self.y_test
except:
pass
scores = self.model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
def load_model(self, model_path=None, weights_path=None):
if not (model_path is None):
self.loadModel(model_path)
if not (weights_path is None):
self.loadWeights(weights_path)
def loadModel(self,model_path):
self.model=load_model(model_path)
def loadWeights(self,weights_path):
self.model.load_weights(weights_path)
def buildFolder(self):
self.timeStamp = zzxFunc.getTimeStamp()
self.saveImgPath = "images/" + self.timeStamp
self.saveModelPath = "savedModels/" + self.timeStamp
zzxFunc.buildDirs(self.saveModelPath)
zzxFunc.buildDirs(self.saveImgPath)
def predict_label(self, data):
return np.argmax(self.model.predict(data), axis=1)
class VGG(zzxModel):
def setArchitecture(self):
model = Sequential()
weight_decay = 0.0005
model.add(Conv2D(64, (3, 3), padding='same',
input_shape=self.input_shape, kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(Dropout(0.3))
model.add(Conv2D(64, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(Dropout(0.4))
model.add(Conv2D(128, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(Dropout(0.4))
model.add(Conv2D(256, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(Dropout(0.4))
model.add(Conv2D(256, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(Dropout(0.4))
model.add(Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(Dropout(0.4))
model.add(Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(Dropout(0.4))
model.add(Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(Dropout(0.4))
model.add(Conv2D(512, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(512, kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation(self.act_func))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(self.num_classes))
model.add(Activation('softmax'))
lr_decay = 1e-6
self.opt = SGD(
lr=self.learning_rate,
decay=lr_decay,
momentum=0.9,
nesterov=True
)
self.loss='categorical_crossentropy'
self.metrics=['accuracy']
model.summary()
self.model= model
def resnet_layer(inputs,
num_filters=16,
kernel_size=3,
strides=1,
activation='relu',
batch_normalization=True,
conv_first=True):
"""2D Convolution-Batch Normalization-Activation stack builder
# Arguments
inputs (tensor): input tensor from input image or previous layer
num_filters (int): Conv2D number of filters
kernel_size (int): Conv2D square kernel dimensions
strides (int): Conv2D square stride dimensions
activation (string): activation name
batch_normalization (bool): whether to include batch normalization
conv_first (bool): conv-bn-activation (True) or
bn-activation-conv (False)
# Returns
x (tensor): tensor as input to the next layer
"""
conv = Conv2D(num_filters,
kernel_size=kernel_size,
strides=strides,
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l2(1e-4))
x = inputs
if conv_first:
x = conv(x)
if batch_normalization:
x = BatchNormalization()(x)
if activation is not None:
x = Activation(activation)(x)
else:
if batch_normalization:
x = BatchNormalization()(x)
if activation is not None:
x = Activation(activation)(x)
x = conv(x)
return x
if __name__=='__main__':
model=zzxModel()
# dataAdv = np.array(pd.read_csv(r'data/adv-CW-L2.csv'))