forked from Project-MONAI/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
151 lines (138 loc) · 5.46 KB
/
utils.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
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from datetime import timedelta
import numpy as np
import torch
import torch.distributed as dist
from monai.apps import DecathlonDataset
from monai.bundle import ConfigParser
from monai.data import DataLoader
from monai.transforms import (
CenterSpatialCropd,
Compose,
EnsureChannelFirstd,
EnsureTyped,
Lambdad,
LoadImaged,
Orientationd,
RandSpatialCropd,
ScaleIntensityRangePercentilesd,
Spacingd,
)
def setup_ddp(rank, world_size):
print(f"Running DDP diffusion example on rank {rank}/world_size {world_size}.")
print(f"Initing to IP {os.environ['MASTER_ADDR']}")
dist.init_process_group(
backend="nccl", init_method="env://", timeout=timedelta(seconds=36000), rank=rank, world_size=world_size
) # gloo, nccl
dist.barrier()
device = torch.device(f"cuda:{rank}")
return dist, device
def prepare_dataloader(
args,
batch_size,
patch_size,
randcrop=True,
rank=0,
world_size=1,
cache=1.0,
download=False,
size_divisible=16,
amp=False,
):
ddp_bool = world_size > 1
channel = args.channel # 0 = Flair, 1 = T1
assert channel in [0, 1, 2, 3], "Choose a valid channel"
if randcrop:
train_crop_transform = RandSpatialCropd(keys=["image"], roi_size=patch_size, random_size=False)
val_patch_size = [int(np.ceil(1.5 * p / size_divisible) * size_divisible) for p in patch_size]
else:
train_crop_transform = CenterSpatialCropd(keys=["image"], roi_size=patch_size)
val_patch_size = patch_size
if amp:
compute_dtype = torch.float16
else:
compute_dtype = torch.float32
train_transforms = Compose(
[
LoadImaged(keys=["image"]),
EnsureChannelFirstd(keys=["image"]),
Lambdad(keys="image", func=lambda x: x[channel, :, :, :]),
EnsureChannelFirstd(keys=["image"], channel_dim="no_channel"),
EnsureTyped(keys=["image"]),
Orientationd(keys=["image"], axcodes="RAS"),
Spacingd(keys=["image"], pixdim=args.spacing, mode=("bilinear")),
train_crop_transform,
ScaleIntensityRangePercentilesd(keys="image", lower=0, upper=99.5, b_min=0, b_max=1),
EnsureTyped(keys="image", dtype=compute_dtype),
]
)
val_transforms = Compose(
[
LoadImaged(keys=["image"]),
EnsureChannelFirstd(keys=["image"]),
Lambdad(keys="image", func=lambda x: x[channel, :, :, :]),
EnsureChannelFirstd(keys=["image"], channel_dim="no_channel"),
EnsureTyped(keys=["image"]),
Orientationd(keys=["image"], axcodes="RAS"),
Spacingd(keys=["image"], pixdim=args.spacing, mode=("bilinear")),
CenterSpatialCropd(keys=["image"], roi_size=val_patch_size),
ScaleIntensityRangePercentilesd(keys="image", lower=0, upper=99.5, b_min=0, b_max=1),
EnsureTyped(keys="image", dtype=compute_dtype),
]
)
os.makedirs(args.data_base_dir, exist_ok=True)
train_ds = DecathlonDataset(
root_dir=args.data_base_dir,
task="Task01_BrainTumour",
section="training", # validation
cache_rate=cache, # you may need a few Gb of RAM... Set to 0 otherwise
num_workers=8,
download=download, # Set download to True if the dataset hasnt been downloaded yet
seed=0,
transform=train_transforms,
)
val_ds = DecathlonDataset(
root_dir=args.data_base_dir,
task="Task01_BrainTumour",
section="validation", # validation
cache_rate=cache, # you may need a few Gb of RAM... Set to 0 otherwise
num_workers=8,
download=download, # Set download to True if the dataset hasnt been downloaded yet
seed=0,
transform=val_transforms,
)
if ddp_bool:
train_sampler = torch.utils.data.distributed.DistributedSampler(train_ds, num_replicas=world_size, rank=rank)
val_sampler = torch.utils.data.distributed.DistributedSampler(val_ds, num_replicas=world_size, rank=rank)
else:
train_sampler = None
val_sampler = None
train_loader = DataLoader(
train_ds, batch_size=batch_size, shuffle=(not ddp_bool), num_workers=0, pin_memory=False, sampler=train_sampler
)
val_loader = DataLoader(
val_ds, batch_size=batch_size, shuffle=False, num_workers=0, pin_memory=False, sampler=val_sampler
)
if rank == 0:
print(f'Image shape {train_ds[0]["image"].shape}')
return train_loader, val_loader
def define_instance(args, instance_def_key):
parser = ConfigParser(vars(args))
parser.parse(True)
return parser.get_parsed_content(instance_def_key, instantiate=True)
def KL_loss(z_mu, z_sigma):
kl_loss = 0.5 * torch.sum(
z_mu.pow(2) + z_sigma.pow(2) - torch.log(z_sigma.pow(2)) - 1,
dim=list(range(1, len(z_sigma.shape))),
)
return torch.sum(kl_loss) / kl_loss.shape[0]