-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_function.py
More file actions
259 lines (197 loc) · 8.79 KB
/
data_function.py
File metadata and controls
259 lines (197 loc) · 8.79 KB
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
from glob import glob
from os.path import dirname, join, basename, isfile
import sys
sys.path.append('./')
import csv
import torch
from medpy.io import load
import numpy as np
from PIL import Image
from torch import nn
import torch.nn.functional as F
import random
import torchio as tio
from torchio import AFFINE, DATA
import torchio
from torchio import ScalarImage, LabelMap, Subject, SubjectsDataset, Queue
from torchio.data import UniformSampler
from torchio.data import WeightedSampler
from torchio.data import LabelSampler
from torchio.transforms import (
RandomFlip,
RandomAffine,
RandomElasticDeformation,
RandomNoise,
RandomMotion,
RandomBiasField,
RescaleIntensity,
Resample,
ToCanonical,
ZNormalization,
CropOrPad,
HistogramStandardization,
OneOf,
Compose,
)
from pathlib import Path
from hparam import hparams as hp
class MedData_train(torch.utils.data.Dataset):
def __init__(self, images_dir, labels_dir):
if hp.mode == '3d':
patch_size = hp.patch_size
elif hp.mode == '2d':
patch_size = (hp.patch_size,hp.patch_size,1)
else:
raise Exception('no such kind of mode!')
queue_length = 32
samples_per_volume = 16
self.subjects = []
if (hp.in_class == 1) and (hp.out_class == 1) :
images_dir = Path(images_dir)
self.image_paths = sorted(images_dir.glob(hp.fold_arch))
labels_dir = Path(labels_dir)
self.label_paths = sorted(labels_dir.glob(hp.fold_arch)) #进行遍历排序
for (image_path, label_path) in zip(self.image_paths, self.label_paths):
#原版
#suject :一份带原片和标签
# subject = tio.Subject(
# source=tio.ScalarImage(image_path),
# label=tio.LabelMap(label_path),
# )
# #subjects:一个集合
# self.subjects.append(subject)
#第一版修改,失败
# ct = tio.ScalarImage(image_path)
# rescale = tio.RescaleIntensity(
# out_min_max=(-512, 512))
# ct_normalized = rescale(ct)
# #第二版修改
# ct = tio.ScalarImage(image_path)
# ct.data = torch.clamp(ct.data,min =-512,max=512)
#第三版修改,对数据集进行一个裁剪,使它能够很好的缩小图像
# subject = tio.Subject(
# source=tio.ScalarImage(image_path),
# label=tio.LabelMap(label_path),
# )
# my_transform = tio.CropOrPad(
# (256, 256, 256),
# mask_name='label',
# )
# transformed = my_transform(subject)
# self.subjects.append(transformed)
#第四版
subject = tio.Subject(
source=tio.ScalarImage(image_path),
label=tio.Image(label_path,type=tio.SAMPLING_MAP),
)
#subjects:一个集合
self.subjects.append(subject)
else:#各大分割的label
images_dir = Path(images_dir)
self.image_paths = sorted(images_dir.glob(hp.fold_arch))
artery_labels_dir = Path(labels_dir+'/artery')
self.artery_label_paths = sorted(artery_labels_dir.glob(hp.fold_arch))
lung_labels_dir = Path(labels_dir+'/lung')
self.lung_label_paths = sorted(lung_labels_dir.glob(hp.fold_arch))
trachea_labels_dir = Path(labels_dir+'/trachea')
self.trachea_label_paths = sorted(trachea_labels_dir.glob(hp.fold_arch))
vein_labels_dir = Path(labels_dir+'/vein')
self.vein_label_paths = sorted(vein_labels_dir.glob(hp.fold_arch))
for (image_path, artery_label_path,lung_label_path,trachea_label_path,vein_label_path) in zip(self.image_paths, self.artery_label_paths,self.lung_label_paths,self.trachea_label_paths,self.vein_label_paths):
subject = tio.Subject(
source=tio.ScalarImage(image_path),
atery=tio.LabelMap(artery_label_path),
lung=tio.LabelMap(lung_label_path),
trachea=tio.LabelMap(trachea_label_path),
vein=tio.LabelMap(vein_label_path),
)
self.subjects.append(subject)
self.transforms = self.transform()
self.training_set = tio.SubjectsDataset(self.subjects, transform=self.transforms) #一个集合
#修改了采样
self.queue_dataset = Queue(
self.training_set,
queue_length,
samples_per_volume,
#UniformSampler(patch_size), #Randomly extract patches from a volume with uniform probability.
#WeightedSampler(patch_size, 'label'),
LabelSampler(patch_size,label_name='label',label_probabilities={0: 0.2, 255: 0.8}),
num_workers = 0,
)
def transform(self):
if hp.mode == '3d':
training_transform = Compose([
# ToCanonical(),
#去掉crop,因为自定义了crop对数据进行了预处理
CropOrPad((hp.crop_or_pad_size, hp.crop_or_pad_size, hp.crop_or_pad_size), mask_name='label',padding_mode='reflect',),
RandomMotion(),
RandomBiasField(),
ZNormalization(),
RandomNoise(),
RandomFlip(axes=(0,)),
Resample((1,1,1)), #重采样
OneOf({
RandomAffine(): 0.8,
RandomElasticDeformation(): 0.2,
}),])
elif hp.mode == '2d':
training_transform = Compose([
CropOrPad((hp.crop_or_pad_size, hp.crop_or_pad_size,1), padding_mode='reflect'),
# RandomMotion(),
RandomBiasField(),
ZNormalization(),
RandomNoise(),
RandomFlip(axes=(0,)),
#Resample((1,1,1)),
# OneOf({
# RandomAffine(): 0.8,
# RandomElasticDeformation(): 0.2,
# }),
])
else:
raise Exception('no such kind of mode!')
return training_transform
class MedData_test(torch.utils.data.Dataset):
def __init__(self, images_dir, labels_dir):
self.subjects = []
if (hp.in_class == 1) and (hp.out_class == 1) :
images_dir = Path(images_dir)
self.image_paths = sorted(images_dir.glob(hp.fold_arch))
# labels_dir = Path(labels_dir)
# self.label_paths = sorted(labels_dir.glob(hp.fold_arch))
#for (image_path, label_path) in zip(self.image_paths, self.label_paths):
for image_path in zip(self.image_paths):
# ct = tio.ScalarImage(image_path)
# ct.data = torch.clamp(ct.data,min =-512,max=512)
subject = tio.Subject(
source=tio.ScalarImage(image_path),
#label=tio.LabelMap(label_path),
)
self.subjects.append(subject)
else:
images_dir = Path(images_dir)
self.image_paths = sorted(images_dir.glob(hp.fold_arch))
artery_labels_dir = Path(labels_dir+'/artery')
self.artery_label_paths = sorted(artery_labels_dir.glob(hp.fold_arch))
lung_labels_dir = Path(labels_dir+'/lung')
self.lung_label_paths = sorted(lung_labels_dir.glob(hp.fold_arch))
trachea_labels_dir = Path(labels_dir+'/trachea')
self.trachea_label_paths = sorted(trachea_labels_dir.glob(hp.fold_arch))
vein_labels_dir = Path(labels_dir+'/vein')
self.vein_label_paths = sorted(vein_labels_dir.glob(hp.fold_arch))
for (image_path, artery_label_path,lung_label_path,trachea_label_path,vein_label_path) in zip(self.image_paths, self.artery_label_paths,self.lung_label_paths,self.trachea_label_paths,self.vein_label_paths):
subject = tio.Subject(
source=tio.ScalarImage(image_path),
atery=tio.LabelMap(artery_label_path),
lung=tio.LabelMap(lung_label_path),
trachea=tio.LabelMap(trachea_label_path),
vein=tio.LabelMap(vein_label_path),
)
self.subjects.append(subject)
# self.transforms = self.transform()
self.training_set = tio.SubjectsDataset(self.subjects, transform=None)
# def transform(self):
# training_transform = Compose([
# ZNormalization(),
# ])
# return training_transform