-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_mm.py
71 lines (51 loc) · 1.93 KB
/
train_mm.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
#@title Trainning
import torch
from torch import nn
from tqdm import tqdm
from sklearn.metrics import f1_score, classification_report
def train_epoch(epoch, model, dataloader, optimizer, scheduler,device, writer=None):
criterion = nn.CrossEntropyLoss()
model.train()
print("Start training ... ")
_loss = 0
for step, (spec, image, label) in (pbar := tqdm(enumerate(dataloader), desc='Epoch: {}: '.format(epoch))):
#pdb.set_trace()
spec = spec.to(device)
image = image.to(device)
label = label.to(device)
optimizer.zero_grad()
# TODO: make it simpler and easier to extend
a, v, out = model(spec.float(), image.float())
loss = criterion(out, label)
loss.backward()
optimizer.step()
# pbar.set_description('Epoch: {} Loss: {:.4f}'.format(epoch, loss.item()))
_loss += loss.item()
scheduler.step()
return _loss / len(dataloader)
def eval(args, model, device, dataloader, test=False):
softmax = nn.Softmax(dim=1)
if args.dataset == 'CREMAD':
n_classes = 6
else:
raise NotImplementedError('Incorrect dataset name {}'.format(args.dataset))
with torch.no_grad():
model.eval()
criterion = nn.CrossEntropyLoss()
_loss = 0
golds = []
preds = []
for step, (spec, image, label) in enumerate(dataloader):
spec = spec.to(device)
image = image.to(device)
label = label.to(device)
a, v, out = model(spec.float(), image.float())
loss = criterion(out, label)
_loss += loss.item()
y_hat = torch.argmax(softmax(out), dim=-1)
golds.extend(label.cpu().numpy())
preds.extend(y_hat.cpu().numpy())
wf1 = f1_score(golds, preds, average='weighted')
if test:
print(classification_report(golds, preds))
return _loss / len(dataloader), wf1