-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexperiment.py
executable file
·294 lines (238 loc) · 10.2 KB
/
experiment.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import sys
import os
import argparse
import logging
import logging.config
import yaml
import pathlib
import builtins
import socket
import time
import random
import numpy as np
import torch
import logging
import torchio as tio
import torch.distributed as dist
import torch.utils.data as data
import wandb
from torch import nn
from os import path
from torch.backends import cudnn
from torch.utils.data import DistributedSampler
from torch.utils.data import DataLoader
from tqdm import tqdm
from dataloader.Maxillo import Maxillo
from dataloader.AugFactory import AugFactory
from losses.LossFactory import LossFactory
from models.ModelFactory import ModelFactory
from optimizers.OptimizerFactory import OptimizerFactory
from schedulers.SchedulerFactory import SchedulerFactory
from eval import Eval as Evaluator
eps = 1e-10
class Experiment:
def __init__(self, config, debug=False):
self.config = config
self.debug = debug
self.epoch = 0
self.metrics = {}
filename = 'splits.json'
if self.debug:
filename = 'splits.json.small'
num_classes = len(self.config.data_loader.labels)
if 'Jaccard' in self.config.loss.name or num_classes == 2:
num_classes = 1
# load model
model_name = self.config.model.name
in_ch = 2 if self.config.experiment.name == 'Generation' else 1
emb_shape = [dim // 8 for dim in self.config.data_loader.patch_shape]
self.model = ModelFactory(model_name, num_classes, in_ch, emb_shape).get().cuda()
self.model = nn.DataParallel(self.model)
wandb.watch(self.model, log_freq=10)
# load optimizer
optim_name = self.config.optimizer.name
train_params = self.model.parameters()
lr = self.config.optimizer.learning_rate
self.optimizer = OptimizerFactory(optim_name, train_params, lr).get()
# load scheduler
sched_name = self.config.lr_scheduler.name
sched_milestones = self.config.lr_scheduler.get('milestones', None)
sched_gamma = self.config.lr_scheduler.get('factor', None)
self.scheduler = SchedulerFactory(
sched_name,
self.optimizer,
milestones=sched_milestones,
gamma=sched_gamma,
mode='max',
verbose=True,
patience=15
).get()
# load loss
self.loss = LossFactory(self.config.loss.name, self.config.data_loader.labels)
# load evaluator
self.evaluator = Evaluator(self.config, skip_dump=True)
self.train_dataset = Maxillo(
root=self.config.data_loader.dataset,
filename=filename,
splits='train',
transform=tio.Compose([
tio.CropOrPad(self.config.data_loader.resize_shape, padding_mode=0),
self.config.data_loader.preprocessing,
self.config.data_loader.augmentations,
]),
# dist_map=['sparse','dense']
)
self.val_dataset = Maxillo(
root=self.config.data_loader.dataset,
filename=filename,
splits='val',
transform=self.config.data_loader.preprocessing,
# dist_map=['sparse', 'dense']
)
self.test_dataset = Maxillo(
root=self.config.data_loader.dataset,
filename=filename,
splits='test',
transform=self.config.data_loader.preprocessing,
# dist_map=['sparse', 'dense']
)
self.synthetic_dataset = Maxillo(
root=self.config.data_loader.dataset,
filename=filename,
splits='synthetic',
transform=self.config.data_loader.preprocessing,
# dist_map=['sparse', 'dense'],
)
# self.test_aggregator = self.train_dataset.get_aggregator(self.config.data_loader)
# self.synthetic_aggregator = self.synthetic_dataset.get_aggregator(self.config.data_loader)
# queue start loading when used, not when instantiated
self.train_loader = self.train_dataset.get_loader(self.config.data_loader)
self.val_loader = self.val_dataset.get_loader(self.config.data_loader)
self.test_loader = self.test_dataset.get_loader(self.config.data_loader)
self.synthetic_loader = self.synthetic_dataset.get_loader(self.config.data_loader)
if self.config.trainer.reload:
self.load()
def save(self, name):
if '.pth' not in name:
name = name + '.pth'
path = os.path.join(self.config.project_dir, self.config.title, 'checkpoints', name)
logging.info(f'Saving checkpoint at {path}')
state = {
'title': self.config.title,
'epoch': self.epoch,
'state_dict': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'metrics': self.metrics,
}
torch.save(state, path)
def load(self):
path = self.config.trainer.checkpoint
logging.info(f'Loading checkpoint from {path}')
state = torch.load(path)
if 'title' in state.keys():
# check that the title headers (without the hash) is the same
self_title_header = self.config.title[:-11]
load_title_header = state['title'][:-11]
if self_title_header == load_title_header:
self.config.title = state['title']
self.optimizer.load_state_dict(state['optimizer'])
self.model.load_state_dict(state['state_dict'])
self.epoch = state['epoch'] + 1
if 'metrics' in state.keys():
self.metrics = state['metrics']
def extract_data_from_patch(self, patch):
volume = patch['data'][tio.DATA].float().cuda()
gt = patch['dense'][tio.DATA].float().cuda()
if 'Generation' in self.__class__.__name__:
sparse = patch['sparse'][tio.DATA].float().cuda()
images = torch.cat([volume, sparse], dim=1)
else:
images = volume
emb_codes = torch.cat((
patch[tio.LOCATION][:,:3],
patch[tio.LOCATION][:,:3] + torch.as_tensor(images.shape[-3:])
), dim=1).float().cuda()
return images, gt, emb_codes
def train(self):
self.model.train()
self.evaluator.reset_eval()
data_loader = self.train_loader
if self.config.data_loader.training_set == 'generated':
logging.info('using the generated dataset')
data_loader = self.synthetic_loader
losses = []
for i, d in tqdm(enumerate(data_loader), total=len(data_loader), desc=f'Train epoch {str(self.epoch)}'):
images, gt, emb_codes = self.extract_data_from_patch(d)
partition_weights = 1
# TODO: Do only if not Competitor
gt_count = torch.sum(gt == 1, dim=list(range(1, gt.ndim)))
if torch.sum(gt_count) == 0: continue
partition_weights = (eps + gt_count) / torch.max(gt_count)
self.optimizer.zero_grad()
preds = self.model(images, emb_codes)
assert preds.ndim == gt.ndim, f'Gt and output dimensions are not the same before loss. {preds.ndim} vs {gt.ndim}'
loss = self.loss(preds, gt, partition_weights)
losses.append(loss.item())
loss.backward()
self.optimizer.step()
preds = (preds > 0.5).squeeze().detach()
gt = gt.squeeze()
self.evaluator.compute_metrics(preds, gt)
epoch_train_loss = sum(losses) / len(losses)
epoch_iou, epoch_dice = self.evaluator.mean_metric(phase='Train')
self.metrics['Train'] = {
'iou': epoch_iou,
'dice': epoch_dice,
}
wandb.log({
f'Epoch': self.epoch,
f'Train/Loss': epoch_train_loss,
f'Train/Dice': epoch_dice,
f'Train/IoU': epoch_iou,
f'Train/Lr': self.optimizer.param_groups[0]['lr']
})
return epoch_train_loss, epoch_iou
def test(self, phase):
self.model.eval()
# with torch.no_grad():
with torch.inference_mode():
self.evaluator.reset_eval()
losses = []
if phase == 'Test':
dataset = self.test_dataset
elif phase == 'Validation':
dataset = self.val_dataset
for i, subject in tqdm(enumerate(dataset), total=len(dataset), desc=f'{phase} epoch {str(self.epoch)}'):
sampler = tio.inference.GridSampler(
subject,
self.config.data_loader.patch_shape,
0
)
loader = DataLoader(sampler, batch_size=self.config.data_loader.batch_size)
aggregator = tio.inference.GridAggregator(sampler)
gt_aggregator = tio.inference.GridAggregator(sampler)
for j, patch in enumerate(loader):
images, gt, emb_codes = self.extract_data_from_patch(patch)
preds = self.model(images, emb_codes)
aggregator.add_batch(preds, patch[tio.LOCATION])
gt_aggregator.add_batch(gt, patch[tio.LOCATION])
output = aggregator.get_output_tensor()
gt = gt_aggregator.get_output_tensor()
partition_weights = 1
gt_count = torch.sum(gt == 1, dim=list(range(1, gt.ndim)))
if torch.sum(gt_count) != 0:
partition_weights = (eps + gt_count) / (eps + torch.max(gt_count))
loss = self.loss(output.unsqueeze(0), gt.unsqueeze(0), partition_weights)
losses.append(loss.item())
output = output.squeeze(0)
output = (output > 0.5)
self.evaluator.compute_metrics(output, gt)
epoch_loss = sum(losses) / len(losses)
epoch_iou, epoch_dice = self.evaluator.mean_metric(phase=phase)
wandb.log({
f'Epoch': self.epoch,
f'{phase}/Loss': epoch_loss,
f'{phase}/Dice': epoch_dice,
f'{phase}/IoU': epoch_iou
})
return epoch_iou, epoch_dice