-
Notifications
You must be signed in to change notification settings - Fork 4
/
augmentation.py
58 lines (49 loc) · 1.57 KB
/
augmentation.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
import torch
import monai.transforms as T
def get_transorms(
new_shape,
bright_range=None,
rotation_range=None,
scale_range=None,
num_classes=None,
to_tensor=True,
probs=[0.5, 0.5, 0.5]):
bright_prob, rot_prob, scale_prob = probs
transform_list = []
transform_list.append(T.NormalizeIntensityd(
keys="img",
nonzero=True,
channel_wise=True)
)
transform_list.append(T.Resized(
keys=["img", "seg"],
spatial_size=new_shape,
mode=["bilinear", "nearest"])
)
if bright_range is not None:
transform_list.append(T.RandAdjustContrastd(
keys="img",
prob=bright_prob,
gamma=bright_range)
)
if rotation_range is not None:
transform_list.append(T.RandRotated(
keys=["img", "seg"],
prob=rot_prob,
range_x=rotation_range,
mode=["bilinear", "nearest"])
)
if scale_range is not None:
transform_list.append(T.RandZoomd(
keys=["img", "seg"],
prob=scale_prob,
min_zoom=scale_range[0],
max_zoom=scale_range[1],
mode=["bilinear", "nearest"])
)
if num_classes is not None:
transform_list.append(T.AsDiscreted(keys="seg", to_onehot=num_classes))
if to_tensor == True:
transform_list.append(T.ToTensord(keys=["img", "seg"], dtype=torch.float32))
transform_list.append(T.EnsureTyped(keys=["img", "seg"], dtype=torch.float32))
return T.compose.Compose(transform_list)