-
Notifications
You must be signed in to change notification settings - Fork 2
/
trainer.py
171 lines (125 loc) · 4.1 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
import torch
import tools.torch_utils
from tools.utils import get_class
import numpy as np
from torch.utils.data import DataLoader
from base_trainer import BaseTrainer
from sumagg.sumagg import SumAgg
from summaries import MeanSummary
from model.model_vgg import Model
from dataset.MedMNIST import MedMNIST, collate_fn
from loss.loss import Loss
class Trainer(BaseTrainer):
def __init__(self, cfg):
super().__init__(cfg)
self.loss_f = Loss(cfg)
def declare_summaries(self):
SumAgg().add_summaries([
MeanSummary("loss_val"),
MeanSummary("acc"),
MeanSummary("auc"),
MeanSummary("preds"),
])
def load_model(self):
model = Model(self.cfg).cuda()
print(model)
return model
def create_optimizer(self, model_parameters):
optimizer = get_class(self.cfg.optimizer.name)(
model_parameters, **self.cfg.optimizer.params
)
return optimizer
def create_scheduler(self, optimizer):
if self.cfg.scheduler.use:
scheduler = get_class(self.cfg.scheduler.name)(optimizer, **self.cfg.scheduler.params)
else:
scheduler = None
return scheduler
def _format_sample(self, sample):
img = [torch.tensor(v) for v in sample["img"]]
img = torch.stack(img, 0).float()#.cuda()
B, H, W, C = img.shape
T = 1
img = img.permute(0, 3, 1, 2) # B, C, H, W
img = img.view(B, T, C, H, W) # B, T, C, H, W
class_ = [torch.tensor(v) for v in sample["class"]]
class_ = torch.cat(class_, 0).float()#.cuda()
class_ = class_.view(B, T)
return {
"img": img,
"class": class_,
}
def _chunk_sample(self, sample, chunk_size):
B, T, C, H, W = sample["img"].shape
chunks = []
for s_start in range(0, T, chunk_size):
s_end = s_start + chunk_size
chunk = {
"img": sample["img"][:, s_start:s_end],
"class": sample["class"][:, s_start:s_end],
}
chunks.append(chunk)
return chunks
def _prepare_sample(self, sample):
sample["img"] = sample["img"].cuda()
return sample
def forward(self, sample, state):
x = sample["img"]
# B, T, C, H, W = x.shape
y = self.model(x)
return y
def compute_loss(self, sample, y):
B, T, n_classes = y.logits.shape
gt = sample["class"]
loss = self.loss_f(y, gt)
preds = self.loss_f.predict(y)
gt_np = sample["class"].cpu().numpy()
acc = (gt_np == preds).sum() / (B*T)
SumAgg().add("acc", acc)
from sklearn.metrics import roc_auc_score
assert T == 1
if len(np.unique(gt_np[:, 0].sum())) < 2:
auc = np.nan
else:
auc = roc_auc_score(gt_np[:, 0], y.logits.softmax(-1)[:,0, 1].detach().cpu().numpy())
SumAgg().add("auc", auc)
return loss
def get_current_score(self):
current_score = SumAgg().epoch_metrics["acc"]
return current_score
def get_dataloaders(self):
dataloader_kwargs = dict(
batch_size=self.cfg.batch_size,
shuffle=True,
num_workers=self.cfg.n_workers,
pin_memory=True,
collate_fn=collate_fn,
)
# Train
dataset = MedMNIST(
cfg=self.cfg,
split="train",
)
dataloader_train = DataLoader(
dataset,
**dataloader_kwargs
)
# Val
dataset = MedMNIST(
cfg=self.cfg,
split="val"
)
dataloader_val = DataLoader(
dataset,
**dataloader_kwargs
)
# Test
dataset = MedMNIST(
cfg=self.cfg,
split="test",
)
dataloader_test = DataLoader(
dataset,
**dataloader_kwargs
)
return dataloader_train, dataloader_val, dataloader_test