-
Notifications
You must be signed in to change notification settings - Fork 0
/
efficientnet.py
359 lines (295 loc) · 11.5 KB
/
efficientnet.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
import argparse
import random
import pprint
import time
import sys
import os
from datetime import timedelta
from torchvision.models import EfficientNet_B0_Weights
from workspace import Workspace
import torch
import torch.optim
import torch.nn as nn
import torch.utils.data
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torchvision.datasets as datasets
import torchvision.transforms as transforms
import torchvision.models as models
from torch.utils.tensorboard import SummaryWriter
from utils import Logger, AverageMeter, accuracy, calc_metrics, RandomFiveCrop
from tqdm import tqdm
# centerloss module
from loss import SparseCenterLoss
parser = argparse.ArgumentParser(description='DACL for FER in the wild')
parser.add_argument('--arch', type=str)
parser.add_argument('--lr', type=float)
parser.add_argument('--wd', type=float)
parser.add_argument('--bs', type=int)
parser.add_argument('--epochs', type=int)
parser.add_argument('--alpha', type=float)
parser.add_argument('--lamb', type=float)
parser.add_argument('--rd', type=str)
parser.add_argument('--pretrained', type=str, default='msceleb')
parser.add_argument('--deterministic', default=False, action='store_true')
def build_model(pretrained=True, fine_tune=True, num_classes=7):
if pretrained:
print('[INFO]: Loading pre-trained weights')
else:
print('[INFO]: Not loading pre-trained weights')
model = models.efficientnet_b0(weights=EfficientNet_B0_Weights.DEFAULT)
if fine_tune:
print('[INFO]: Fine-tuning all layers...')
for params in model.parameters():
params.requires_grad = True
elif not fine_tune:
print('[INFO]: Freezing hidden layers...')
for params in model.parameters():
params.requires_grad = False
# Change the final classification head.
model.classifier[1] = nn.Linear(in_features=1280, out_features=num_classes)
return model
def main(cfg):
global device
if torch.cuda.is_available():
device = torch.device('cuda')
cudnn.benchmark = True
else:
device = torch.device('cpu')
if cfg['deterministic']:
random.seed(cfg['seed'])
torch.manual_seed(cfg['seed'])
cudnn.deterministic = True
cudnn.benchmark = False
# Loading RAF-DB
# -----------------
print('[>] Loading dataset '.ljust(64, '-'))
normalize = transforms.Normalize(mean=[0.5752, 0.4495, 0.4012],
std=[0.2086, 0.1911, 0.1827])
# train set
train_set = datasets.ImageFolder(
root=os.path.join(cfg['root_dir'], 'train'),
transform=transforms.Compose([
transforms.Resize(256),
RandomFiveCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
)
train_loader = torch.utils.data.DataLoader(
train_set,
batch_size=cfg['batch_size'], shuffle=True,
num_workers=cfg['workers'], pin_memory=True)
# test set
val_loader = torch.utils.data.DataLoader(
dataset=datasets.ImageFolder(
root=os.path.join(cfg['root_dir'], 'validation'),
transform=transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
])
),
batch_size=cfg['batch_size'], shuffle=False,
num_workers=cfg['workers'], pin_memory=True
)
print('[*] Loaded dataset!')
# Create Model
# ------------
model = build_model()
model = torch.nn.DataParallel(model).to(device)
print('[*] Model initialized!')
# define loss function (criterion) and optimizer
# ----------------------------------------------
feat_size = 1280
criterion = {
'softmax': nn.CrossEntropyLoss().to(device),
# 'center': SparseCenterLoss(7, feat_size).to(device)
}
optimizer = {
'softmax': torch.optim.SGD(model.parameters(), cfg['lr'],
momentum=cfg['momentum'],
weight_decay=cfg['weight_decay']),
# 'center': torch.optim.SGD(criterion['center'].parameters(), cfg['alpha'])
}
# lr scheduler
scheduler = torch.optim.lr_scheduler.StepLR(optimizer['softmax'], step_size=20, gamma=0.1)
# training and evaluation
# -----------------------
global best_valid
best_valid = dict.fromkeys(['acc', 'rec', 'f1', 'aucpr', 'aucroc'], 0.0)
print('[>] Begin Training '.ljust(64, '-'))
for epoch in range(1, cfg['epochs'] + 1):
start = time.time()
# train for one epoch
train(train_loader, model, criterion, optimizer, epoch, cfg)
# validate for one epoch
validate(val_loader, model, criterion, epoch, cfg)
# progress
end = time.time()
progress = (
f'[*] epoch time = {end - start:.2f}s | '
f'lr = {optimizer["softmax"].param_groups[0]["lr"]}\n'
)
print(progress)
# lr step
scheduler.step()
# best test info
# ---------------
print('[>] Best Valid '.ljust(64, '-'))
stat = (
f'[+] acc={best_valid["acc"]:.4f}\n'
f'[+] rec={best_valid["rec"]:.4f}\n'
f'[+] f1={best_valid["f1"]:.4f}\n'
f'[+] aucpr={best_valid["aucpr"]:.4f}\n'
f'[+] aucroc={best_valid["aucroc"]:.4f}'
)
print(stat)
def train(train_loader, model, criterion, optimizer, epoch, cfg):
losses = {
'softmax': AverageMeter(),
}
accs = AverageMeter()
y_pred, y_true, y_scores = [], [], []
# switch to train mode
model.train()
with tqdm(total=int(len(train_loader.dataset) / cfg['batch_size'])) as pbar:
for i, (images, target) in enumerate(train_loader):
images = images.to(device)
target = target.to(device)
# compute output
output = model(images)
summary(model, x)
l_softmax = criterion['softmax'](output, target)
# l_center = criterion['center'](feat, target)
# l_total = l_softmax + cfg['lamb'] * l_center
# measure accuracy and record loss
acc, pred = accuracy(output, target)
losses['softmax'].update(l_softmax.item(), images.size(0))
# losses['center'].update(l_center.item(), images.size(0))
# losses['total'].update(l_total.item(), images.size(0))
accs.update(acc.item(), images.size(0))
# collect for metrics
y_pred.append(pred)
y_true.append(target)
y_scores.append(output.data)
# compute grads + opt step
optimizer['softmax'].zero_grad()
# optimizer['center'].zero_grad()
# l_total.backward()
l_softmax.backward()
optimizer['softmax'].step()
# optimizer['center'].step()
# progressbar
pbar.set_description(f'TRAINING [{epoch:03d}/{cfg["epochs"]}]')
pbar.set_postfix( {'acc': accs.avg})
pbar.update(1)
metrics = calc_metrics(y_pred, y_true, y_scores)
progress = (
f'[-] TRAIN [{epoch:03d}/{cfg["epochs"]}] | '
# f'L={losses["total"].avg:.4f} | '
f'Ls={losses["softmax"].avg:.4f} | '
# f'Lsc={losses["center"].avg:.4f} | '
f'acc={accs.avg:.4f} | '
f'rec={metrics["rec"]:.4f} | '
f'f1={metrics["f1"]:.4f} | '
f'aucpr={metrics["aucpr"]:.4f} | '
f'aucroc={metrics["aucroc"]:.4f}'
)
print(progress)
write_log(losses, accs.avg, metrics, epoch, tag='train')
def validate(valid_loader, model, criterion, epoch, cfg):
losses = {
'softmax': AverageMeter(),
# 'center': AverageMeter(),
'total': AverageMeter()
}
accs = AverageMeter()
y_pred, y_true, y_scores = [], [], []
# switch to evaluate mode
model.eval()
with tqdm(total=int(len(valid_loader.dataset) / cfg['batch_size'])) as pbar:
with torch.no_grad():
for i, (images, target) in enumerate(valid_loader):
images = images.to(device)
target = target.to(device)
# compute output
output= model(images)
l_softmax = criterion['softmax'](output, target)
# l_center = criterion['center'](feat, A, target)
# l_total = l_softmax + cfg['lamb'] * l_center
# measure accuracy and record loss
acc, pred = accuracy(output, target)
losses['softmax'].update(l_softmax.item(), images.size(0))
# losses['center'].update(l_center.item(), images.size(0))
# losses['total'].update(l_total.item(), images.size(0))
accs.update(acc.item(), images.size(0))
# collect for metrics
y_pred.append(pred)
y_true.append(target)
y_scores.append(output.data)
# progressbar
pbar.set_description(f'VALIDATING [{epoch:03d}/{cfg["epochs"]}]')
pbar.update(1)
metrics = calc_metrics(y_pred, y_true, y_scores)
progress = (
f'[-] VALID [{epoch:03d}/{cfg["epochs"]}] | '
# f'L={losses["total"].avg:.4f} | '
f'Ls={losses["softmax"].avg:.4f} | '
# f'Lsc={losses["center"].avg:.4f} | '
f'acc={accs.avg:.4f} | '
f'rec={metrics["rec"]:.4f} | '
f'f1={metrics["f1"]:.4f} | '
f'aucpr={metrics["aucpr"]:.4f} | '
f'aucroc={metrics["aucroc"]:.4f}'
)
print(progress)
# save model checkpoints for best test
if accs.avg > best_valid['acc']:
save_checkpoint(epoch, model, cfg, tag='best_valid_acc.pth')
if metrics['rec'] > best_valid['rec']:
save_checkpoint(epoch, model, cfg, tag='best_valid_rec.pth')
best_valid['acc'] = max(best_valid['acc'], accs.avg)
best_valid['rec'] = max(best_valid['rec'], metrics['rec'])
best_valid['f1'] = max(best_valid['f1'], metrics['f1'])
best_valid['aucpr'] = max(best_valid['aucpr'], metrics['aucpr'])
best_valid['aucroc'] = max(best_valid['aucroc'], metrics['aucroc'])
write_log(losses, accs.avg, metrics, epoch, tag='test')
def write_log(losses, acc, metrics, e, tag='set'):
# tensorboard
writer.add_scalar(f'L_softmax/{tag}', losses['softmax'].avg, e)
# writer.add_scalar(f'L_center/{tag}', losses['center'].avg, e)
# writer.add_scalar(f'L_total/{tag}', losses['total'].avg, e)
writer.add_scalar(f'acc/{tag}', acc, e)
writer.add_scalar(f'rec/{tag}', metrics['rec'], e)
writer.add_scalar(f'f1/{tag}', metrics['f1'], e)
writer.add_scalar(f'aucpr/{tag}', metrics['aucpr'], e)
writer.add_scalar(f'aucroc/{tag}', metrics['aucroc'], e)
def save_checkpoint(epoch, model, cfg, tag):
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
}, os.path.join(cfg['save_path'], tag))
if __name__ == '__main__':
# setting up workspace
args = parser.parse_args()
workspace = Workspace(args)
cfg = workspace.config
# setting up writers
global writer
writer = SummaryWriter(cfg['save_path'])
sys.stdout = Logger(os.path.join(cfg['save_path'], 'log.log'))
# print finalized parameter config
print('[>] Configuration '.ljust(64, '-'))
pp = pprint.PrettyPrinter(indent=2)
print(pp.pformat(cfg))
# -----------------
start = time.time()
main(cfg)
end = time.time()
# -----------------
print('\n[*] Fini! '.ljust(64, '-'))
print(f'[!] total time = {timedelta(seconds=end - start)}s')
sys.stdout.flush()