-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_qsm.py
188 lines (159 loc) · 7.64 KB
/
load_qsm.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
from genericpath import exists
import os
from imageio import save
import torch
import numpy as np
import pandas as pd
from scipy.ndimage import binary_fill_holes
from torch.utils.data import Dataset
import nibabel as nib
import random
import torch.nn.functional as Func
from monai import transforms
def nii_loader(path):
img = nib.load(str(path))
data = img.get_data()
return data
def read_table(path):
return(pd.read_excel(path).values)
def crop(image, mask):
nonzero_mask = image != 0
nonzero_mask = binary_fill_holes(nonzero_mask)
mask_voxel_coords = np.where(nonzero_mask != 0)
minzidx = int(np.min(mask_voxel_coords[0]))
maxzidx = int(np.max(mask_voxel_coords[0])) + 1
minxidx = int(np.min(mask_voxel_coords[1]))
maxxidx = int(np.max(mask_voxel_coords[1])) + 1
minyidx = int(np.min(mask_voxel_coords[2]))
maxyidx = int(np.max(mask_voxel_coords[2])) + 1
bbox = [[minzidx, maxzidx], [minxidx, maxxidx], [minyidx, maxyidx]]
resizer = (slice(bbox[0][0], bbox[0][1]), slice(bbox[1][0], bbox[1][1]), slice(bbox[2][0], bbox[2][1]))
cropped_image = image[resizer]
if mask is not None:
cropped_mask = mask[resizer]
else:
cropped_mask = None
return cropped_image, cropped_mask, np.array(bbox)
def dice_coeff(y_pred, y_true, num_classes):
y_pred = Func.softmax(y_pred, dim = 1)
eps = 1.
dice = 0
for c in range(num_classes):
if c == 0:
continue
jaccard_target = (y_true == c).float()
jaccard_output = y_pred[:, c]
intersection = (jaccard_output * jaccard_target).sum()
union = jaccard_output.sum() + jaccard_target.sum()
dice += (((2 * intersection + eps) / (union + eps)) / (num_classes - 1))
return dice
size = (80, 96, 80)
train_aug = transforms.Compose(
[
transforms.AddChanneld(keys=["image", "label"]),
# transforms.Resized(keys=["image", "label"], spatial_size=size, mode=['trilinear', 'nearest'], align_corners=[True, None]),
transforms.RandFlipd(keys=["image", "label"], prob=0.2, spatial_axis=0),
transforms.RandFlipd(keys=["image", "label"], prob=0.2, spatial_axis=1),
transforms.RandFlipd(keys=["image", "label"], prob=0.2, spatial_axis=2),
transforms.RandRotate90d(keys=["image", "label"], prob=0.2, max_k=3, spatial_axes=(0,1)),
transforms.RandRotated(keys=["image", "label"], range_x=0.5, range_y=0.5, range_z=0.5, prob=0.1, keep_size=True,
mode='bilinear', padding_mode='border', align_corners=False),
# transforms.RandCropByLabelClasses(keys=["image", "label"], label_key="label", spatial_size=[3, 3],
# ratios=[1, 2, 2, 2, 2, 2, 2, 2, 2], num_classes=9, num_samples=1)
# transforms.RandSpatialCropd(keys=["image", "label"], roi_size=min_size, max_roi_size=None, random_center=True, random_size=True, allow_missing_keys=False),
transforms.RandAffined(keys=["image", "label"], spatial_size=None, prob=0.1, rotate_range=None, shear_range=(0.5, 0.5),
translate_range=None, scale_range=None, mode='bilinear', padding_mode='reflection', allow_missing_keys=False),
# transforms.RandScaleIntensityd(keys="image", factors=0.1, prob=0.2),
# transforms.RandGaussianSmoothd(keys="image", sigma_x=(0.25, 1.5), sigma_y=(0.25, 1.5), sigma_z=(0.25, 1.5), prob=0.1, approx='erf'),
# transforms.RandZoomd(keys=["image", "label"], prob=0.1, min_zoom=0.8, max_zoom=1.2, mode=['trilinear', 'nearest'], align_corners=[True, None], padding_mode="edge"),
# transforms.RandAdjustContrastd(keys="image", prob=0.1, gamma=(0.5, 4.5), allow_missing_keys=False),
# transforms.RandShiftIntensityd(keys="image", offsets=0.1, prob=0.2),
# transforms.Rand3DElasticd(keys=["image", "label"], sigma_range=(5, 7), magnitude_range=(50, 150), prob=0.1),
]
)
test_aug = transforms.Compose(
[
transforms.AddChanneld(keys=["image", "label"])
]
)
class TrainDataset(Dataset):
def __init__(self, excel_path, img_root, mask_root, file_list, num_class, loader=nii_loader, table_reader=read_table, transform=train_aug):
self.img_root = img_root
self.mask_root = mask_root
self.file_list = file_list
self.loader = loader
self.table = table_reader(excel_path)
self.transform = transform
self.num_class = num_class
def __getitem__(self, index):
file_name = self.file_list[index]
age = None
for f in self.table:
sid = str(f[0])
if sid not in file_name:
continue
age = int(f[2])
gender = int(f[1])
if age is None:
print("age is none ", file_name)
data_list = os.listdir(os.path.join(self.img_root, file_name))
for f in data_list:
if f.endswith(".nii") or f.endswith(".nii.gz"):
img_path = os.path.join(self.img_root, file_name, f)
img = self.loader(img_path)
# img_path = os.path.join(self.img_root, file_name)
# img = self.loader(img_path)
# mask_list = os.listdir(os.path.join(self.mask_root, file_name))
# for f in mask_list:
# mask_path = os.path.join(self.mask_root, file_name + '.nii.gz')
mask_path = os.path.join(self.mask_root, file_name)
mask = self.loader(mask_path)
# mask = None
img, mask, bbox = crop(img, mask)
if mask is None:
img = self.transform(img)
else:
data = {'image': img, 'label': mask}
aug_data = self.transform(data)
img, mask = aug_data['image'], aug_data['label']
img = np.expand_dims(img, axis=(0))
img = np.ascontiguousarray(img, dtype= np.float32)
img = torch.from_numpy(img).type(torch.FloatTensor)
img = Func.interpolate(img, size, mode = 'trilinear', align_corners = True)
img = img[0]
if mask is not None:
mask = np.expand_dims(mask, axis=(0))
mask = np.ascontiguousarray(mask, dtype= np.float32)
mask = torch.from_numpy(mask).type(torch.FloatTensor)
mask = Func.interpolate(mask, size, mode = 'nearest')
mask = mask[0, 0]
if mask is None:
return img, file_name, age, gender, bbox
else:
return img, mask, file_name, age, gender, bbox
def __len__(self):
return len(self.file_list)
def SaveResult(mask_root, save_path, img, name, bbox):
print(name, img.shape)
img_out = np.zeros(img[0].shape)
img_out = np.argmax(img, axis = 0)
img = img_out.astype(np.uint8)
# mask_list = os.listdir(os.path.join(mask_root, name))
# for f in mask_list:
# mask_path = os.path.join(mask_root, name)# + .nii.gz")
mask_path = os.path.join(mask_root, name)
mask = nib.load(mask_path)
affine = mask.affine
header = mask.header
real_size = (bbox[0][1] - bbox[0][0], bbox[1][1] - bbox[1][0], bbox[2][1] - bbox[2][0])
out = np.zeros(mask.shape)
img = np.expand_dims(img, axis=(0,1))
img = np.ascontiguousarray(img, dtype= np.float32)
img = torch.from_numpy(img).type(torch.FloatTensor)
img = Func.interpolate(img, real_size, mode = 'nearest')
img = img[0, 0].data.numpy().astype(np.uint8)
resizer = (slice(bbox[0][0], bbox[0][1]), slice(bbox[1][0], bbox[1][1]), slice(bbox[2][0], bbox[2][1]))
out[resizer] = img
np.round(out)
new_img = nib.Nifti1Image(out.astype(np.uint8), affine = affine, header = header)
nib.save(new_img, os.path.join(save_path, name+'.nii.gz'))