This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
train_unpaired.py
375 lines (296 loc) · 13 KB
/
train_unpaired.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import json
from collections import defaultdict
from contextlib import contextmanager
from pathlib import Path
import einops
import pickle
import numpy as np
from typing import Dict, Optional, Tuple, Union
from torch.utils.tensorboard.writer import SummaryWriter
import torch
import torch.nn.functional as F
import nibabel as nib
from torch.utils.data import Dataset, DataLoader
from typer import Typer
from tqdm import trange, tqdm
from common import (
concat_flow,
identity_grid_torch,
load_keypoints,
tb_log,
torch2skimage_disp,
warp_image,
)
from differentiable_metrics import (
MINDLoss,
DiceLoss,
Grad,
TotalRegistrationLoss,
MutualInformationLoss,
)
from train import PatchDataset
from networks import SomeNet
app1 = Typer()
get_spacing = lambda x: np.sqrt(np.sum(x.affine[:3, :3] * x.affine[:3, :3], axis=0))
@contextmanager
def evaluating(net):
"""Temporarily switch to evaluation mode."""
istrain = net.training
try:
net.eval()
yield net
finally:
if istrain:
net.train()
class ImageDataset(Dataset):
"""
Loads images
"""
def __init__(
self,
data_json: Path,
split: str,
downsample: int
):
with open(data_json, "r") as f:
data = json.load(f)[split]
self.data = data
self.downsample = downsample
def __len__(self):
return len(self.data)
def __getitem__(self, index):
data = self.data[index]
image_nib = nib.load(data["image"])
image = torch.from_numpy(image_nib.get_fdata())
rshape = tuple(i // self.downsample for i in image.shape[-3:])
image = (image - image.min())/(image.max()-image.min())
image = image.squeeze().unsqueeze(0)
image = F.interpolate(image.unsqueeze(0), rshape).squeeze(0).float()
ret = {"image": image}
if "segmentation" in data:
segmentation_nib = nib.load(data["segmentation"])
segmentation = torch.from_numpy(segmentation_nib.get_fdata()).float()
ret["segmentation"] = segmentatation
if "mask" in data:
mask_nib = nib.load(data["mask"])
mask = torch.from_numpy(mask_nib.get_fdata()).float()
ret["mask"] = mask
return ret
@app1.command()
def train_stage2(
data_json: Path,
stage1_downsample: int,
stage1_model: Path,
stage2_downsample: int,
stage2_patchfactor: int,
checkpoint_dir: Path,
start: Optional[Path]= None,
device: str = "cuda",
iters: int = 12,
search_range: int = 3,
steps: int=50000,
lr: float=3e-4,
image_loss_weight: float=1,
reg_loss_weight: float=.01,
log_freq: int=1,
val_freq: int=1,
save_freq: int=1,
stage1_model_iters: int=12,
stage1_model_search_range: int=3
):
train_dataset = ImageDataset(data_json, split="train", downsample=stage1_downsample)
val_dataset = PatchDataset(data_json, split="val", res_factor=stage1_downsample, patch_factor=stage1_downsample)
train_loader = DataLoader(train_dataset, batch_size=2, shuffle=True) #, num_workers=4)
val_loader = DataLoader(val_dataset, batch_size=1, shuffle=True) #, num_workers=4)
r, p = 1 / stage2_downsample, 1 / stage2_patchfactor
n_patches = (((r - p) / p) + 1) ** 2
chan_split = r / p
model_stage1 = SomeNet(iters=stage1_model_iters, search_range=stage1_model_search_range).to(device)
model_stage1 = model_stage1.eval().to(device)
model_stage1.requires_grad = False
model = SomeNet(iters=iters, search_range=search_range)
step_count = 0
if start is not None:
model.load_state_dict(torch.load(start))
step_count = int(start.name.split("_")[-1].split(".")[0])
opt = torch.optim.Adam(model.parameters(), lr=lr)
checkpoint_dir.mkdir(exist_ok=True)
writer = SummaryWriter(log_dir=checkpoint_dir)
print(f"Dataset size is {len(train_dataset)}")
print(f"Starting training from step {step_count}")
while step_count + 1 < steps:
for step_count, data in zip(trange(step_count, steps), train_loader):
fixed = data["image"][0,...].unsqueeze(0)
moving = data["image"][1,...].unsqueeze(0)
fixed, moving = fixed.to(device), moving.to(device)
flow, hidden = model(fixed, moving)
moved = warp_image(flow, moving)
losses_dict: Dict[str, torch.Tensor] = {}
losses_dict["image_loss"] = image_loss_weight * MutualInformationLoss()(
moved.squeeze(), fixed.squeeze()
)
losses_dict["grad"] = reg_loss_weight * Grad()(flow)
if "segmentation" in data:
fixed_segmentation = data["segmentataion"][0,...].unsqueeze(0)
moving_segmentation = data["segmentataion"][1,...].unsqueeze(0)
moved_segmentation = warp_image(flow, moving_mask)
fixed_segmentation = torch.round(fixed_segmentation)
moved_segmentation = torch.round(moved_segmentation)
losses_dict["dice_loss"] = seg_loss_weight * DiceLoss()(
fixed_segmenatation, moved_segmentation())
total_loss = sum(losses_dict.values())
assert isinstance(total_loss, torch.Tensor)
losses_dict_log = {k: v.item() for k, v in losses_dict.items()}
opt.zero_grad()
total_loss.backward()
opt.step()
if step_count % log_freq == 0:
tb_log(
writer,
losses_dict_log,
step=step_count,
moving_fixed_moved=(moving, fixed, moved),
)
if val_freq > 0 and step_count % val_freq == 0 and step_count > 0:
losses_cum_dict = defaultdict(list)
with torch.no_grad(), evaluating(model):
for data in val_loader:
fixed, moving = data["fixed_image"], data["moving_image"]
fixed, moving = fixed.to(device), moving.to(device)
flow, hidden = model(fixed, moving)
moved = warp_image(flow, moving)
losses_cum_dict["image_loss"].append(
(
image_loss_weight
* MutualInformationLoss()(
moved.squeeze(), fixed.squeeze()
)
).item()
)
losses_cum_dict["grad"].append(
(reg_loss_weight * Grad()(flow)).item()
)
if "fixed_segmentation" in data:
fixed_segmentation = data["fixed_segmentation"].to(device).float()
moving_segmentation = data["moving_segmentation"].to(device).float()
moved_segmentation = warp_image(flow, moving_mask)
fixed_segmentation = torch.round(fixed_segmentation)
moved_segmentation = torch.round(moved_segmentation)
losses_cum_dict["dice_loss"].append(
seg_loss_weight
* DiceLoss()(fixed_segmenatation, moved_segmentation())
)
for k, v in losses_cum_dict.items():
writer.add_scalar(
f"val_{k}", np.mean(v).item(), global_step=step_count
)
if step_count % save_freq == 0:
torch.save(
model.state_dict(),
checkpoint_dir / f"rnn{stage1_downsample}x_{step_count}.pth",
)
torch.save(model.state_dict(), checkpoint_dir / f"rnn{stage1_downsample}x_{step_count}.pth")
@app1.command()
def train_stage1(
data_json: Path,
stage1_downsample: int,
checkpoint_dir: Path,
start: Optional[Path]= None,
device: str = "cuda",
iters: int = 12,
search_range: int = 3,
steps: int=50000,
lr: float=3e-4,
image_loss_weight: float=1,
reg_loss_weight: float=.01,
log_freq: int=1,
val_freq: int=1,
save_freq: int=1,
):
train_dataset = ImageDataset(data_json, split="train", downsample=stage1_downsample)
val_dataset = PatchDataset(data_json, split="val", res_factor=stage1_downsample, patch_factor=stage1_downsample)
train_loader = DataLoader(train_dataset, batch_size=2, shuffle=True) #, num_workers=4)
val_loader = DataLoader(val_dataset, batch_size=1, shuffle=True) #, num_workers=4)
model = SomeNet(iters=iters, search_range=search_range).to(device)
step_count = 0
if start is not None:
model.load_state_dict(torch.load(start))
step_count = int(start.name.split("_")[-1].split(".")[0])
opt = torch.optim.Adam(model.parameters(), lr=lr)
checkpoint_dir.mkdir(exist_ok=True)
writer = SummaryWriter(log_dir=checkpoint_dir)
print(f"Dataset size is {len(train_dataset)}")
print(f"Starting training from step {step_count}")
while step_count + 1 < steps:
for step_count, data in zip(trange(step_count, steps), train_loader):
fixed = data["image"][0,...].unsqueeze(0)
moving = data["image"][1,...].unsqueeze(0)
fixed, moving = fixed.to(device), moving.to(device)
flow, hidden = model(fixed, moving)
moved = warp_image(flow, moving)
losses_dict: Dict[str, torch.Tensor] = {}
losses_dict["image_loss"] = image_loss_weight * MutualInformationLoss()(
moved.squeeze(), fixed.squeeze()
)
losses_dict["grad"] = reg_loss_weight * Grad()(flow)
if "segmentation" in data:
fixed_segmentation = data["segmentataion"][0,...].unsqueeze(0)
moving_segmentation = data["segmentataion"][1,...].unsqueeze(0)
moved_segmentation = warp_image(flow, moving_mask)
fixed_segmentation = torch.round(fixed_segmentation)
moved_segmentation = torch.round(moved_segmentation)
losses_dict["dice_loss"] = seg_loss_weight * DiceLoss()(
fixed_segmenatation, moved_segmentation())
total_loss = sum(losses_dict.values())
assert isinstance(total_loss, torch.Tensor)
losses_dict_log = {k: v.item() for k, v in losses_dict.items()}
opt.zero_grad()
total_loss.backward()
opt.step()
if step_count % log_freq == 0:
tb_log(
writer,
losses_dict_log,
step=step_count,
moving_fixed_moved=(moving, fixed, moved),
)
if val_freq > 0 and step_count % val_freq == 0 and step_count > 0:
losses_cum_dict = defaultdict(list)
with torch.no_grad(), evaluating(model):
for data in val_loader:
fixed, moving = data["fixed_image"], data["moving_image"]
fixed, moving = fixed.to(device), moving.to(device)
flow, hidden = model(fixed, moving)
moved = warp_image(flow, moving)
losses_cum_dict["image_loss"].append(
(
image_loss_weight
* MutualInformationLoss()(
moved.squeeze(), fixed.squeeze()
)
).item()
)
losses_cum_dict["grad"].append(
(reg_loss_weight * Grad()(flow)).item()
)
if "fixed_segmentation" in data:
fixed_segmentation = data["fixed_segmentation"].to(device).float()
moving_segmentation = data["moving_segmentation"].to(device).float()
moved_segmentation = warp_image(flow, moving_mask)
fixed_segmentation = torch.round(fixed_segmentation)
moved_segmentation = torch.round(moved_segmentation)
losses_cum_dict["dice_loss"].append(
seg_loss_weight
* DiceLoss()(fixed_segmenatation, moved_segmentation())
)
for k, v in losses_cum_dict.items():
writer.add_scalar(
f"val_{k}", np.mean(v).item(), global_step=step_count
)
if step_count % save_freq == 0:
torch.save(
model.state_dict(),
checkpoint_dir / f"rnn{stage1_downsample}x_{step_count}.pth",
)
torch.save(model.state_dict(), checkpoint_dir / f"rnn{stage1_downsample}x_{step_count}.pth")
app1()