-
Notifications
You must be signed in to change notification settings - Fork 1
/
esr_rafdb.py
536 lines (415 loc) · 21.2 KB
/
esr_rafdb.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
"""
Experiments on training diversified ESR for facial expression recognition on FER+.
Code is adapted based on:
https://github.com/siqueira-hc/Efficient-Facial-Feature-Learning-with-Wide-Ensemble-based-Convolutional-Neural-Networks
"""
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "5"
# External Libraries
from torch.utils.data import DataLoader
from torchvision import transforms
import torch.nn.functional as F
import torch.optim as optim
import torch.nn as nn
import numpy as np
import torch
# Standard Libraries
from os import path, makedirs
import copy
# Modules
from utils import udata, umath
from model.diversified_esr import ESR
from model.cbam import CBAM
class BranchDiversity(nn.Module):
def __init__(self, ):
super(BranchDiversity, self).__init__()
self.direct_div = 0
self.det_div = 0
self.logdet_div = 0
def forward(self, x, type='spatial'):
num_branches = x.size(0)
gamma = 10
snm = torch.zeros((num_branches, num_branches))
# Spatial attnention diversity
if type == 'spatial': # num_branch x batch_size x 6 x 6
# diversity between spatial attention heads
for i in range(num_branches):
for j in range(num_branches):
if i != j:
diff = torch.exp(-1 * gamma * torch.sum(torch.square(x[i, :, :, :] - x[j, :, :, :]), (1, 2)))
# size: batch_size
diff = torch.mean(diff) # (1/num_branches) * torch.sum(diff) # size: 1
snm[i, j] = diff
self.direct_div = torch.sum(snm)
self.det_div = -1 * torch.det(snm)
self.logdet_div = -1 * torch.logdet(snm)
# Channel attn diversity
elif type == 'channel': # num_branch x batch_size x 512
# diversity between channels of attention heads
for i in range(num_branches):
for j in range(num_branches):
if i != j:
diff = torch.exp(
-1 * gamma * torch.sum(torch.square(x[i, :, :] - x[j, :, :]), 1)) # size: batch_size
diff = torch.mean(diff) # (1/num_branches) * torch.sum(diff) # size: 1
snm[i, j] = diff
self.direct_div = torch.sum(snm)
self.det_div = -1 * torch.det(snm)
self.logdet_div = -1 * torch.logdet(snm)
return self
class Base(nn.Module):
def __init__(self):
super(Base, self).__init__()
self.conv1 = nn.Conv2d(3, 64, 5, 1)
self.conv2 = nn.Conv2d(64, 128, 3, 1)
self.conv3 = nn.Conv2d(128, 128, 3, 1)
self.conv4 = nn.Conv2d(128, 128, 3, 1)
self.bn1 = nn.BatchNorm2d(64)
self.bn2 = nn.BatchNorm2d(128)
self.bn3 = nn.BatchNorm2d(128)
self.bn4 = nn.BatchNorm2d(128)
self.pool = nn.MaxPool2d(2, 2)
def forward(self, x_base_to_process):
x_base = F.relu(self.bn1(self.conv1(x_base_to_process)))
x_base = self.pool(F.relu(self.bn2(self.conv2(x_base))))
x_base = F.relu(self.bn3(self.conv3(x_base)))
x_base = self.pool(F.relu(self.bn4(self.conv4(x_base))))
return x_base
class Branch(nn.Module):
def __init__(self):
super(Branch, self).__init__()
self.conv1 = nn.Conv2d(128, 128, 3, 1)
self.conv2 = nn.Conv2d(128, 256, 3, 1)
self.conv3 = nn.Conv2d(256, 256, 3, 1)
self.conv4 = nn.Conv2d(256, 512, 3, 1, 1)
self.bn1 = nn.BatchNorm2d(128)
self.bn2 = nn.BatchNorm2d(256)
self.bn3 = nn.BatchNorm2d(256)
self.bn4 = nn.BatchNorm2d(512)
self.cbam1 = CBAM(gate_channels=128, reduction_ratio=16, pool_types=['avg', 'max'], no_spatial=False)
self.cbam2 = CBAM(gate_channels=256, reduction_ratio=16, pool_types=['avg', 'max'], no_spatial=False)
self.cbam3 = CBAM(gate_channels=256, reduction_ratio=16, pool_types=['avg', 'max'], no_spatial=False)
self.cbam4 = CBAM(gate_channels=512, reduction_ratio=16, pool_types=['avg', 'max'], no_spatial=False)
self.fc = nn.Linear(512, 7)
self.pool = nn.MaxPool2d(2, 2)
self.global_pool = nn.AdaptiveAvgPool2d(1)
def forward(self, x_branch_to_process):
x_conv_branch = F.relu(self.bn1(self.conv1(x_branch_to_process)))
x_conv_branch, _, _ = self.cbam1(x_conv_branch)
x_conv_branch = self.pool(F.relu(self.bn2(self.conv2(x_conv_branch))))
x_conv_branch, _, _ = self.cbam2(x_conv_branch)
x_conv_branch = F.relu(self.bn3(self.conv3(x_conv_branch)))
x_conv_branch, _, _ = self.cbam3(x_conv_branch)
x_conv_branch = F.relu(self.bn4(self.conv4(x_conv_branch)))
x_conv_branch, attn_ch, attn_sp = self.cbam4(x_conv_branch)
x_conv_branch = self.global_pool(x_conv_branch) # N x 512 x 1 x 1
x_conv_branch = x_conv_branch.view(-1, 512) # N x 512
discrete_emotion = self.fc(x_conv_branch)
return discrete_emotion, attn_ch, attn_sp
class Ensemble(nn.Module):
def __init__(self):
super(Ensemble, self).__init__()
self.base = Base()
self.branches = []
def get_ensemble_size(self):
return len(self.branches)
def add_branch(self):
self.branches.append(Branch())
def forward(self, x):
x_base = self.base(x)
emotions = []
attn_heads_sp = []
attn_heads_ch = []
for branch in self.branches:
output_emotion, attn_ch, attn_sp = branch(x_base)
emotions.append(output_emotion)
attn_heads_sp.append(attn_sp[:, 0, :, :])
attn_heads_sp.append(attn_ch)
attn_heads_sp = torch.stack(attn_heads_sp)
attn_heads_ch = torch.stack(attn_heads_ch)
return emotions, attn_heads_sp, attn_heads_ch
@staticmethod
def save(state_dicts, base_path_to_save_model, current_branch_save):
if not path.isdir(path.join(base_path_to_save_model, str(len(state_dicts) - 1 - current_branch_save))):
makedirs(path.join(base_path_to_save_model, str(len(state_dicts) - 1 - current_branch_save)))
torch.save(state_dicts[0],
path.join(base_path_to_save_model,
str(len(state_dicts) - 1 - current_branch_save),
"Net-Base-Shared_Representations.pt"))
for i in range(1, len(state_dicts)):
torch.save(state_dicts[i],
path.join(base_path_to_save_model,
str(len(state_dicts) - 1 - current_branch_save),
"Net-Branch_{}.pt".format(i)))
print("Network has been "
"saved at: {}".format(path.join(base_path_to_save_model,
str(len(state_dicts) - 1 - current_branch_save))))
@staticmethod
def load(device_to_load, ensemble_size):
# Load ESR-9
esr = ESR(device_to_load, ensemble_size=ensemble_size)
esr.load(device=device_to_load)
loaded_model = Ensemble()
loaded_model.branches = []
# Load the base of the network
loaded_model.base = esr.base
# Load branches
for i in range(ensemble_size):
loaded_model_branch = Branch()
loaded_model_branch.conv1 = esr.convolutional_branches[i].conv1
loaded_model_branch.conv2 = esr.convolutional_branches[i].conv2
loaded_model_branch.conv3 = esr.convolutional_branches[i].conv3
loaded_model_branch.conv4 = esr.convolutional_branches[i].conv4
loaded_model_branch.bn1 = esr.convolutional_branches[i].bn1
loaded_model_branch.bn2 = esr.convolutional_branches[i].bn2
loaded_model_branch.bn3 = esr.convolutional_branches[i].bn3
loaded_model_branch.bn4 = esr.convolutional_branches[i].bn4
loaded_model_branch.cbam1 = esr.convolutional_branches[i].cbam1
loaded_model_branch.cbam2 = esr.convolutional_branches[i].cbam2
loaded_model_branch.cbam3 = esr.convolutional_branches[i].cbam3
loaded_model_branch.cbam4 = esr.convolutional_branches[i].cbam4
loaded_model.branches.append(loaded_model_branch)
return loaded_model
def to_state_dict(self):
state_dicts = [copy.deepcopy(self.base.state_dict())]
for b in self.branches:
state_dicts.append(copy.deepcopy(b.state_dict()))
return state_dicts
def to_device(self, device_to_process="cpu"):
self.to(device_to_process)
self.base.to(device_to_process)
for b_td in self.branches:
b_td.to(device_to_process)
def reload(self, best_configuration):
self.base.load_state_dict(best_configuration[0])
for i in range(self.get_ensemble_size()):
self.branches[i].load_state_dict(best_configuration[i + 1])
def evaluate(val_model_eval, val_loader_eval, val_criterion_eval, device_to_process="cpu",
current_branch_on_training_val=0):
running_val_loss = [0.0 for _ in range(val_model_eval.get_ensemble_size())]
running_val_corrects = [0 for _ in range(val_model_eval.get_ensemble_size() + 1)]
running_val_steps = [0 for _ in range(val_model_eval.get_ensemble_size())]
for inputs_eval, labels_eval in val_loader_eval:
inputs_eval, labels_eval = inputs_eval.to(device_to_process), labels_eval.to(device_to_process)
outputs_eval, _, _ = val_model_eval(inputs_eval)
outputs_eval = outputs_eval[:val_model_eval.get_ensemble_size() - current_branch_on_training_val]
# Ensemble prediction
overall_preds = torch.zeros(outputs_eval[0].size()).to(device_to_process)
for o_eval, outputs_per_branch_eval in enumerate(outputs_eval, 0):
_, preds_eval = torch.max(outputs_per_branch_eval, 1)
running_val_corrects[o_eval] += torch.sum(preds_eval == labels_eval).cpu().numpy()
loss_eval = val_criterion_eval(outputs_per_branch_eval, labels_eval)
running_val_loss[o_eval] += loss_eval.item()
running_val_steps[o_eval] += 1
for v_i, v_p in enumerate(preds_eval, 0):
overall_preds[v_i, v_p] += 1
# Compute accuracy of ensemble predictions
_, preds_eval = torch.max(overall_preds, 1)
running_val_corrects[-1] += torch.sum(preds_eval == labels_eval).cpu().numpy()
for b_eval in range(val_model_eval.get_ensemble_size()):
div = running_val_steps[b_eval] if running_val_steps[b_eval] != 0 else 1
running_val_loss[b_eval] /= div
return running_val_loss, running_val_corrects
def plot(his_loss, his_acc, his_val_loss, his_val_acc, branch_idx, base_path_his):
accuracies_plot = []
legends_plot_acc = []
losses_plot = [[range(len(his_loss)), his_loss]]
legends_plot_loss = ["Training"]
# Acc
for b_plot in range(len(his_acc)):
accuracies_plot.append([range(len(his_acc[b_plot])), his_acc[b_plot]])
legends_plot_acc.append("Training ({})".format(b_plot + 1))
accuracies_plot.append([range(len(his_val_acc[b_plot])), his_val_acc[b_plot]])
legends_plot_acc.append("Validation ({})".format(b_plot + 1))
# Ensemble acc
accuracies_plot.append([range(len(his_val_acc[-1])), his_val_acc[-1]])
legends_plot_acc.append("Validation (E)")
# Loss
for b_plot in range(len(his_val_loss)):
losses_plot.append([range(len(his_val_loss[b_plot])), his_val_loss[b_plot]])
legends_plot_loss.append("Validation ({})".format(b_plot + 1))
# Loss
umath.plot(losses_plot,
title="Training and Validation Losses vs. Epochs for Branch {}".format(branch_idx),
legends=legends_plot_loss,
file_path=base_path_his,
file_name="Loss_Branch_{}".format(branch_idx),
axis_x="Training Epoch",
axis_y="Loss")
# Accuracy
umath.plot(accuracies_plot,
title="Training and Validation Accuracies vs. Epochs for Branch {}".format(branch_idx),
legends=legends_plot_acc,
file_path=base_path_his,
file_name="Acc_Branch_{}".format(branch_idx),
axis_x="Training Epoch",
axis_y="Accuracy",
limits_axis_y=(0.0, 1.0, 0.025))
# Save plots
np.save(path.join(base_path_his, "Loss_Branch_{}".format(branch_idx)), np.array(his_loss))
np.save(path.join(base_path_his, "Acc_Branch_{}".format(branch_idx)), np.array(his_acc))
np.save(path.join(base_path_his, "Loss_Val_Branch_{}".format(branch_idx)), np.array(his_val_loss))
np.save(path.join(base_path_his, "Acc_Val_Branch_{}".format(branch_idx)), np.array(his_val_acc))
def main():
# Experimental variables
base_path_experiment = "./experiments/RAFDB/"
name_experiment = "Diversified_ESR_9"
base_path_to_dataset = "../FER_data/RAF-DB/basic/"
num_branches_trained_network = 9 # for ESR-15, set it to 15
validation_interval = 1
max_training_epoch = 100
current_branch_on_training = 8 # for ESR-15, set it to 14
# Make dir
if not path.isdir(path.join(base_path_experiment, name_experiment)):
makedirs(path.join(base_path_experiment, name_experiment))
# Define transforms
data_transforms = transforms.Compose([transforms.Resize((96, 96)),
transforms.RandomHorizontalFlip(),
transforms.RandomApply([
transforms.RandomRotation(20),
transforms.RandomCrop(96, padding=32)], p=0.2),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
transforms.RandomErasing(scale=(0.02, 0.25)),
])
# Running device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Starting: {}".format(str(name_experiment)))
print("Running on {}".format(device))
# Load network trained on AffectNet
net = Ensemble.load(device, num_branches_trained_network)
# Send params to device
net.to_device(device)
# Set optimizer
optimizer = optim.SGD([{"params": net.base.parameters(), "lr": 0.1, "momentum": 0.9},
{"params": net.branches[0].parameters(), "lr": 0.1, "momentum": 0.9}])
for b in range(1, net.get_ensemble_size()):
optimizer.add_param_group({"params": net.branches[b].parameters(), "lr": 0.02, "momentum": 0.9})
# Define criterion
criterion_ce = nn.CrossEntropyLoss()
criterion_div = BranchDiversity()
# Load validation set
data_transforms_val = transforms.Compose([transforms.Resize((96, 96)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
# max_loaded_images_per_label=100000 loads the whole validation set
val_data = udata.RafDataSet(base_path_to_dataset, phase='test', transform=data_transforms_val)
val_loader = DataLoader(val_data, batch_size=32, shuffle=False, num_workers=8)
# Fine-tune ESR-9
for branch_on_training in range(num_branches_trained_network):
# Load training data
train_data = udata.RafDataSet(base_path_to_dataset, phase='train', transform=data_transforms)
# Best network
best_ensemble = net.to_state_dict()
best_ensemble_acc = 0.0
# Initialize scheduler
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.75, last_epoch=-1)
# History
history_loss = []
history_acc = [[] for _ in range(net.get_ensemble_size())]
history_val_loss = [[] for _ in range(net.get_ensemble_size())]
history_val_acc = [[] for _ in range(net.get_ensemble_size() + 1)]
# Training branch
for epoch in range(max_training_epoch):
train_loader = DataLoader(train_data, batch_size=32, shuffle=True, num_workers=8)
running_loss = 0.0
running_corrects = [0.0 for _ in range(net.get_ensemble_size())]
running_updates = 0
scheduler.step()
for inputs, labels in train_loader:
# Get the inputs
inputs, labels = inputs.to(device), labels.to(device)
# Set gradients to zero
optimizer.zero_grad()
# Forward
emotions, attn_sp, attn_ch = net(inputs)
confs_preds = [torch.max(o, 1) for o in emotions]
# Compute loss
loss = 0.0
for i_4 in range(net.get_ensemble_size() - current_branch_on_training):
preds = confs_preds[i_4][1]
running_corrects[i_4] += torch.sum(preds == labels).cpu().numpy()
loss += criterion_ce(emotions[i_4], labels)
if net.get_ensemble_size() > 1:
div_sp = criterion_div(attn_sp, type='spatial').det_div
loss += div_sp
div_ch = criterion_div(attn_sp, type='channel').det_div
loss += div_ch
# Backward
loss.backward()
# Optimize
optimizer.step()
# Save loss
running_loss += loss.item()
running_updates += 1
# Statistics
print("[Branch {:d}, Epochs {:d}--{:d}] "
"Loss: {:.4f} Acc: {}".format(net.get_ensemble_size() - current_branch_on_training,
epoch + 1,
max_training_epoch,
running_loss / running_updates,
np.array(running_corrects) / len(train_data)))
# Validation
if ((epoch % validation_interval) == 0) or ((epoch + 1) == max_training_epoch):
net.eval()
val_loss, val_corrects = evaluate(net, val_loader, criterion_ce, device, current_branch_on_training)
print("\nValidation - [Branch {:d}, Epochs {:d}--{:d}] Loss: {:.4f} Acc: {}\n\n".format(
net.get_ensemble_size() - current_branch_on_training,
epoch + 1,
max_training_epoch,
val_loss[-1],
np.array(val_corrects) / len(val_data)))
# Add to history training and validation statistics
history_loss.append(running_loss / running_updates)
for i_4 in range(net.get_ensemble_size()):
history_acc[i_4].append(running_corrects[i_4] / len(train_data))
for b in range(net.get_ensemble_size()):
history_val_loss[b].append(val_loss[b])
history_val_acc[b].append(float(val_corrects[b]) / len(val_data))
# Add ensemble accuracy to history
history_val_acc[-1].append(float(val_corrects[-1]) / len(val_data))
# Save best ensemble
ensemble_acc = (float(val_corrects[-1]) / len(val_data))
if ensemble_acc >= best_ensemble_acc:
best_ensemble_acc = ensemble_acc
best_ensemble = net.to_state_dict()
# Save network
Ensemble.save(best_ensemble,
path.join(base_path_experiment, name_experiment, "Saved Networks"),
current_branch_on_training)
# Save graphs
plot(history_loss,
history_acc,
history_val_loss,
history_val_acc,
net.get_ensemble_size() - current_branch_on_training,
path.join(base_path_experiment, name_experiment))
net.train()
# Change branch on training
if current_branch_on_training > 0:
# Decrease max training epoch
max_training_epoch = 60
# Reload best configuration
net.reload(best_ensemble)
# Set optimizer
optimizer = optim.SGD([{"params": net.base.parameters(), "lr": 0.02, "momentum": 0.9},
{"params": net.branches[
net.get_ensemble_size() - current_branch_on_training].parameters(),
"lr": 0.1,
"momentum": 0.9
}])
# Trained branches
for b in range(net.get_ensemble_size()):
if b != (net.get_ensemble_size() - current_branch_on_training):
optimizer.add_param_group({"params": net.branches[b].parameters(), "lr": 0.02, "momentum": 0.9})
# Change branch on training
current_branch_on_training -= 1
# Finish training after fine-tuning all branches
else:
break
if __name__ == "__main__":
print("Processing...")
main()
print("Process has finished!")