-
Notifications
You must be signed in to change notification settings - Fork 15
/
CNN_trainer.py
371 lines (295 loc) · 15.8 KB
/
CNN_trainer.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
import os
import sys
sys.path.append("../")
import tensorflow as tf
from UTKdata_engine import DataEngineTypical as DET
from face_recognition.model_scripts import main_model_architect as MMA
from face_recognition.model_scripts import tensorboard_helper as TBH
class MainModel(MMA.MainModel):
@property
def __name__(self):
return "FromArcFace"
@tf.function
def test_step_reg(self, x, y):
age_pred, sex_pred, eth_pred = self.model([x, y], training=False)
loss = self.loss_function(y, age_pred, sex_pred, eth_pred)
reg_loss = tf.add_n(self.model.losses)
return age_pred, sex_pred, eth_pred, loss, reg_loss
@tf.function
def train_step_reg(self, x, y):
with tf.GradientTape() as tape:
age_pred, sex_pred, eth_pred = self.model([x, y], training=True)
loss = self.loss_function(y, age_pred, sex_pred, eth_pred)
reg_loss = tf.add_n(self.model.losses)
loss_all = tf.add(loss, reg_loss)
gradients = tape.gradient(loss_all, self.model.trainable_variables)
self.optimizer.apply_gradients(zip(gradients, self.model.trainable_variables))
return age_pred, sex_pred, eth_pred, loss, reg_loss
def ASE_loss_function(self, y_real, age_pred, sex_pred, eth_pred):
age_real, sex_real, eth_real = tf.split(y_real, 3, axis=1)
age_loss = self.scc_loss(age_real, age_pred)
sex_loss = self.scc_loss(sex_real, sex_pred)
eth_loss = self.scc_loss(eth_real, eth_pred)
return tf.add_n([age_loss, sex_loss, eth_loss])
def __init__(self):
super(MainModel, self).__init__()
self.scc_loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
# self.mse_loss = tf.keras.losses.MeanSquaredError()
self.loss_function = self.ASE_loss_function
def __call__(self, input_shape, weights: str = None, arcface_model_path: str = None,
ASE_model_path: str = None, num_classes: int = 10,
regularizer_l: float = 5e-4, use_arcface: bool = True, optimizer="ADAM", learning_rate: float = 0.1,
pooling_layer: tf.keras.layers.Layer = tf.keras.layers.GlobalAveragePooling2D):
self.last_lr = learning_rate
if optimizer == "ADAM":
self.optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
print("[*] ADAM chosen as optimizer")
elif optimizer == "SGD":
self.optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate, momentum=0.9)
print("[*] SGD chosen as optimizer")
elif optimizer == "MOMENTUM":
self.optimizer = tf.compat.v1.train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.9)
# MomentumOptimizer is not recommended, it is from TF 1.x makes problem at learning rate change, i will update if TF 2.x version comes out
print("[*] MomentumOptimizer chosen as optimizer")
else:
raise Exception(f"{optimizer} is not a valid name! Go with either ADAM, SGD or MOMENTUM")
if ASE_model_path is not None and os.path.exists(ASE_model_path):
self.model = tf.keras.models.load_model(ASE_model_path, custom_objects={"ArcFaceLayer": MMA.ArcFaceLayer})
self.model.trainable = True
self.change_regularizer_l(regularizer_l)
self.model.summary()
print(f"[*] ASE model loaded from {ASE_model_path}")
elif arcface_model_path is not None and os.path.exists(arcface_model_path):
self.model = tf.keras.models.load_model(arcface_model_path)
self.model.trainable = True
self.change_regularizer_l(regularizer_l)
x1 = self.model.layers[-1].output
x = MMA.BatchNormalization(momentum=0.9, scale=False, epsilon=2e-5, name="bn_ase_sub")(x1)
label_input_layer = tf.keras.layers.Input((None,), dtype=tf.int64)
if use_arcface:
x_age = MMA.ArcFaceLayer(num_classes=24, arc_m=0.5, arc_s=10., regularizer_l=regularizer_l,
name="ageArcFace")(x, label_input_layer)
x_sex = MMA.ArcFaceLayer(num_classes=2, arc_m=0.5, arc_s=10., regularizer_l=regularizer_l,
name="sexArcFace")(x, label_input_layer)
x_eth = MMA.ArcFaceLayer(num_classes=5, arc_m=0.5, arc_s=10., regularizer_l=regularizer_l,
name="ethArcFace")(x, label_input_layer)
else:
x_age = tf.keras.layers.Dense(24, activation=None, name="ageClassificationLayer",
kernel_regularizer=tf.keras.regularizers.l2(regularizer_l))(x)
x_sex = tf.keras.layers.Dense(2, activation=None, name="sexClassificationLayer",
kernel_regularizer=tf.keras.regularizers.l2(regularizer_l))(x)
x_eth = tf.keras.layers.Dense(5, activation=None, name="ethClassificationLayer",
kernel_regularizer=tf.keras.regularizers.l2(regularizer_l))(x)
self.model = tf.keras.models.Model([self.model.layers[0].input, label_input_layer], [x_age, x_sex, x_eth],
name=f"{self.__name__}-ASE")
self.model.summary()
print(f"[*] Model created from ArcFace Model that placed in {arcface_model_path}")
else:
label_input_layer = tf.keras.layers.Input((None,), dtype=tf.int64)
self.model = self.get_model(input_shape=input_shape, weights=weights)
self.model.trainable = True
self.change_regularizer_l(regularizer_l)
# ACCORDING TO ARCFACE PAPER
x = pooling_layer()(self.model.layers[-1].output)
x = MMA.BatchNormalization(momentum=0.9, epsilon=2e-5)(x)
x = tf.keras.layers.Dropout(0.4)(x)
x1 = tf.keras.layers.Dense(512, activation=None, name="features_without_bn", use_bias=True,
kernel_regularizer=tf.keras.regularizers.l2(regularizer_l))(x)
x = MMA.BatchNormalization(momentum=0.9, scale=False, epsilon=2e-5)(x1)
if use_arcface:
x_age = MMA.ArcFaceLayer(num_classes=24, arc_m=0.5, arc_s=64., regularizer_l=regularizer_l,
name="ageArcFace")(x, label_input_layer)
x_sex = MMA.ArcFaceLayer(num_classes=2, arc_m=0.5, arc_s=64., regularizer_l=regularizer_l,
name="sexArcFace")(x, label_input_layer)
x_eth = MMA.ArcFaceLayer(num_classes=5, arc_m=0.5, arc_s=64., regularizer_l=regularizer_l,
name="ethArcFace")(x, label_input_layer)
else:
x_age = tf.keras.layers.Dense(24, activation=None, name="ageClassificationLayer",
kernel_regularizer=tf.keras.regularizers.l2(regularizer_l))(x)
x_sex = tf.keras.layers.Dense(2, activation=None, name="sexClassificationLayer",
kernel_regularizer=tf.keras.regularizers.l2(regularizer_l))(x)
x_eth = tf.keras.layers.Dense(5, activation=None, name="ethClassificationLayer",
kernel_regularizer=tf.keras.regularizers.l2(regularizer_l))(x)
self.model = tf.keras.models.Model([self.model.layers[0].input, label_input_layer], [x_age, x_sex, x_eth],
name=f"{self.__name__}-ASE")
self.model.summary()
print("[*] Model structure created from scratch")
class ResNet50(MainModel, MMA.ResNet50):
pass
class Trainer:
@staticmethod
def get_wrong(y_real, y_pred):
return tf.where(
tf.cast(tf.equal(tf.argmax(tf.nn.softmax(y_pred), -1), tf.cast(y_real, tf.int64)), tf.float32) == 00)
@staticmethod
def calculate_accuracy(y_real, age_pred, sex_pred, eth_pred):
age_real, sex_real, eth_real = tf.split(y_real, 3, axis=1)
acc_age = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(tf.nn.softmax(age_pred), axis=-1), tf.cast(age_real, tf.int64)), dtype=tf.float32))
acc_sex = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(tf.nn.softmax(sex_pred), axis=-1), tf.cast(sex_real, tf.int64)), dtype=tf.float32))
acc_eth = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(tf.nn.softmax(eth_pred), axis=-1), tf.cast(eth_real, tf.int64)), dtype=tf.float32))
return acc_age, acc_sex, acc_eth
def only_test(self, dataset_test=None):
if dataset_test is None:
if self.dataset_engine.dataset_test is None:
raise Exception("there is no defined test dataset")
dataset_test = self.dataset_engine.dataset_test
acc_age_mean = tf.keras.metrics.Mean()
acc_sex_mean = tf.keras.metrics.Mean()
acc_eth_mean = tf.keras.metrics.Mean()
loss_mean = tf.keras.metrics.Mean()
for i, (x, y) in enumerate(dataset_test):
age_pred, sex_pred, eth_pred, loss, reg_loss = self.model_engine.test_step_reg(x, y)
acc_age, acc_sex, acc_eth = self.calculate_accuracy(y, age_pred, sex_pred, eth_pred)
acc_age_mean(acc_age)
acc_sex_mean(acc_sex)
acc_eth_mean(acc_eth)
loss_mean(loss)
print(f"[*] Step {i}, Age Acc --> %{acc_age} || Sex Acc --> %{acc_sex} || Eth Acc --> %{acc_eth}"
f" || Loss --> {loss} || Reg Loss --> {reg_loss}")
print(f"\n\n[*] Age Acc Mean --> %{acc_age_mean.result().numpy()} || Sex Acc Mean --> %{acc_sex_mean.result().numpy()} || Eth Acc Mean --> %{acc_eth_mean.result().numpy()}"
f" || Loss Mean --> {loss_mean.result().numpy()}")
return acc_age_mean, acc_sex_mean, acc_eth_mean, loss_mean
def __init__(self, model_engine: MMA, dataset_engine: DET, tensorboard_engine: TBH, use_arcface: bool,
learning_rate: float = 0.01,
model_path: str = "classifier_model.tf", arcface_model_path: str = "arcface_final.h5",
pooling_layer: tf.keras.layers.Layer = tf.keras.layers.GlobalAveragePooling2D,
lr_step_dict: dict = None,
optimizer: str = "ADAM", regularizer_l: float = 5e-4):
self.model_path = model_path
self.model_engine = model_engine
self.dataset_engine = dataset_engine
self.tensorboard_engine = tensorboard_engine
self.use_arcface = use_arcface
self.lr_step_dict = lr_step_dict
self.num_classes = 2 # 2 for deepfake
tf.io.gfile.makedirs("/".join(self.model_path.split("/")[:-1]))
self.tb_delete_if_exists = True
if self.lr_step_dict is not None:
print("[*] LEARNING RATE WILL BE CHECKED WHEN step\\alfa_divided_ten == 0")
learning_rate = list(self.lr_step_dict.values())[0]
self.model_engine(
input_shape=(112, 112, 3),
num_classes=self.num_classes,
learning_rate=learning_rate,
regularizer_l=regularizer_l, # weight decay, train once with 5e-4 and then try something lower such 1e-5
pooling_layer=pooling_layer, # Recommended: GlobalAveragePooling
use_arcface=self.use_arcface, # set False if you want to train it as regular classification
ASE_model_path=self.model_path, # path of keras model(h5) , it is okay if doesn't exists
arcface_model_path=arcface_model_path, # path of arcface keras model(h5) , it is okay if doesn't exists
optimizer=optimizer # Recommended: SGD
)
def __call__(self, max_iteration: int = None, alfa_step=1000, qin: int = 10):
if max_iteration is not None and max_iteration <= 0:
max_iteration = None
alfa_divided_ten = int(alfa_step / 10)
alfa_multiplied_qin = int(alfa_step * qin)
print(f"[*] Possible maximum step: {tf.data.experimental.cardinality(self.dataset_engine.dataset)}\n")
acc_age_mean = tf.keras.metrics.Mean()
acc_sex_mean = tf.keras.metrics.Mean()
acc_eth_mean = tf.keras.metrics.Mean()
loss_mean = tf.keras.metrics.Mean()
self.tensorboard_engine.initialize(
delete_if_exists=self.tb_delete_if_exists
)
print(f"[*] TensorBoard initialized on {self.tensorboard_engine.logdir}")
for i, (x, y) in enumerate(self.dataset_engine.dataset):
age_pred, sex_pred, eth_pred, loss, reg_loss = self.model_engine.train_step_reg(x, y)
acc_age, acc_sex, acc_eth = self.calculate_accuracy(y, age_pred, sex_pred, eth_pred)
acc_age_mean(acc_age)
acc_sex_mean(acc_sex)
acc_eth_mean(acc_eth)
loss_mean(loss)
self.tensorboard_engine({"loss": loss, "reg_loss": reg_loss,
"age_acc": acc_age, "acc_sex": acc_sex, "acc_eth": acc_eth})
if i % alfa_divided_ten == 0:
if i % alfa_step == 0 and i > 10:
self.model_engine.model.save(self.model_path)
print(f"[{i}] Model saved to {self.model_path}")
print(f"[{i}] Loss: {loss_mean.result().numpy()} || Reg Loss: {reg_loss.numpy()} ||"
f" Age Acc: %{acc_age_mean.result().numpy()} || Sex Acc: %{acc_sex_mean.result().numpy()} || Eth Acc: %{acc_eth_mean.result().numpy()} ||"
f" LR: {self.model_engine.optimizer.learning_rate.numpy()}")
acc_age_mean.reset_states()
acc_sex_mean.reset_states()
acc_eth_mean.reset_states()
loss_mean.reset_states()
if self.lr_step_dict is not None:
lower_found = False
for key in self.lr_step_dict:
if i < int(key):
lower_found = True
lr_should_be = self.lr_step_dict[key]
if lr_should_be != self.model_engine.last_lr:
self.model_engine.change_learning_rate_of_optimizer(lr_should_be)
print(f"[{i}] Learning Rate set to --> {lr_should_be}")
break
if not lower_found:
print(f"[{i}] Reached to given maximum steps in 'lr_step_dict'({list(self.lr_step_dict.keys())[-1]})")
self.model_engine.model.save(self.model_path)
print(f"[{i}] Model saved to {self.model_path}, end of training.")
break
if i % alfa_multiplied_qin == 0 and self.dataset_engine.dataset_test is not None and i > 10:
print("[*] Calculating validation loss and accuracy, this may take some time")
acc_age_mean.reset_states()
acc_sex_mean.reset_states()
acc_eth_mean.reset_states()
loss_mean.reset_states()
for x_test, y_test in self.dataset_engine.dataset_test:
age_pred, sex_pred, eth_pred, loss, reg_loss = self.model_engine.test_step_reg(x_test, y_test)
acc_age, acc_sex, acc_eth = self.calculate_accuracy(y_test, age_pred, sex_pred, eth_pred)
self.tensorboard_engine({"val. loss": loss, "val. age_acc": acc_age, "val. acc_sex": acc_sex, "val. acc_eth": acc_eth})
acc_age_mean(acc_age)
acc_sex_mean(acc_sex)
acc_eth_mean(acc_eth)
loss_mean(loss)
print(f"[{i}] Val. Loss --> {loss_mean.result().numpy()} || "
f"Val. Age Acc: %{acc_age_mean.result().numpy()} || Val. Sex Acc: %{acc_sex_mean.result().numpy()} || Val. Eth Acc: %{acc_eth_mean.result().numpy()} ||")
acc_age_mean.reset_states()
acc_sex_mean.reset_states()
acc_eth_mean.reset_states()
loss_mean.reset_states()
if max_iteration is not None and i >= max_iteration:
print(f"[{i}] Reached to given maximum iteration({max_iteration})")
self.model_engine.model.save(self.model_path)
print(f"[{i}] Model saved to {self.model_path}, end of training.")
break
if max_iteration is None:
print(f"[*] Reached to end of dataset")
self.model_engine.model.save(self.model_path)
print(f"[*] Model saved to {self.model_path}, end of training.")
def save_final_model(self, path: str = "deepfake_final.h5", n: int = -1, sum_it: bool = True):
m = tf.keras.models.Model(self.model_engine.model.layers[0].input, self.model_engine.model.layers[n].output)
if sum_it:
m.summary()
m.save(path)
print(f"[*] Final feature extractor saved to {path}")
if __name__ == '__main__':
TDOM = DET(
"../datasets/UTKFace/", # tfrecord path
batch_size=16,
epochs=-1, # set to "-1" so it can stream forever
buffer_size=100000,
reshuffle_each_iteration=True, # set True if you set test_batch to 0
) # TDOM for "Tensorflow Dataset Object Manager"
TBE = TBH.TensorBoardCallback(
logdir="classifier_tensorboard" # folder to write TensorBoard
) # TBE for "TensorBoard Engine"
ME = ResNet50() # ME for "Model Engine"
k_value: float = 0.5
trainer = Trainer(
model_engine=ME,
dataset_engine=TDOM,
tensorboard_engine=TBE,
use_arcface=False, # set False if you want to train a normal classification model
learning_rate=0.004, # it doesn't matter if you set lr_step_dict to anything but None
model_path="ASE_model.h5", # it will save only weights, you can chose "h5" as extension too
arcface_model_path="models_all/arcface_final.h5",
optimizer="SGD", # SGD, ADAM or MOMENTUM. MOMENTUM is not recommended
lr_step_dict={
int(60000 * k_value): 0.01,
int(80000 * k_value): 0.001,
int(100000 * k_value): 0.0005,
int(140000 * k_value): 0.0001,
},
regularizer_l=5e-4 # "l" parameter for l2 regularizer
)
trainer(max_iteration=-1, alfa_step=5000, qin=2)