-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
73 lines (60 loc) · 1.88 KB
/
train.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Trainer script. Example run command: train.py save_to_folder configs/cnn.gin.
"""
import os
import gin
from gin.config import _CONFIG
import torch
import pickle
import logging
from functools import partial
logger = logging.getLogger(__name__)
from src import dataset
from src import callbacks as avail_callbacks
from src.model import MMTM_MVCNN
from src.training_loop import training_loop
from src.utils import gin_wrap
def blend_loss(y_hat, y):
loss_func = torch.nn.CrossEntropyLoss()
losses = []
for y_pred in y_hat:
losses.append(loss_func(y_pred, y))
return sum(losses)
def acc(y_pred, y_true):
if isinstance(y_pred, list):
y_pred = torch.mean(torch.stack([out.data for out in y_pred], 0), 0)
_, y_pred = y_pred.max(1)
if len(y_true)==2:
acc_pred = (y_pred == y_true[0]).float().mean()
else:
acc_pred = (y_pred == y_true).float().mean()
return acc_pred * 100
@gin.configurable
def train(save_path, wd, lr, momentum, batch_size, callbacks=[]):
model = MMTM_MVCNN()
train, valid, test = dataset.get_mvdcndata(batch_size=batch_size)
optimizer = torch.optim.SGD(model.parameters(),
lr=lr,
weight_decay=wd,
momentum=momentum)
callbacks_constructed = []
for name in callbacks:
if name in avail_callbacks.__dict__:
clbk = avail_callbacks.__dict__[name]()
callbacks_constructed.append(clbk)
training_loop(model=model,
optimizer=optimizer,
loss_function=blend_loss,
metrics=[acc],
train=train, valid=valid, test=test,
steps_per_epoch=len(train),
validation_steps=len(valid),
test_steps=len(test),
save_path=save_path,
config=_CONFIG,
custom_callbacks=callbacks_constructed
)
if __name__ == "__main__":
gin_wrap(train)