-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain.py
150 lines (118 loc) · 5.33 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 numpy as np
from sklearn import metrics
import torch
import datasets
import models
from instrumentation import compute_metrics
import losses
import datetime
import os
from tqdm import tqdm
def run_train(P):
dataset = datasets.get_data(P)
dataloader = {}
for phase in ['train', 'val', 'test']:
dataloader[phase] = torch.utils.data.DataLoader(
dataset[phase],
batch_size = P['bsize'],
shuffle = phase == 'train',
sampler = None,
num_workers = P['num_workers'],
drop_last = False,
pin_memory = True
)
model = models.ImageClassifier(P)
feature_extractor_params = [param for param in list(model.feature_extractor.parameters()) if param.requires_grad]
linear_classifier_params = [param for param in list(model.linear_classifier.parameters()) if param.requires_grad]
opt_params = [
{'params': feature_extractor_params, 'lr' : P['lr']},
{'params': linear_classifier_params, 'lr' : P['lr_mult'] * P['lr']}
]
if P['optimizer'] == 'adam':
optimizer = torch.optim.Adam(opt_params, lr=P['lr'])
elif P['optimizer'] == 'sgd':
optimizer = torch.optim.SGD(opt_params, lr=P['lr'], momentum=0.9, weight_decay=0.001)
# training loop
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model.to(device)
bestmap_val = 0
bestmap_test = 0
for epoch in range(1, P['num_epochs']+1):
for phase in ['train', 'val']:
if phase == 'train':
model.train()
else:
model.eval()
y_pred = np.zeros((len(dataset[phase]), P['num_classes']))
y_true = np.zeros((len(dataset[phase]), P['num_classes']))
batch_stack = 0
with torch.set_grad_enabled(phase == 'train'):
for batch in tqdm(dataloader[phase]):
# Move data to GPU
image = batch['image'].to(device, non_blocking=True)
label_vec_obs = batch['label_vec_obs'].to(device, non_blocking=True)
label_vec_true = batch['label_vec_true'].clone().numpy()
idx = batch['idx']
# Forward pass
optimizer.zero_grad()
logits = model(image)
if logits.dim() == 1:
logits = torch.unsqueeze(logits, 0)
preds = torch.sigmoid(logits)
if phase == 'train':
loss, correction_idx = losses.compute_batch_loss(logits, label_vec_obs, P)
loss.backward()
optimizer.step()
if P['mod_scheme'] is 'LL-Cp' and correction_idx is not None:
dataset[phase].label_matrix_obs[idx[correction_idx[0].cpu()], correction_idx[1].cpu()] = 1.0
else:
preds_np = preds.cpu().numpy()
this_batch_size = preds_np.shape[0]
y_pred[batch_stack : batch_stack+this_batch_size] = preds_np
y_true[batch_stack : batch_stack+this_batch_size] = label_vec_true
batch_stack += this_batch_size
metrics = compute_metrics(y_pred, y_true)
del y_pred
del y_true
map_val = metrics['map']
P['clean_rate'] -= P['delta_rel']
print(f"Epoch {epoch} : val mAP {map_val:.3f}")
if bestmap_val < map_val:
bestmap_val = map_val
bestmap_epoch = epoch
print(f'Saving model weight for best val mAP {bestmap_val:.3f}')
path = os.path.join(P['save_path'], 'bestmodel.pt')
torch.save((model.state_dict(), P), path)
elif bestmap_val - map_val > 3:
print('Early stopped.')
break
# Test phase
model_state, _ = torch.load(path)
model.load_state_dict(model_state)
phase = 'test'
model.eval()
y_pred = np.zeros((len(dataset[phase]), P['num_classes']))
y_true = np.zeros((len(dataset[phase]), P['num_classes']))
batch_stack = 0
with torch.set_grad_enabled(phase == 'train'):
for batch in tqdm(dataloader[phase]):
# Move data to GPU
image = batch['image'].to(device, non_blocking=True)
label_vec_obs = batch['label_vec_obs'].to(device, non_blocking=True)
label_vec_true = batch['label_vec_true'].clone().numpy()
idx = batch['idx']
# Forward pass
optimizer.zero_grad()
logits = model(image)
if logits.dim() == 1:
logits = torch.unsqueeze(logits, 0)
preds = torch.sigmoid(logits)
preds_np = preds.cpu().numpy()
this_batch_size = preds_np.shape[0]
y_pred[batch_stack : batch_stack+this_batch_size] = preds_np
y_true[batch_stack : batch_stack+this_batch_size] = label_vec_true
batch_stack += this_batch_size
metrics = compute_metrics(y_pred, y_true)
map_test = metrics['map']
print('Training procedure completed!')
print(f'Test mAP : {map_test:.3f} when trained until epoch {bestmap_epoch}')