-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain.py
150 lines (122 loc) · 5.7 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
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
import time
import copy
import torch
import numpy as np
from tqdm import tqdm
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np
from infomax_loss import DeepInfoMaxLoss
from utils import dice_score, eval_model, dice_loss, calc_loss, calc_bce_pos
# Find device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Hard Code Domain Loss
domain_loss_func = nn.BCEWithLogitsLoss()
def train_model(model, dataloaders, num_iterations=5000, warmup_period=500, domain_w=0., proto_w=0., infomax_loss_type="concat"):
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
best_score = 0.0
if (proto_w != 0):
# infomax loss
loss_fn = DeepInfoMaxLoss(type=infomax_loss_type).to(device)
optimizer_fn = optim.Adam(loss_fn.parameters(), lr=1e-4)
optimizer_flag = 0
# Optimizer
optimizer = optim.Adam(model.parameters(), lr=1e-4)
# Phase
phase = "train"
domain_iter = iter(dataloaders['domain'])
train_iter = iter(dataloaders['train'])
# Running Loss
running_loss_domain = 0.0
running_loss_mask = 0.0
running_loss_proto = 0.0
# Iterations
warmup_period = warmup_period
proto_start_epoch = warmup_period
for it in range(num_iterations):
if (it > warmup_period) & (proto_w != 0):
if optimizer_flag == 0:
optimizer_flag = 1
if it % 200 == 0:
model.eval()
score = eval_model(dataloaders['val'], model)
print('Dice Score: {}'.format(score))
if score > best_score:
best_score = score
best_model_wts = copy.deepcopy(model.state_dict())
model.train()
# Each epoch has a training and validation phase
if (domain_w != 0) or (proto_w != 0):
try:
inputs_unlab, masks_unlab, _, domain_unlab, domain_tracker_unlab = next(domain_iter)
except StopIteration:
domain_iter = iter(dataloaders['domain'])
inputs_unlab, masks_unlab, _, domain_unlab, domain_tracker_unlab = next(domain_iter)
inputs_unlab = inputs_unlab.to(device)
masks_unlab = masks_unlab.to(device, dtype=torch.float)
domain_unlab = domain_unlab.to(device)
try:
inputs_lab, masks_lab, _, domain_lab, domain_tracker_lab = next(train_iter)
except StopIteration:
train_iter = iter(dataloaders['train'])
inputs_lab, masks_lab, _, domain_lab, domain_tracker_lab = next(train_iter)
inputs_lab = inputs_lab.to(device)
domain_lab = domain_lab.to(device)
masks_lab = masks_lab.to(device, dtype=torch.float)
# zero the parameter gradients
optimizer.zero_grad()
if optimizer_flag != 0:
optimizer_fn.zero_grad()
if (it > warmup_period) & (proto_w != 0):
logit_label, domain_lab_pred, proto_label_pos, proto_label_neg, \
logit_unlabel, domain_unlab_pred, proto_unlabel_pos, proto_unlabel_neg = model(inputs_lab, inputs_unlab,\
x_label_map=masks_lab)
else:
if (it>warmup_period) & (domain_w != 0):
# forward
# track history if only in train
logit_label, domain_lab_pred, proto_label_pos, proto_label_neg, \
logit_unlabel, domain_unlab_pred, proto_unlabel_pos, proto_unlabel_neg = model(inputs_lab, inputs_unlab)
else:
logit_label, domain_lab_pred, proto_label_pos, proto_label_neg = model(inputs_lab)
loss = 0
loss_mask = calc_loss(logit_label, masks_lab)
loss = loss + 1.*loss_mask
if it > warmup_period:
if (domain_w != 0):
loss_domain_lab = domain_loss_func(domain_lab_pred, domain_lab)
loss = loss + domain_w*loss_domain_lab
loss_domain_unlab = domain_loss_func(domain_unlab_pred, domain_unlab)
loss = loss + domain_w*loss_domain_unlab
if (proto_w != 0):
loss_proto = loss_fn(proto_label_pos, proto_label_neg, proto_unlabel_pos)
loss = loss + proto_w*loss_proto
loss.backward()
optimizer.step()
if optimizer_flag != 0:
optimizer_fn.step()
# statistics
running_loss_mask += loss_mask.item() * inputs_lab.size(0)
if it > warmup_period:
if (domain_w != 0):
running_loss_domain += loss_domain_lab.item() * inputs_lab.size(0)
running_loss_domain += loss_domain_unlab.item() * inputs_unlab.size(0)
if (proto_w != 0):
running_loss_proto += loss_proto.item() * inputs_lab.size(0)
if it % 50 == 0:
epoch_loss_mask = running_loss_mask / 50
epoch_loss_domain = running_loss_domain / 100
epoch_loss_proto = running_loss_proto / 50
print('Proto Loss: {}'.format(epoch_loss_proto))
running_loss_domain = 0.0
running_loss_mask = 0.0
running_loss_proto = 0.0
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print('Best val Score: {:4f}'.format(best_score))
# load best model weights
model.load_state_dict(best_model_wts)
return model