-
Notifications
You must be signed in to change notification settings - Fork 0
/
asformer.py
573 lines (462 loc) · 24.6 KB
/
asformer.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
import time
import pickle
import json
from math import ceil
from pathlib import Path
import datetime
from tqdm import tqdm
import copy
import numpy as np
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import optim
from torch.utils.tensorboard import SummaryWriter
from utils import Bar
from utils.viz import viz_results_paper
from utils.averagemeter import AverageMeter
from utils.utils import torch_to_list
from eval import Metric
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def exponential_descrease(idx_decoder, p=3):
return math.exp(-p*idx_decoder)
class AttentionHelper(nn.Module):
def __init__(self):
super(AttentionHelper, self).__init__()
self.softmax = nn.Softmax(dim=-1)
def scalar_dot_att(self, proj_query, proj_key, proj_val, padding_mask):
'''
scalar dot attention.
:param proj_query: shape of (B, C, L) => (Batch_Size, Feature_Dimension, Length)
:param proj_key: shape of (B, C, L)
:param proj_val: shape of (B, C, L)
:param padding_mask: shape of (B, C, L)
:return: attention value of shape (B, C, L)
'''
m, c1, l1 = proj_query.shape
m, c2, l2 = proj_key.shape
assert c1 == c2
energy = torch.bmm(proj_query.permute(0, 2, 1), proj_key) # out of shape (B, L1, L2)
attention = energy / np.sqrt(c1)
attention = attention + torch.log(padding_mask + 1e-6) # mask the zero paddings. log(1e-6) for zero paddings
attention = self.softmax(attention)
attention = attention * padding_mask
attention = attention.permute(0,2,1)
out = torch.bmm(proj_val, attention)
return out, attention
class AttLayer(nn.Module):
def __init__(self, q_dim, k_dim, v_dim, r1, r2, r3, bl, stage, att_type): # r1 = r2
super(AttLayer, self).__init__()
self.query_conv = nn.Conv1d(in_channels=q_dim, out_channels=q_dim // r1, kernel_size=1)
self.key_conv = nn.Conv1d(in_channels=k_dim, out_channels=k_dim // r2, kernel_size=1)
self.value_conv = nn.Conv1d(in_channels=v_dim, out_channels=v_dim // r3, kernel_size=1)
self.conv_out = nn.Conv1d(in_channels=v_dim // r3, out_channels=v_dim, kernel_size=1)
self.bl = bl
self.stage = stage
self.att_type = att_type
assert self.att_type in ['normal_att', 'block_att', 'sliding_att']
assert self.stage in ['encoder','decoder']
self.att_helper = AttentionHelper()
self.window_mask = self.construct_window_mask()
def construct_window_mask(self):
'''
construct window mask of shape (1, l, l + l//2 + l//2), used for sliding window self attention
'''
window_mask = torch.zeros((1, self.bl, self.bl + 2* (self.bl //2)))
for i in range(self.bl):
window_mask[:, :, i:i+self.bl] = 1
return window_mask.to(device)
def forward(self, x1, x2, mask):
# x1 from the encoder
# x2 from the decoder
query = self.query_conv(x1)
key = self.key_conv(x1)
if self.stage == 'decoder':
assert x2 is not None
value = self.value_conv(x2)
else:
value = self.value_conv(x1)
if self.att_type == 'normal_att':
return self._normal_self_att(query, key, value, mask)
elif self.att_type == 'block_att':
return self._block_wise_self_att(query, key, value, mask)
elif self.att_type == 'sliding_att':
return self._sliding_window_self_att(query, key, value, mask)
def _normal_self_att(self,q,k,v, mask):
m_batchsize, c1, L = q.size()
_,c2,L = k.size()
_,c3,L = v.size()
padding_mask = torch.ones((m_batchsize, 1, L)).to(device) * mask[:,0:1,:]
output, attentions = self.att_helper.scalar_dot_att(q, k, v, padding_mask)
output = self.conv_out(F.relu(output))
output = output[:, :, 0:L]
return output * mask[:, 0:1, :]
def _block_wise_self_att(self, q,k,v, mask):
m_batchsize, c1, L = q.size()
_,c2,L = k.size()
_,c3,L = v.size()
nb = L // self.bl
if L % self.bl != 0:
q = torch.cat([q, torch.zeros((m_batchsize, c1, self.bl - L % self.bl)).to(device)], dim=-1)
k = torch.cat([k, torch.zeros((m_batchsize, c2, self.bl - L % self.bl)).to(device)], dim=-1)
v = torch.cat([v, torch.zeros((m_batchsize, c3, self.bl - L % self.bl)).to(device)], dim=-1)
nb += 1
padding_mask = torch.cat([torch.ones((m_batchsize, 1, L)).to(device) * mask[:,0:1,:], torch.zeros((m_batchsize, 1, self.bl * nb - L)).to(device)],dim=-1)
q = q.reshape(m_batchsize, c1, nb, self.bl).permute(0, 2, 1, 3).reshape(m_batchsize * nb, c1, self.bl)
padding_mask = padding_mask.reshape(m_batchsize, 1, nb, self.bl).permute(0, 2, 1, 3).reshape(m_batchsize * nb,1, self.bl)
k = k.reshape(m_batchsize, c2, nb, self.bl).permute(0, 2, 1, 3).reshape(m_batchsize * nb, c2, self.bl)
v = v.reshape(m_batchsize, c3, nb, self.bl).permute(0, 2, 1, 3).reshape(m_batchsize * nb, c3, self.bl)
output, attentions = self.att_helper.scalar_dot_att(q, k, v, padding_mask)
output = self.conv_out(F.relu(output))
output = output.reshape(m_batchsize, nb, c3, self.bl).permute(0, 2, 1, 3).reshape(m_batchsize, c3, nb * self.bl)
output = output[:, :, 0:L]
return output * mask[:, 0:1, :]
def _sliding_window_self_att(self, q,k,v, mask):
m_batchsize, c1, L = q.size()
_, c2, _ = k.size()
_, c3, _ = v.size()
assert m_batchsize == 1 # currently, we only accept input with batch size 1
# padding zeros for the last segment
nb = L // self.bl
if L % self.bl != 0:
q = torch.cat([q, torch.zeros((m_batchsize, c1, self.bl - L % self.bl)).to(device)], dim=-1)
k = torch.cat([k, torch.zeros((m_batchsize, c2, self.bl - L % self.bl)).to(device)], dim=-1)
v = torch.cat([v, torch.zeros((m_batchsize, c3, self.bl - L % self.bl)).to(device)], dim=-1)
nb += 1
padding_mask = torch.cat([torch.ones((m_batchsize, 1, L)).to(device) * mask[:,0:1,:], torch.zeros((m_batchsize, 1, self.bl * nb - L)).to(device)],dim=-1)
# sliding window approach, by splitting query_proj and key_proj into shape (c1, l) x (c1, 2l)
# sliding window for query_proj: reshape
q = q.reshape(m_batchsize, c1, nb, self.bl).permute(0, 2, 1, 3).reshape(m_batchsize * nb, c1, self.bl)
# sliding window approach for key_proj
# 1. add paddings at the start and end
k = torch.cat([torch.zeros(m_batchsize, c2, self.bl // 2).to(device), k, torch.zeros(m_batchsize, c2, self.bl // 2).to(device)], dim=-1)
v = torch.cat([torch.zeros(m_batchsize, c3, self.bl // 2).to(device), v, torch.zeros(m_batchsize, c3, self.bl // 2).to(device)], dim=-1)
padding_mask = torch.cat([torch.zeros(m_batchsize, 1, self.bl // 2).to(device), padding_mask, torch.zeros(m_batchsize, 1, self.bl // 2).to(device)], dim=-1)
# 2. reshape key_proj of shape (m_batchsize*nb, c1, 2*self.bl)
k = torch.cat([k[:,:, i*self.bl:(i+1)*self.bl+(self.bl//2)*2] for i in range(nb)], dim=0) # special case when self.bl = 1
v = torch.cat([v[:,:, i*self.bl:(i+1)*self.bl+(self.bl//2)*2] for i in range(nb)], dim=0)
# 3. construct window mask of shape (1, l, 2l), and use it to generate final mask
padding_mask = torch.cat([padding_mask[:,:, i*self.bl:(i+1)*self.bl+(self.bl//2)*2] for i in range(nb)], dim=0) # of shape (m*nb, 1, 2l)
final_mask = self.window_mask.repeat(m_batchsize * nb, 1, 1) * padding_mask
output, attention = self.att_helper.scalar_dot_att(q, k, v, final_mask)
output = self.conv_out(F.relu(output))
output = output.reshape(m_batchsize, nb, -1, self.bl).permute(0, 2, 1, 3).reshape(m_batchsize, -1, nb * self.bl)
output = output[:, :, 0:L]
return output * mask[:, 0:1, :]
class MultiHeadAttLayer(nn.Module):
def __init__(self, q_dim, k_dim, v_dim, r1, r2, r3, bl, stage, att_type, num_head):
super(MultiHeadAttLayer, self).__init__()
# assert v_dim % num_head == 0
self.conv_out = nn.Conv1d(v_dim * num_head, v_dim, 1)
self.layers = nn.ModuleList(
[copy.deepcopy(AttLayer(q_dim, k_dim, v_dim, r1, r2, r3, bl, stage, att_type)) for i in range(num_head)])
self.dropout = nn.Dropout(p=0.5)
def forward(self, x1, x2, mask):
out = torch.cat([layer(x1, x2, mask) for layer in self.layers], dim=1)
out = self.conv_out(self.dropout(out))
return out
class ConvFeedForward(nn.Module):
def __init__(self, dilation, in_channels, out_channels):
super(ConvFeedForward, self).__init__()
self.layer = nn.Sequential(
nn.Conv1d(in_channels, out_channels, 3, padding=dilation, dilation=dilation),
nn.ReLU()
)
def forward(self, x):
return self.layer(x)
class FCFeedForward(nn.Module):
def __init__(self, in_channels, out_channels):
super(FCFeedForward, self).__init__()
self.layer = nn.Sequential(
nn.Conv1d(in_channels, out_channels, 1), # conv1d equals fc
nn.ReLU(),
nn.Dropout(),
nn.Conv1d(out_channels, out_channels, 1)
)
def forward(self, x):
return self.layer(x)
class AttModule(nn.Module):
def __init__(self, dilation, in_channels, out_channels, r1, r2, att_type, stage, alpha):
super(AttModule, self).__init__()
self.feed_forward = ConvFeedForward(dilation, in_channels, out_channels)
self.instance_norm = nn.InstanceNorm1d(in_channels, track_running_stats=False)
self.att_layer = AttLayer(in_channels, in_channels, out_channels, r1, r1, r2, dilation, att_type=att_type, stage=stage) # dilation
self.conv_1x1 = nn.Conv1d(out_channels, out_channels, 1)
self.dropout = nn.Dropout()
self.alpha = alpha
def forward(self, x, f, mask):
out = self.feed_forward(x)
out = self.alpha * self.att_layer(self.instance_norm(out), f, mask) + out
out = self.conv_1x1(out)
out = self.dropout(out)
return (x + out) * mask[:, 0:1, :]
class PositionalEncoding(nn.Module):
"Implement the PE function."
def __init__(self, d_model, max_len=10000):
super(PositionalEncoding, self).__init__()
# Compute the positional encodings once in log space.
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2) *
-(math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0).permute(0,2,1) # of shape (1, d_model, l)
self.pe = nn.Parameter(pe, requires_grad=True)
# self.register_buffer('pe', pe)
def forward(self, x):
return x + self.pe[:, :, 0:x.shape[2]]
class Encoder(nn.Module):
def __init__(self, num_layers, r1, r2, num_f_maps, input_dim, num_classes, channel_masking_rate, att_type, alpha):
super(Encoder, self).__init__()
self.conv_1x1 = nn.Conv1d(input_dim, num_f_maps, 1) # fc layer
self.layers = nn.ModuleList(
[AttModule(2 ** i, num_f_maps, num_f_maps, r1, r2, att_type, 'encoder', alpha) for i in # 2**i
range(num_layers)])
self.conv_out = nn.Conv1d(num_f_maps, num_classes, 1)
self.dropout = nn.Dropout2d(p=channel_masking_rate)
self.channel_masking_rate = channel_masking_rate
def forward(self, x, mask):
'''
:param x: (N, C, L)
:param mask:
:return:
'''
if self.channel_masking_rate > 0:
x = x.unsqueeze(2)
x = self.dropout(x)
x = x.squeeze(2)
feature = self.conv_1x1(x)
for layer in self.layers:
feature = layer(feature, None, mask)
out = self.conv_out(feature) * mask[:, 0:1, :]
return out, feature
class Decoder(nn.Module):
def __init__(self, num_layers, r1, r2, num_f_maps, input_dim, num_classes, att_type, alpha):
super(Decoder, self).__init__()# self.position_en = PositionalEncoding(d_model=num_f_maps)
self.conv_1x1 = nn.Conv1d(input_dim, num_f_maps, 1)
self.layers = nn.ModuleList(
[AttModule(2 ** i, num_f_maps, num_f_maps, r1, r2, att_type, 'decoder', alpha) for i in # 2 ** i
range(num_layers)])
self.conv_out = nn.Conv1d(num_f_maps, num_classes, 1)
def forward(self, x, fencoder, mask):
feature = self.conv_1x1(x)
for layer in self.layers:
feature = layer(feature, fencoder, mask)
out = self.conv_out(feature) * mask[:, 0:1, :]
return out, feature
class MyTransformer(nn.Module):
def __init__(self, num_decoders, num_layers, r1, r2, num_f_maps, input_dim, num_classes, channel_masking_rate):
super(MyTransformer, self).__init__()
self.encoder = Encoder(num_layers, r1, r2, num_f_maps, input_dim, num_classes, channel_masking_rate, att_type='sliding_att', alpha=1)
self.decoders = nn.ModuleList([copy.deepcopy(Decoder(num_layers, r1, r2, num_f_maps, num_classes, num_classes, att_type='sliding_att', alpha=exponential_descrease(s))) for s in range(num_decoders)]) # num_decoders
def forward(self, x, mask):
out, feature = self.encoder(x, mask)
outputs = out.unsqueeze(0)
for decoder in self.decoders:
out, feature = decoder(F.softmax(out, dim=1) * mask[:, 0:1, :], feature* mask[:, 0:1, :], mask)
outputs = torch.cat((outputs, out.unsqueeze(0)), dim=0)
return outputs
################ Trainer ####################
class ASFormerTrainer:
def __init__(self, num_decoders, num_layers, r1, r2, num_f_maps, input_dim, num_classes, channel_masking_rate, weights, save_dir):
self.model = MyTransformer(num_decoders, num_layers, r1, r2, num_f_maps, input_dim, num_classes, channel_masking_rate)
## To add : weigths !!
if weights is None:
self.ce = nn.CrossEntropyLoss(ignore_index=-100)
else:
self.ce = nn.CrossEntropyLoss(weight=torch.tensor(weights).to(device), ignore_index=-100)
print('Model Size: ', sum(p.numel() for p in self.model.parameters()))
self.mse = nn.MSELoss(reduction='none')
self.num_classes = num_classes
self.writer = SummaryWriter(log_dir=f'{save_dir}/logs')
self.global_counter = 0
self.train_result_dict = {}
self.test_result_dict = {}
self.mean_f1b_test = 0
def train(self, save_dir, batch_gen, num_epochs, batch_size, learning_rate, eval_args):
self.model.train()
self.model.to(device)
optimizer = optim.Adam(self.model.parameters(), lr=learning_rate, weight_decay=1e-5)
print('LR:{}'.format(learning_rate))
early_stopping = EarlyStopping(patience=4, min_delta=0)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=2, verbose=True)
for epoch in range(num_epochs):
epoch_loss = 0
correct = 0
count = 0
total = 0
end = time.time()
batch_time = AverageMeter()
data_time = AverageMeter()
bar = Bar("E%d" % (epoch + 1), max=batch_gen.get_max_index())
get_metrics_train = Metric('train')
while batch_gen.has_next():
#batch_input, batch_target, mask, vids = batch_gen.next_batch(batch_size, False)
batch_input, batch_target, batch_target_eval, mask = batch_gen.next_batch(batch_size)
batch_input, batch_target, mask = batch_input.to(device), batch_target.to(device), mask.to(device)
optimizer.zero_grad()
ps = self.model(batch_input, mask)
loss = 0
for p in ps:
loss += self.ce(p.transpose(2, 1).contiguous().view(-1, self.num_classes), batch_target.view(-1))
loss += 0.15 * torch.mean(torch.clamp(
self.mse(F.log_softmax(p[:, :, 1:], dim=1), F.log_softmax(p.detach()[:, :, :-1], dim=1)), min=0,
max=16) * mask[:, :, 1:])
epoch_loss += loss.item()
loss.backward()
optimizer.step()
_, predicted = torch.max(ps.data[-1], 1)
correct += ((predicted == batch_target).float() * mask[:, 0, :].squeeze(1)).sum().item()
total += torch.sum(mask[:, 0, :]).item()
gt = batch_target
gt_eval = batch_target_eval
try:
get_metrics_train.calc_scores_per_batch(predicted, gt, gt_eval, mask)
except ValueError: #raised if `y` is empty.
pass
#get_metrics_train.calc_scores_per_batch(predicted, gt, gt_eval, mask)
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
# plot progress
bar.suffix = "({batch}/{size}) Batch: {bt:.1f}s | Total: {total:} | ETA: {eta:} | Loss: {loss:}".format(
batch=count + 1,
size=batch_gen.get_max_index() / batch_size,
bt=batch_time.avg,
total=bar.elapsed_td,
eta=datetime.timedelta(seconds=ceil((bar.eta_td/batch_size).total_seconds())),
#lr = round(optimizer._optimizer.param_groups[0]['lr'], 7),
loss=loss.item()
)
bar.next()
count += 1
if count % 50 == 0:
print(bar.suffix)
scheduler.step(epoch_loss)
batch_gen.reset()
print("[epoch %d]: epoch loss = %f, acc = %f" % (epoch + 1, epoch_loss / len(batch_gen.list_of_examples),
float(correct) / total))
torch.save(self.model.state_dict(), save_dir + "/epoch-" + str(epoch + 1) + ".model")
torch.save(optimizer.state_dict(), save_dir + "/epoch-" + str(epoch + 1) + ".opt")
try:
get_metrics_train.calc_metrics()
result_dict = get_metrics_train.save_print_metrics(self.writer, save_dir, epoch, epoch_loss/(len(batch_gen.list_of_examples)/batch_size))
self.train_result_dict.update(result_dict)
except ValueError: #raised if `y` is empty.
pass
eval_args[7] = epoch
eval_args[1] = save_dir + "/epoch-" + str(epoch+1) + ".model"
self.test(*eval_args)
with open(f'{save_dir}/train_results.json', 'w') as fp:
json.dump(self.train_result_dict, fp, indent=4)
with open(f'{save_dir}/eval_results.json', 'w') as fp:
json.dump(self.test_result_dict, fp, indent=4)
self.writer.close()
early_stopping(self.mean_f1b_test)
if early_stopping.early_stop:
break
def test(self, args, model_dir, results_dir, features_dict, gt_dict, gt_dict_dil, vid_list_file, epoch, device, mode, CP_dict=None):
save_score_dict = {}
metrics_per_signer = {}
get_metrics_test = Metric(mode)
self.model.eval()
with torch.no_grad():
if CP_dict is None:
self.model.to(device)
#self.model.load_state_dict(torch.load(model_dir))
epoch_loss = 0
for vid in tqdm(vid_list_file):
features = np.swapaxes(features_dict[vid], 0, 1)
input_x = torch.tensor(features, dtype=torch.float)
input_x.unsqueeze_(0)
input_x = input_x.to(device)
mask = torch.ones(input_x.size(), device = device)
predictions = self.model(input_x, mask)
num_iter = 1
pred_prob = torch_to_list(nn.Softmax(dim = 2)(predictions.detach()))
predicted = torch.tensor(np.where(np.asarray(pred_prob) > args.classification_threshold, 1, 0)).to(device)
gt = torch.tensor(gt_dict[vid]).to(device)
gt_eval = torch.tensor(gt_dict_dil[vid]).to(device)
loss = 0
for p in predictions :
loss += self.ce(p.transpose(2, 1).contiguous().view(-1, self.num_classes), gt.view(-1))
loss += 0.15 * torch.mean(torch.clamp(
self.mse(F.log_softmax(p[:, :, 1:], dim=1), F.log_softmax(p.detach()[:, :, :-1], dim=1)), min=0,
max=16))
epoch_loss += loss.item()
cut_endpoints = True
if cut_endpoints:
if torch.sum(predicted[-1, :, 1, -2:]) > 0 and sum(gt_eval[-4:]) == 0:
## !! should we put predicted[-2:,:,0] or predicted[-2,:,:1] !!
for j in range(predicted.size(3)-1, 0, -1):
if predicted[-1, 0, 1, j] != 0:
predicted[-1, 0, 1, j] = 0
#predicted[-1, 0, 0, j] = 1
elif bool(predicted[-1, 0, 1, j] == 0) and j < predicted.size(3) - 2:
break
if torch.sum(predicted[-1, :, 1, :2]) > 0 and sum(gt_eval[:4]) == 0:
check = 0
for j, item in enumerate(predicted[-1, 0, 1, :2]):
if item != 0:
predicted[-1, 0, 1, j] = 0
#predicted[-1, 0, 0, j] = 1
check = 1
elif item == 0 and (j > 2 or check):
break
get_metrics_test.calc_scores_per_batch(predicted[-1,:,1,:], gt.unsqueeze(0), gt_eval.unsqueeze(0)) ## !! predicted[:,:,0] ou predicted[:,:,1]
save_score_dict[vid] = {}
save_score_dict[vid]['scores'] = np.asarray(pred_prob) ## check this
save_score_dict[vid]['gt'] = torch_to_list(gt)
if mode == 'test' and args.viz_results:
if not isinstance(vid, int):
f_name = vid.split('/')[-1].split('.')[0]
else:
f_name = str(vid)
viz_results_paper(
gt,
torch_to_list(predicted),
name=results_dir + "/" + f'{f_name}',
pred_prob=pred_prob,
)
if mode == 'test':
pickle.dump(save_score_dict, open(f'{results_dir}/scores.pkl', "wb"))
get_metrics_test.calc_metrics()
self.mean_f1b_test = get_metrics_test.mean_f1b
save_dir = results_dir if mode == 'test' else Path(model_dir).parent
result_dict = get_metrics_test.save_print_metrics(self.writer, save_dir, epoch, epoch_loss/len(vid_list_file))
self.test_result_dict.update(result_dict)
if mode == 'test':
with open(f'{results_dir}/eval_results.json', 'w') as fp:
json.dump(self.test_result_dict, fp, indent=4)
class EarlyStopping():
"""
Early stopping to stop the training when the loss does not improve after
certain epochs.
"""
def __init__(self, patience=5, min_delta=0):
"""
:param patience: how many epochs to wait before stopping when loss is
not improving
:param min_delta: minimum difference between new loss and old loss for
new loss to be considered as an improvement
"""
self.patience = patience
self.min_delta = min_delta
self.counter = 0
self.best_loss = None
self.early_stop = False
def __call__(self, val_loss):
if self.best_loss == None:
self.best_loss = val_loss
elif self.best_loss - val_loss < self.min_delta:
self.best_loss = val_loss
# reset counter if validation loss improves
self.counter = 0
elif self.best_loss - val_loss > self.min_delta:
self.counter += 1
print(f"INFO: Early stopping counter {self.counter} of {self.patience}")
if self.counter >= self.patience:
print('INFO: Early stopping')
self.early_stop = True