-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_amigos.py
198 lines (180 loc) · 7.5 KB
/
train_amigos.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
import os
from datetime import datetime
import random
import torch
from torch import optim
from torch import nn
from torch.nn import functional as F
from torch.utils import tensorboard
from torch.utils import data
from torch.cuda.amp import GradScaler
from torchvision import transforms
from config import *
from train_utils import *
from AMIGOS import AMIGOS
from AMIGOS import amigos_utils
torch.backends.cudnn.enabled = False
kwargs = {'num_workers': 4, 'pin_memory': True, 'shuffle': True}
cnf = get_config(sys.argv)
def main() -> None:
log_writer = tensorboard.SummaryWriter(
log_dir=os.path.join(*[cnf.log_dir, 'AMIGOS', now + cnf.model_basename])
)
load_transform = transforms.Compose([
IgnoreFiles('face-0'),
TemporalDownSample(cnf.downsample),
RandomSequence(cnf.input_shape[1] * (cnf.window * 2 + 1))
])
temporal_transform = transforms.Compose([
transforms.ColorJitter(0.2, 0.2, 0.2),
transforms.RandomHorizontalFlip(),
transforms.Normalize([0.4168, 0.3074, 0.2607], [0.2426, 0.1997, 0.1870])
])
space_transform = transforms.Compose([
transforms.Resize((cnf.input_shape[0], cnf.input_shape[0])),
transforms.ToTensor()
])
target_transform = [torch.FloatTensor, amigos_utils.custom_transforms.AnnotatorsAverage()]
target_transform = transforms.Compose(target_transform)
dataset = AMIGOS(
root_path=cnf.dataset_root,
annotation_file=cnf.label_path,
spatial_transform=space_transform,
temporal_transform=temporal_transform,
target_transform=target_transform,
load_transform=load_transform
)
temporal_transform = transforms.Compose([
transforms.Normalize([0.4168, 0.3074, 0.2607], [0.2426, 0.1997, 0.1870])
])
load_transform = transforms.Compose([
IgnoreFiles('face-0'),
TemporalDownSample(cnf.downsample),
# RandomSequence(cnf.input_shape[1] * (cnf.window * 2 + 1))
])
val_dataset = AMIGOS(
root_path=cnf.dataset_root,
annotation_file=cnf.label_path,
spatial_transform=space_transform,
temporal_transform=temporal_transform,
target_transform=target_transform,
load_transform=load_transform
)
amigos_idx = amigos_utils.get_subject_idx(dataset.data)
if cnf.amigos_ignore:
print(amigos_idx)
for ign in cnf.amigos_ignore:
if ign in amigos_idx:
amigos_idx.remove(ign)
for amigo in amigos_idx:
val_idx = amigos_utils.get_indices_in_set(val_dataset.data, [amigo])
train_idx = amigos_utils.get_indices_in_set(
dataset.data,
[amig_idx for amig_idx in amigos_utils.get_subject_idx(dataset.data) if amig_idx != amigo]
)
assert len([value for value in train_idx if value in val_idx]) == 0
new_train = random.sample(train_idx, len(train_idx)//5)
print("AMIGO {} - Test: {} Train: {}".format(amigo, len(val_idx), len(new_train)))
val_set = data.Subset(val_dataset, val_idx)
train_dataset = data.Subset(dataset, new_train)
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=cnf.batch_size,
collate_fn=amigos_utils.series_collate,
**kwargs
)
test_loader = torch.utils.data.DataLoader(
val_set,
batch_size=1,
collate_fn=amigos_utils.series_collate,
**kwargs
)
if cnf.lfb:
backbone = nn.DataParallel(Backbone()).cuda()
neck = nn.DataParallel(Neck(backbone.module.interim_dim)).cuda()
head = nn.DataParallel(NLB_Head_AV(backbone.module.interim_dim)).cuda()
else:
backbone = nn.DataParallel(Backbone()).cuda()
neck = nn.DataParallel(Neck(backbone.module.interim_dim)).cuda()
head = nn.DataParallel(LN_Head_VA(backbone.module.interim_dim)).cuda()
net = [backbone, neck, head]
optimizer = optim.SGD(
[{'params': backbone.parameters()}, {'params': neck.parameters()}, {'params': head.parameters()}],
lr=cnf.lr,
weight_decay=5e-4
)
scaler = GradScaler()
last_loss = list()
lr = cnf.lr
for epoch in range(cnf.num_epochs):
if (epoch % 5 == 0) and (epoch != 0):
lr *= 0.1
optimizer.param_groups[0]['lr'] = lr / 2.6
optimizer.param_groups[1]['lr'] = lr
optimizer.param_groups[2]['lr'] = lr
num_iter = len(train_loader)
net = [el.train() for el in net]
for batch_idx, (inputs, labels, _) in enumerate(train_loader):
iter_idx = (epoch * num_iter) + batch_idx
inputs, labels = inputs.cuda(), labels.cuda()
if cnf.window > 0:
inputs, lfb_feats = get_lfb(cnf, backbone, neck, inputs)
else:
lfb_feats = None
optimizer.zero_grad()
with autocast():
frames = backbone(inputs)
clips = neck(frames)
outputs = head(clips, lfb_feats)
y_out = outputs['pred']
mae = F.l1_loss(y_out, labels, reduction='mean')
mse = F.mse_loss(y_out, labels, reduction='mean')
rmse = torch.sqrt(mse)
pcc = PCC(y_out, labels)
ccc = CCC(y_out, labels)
distance, similarity = info_recon_loss(clips, labels)
cnt_mse = F.mse_loss(distance, similarity, reduction='mean')
cnt_rmse = torch.sqrt(cnt_mse)
loss = (1-ccc).mean() + cnf.lamda * cnt_rmse
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
log_train(log_writer, loss, mae, mse, rmse, ccc, pcc, cnf.symptom_names, iter_idx)
sys.stdout.write(
'\r {}| Epoch [{}/{}] Iter[{}/{}]\t loss: {:.2f} \t MAE: {:.2f} \t MSE: {:.2f} \t RMSE: {:.2f} \t CCC:{} \t PCC:{} '.format(
cnf.dataset_name,
epoch,
cnf.num_epochs,
batch_idx + 1,
num_iter,
loss.item(),
mae.item(),
mse.item(),
rmse.item(),
['%.2f' % elem for elem in ccc.tolist()],
['%.2f' % elem for elem in pcc.tolist()]
)
)
sys.stdout.flush()
del inputs
del lfb_feats
del labels # free mem
torch.cuda.empty_cache()
# break
val_log(cnf, epoch, net, test_loader, log_writer, last_loss)
if last_loss[-1] == min(last_loss):
savemodel = 'models/{}'.format(cnf.model_basename)
if not os.path.exists(savemodel):
os.makedirs(savemodel)
torch.save({
'epoch': epoch,
'backbone': backbone.state_dict(),
'neck': neck.state_dict(),
'head': head.state_dict()
},
os.path.join(savemodel, 'pid_{}.pth.tar'.format(amigo))
)
if __name__ == '__main__':
now = datetime.now().strftime('%b%d_%H-%M-%S_')
cnf.input_shape = [224, 16]
main()