-
Notifications
You must be signed in to change notification settings - Fork 10
/
modellearning.py
208 lines (164 loc) · 8.41 KB
/
modellearning.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
import copy
import torch
import time
import torch.nn.functional as F
import pdb
from utils import modelserial
device = torch.device("cuda:0" if torch.cuda.is_available() > 0 else "cpu")
def train(model, dataloader, criterion, optimizer, scheduler, backbone='resnet', datasetname=None, isckpt=False, epochs=30):
# get the size of train and evaluation data
if isinstance(dataloader, dict):
dataset_sizes = {x: len(dataloader[x].dataset) for x in dataloader.keys()}
print(dataset_sizes)
else:
dataset_size = len(dataloader.dataset)
if not isinstance(criterion, list):
criterion = [criterion]
best_model_params = copy.deepcopy(model.state_dict())
best_acc = 0.0
global_step = 0
global_step_resume = 0
best_epoch = 0
best_step = 0
start_epoch = -1
if isckpt:
checkpoint = modelserial.loadCheckpoint(datasetname)
start_epoch = checkpoint['epoch']
best_acc = checkpoint['best_acc']
model.load_state_dict(checkpoint['state_dict'])
best_model_params = checkpoint['best_state_dict']
best_epoch = checkpoint['best_epoch']
since = time.time()
for epoch in range(start_epoch+1, epochs):
print('Epoch {}/{}'.format(epoch, epochs))
print('-' * 10)
for phase in ['trainval', 'test']:
if phase == 'trainval':
scheduler.step()
model.train() # Set model to training mode
global_step = global_step_resume
else:
model.eval() # Set model to evaluate mode
global_step_resume = global_step
running_cls_loss = 0.0
running_reg_loss = 0.0
running_corrects = 0
# Iterate over data.
for inputs, labels in dataloader[phase]:
inputs = inputs.to(device)
labels = labels.to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward
with torch.set_grad_enabled(phase == 'trainval'):
if model.module.nparts == 1:
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
all_loss = criterion[0](outputs, labels)
else:
if datasetname is 'stdogs' and backbone is 'resnet':
outputs_ulti, outputs_plty, _, ulti_ftrs, plty_ftrs, _ = model(inputs)
_, preds = torch.max(outputs_ulti+outputs_plty, 1)
cls_loss = criterion[0](outputs_ulti+outputs_plty, labels)
else:
outputs_ulti, outputs_plty, outputs_cmbn, ulti_ftrs, plty_ftrs, cmbn_ftrs = model(inputs)
_, preds = torch.max(outputs_ulti+outputs_plty+outputs_cmbn, 1)
cls_loss = criterion[0](outputs_ulti+outputs_plty+outputs_cmbn, labels)
reg_loss_cmbn = criterion[3](cmbn_ftrs)
outputs_cmbn = F.log_softmax(outputs_cmbn, 1)
reg_loss_ulti = criterion[1](ulti_ftrs)
reg_loss_plty = criterion[2](plty_ftrs)
outputs_plty = F.log_softmax(outputs_plty, 1)
outputs_ulti = F.softmax(outputs_ulti, 1)
if datasetname is 'stdogs' and backbone is 'resnet':
kl_loss = (criterion[4](outputs_plty, outputs_ulti)) / inputs.size(0)
all_loss = reg_loss_ulti + reg_loss_plty + kl_loss + cls_loss
else:
kl_loss = (criterion[4](outputs_plty, outputs_ulti) + criterion[4](outputs_cmbn, outputs_ulti)) / inputs.size(0)
all_loss = reg_loss_ulti + reg_loss_plty + reg_loss_cmbn + kl_loss + cls_loss
# backward + optimize only if in training phase
if phase == 'trainval':
all_loss.backward()
optimizer.step()
# statistics
if model.module.nparts == 1:
running_cls_loss += all_loss.item() * inputs.size(0)
else:
running_cls_loss += cls_loss.item() * inputs.size(0)
if datasetname is 'stdogs' and backbone is 'resnet':
running_reg_loss += (reg_loss_ulti.item() + reg_loss_plty.item() + kl_loss.item()) * inputs.size(0)
else:
running_reg_loss += (reg_loss_ulti.item() + reg_loss_plty.item() + reg_loss_cmbn.item() + kl_loss.item()) * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
if model.module.nparts == 1:
epoch_loss = running_cls_loss / dataset_sizes[phase]
else:
epoch_loss = (running_cls_loss + running_reg_loss) / dataset_sizes[phase]
epoch_acc = running_corrects.double() / dataset_sizes[phase]
print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))
# deep copy the model
if phase == 'test' and epoch_acc > best_acc:
best_acc = epoch_acc
best_epoch = epoch
best_step = global_step_resume
best_model_params = copy.deepcopy(model.state_dict())
if phase == 'test' and epoch % 2 == 1:
modelserial.saveCheckpoint({'epoch': epoch,
'best_epoch': best_epoch,
'state_dict': model.state_dict(),
'best_state_dict': best_model_params,
'best_acc': best_acc}, datasetname)
print()
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
print('Best test Acc: {:4f}'.format(best_acc))
rsltparams = dict()
rsltparams['val_acc'] = best_acc.item()
rsltparams['gamma1'] = criterion[1].gamma
rsltparams['gamma2'] = criterion[2].gamma
rsltparams['gamma3'] = criterion[3].gamma
rsltparams['lr'] = optimizer.param_groups[0]['lr']
rsltparams['best_epoch'] = best_epoch
rsltparams['best_step'] = best_step
# load best model weights
model.load_state_dict(best_model_params)
return model, rsltparams
def eval(model, dataloader=None, datasetname=None):
model.eval()
datasize = len(dataloader.dataset)
running_corrects = 0
num_label_counts = dict()
pred_label_counts = dict()
for inputs, labels in dataloader:
for label in labels.data:
num_label_counts.setdefault(label.item(), 0)
num_label_counts[label.item()] += 1
inputs = inputs.to(device)
labels = labels.to(device)
with torch.no_grad():
if model.module.nparts == 1:
outputs = model(inputs)
preds = torch.argmax(outputs, dim=1)
else:
outputs_ulti, outputs_plty, outputs_cmbn, _, _, _ = model(inputs)
if datasetname is 'stdogs':
preds = torch.argmax(outputs_ulti + outputs_plty, dim=1)
else:
preds = torch.argmax(outputs_ulti + outputs_plty + outputs_cmbn, dim=1)
if datasetname is 'vggaircraft':
for i, label in enumerate(preds.data):
if label == labels[i]:
pred_label_counts.setdefault(label.item(), 0)
pred_label_counts[label.item()] += 1
running_corrects += torch.sum(preds == labels.data)
acc = torch.div(running_corrects.double(), datasize).item()
print("{}: Test Accuracy: {}".format(datasetname, acc))
if datasetname is 'vggaircraft':
running_corrects_ = 0
for key in pred_label_counts.keys():
running_corrects_ += pred_label_counts[key] / num_label_counts[key]
avg_acc = running_corrects_ / len(num_label_counts)
print("{}: Class Average Accuracy: - {}".format(datasetname, avg_acc))
rsltparams = dict()
rsltparams['test_acc'] = acc
return rsltparams