-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloader.py
198 lines (175 loc) · 7.24 KB
/
loader.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
import random
import math
import numpy as np
import os
from monai.transforms import (
AddChanneld,
Compose,
CropForegroundd,
LoadImaged,
Orientationd,
RandCropByPosNegLabeld,
ScaleIntensityRanged,
Spacingd,
ToTensord,
RandSpatialCropSamplesd,
SpatialPadd,
NormalizeIntensityd,
AsChannelFirstd,
NormalizeIntensityd,
RandFlipd,
RandGaussianNoised,
ThresholdIntensityd,
Rand3DElastic,
SpatialCropd,
)
from monai.data import (
CacheDataset,
SmartCacheDataset,
load_decathlon_datalist,
DataLoader,
Dataset,
DistributedSampler
)
def get_loader(args):
datadir = args.data_dir
#jsonlist = os.path.join(datadir, args.json_list)
jsonlist = args.json_list
num_workers = args.num_workers
#num_none_list = [669, 966, 1567]
new_datalist = []
datalist = load_decathlon_datalist(jsonlist, False, "training", base_dir=datadir)
for item in datalist:
item_name = ''.join(item['image']).split('.')[0].split('/')[-2]
item_num = int(''.join(item_name).split('_')[1])
#if item_num in num_none_list:
#continue
item_dict = {'image': item['image'], 'name': item_name}
new_datalist.append(item_dict)
new_vallist = []
vallist = load_decathlon_datalist(jsonlist, False, "validation", base_dir=datadir)
for item in vallist:
item_name = ''.join(item['image']).split('.')[0].split('/')[-2]
item_num = int(''.join(item_name).split('_')[1])
#if item_num in num_none_list:
#continue
item_dict = {'image': item['image'], 'name': item_name}
new_vallist.append(item_dict)
datalist = new_datalist
val_files = new_vallist
print('Dataset all training: number of data: {}'.format(len(datalist)))
print('Dataset all validation: number of data: {}'.format(len(val_files)))
train_transforms = Compose(
[
LoadImaged(keys=["image"]),
AddChanneld(keys=["image"]),
Orientationd(keys=["image"], axcodes="RAI", as_closest_canonical=True),
ThresholdIntensityd(keys=["image"],
threshold=args.a_max,
above=False,
cval=args.a_max,
allow_missing_keys=False),
ThresholdIntensityd(keys=["image"],
threshold=args.a_min,
above=True,
cval=args.a_min,
allow_missing_keys=False),
ScaleIntensityRanged(keys=["image"],
a_min=args.a_min,
a_max=args.a_max,
b_min=args.b_min,
b_max=args.b_max,
clip=True),
# SpatialPadd(keys="image", spatial_size=[args.roi_x,
# args.roi_y,
# args.roi_z]),
# CropForegroundd(keys=["image"], source_key="image", k_divisible=[args.roi_x,
# args.roi_y,
# args.roi_z]),
# RandSpatialCropSamplesd(
# keys=["image"],
# roi_size=[args.roi_x,
# args.roi_y,
# args.roi_z],
# num_samples=args.global_crops_number,
# random_center=True,
# random_size=False
# ),
ToTensord(keys=["image"]),
]
)
val_transforms = Compose(
[
LoadImaged(keys=["image"]),
AddChanneld(keys=["image"]),
Orientationd(keys=["image"], axcodes="RAI", as_closest_canonical=True),
ThresholdIntensityd(keys=["image"],
threshold=args.a_max,
above=False,
cval=args.a_max,
allow_missing_keys=False),
ThresholdIntensityd(keys=["image"],
threshold=args.a_min,
above=True,
cval=args.a_min,
allow_missing_keys=False),
ScaleIntensityRanged(keys=["image"],
a_min=args.a_min,
a_max=args.a_max,
b_min=args.b_min,
b_max=args.b_max,
clip=True),
# SpatialPadd(keys="image", spatial_size=[args.roi_x,
# args.roi_y,
# args.roi_z]),
# CropForegroundd(keys=["image"], source_key="image", k_divisible=[args.roi_x,
# args.roi_y,
# args.roi_z]),
# RandSpatialCropSamplesd(
# keys=["image"],
# roi_size=[args.roi_x,
# args.roi_y,
# args.roi_z],
# num_samples=args.global_crops_number,
# random_center=True,
# random_size=False
# ),
ToTensord(keys=["image"]),
]
)
if args.normal_dataset:
print('Using Normal dataset')
dataset = Dataset(data=datalist, transform=train_transforms)
elif args.smartcache_dataset:
print('Using SmartCacheDataset')
dataset = SmartCacheDataset(data=datalist,
transform=train_transforms,
replace_rate=1,
cache_rate=0.1)
else:
print('Using MONAI Cache Dataset')
dataset = CacheDataset(data=datalist,
transform=train_transforms,
cache_rate=1,
num_workers=num_workers)
if args.distributed:
train_sampler = DistributedSampler(dataset=dataset,
even_divisible=True,
shuffle=True)
else:
train_sampler = None
train_loader = DataLoader(dataset,
batch_size=args.batch_size,
num_workers=num_workers,
sampler=train_sampler,
drop_last=True)
val_ds = SmartCacheDataset(data=val_files,
transform=val_transforms,
replace_rate=1,
cache_rate=0.1)
val_loader = DataLoader(val_ds,
batch_size=args.batch_size,
num_workers=num_workers,
shuffle=False,
drop_last=True)
return train_loader, val_loader