-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
411 lines (306 loc) · 12.5 KB
/
dataset.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# -*- coding: utf-8 -*-
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision.transforms import v2
import random
import logging
from glob import glob
import os
from pathlib import Path
import numpy as np
import pydicom
import nibabel as nib
import torchio as tio
from skimage import morphology
INPUT_SIZE = [64, 224, 224]
PREPROCESSING_P = 0.5
def resample(nifti_img: nib.Nifti1Image) -> nib.Nifti1Image:
"""
Resamples a NIfTI image to the target orientation.
Args:
nifti_img (nib.Nifti1Image): The input NIfTI image to be resampled.
Returns:
nib.Nifti1Image: The resampled NIfTI image.
"""
orig_orientation = nib.orientations.io_orientation(nifti_img.affine)
target_orientation = nib.orientations.axcodes2ornt(('R', 'A', 'S'))
transform = nib.orientations.ornt_transform(orig_orientation, target_orientation)
return nifti_img.as_reoriented(transform)
def prepare_slices(fp: str) -> np.array:
"""
Reads a DICOM file and returns the pixel array.
Parameters:
fp (str): The file path of the DICOM file.
Returns:
np.array: The pixel array of the DICOM file.
"""
return pydicom.dcmread(fp).pixel_array
def order_slices(fp: str) -> int:
"""
Reads a DICOM file and returns the instance number.
Parameters:
fp (str): The file path of the DICOM file.
Returns:
int: The instance number of the DICOM file.
"""
return pydicom.dcmread(fp, stop_before_pixels=True).InstanceNumber
def create_affine(sorted_dicoms: list) -> np.matrix:
"""
Create an affine matrix based on the DICOM metadata.
Args:
sorted_dicoms (list): A list of sorted DICOM file paths.
Returns:
numpy.matrix: The affine matrix.
Adapted from https://dicom2nifti.readthedocs.io/en/latest/_modules/dicom2nifti/common.html#create_affine.
"""
dicom_first = pydicom.dcmread(sorted_dicoms[0])
dicom_last = pydicom.dcmread(sorted_dicoms[-1])
# Create affine matrix (http://nipy.sourceforge.net/nibabel/dicom/dicom_orientation.html#dicom-slice-affine)
image_orient1 = np.array(dicom_first.ImageOrientationPatient)[0:3]
image_orient2 = np.array(dicom_first.ImageOrientationPatient)[3:6]
delta_r = float(dicom_first.PixelSpacing[0])
delta_c = float(dicom_first.PixelSpacing[1])
image_pos = np.array(dicom_first.ImagePositionPatient)
last_image_pos = np.array(dicom_last.ImagePositionPatient)
if len(sorted_dicoms) == 1:
# Single slice
step = [0, 0, -1]
else:
step = (image_pos - last_image_pos) / (1 - len(sorted_dicoms))
affine = np.matrix([[-image_orient1[0] * delta_c, -image_orient2[0] * delta_r, -step[0], -image_pos[0]],
[-image_orient1[1] * delta_c, -image_orient2[1] * delta_r, -step[1], -image_pos[1]],
[image_orient1[2] * delta_c, image_orient2[2] * delta_r, step[2], image_pos[2]],
[0, 0, 0, 1]])
return affine
def dcm2nifti(dir_path: str, transpose: bool = False) -> nib.Nifti1Image:
"""
Convert DICOM files in a directory to a NIfTI image.
Args:
dir_path (str): The path to the directory containing the DICOM files.
transpose (bool, optional): Whether to transpose the resulting NIfTI image. Defaults to False.
Returns:
nib.Nifti1Image: The converted NIfTI image.
"""
if os.path.isdir(dir_path):
# sort files by instance number
files = sorted(glob(os.path.join(dir_path, '**', '*.dcm'), recursive=True), key=order_slices)
else:
files = [dir_path]
affine = create_affine(files)
slices = [prepare_slices(f) for f in files]
volume = np.array(slices)
volume = np.transpose(volume, (2, 1, 0))
nifti = nib.Nifti1Image(volume, affine)
nifti = resample(nifti)
if transpose:
nifti = np.transpose(nifti.get_fdata().copy(), (2, 0, 1))
return nifti
def nifti2dcm(nifti_file: nib.Nifti1Image, dcm_dir: str, out_dir: str) -> None:
"""
Convert a NIfTI file to a series of DICOM files.
Args:
nifti_file (nibabel.Nifti1Image): The NIfTI file to convert.
dcm_dir (str): The directory containing the DICOM files used for reference.
out_dir (str): The output directory to save the converted DICOM files.
Returns:
None
"""
if os.path.isdir(dcm_dir):
files = sorted(glob(os.path.join(dcm_dir, '**', '*.dcm'), recursive=True), key=order_slices)
else:
files = [dcm_dir]
target_affine = create_affine(files)
orig_orientation = nib.orientations.io_orientation(nifti_file.affine)
target_orientation = nib.orientations.io_orientation(target_affine)
transform = nib.orientations.ornt_transform(orig_orientation, target_orientation)
nifti_file = nifti_file.as_reoriented(transform)
nifti_array = nifti_file.get_fdata()
nifti_array = np.transpose(nifti_array, (2, 1, 0))
number_slices = nifti_array.shape[0]
Path(out_dir).mkdir(parents=True, exist_ok=True)
for slice_ in range(number_slices):
dcm = pydicom.dcmread(files[slice_], stop_before_pixels=False)
dcm.PixelData = (
(nifti_array[slice_, ...]).astype(np.uint16).tobytes()
)
pydicom.dcmwrite(
filename=os.path.join(out_dir, f'slice{slice_}.dcm'),
dataset=dcm,
)
class SegmentationDataset(Dataset):
"""
A PyTorch dataset for segmentation tasks.
Args:
path_list (dict): A list of paths to the image and mask files.
train (bool): A flag indicating whether the dataset is for training or not.
Attributes:
path_list (dict): A list of paths to the image and mask files.
train (bool): A flag indicating whether the dataset is for training or not.
Methods:
__len__(): Returns the length of the dataset.
__getitem__(idx): Returns the data at the given index.
Static Methods:
normalize(data): Normalizes the input tensor.
"""
def __init__(self, path_list: dict, train: bool) -> None:
super().__init__()
self.path_list = path_list
self.train = train
self.up = torch.nn.Upsample(size=(INPUT_SIZE))
self.transforms_dict = {
tio.RandomAffine(): 0.75,
tio.RandomElasticDeformation(): 0.25,
}
def __len__(self) -> int:
return len(self.path_list)
def __getitem__(self, idx: int) -> dict:
"""
Retrieves the data at the given index.
Args:
idx (int): The index of the data to retrieve.
Returns:
dict: A dictionary containing the image and mask data.
"""
self.transpose = tio.Lambda(lambda x: x.permute(0, 3, 1, 2))
image = resample(nib.load(self.path_list["image_path"][idx]))
mask = resample(nib.load(self.path_list["mask_path"][idx]))
subject = tio.Subject(
image=tio.ScalarImage(tensor=image.get_fdata()[None, ...].copy(), affine=image.affine),
mask=tio.LabelMap(tensor=mask.get_fdata()[None, ...].copy(), affine=mask.affine),
)
if self.train:
transform = tio.OneOf(self.transforms_dict)
subject = transform(subject)
if random.random() > 0.25:
bias = tio.transforms.RandomBiasField()
subject = bias(subject)
if random.random() > 0.25:
noise = tio.transforms.RandomNoise()
subject = noise(subject)
if random.random() > 0.25:
gamma = tio.transforms.RandomGamma()
subject = gamma(subject)
if random.random() > 0.25:
spike = tio.transforms.RandomSpike()
subject = spike(subject)
subject = self.transpose(subject)
data_dict = {
"image": subject["image"].data,
"mask": subject["mask"].data,
}
data_dict["image"] = v2.Lambda(lambda x: self.up(x.unsqueeze(0)).squeeze(0))(data_dict['image'])
data_dict["mask"] = v2.Lambda(lambda x: self.up(x.unsqueeze(0)).squeeze(0))(data_dict['mask'])
data_dict["image"] = v2.Lambda(lambda x: self._normalize(x))(data_dict["image"])
return data_dict
@staticmethod
def _normalize(data: torch.tensor) -> torch.tensor:
"""
Normalizes the input tensor.
Args:
data (torch.tensor): The input tensor to be normalized.
Returns:
torch.tensor: The normalized tensor.
"""
if data.max() > 0:
data = (data - data.min()) / (data.max() - data.min())
return data
class InferenceDataset(SegmentationDataset):
"""
Dataset class for inference.
Args:
path_list (dict): A dictionary containing the paths to the images.
Attributes:
up (torch.nn.Upsample): Upsampling layer.
"""
def __init__(self, path_list: dict) -> None:
super().__init__(path_list, train=False)
self.up = torch.nn.Upsample(size=(INPUT_SIZE))
def __len__(self) -> int:
return len(self.path_list["image_path"])
def __getitem__(self, idx: int) -> dict:
"""
Retrieves an item from the dataset.
Args:
idx (int): Index of the item to retrieve.
Returns:
dict: A dictionary containing the image and file name.
"""
file_path = self.path_list["image_path"][idx]
if file_path.endswith(".nii") or file_path.endswith(".nii.gz"):
nifti_img = nib.load(file_path)
pixels = resample(nifti_img).get_fdata().copy()
pixels = np.transpose(pixels, (2, 0, 1))
else:
# assert os.path.isdir(file_path), "DICOM must be volume in folder."
pixels = dcm2nifti(file_path, transpose=True)
x = torch.from_numpy(pixels).unsqueeze(dim=0)
data_dict = {
"image": x,
"file_name": file_path,
}
data_dict["image"] = v2.Lambda(lambda x: self.up(x.unsqueeze(0)).squeeze(0))(data_dict['image'])
data_dict["image"] = v2.Lambda(lambda x: self._normalize(x))(data_dict["image"])
return data_dict
def get_loaders(
train_paths: dict | None = None,
val_paths: dict | None = None,
batch_size: int = 16,
) -> tuple[DataLoader, DataLoader]:
"""
Returns the data loaders for training and validation datasets.
Args:
train_paths (dict): List of file paths for training data.
val_paths (dict): List of file paths for validation data.
batch_size (int): Number of samples per batch.
Returns:
tuple: A tuple containing the training and validation data loaders.
"""
dataloader_params = {
"shuffle": True,
"num_workers": 8,
"pin_memory": True,
}
train_dataset = SegmentationDataset(train_paths, train=True)
val_dataset = SegmentationDataset(val_paths, train=False)
train_loader = DataLoader(train_dataset, batch_size=batch_size, **dataloader_params)
val_loader = DataLoader(val_dataset, batch_size=batch_size, **dataloader_params)
return train_loader, val_loader
def get_inference_loader(
data_path: str,
batch_size: int = 16,
) -> DataLoader:
"""
Create a data loader for inference.
Args:
data_path (str): The path to the data.
batch_size (int, optional): The batch size. Defaults to 16.
Returns:
DataLoader: The data loader for inference.
"""
dataloader_params = {
"batch_size": batch_size,
"num_workers": 4,
"pin_memory": True,
"prefetch_factor": 4,
}
potential_paths = {
"image_path": glob(os.path.join(data_path, "*"), recursive=True),
}
valid_paths = {"image_path": []}
for file in potential_paths["image_path"]:
file = file.lower()
if file.endswith(".nii") or file.endswith(".nii.gz"):
valid_paths["image_path"].append(file)
elif file.endswith('.dcm'):
valid_paths["image_path"].append(file)
elif os.path.isdir(file):
valid_paths["image_path"].append(file)
else:
try:
pydicom.dcmread(file, stop_before_pixels=True)
valid_paths["image_path"].append(file)
except Exception:
logging.warning(f"Wrong file format: {file}\nAccepted file formats are .nii, .nii.gz and .dcm")
inference_dataset = InferenceDataset(valid_paths)
inference_loader = DataLoader(inference_dataset, **dataloader_params)
return inference_loader