-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.py
162 lines (137 loc) · 6.22 KB
/
data.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
#!/usr/bin/env python3
# encoding: utf-8
import glob
import os
import numpy as np
import nibabel as nib
import torch
from torch.utils.data import Dataset, DataLoader
import random
class Brats2018(Dataset):
def __init__(self, patients_dir, crop_size, modes, train=True):
self.patients_dir = patients_dir
self.modes = modes
self.train = train
self.crop_size = crop_size
def __len__(self):
return len(self.patients_dir)
def __getitem__(self, index):
patient_dir = self.patients_dir[index]
volumes = []
modes = list(self.modes) + ['seg']
for mode in modes:
patient_id = os.path.split(patient_dir)[-1]
volume_path = os.path.join(patient_dir, patient_id + "_" + mode + '.nii.gz')
volume = nib.load(volume_path).get_data()
if not mode == "seg":
volume = self.normlize(volume) # [0, 1.0]
volumes.append(volume) # [h, w, d]
seg_volume = volumes[-1]
volumes = volumes[:-1]
volume, seg_volume = self.aug_sample(volumes, seg_volume)
ed_volume = (seg_volume == 2) # peritumoral edema ED
net_volume = (seg_volume == 1) # enhancing tumor core NET
et_volume = (seg_volume == 4) # enhancing tumor ET
bg_volume = (seg_volume == 0)
seg_volume = [ed_volume, net_volume, et_volume, bg_volume]
seg_volume = np.concatenate(seg_volume, axis=0).astype("float32")
return (torch.tensor(volume.copy(), dtype=torch.float),
torch.tensor(seg_volume.copy(), dtype=torch.float))
def aug_sample(self, volumes, mask):
"""
Args:
volumes: list of array, [h, w, d]
mask: array [h, w, d], segmentation volume
Ret: x, y: [channel, h, w, d]
"""
x = np.stack(volumes, axis=0) # [N, H, W, D]
y = np.expand_dims(mask, axis=0) # [channel, h, w, d]
if self.train:
# crop volume
x, y = self.random_crop(x, y)
if random.random() < 0.5:
x = np.flip(x, axis=1)
y = np.flip(y, axis=1)
if random.random() < 0.5:
x = np.flip(x, axis=2)
y = np.flip(y, axis=2)
if random.random() < 0.5:
x = np.flip(x, axis=3)
y = np.flip(y, axis=3)
else:
x, y = self.center_crop(x, y)
return x, y
def random_crop(self, x, y):
"""
Args:
x: 4d array, [channel, h, w, d]
"""
crop_size = self.crop_size
height, width, depth = x.shape[-3:]
sx = random.randint(0, height - crop_size[0] - 1)
sy = random.randint(0, width - crop_size[1] - 1)
sz = random.randint(0, depth - crop_size[2] - 1)
crop_volume = x[:, sx:sx + crop_size[0], sy:sy + crop_size[1], sz:sz + crop_size[2]]
crop_seg = y[:, sx:sx + crop_size[0], sy:sy + crop_size[1], sz:sz + crop_size[2]]
return crop_volume, crop_seg
def center_crop(self, x, y):
crop_size = self.crop_size
height, width, depth = x.shape[-3:]
sx = (height - crop_size[0] - 1) // 2
sy = (width - crop_size[1] - 1) // 2
sz = (depth - crop_size[2] - 1) // 2
crop_volume = x[:, sx:sx + crop_size[0], sy:sy + crop_size[1], sz:sz + crop_size[2]]
crop_seg = y[:, sx:sx + crop_size[0], sy:sy + crop_size[1], sz:sz + crop_size[2]]
return crop_volume, crop_seg
def normlize(self, x):
return (x - x.min()) / (x.max() - x.min())
def split_dataset(data_root, nfold=5, seed=42, select=0):
patients_dir = glob.glob(os.path.join(data_root, "*GG", "Brats18*"))
n_patients = len(patients_dir)
print(f"total patients: {n_patients}")
pid_idx = np.arange(n_patients)
np.random.seed(seed)
np.random.shuffle(pid_idx)
n_fold_list = np.split(pid_idx, nfold)
print("***********no pro**********")
print(f"split {len(n_fold_list)} folds and every fold have {len(n_fold_list[0])} patients")
val_patients_list = []
train_patients_list = []
for i, fold in enumerate(n_fold_list):
if i == select:
for idx in fold:
val_patients_list.append(patients_dir[idx])
else:
for idx in fold:
train_patients_list.append(patients_dir[idx])
print(f"train patients: {len(train_patients_list)}, test patients: {len(val_patients_list)}")
return train_patients_list, val_patients_list
def make_data_loaders(cfg):
train_list, val_list = split_dataset(cfg.DATASET.DATA_ROOT, cfg.DATASET.NUM_FOLDS, cfg.DATASET.SELECT_FOLD)
print("train_list:", train_list)
print("val_list:", val_list)
train_ds = Brats2018(train_list, crop_size=cfg.DATASET.INPUT_SHAPE, modes=cfg.DATASET.USE_MODES, train=True)
val_ds = Brats2018(val_list, crop_size=cfg.DATASET.INPUT_SHAPE, modes=cfg.DATASET.USE_MODES, train=False)
loaders = {}
loaders['train'] = DataLoader(train_ds, batch_size=cfg.DATALOADER.BATCH_SIZE,
num_workers=cfg.DATALOADER.NUM_WORKERS,
pin_memory=True,
shuffle=True)
loaders['eval'] = DataLoader(val_ds, batch_size=cfg.DATALOADER.BATCH_SIZE,
num_workers=cfg.DATALOADER.NUM_WORKERS,
pin_memory=True,
shuffle=False)
return loaders
if __name__ == "__main__":
from config import _C as cfg
train_list, val_list = split_dataset(cfg.DATASET.DATA_ROOT, cfg.DATASET.NUM_FOLDS, cfg.DATASET.SELECT_FOLD)
train_ds = Brats2018(train_list, crop_size=cfg.DATASET.INPUT_SHAPE, modes=cfg.DATASET.USE_MODES, train=True)
val_ds = Brats2018(val_list, crop_size=cfg.DATASET.INPUT_SHAPE, modes=cfg.DATASET.USE_MODES, train=False)
for i in range(len(train_ds)):
x, y = train_ds[i]
volume = (x.numpy()[0] * 255).astype('uint8')
seg = (np.sum(y.numpy(), axis=0)).astype('uint8')
volume = nib.Nifti1Image(volume, np.eye(4))
seg = nib.Nifti1Image(seg, np.eye(4))
nib.save(volume, 'test'+str(i)+'_volume.nii.gz')
nib.save(seg, 'test' + str(i) + '_seg.nii.gz')