-
Notifications
You must be signed in to change notification settings - Fork 4
/
trainer.py
192 lines (147 loc) · 7.31 KB
/
trainer.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
import gc
import statistics
import numpy as np
import torch
import torch.nn.functional as F
import pytorch_lightning as pl
import torchmetrics
from torch import nn
from torch.optim import lr_scheduler
from network.base_classifier import ClassifierLossModuleList
class LocalModule(pl.LightningModule):
def __init__(self, backbone, loss_networks, metrics_fn, args, num_classes, valid_as_train=True):
super(LocalModule, self).__init__()
# disable auto optim
self.automatic_optimization = False
self.args = args
self.model = backbone
# adding a dummy loss network
loss_networks.insert(0, ClassifierLossModuleList())
self.loss_nets = nn.ModuleList(loss_networks)
self.K = self.model.K
self.lr = args.lr
self.accumulate_grad_batches = args.accumulate_grad_batches
self.train_metrics, self.eval_metrics, self.test_metrics = metrics_fn
self.acc_metrics = torchmetrics.Accuracy(num_classes=num_classes)
eval_test_prefix = "test_" + self.eval_metrics.prefix if self.eval_metrics.prefix is not None else None
eval_test_postfix = self.eval_metrics.postfix + "_test" if self.eval_metrics.postfix is not None else None
self.eval_test_metrics = self.eval_metrics.clone(prefix=eval_test_prefix, postfix=eval_test_postfix)
self.valid_as_train=valid_as_train
self.save_hyperparameters("args")
def forward(self, x, label=None, ki=-1):
# in lightning, forward defines the prediction/inference actions
if ki == 0:
return self.model(x, ki)
loss_fn = self.loss_nets[ki] if ki > 0 else self.loss_nets[self.K]
label = label if loss_fn.clf_loss_dict["require_label"] else None
features = self.model(x, ki=ki)
if not loss_fn.clf_loss_dict["require_x"]:
x = None
loss = self.loss_nets[ki](features, x=x, label=label)
y = None
if isinstance(loss, (list, tuple)):
loss, y = loss
return features, y, loss
def training_step(self, batch, batch_idx):
img, label = batch
retry_time = 0
while retry_time < 2:
try:
if retry_time >= 1:
print("Halfing image size")
with torch.no_grad():
img_size = img.shape[-2:]
img_size = img_size[0] // 6, img_size[1] // 6
img = img[:, :, img_size[0]: -img_size[0], img_size[1]: -img_size[1]]
ft = self(img, label=None, ki=0)
for cur_k in range(1, self.K + 1):
ft, y_k, loss_k = self(ft, label, ki=cur_k)
y_prob_k = F.softmax(y_k, dim=1)
self.manual_backward(loss_k / self.accumulate_grad_batches)
ft = ft.detach()
self.log("loss/train_%d" % cur_k, loss_k, on_step=True, on_epoch=True, sync_dist=True)
self.log("acc/train_%d" % cur_k, self.acc_metrics(y_prob_k, label), on_step=False, on_epoch=True)
# gc.collect()
# torch.cuda.empty_cache()
opt = self.optimizers()
if (batch_idx + 1) % self.accumulate_grad_batches == 0:
opt.step()
opt.zero_grad()
loss = loss_k
y_prob = y_prob_k
break
except RuntimeError as e:
retry_time += 1
print('Runtime Error {}\nRun Again......{}/{}'.format(e, retry_time, 2))
# gc.collect()
# torch.cuda.empty_cache()
if retry_time >= 2:
print('Give up!')
return {"loss": torch.zeros(1, device=label.device).mean(),
"y_prob_batch": None, "label_batch": None}
# return None
return {"loss": loss.detach(), "y_prob_batch": y_prob.detach(), "label_batch": label.detach()}
def validation_step(self, batch, batch_idx, dataloader_idx):
img, label = batch
# ft, y, loss = self(img, label, ki=-1)
if self.valid_as_train:
ft = self(img, label=None, ki=0)
with torch.no_grad():
for cur_k in range(1, self.K + 1):
ft, y_k, loss_k = self(ft, label, ki=cur_k)
y_prob_k = F.softmax(y_k, dim=1)
self.log("loss/part_%d" % cur_k, loss_k, on_step=True, on_epoch=True, sync_dist=True)
self.log("acc/part_%d" % cur_k, self.acc_metrics(y_prob_k, label), on_step=False, on_epoch=True)
else:
ft, y_k, loss_k = self(img, label, ki=-1)
# y_prob = F.softmax(y, dim=1)
y_prob = y_k
loss = loss_k
self.log("loss", loss, on_step=True, on_epoch=True, sync_dist=True)
self.log("acc", self.acc_metrics(y_prob, label), on_step=False, on_epoch=True)
return {"loss": loss.detach(), "y_prob_batch": y_prob.detach(), "label_batch": label.detach()}
def test_step(self, batch, batch_idx):
img, label = batch
ft, y, loss = self(img, label, ki=-1)
y_prob = F.softmax(y, dim=1)
self.log("loss/test", loss, on_step=False, on_epoch=True, sync_dist=True)
return {"loss": loss, "y_prob_batch": y_prob.detach(), "label_batch": label.detach()}
def training_epoch_end(self, outs):
y_prob = torch.cat([o["y_prob_batch"] for o in outs if o is not None and o["y_prob_batch"] is not None], dim=0)
label = torch.cat([o["label_batch"] for o in outs if o is not None and o["label_batch"] is not None], dim=0)
metrics = self.train_metrics(y_prob, label)
# metrics['step'] = self.current_epoch
self.logger.log_metrics(metrics, step=self.current_epoch)
sch = self.lr_schedulers()
sch.step()
def validation_epoch_end(self, outs_dl):
for outs, metrics_fn in zip(outs_dl, [self.eval_metrics, self.eval_test_metrics]):
y_prob = torch.cat([o["y_prob_batch"] for o in outs if o is not None], dim=0)
label = torch.cat([o["label_batch"] for o in outs if o is not None], dim=0)
metrics = metrics_fn(y_prob, label)
if not self.trainer.sanity_checking:
self.logger.log_metrics(metrics, step=self.current_epoch)
def test_epoch_end(self, outs):
y_prob = torch.cat([o["y_prob_batch"] for o in outs], dim=0)
label = torch.cat([o["label_batch"] for o in outs], dim=0)
metrics = self.test_metrics(y_prob, label)
# metrics['step'] = self.current_epoch
self.logger.log_metrics(metrics)
print("Testing metrics:")
print(metrics)
return metrics
def configure_optimizers(self):
if self.args.weight_decay is None:
self.args.weight_decay = 1e-2
cus_optimizer = torch.optim.AdamW(
[
{"params": self.model.parameters(), "lr": self.lr * self.args.lr_factor},
{"params": self.loss_nets.parameters(), }
],
# self.parameters(),
lr=self.lr, weight_decay=self.args.weight_decay)
cus_sch = lr_scheduler.MultiStepLR(cus_optimizer, self.args.decay_multi_epochs, last_epoch=-1, verbose=True)
return {
"optimizer": cus_optimizer,
"lr_scheduler": cus_sch
}