-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
161 lines (130 loc) · 6.67 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import glob
import torch
import numpy as np
import torchio as tio
from torch.utils.data import Dataset
from utils.to_onehot import to_onehot
RAW_DATA_FOLDER = os.getenv("HOME")
RAW_DATA_FOLDER_ISOMETRIC = os.path.join(RAW_DATA_FOLDER, "DataSets/outputs_registered_high/isometric_cliped_and_normalized")
TEMPLATE_1_PATH = os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'model_fusion/group_1.npz')
TEMPLATE_2_PATH = os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'model_fusion/group_2.npz')
TEMPLATE_3_PATH = os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'model_fusion/group_3.npz')
TEMPLATE_4_PATH = os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'model_fusion/group_4.npz')
TEMPLATE_5_PATH = os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'model_fusion/group_5.npz')
TEMPLATE_6_PATH = os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'model_fusion/group_6.npz')
TEMPLATE_7_PATH = os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'model_fusion/group_7.npz')
TEMPLATE_8_PATH = os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'model_fusion/group_8.npz')
TEMPLATE_9_PATH = os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'model_fusion/group_9.npz')
TEMPLATE_10_PATH = os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'model_fusion/group_10.npz')
TEMPLATE_11_PATH = os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'model_fusion/group_11.npz')
def buscaImagesByGoup(GROUP):
images_all = glob.glob(os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'groups','group_'+str(GROUP),'npz_rigid/*.npz'))
#print(len(images_all))
return [os.path.basename(npz_path).replace("_affine3D.npz", '').replace(".npz", '') for npz_path in images_all]
class CTDataset3DWithTemplate(Dataset):
def __init__(self, mode, labels_name=[1,2,3,4,5], bronchi=False, transforms=None):
self.bronchi = bronchi
self.mode = mode
self.labels_name = labels_name
self.dataset = sorted(glob.glob(os.path.join(RAW_DATA_FOLDER_ISOMETRIC, 'npz_rigid', mode, "*.npz")))
self.group_1 = buscaImagesByGoup(GROUP = 1)
self.group_2 = buscaImagesByGoup(GROUP = 2)
self.group_3 = buscaImagesByGoup(GROUP = 3)
self.group_4 = buscaImagesByGoup(GROUP = 4)
self.group_5 = buscaImagesByGoup(GROUP = 5)
self.group_6 = buscaImagesByGoup(GROUP = 6)
self.group_7 = buscaImagesByGoup(GROUP = 7)
self.group_8 = buscaImagesByGoup(GROUP = 8)
self.group_9 = buscaImagesByGoup(GROUP = 9)
self.group_10 = buscaImagesByGoup(GROUP = 10)
self.group_11 = buscaImagesByGoup(GROUP = 11)
print('\tFolder:', RAW_DATA_FOLDER_ISOMETRIC)
print('\tTamanho do dataset ({}): {}'.format(mode, len(self.dataset)))
print('\tMode:', self.mode)
def __len__(self):
return len(self.dataset)
def __getitem__(self, i):
npz_path = self.dataset[i]
npz = np.load(npz_path)
img, tgt = npz["image"][:].astype(np.float32), npz["label"][:].astype(np.float32)
ID = os.path.basename(npz_path).replace('.npz','').replace('_affine3D','').replace('_rigid3D','')
img = img.transpose(2,1,0)
tgt = tgt.transpose(2,1,0)
if len(tgt.shape)==3:
if tgt.max()==8 or tgt.max()==520:
mask_one, onehot, labels_one = to_onehot(tgt, [7,8,4,5,6], single_foregound_lable=False, onehot_type=np.dtype(np.int8))
elif tgt.max()==5:
mask_one, onehot, labels_one = to_onehot(tgt, [1,2,3,4,5], single_foregound_lable=False, onehot_type=np.dtype(np.int8))
tgt = onehot
img = np.expand_dims(img, 0)
my_ID = []
my_ID.append(ID)
if my_ID[0] in self.group_1:
template = np.load(TEMPLATE_1_PATH)["model"][:].astype(np.float32)
elif my_ID[0] in self.group_2:
template = np.load(TEMPLATE_2_PATH)["model"][:].astype(np.float32)
elif my_ID[0] in self.group_3:
template = np.load(TEMPLATE_3_PATH)["model"][:].astype(np.float32)
elif my_ID[0] in self.group_4:
template = np.load(TEMPLATE_4_PATH)["model"][:].astype(np.float32)
elif my_ID[0] in self.group_5:
template = np.load(TEMPLATE_5_PATH)["model"][:].astype(np.float32)
elif my_ID[0] in self.group_6:
template = np.load(TEMPLATE_6_PATH)["model"][:].astype(np.float32)
elif my_ID[0] in self.group_7:
template = np.load(TEMPLATE_7_PATH)["model"][:].astype(np.float32)
elif my_ID[0] in self.group_8:
template = np.load(TEMPLATE_8_PATH)["model"][:].astype(np.float32)
elif my_ID[0] in self.group_9:
template = np.load(TEMPLATE_9_PATH)["model"][:].astype(np.float32)
elif my_ID[0] in self.group_10:
template = np.load(TEMPLATE_10_PATH)["model"][:].astype(np.float32)
elif my_ID[0] in self.group_11:
template = np.load(TEMPLATE_11_PATH)["model"][:].astype(np.float32)
else:
print('Template model não expecificado.')
##########################################################################
subject = tio.Subject(
image=tio.ScalarImage(tensor = img),
label=tio.LabelMap(tensor = tgt),
template=tio.LabelMap(tensor = template),
)
transform = tio.Resize((128, 128, 128))
transformed = transform(subject)
img_high = transformed.image.numpy()
tgt_high = transformed.label.numpy()
template_high = transformed.template.numpy()
#assert template_high.shape == tgt_high.shape, 'Template and label should be same shape, instead are {}, {}'.format(template.shape, tgt.shape)
#assert len(template.shape) == 4, 'Inputs should be B*W*H*N tensors, instead have shape {}'.format(img.shape)
#showImages(img_high, tgt_high)
##########################################################################
if len(tgt.shape)==3:
if tgt.max()==8 or tgt.max()==520:
mask_one, onehot, labels_one = to_onehot(tgt, [7,8,4,5,6], single_foregound_lable=False, onehot_type=np.dtype(np.int8))
elif tgt.max()==5:
mask_one, onehot, labels_one = to_onehot(tgt, [1,2,3,4,5], single_foregound_lable=False, onehot_type=np.dtype(np.int8))
tgt = onehot
img = np.expand_dims(img, 0)
assert tgt.shape==template.shape, f'Label and template with different shapes: {tgt.shape} and {template.shape}'
#showImages(img, tgt)
##########################################################################
segmentation = np.array(tgt.squeeze().argmax(axis=0)).astype(np.uint8)
if segmentation.max()>8:
segmentation[segmentation>8]=0
segmentation[segmentation>0]=1
new_image = np.zeros(img[0].shape).astype(img.dtype)
new_image = np.where(segmentation == 1, img, img.min())
img = new_image
#showImages(img, tgt)
#showImages(img, template)
##########################################################################
img_high = torch.from_numpy(img_high).float()
tgt_high = torch.from_numpy(tgt_high).float()
template_high = torch.from_numpy(template_high).float()
img = torch.from_numpy(img).float()
tgt = torch.from_numpy(tgt).float()
template = torch.from_numpy(template).float()
return {"image_h": img_high, "label_h": tgt_high, 'template_high': template_high, "image": img, "label": tgt, "template": template, "npz_path": npz_path, "ID":ID}