-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataset.py
379 lines (348 loc) · 16.2 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
import torch.utils.data as data
import PIL.Image as Image
from sklearn.model_selection import train_test_split
import os
import random
import numpy as np
from skimage.io import imread
import cv2
from glob import glob
import imageio
class LiverDataset(data.Dataset):
def __init__(self, state, transform=None, target_transform=None):
self.state = state
self.train_root = r"E:\codes\new\u_net_liver-master\data\liver\train"
self.val_root = r"E:\codes\new\u_net_liver-master\data\liver\val"
self.test_root = self.val_root
self.pics,self.masks = self.getDataPath()
self.transform = transform
self.target_transform = target_transform
def getDataPath(self):
assert self.state =='train' or self.state == 'val' or self.state =='test'
if self.state == 'train':
root = self.train_root
if self.state == 'val':
root = self.val_root
if self.state == 'test':
root = self.test_root
pics = []
masks = []
n = len(os.listdir(root)) // 2 # 因为数据集中一套训练数据包含有训练图和mask图,所以要除2
for i in range(n):
img = os.path.join(root, "%03d.png" % i) # liver is %03d
mask = os.path.join(root, "%03d_mask.png" % i)
pics.append(img)
masks.append(mask)
#imgs.append((img, mask))
return pics,masks
def __getitem__(self, index):
#x_path, y_path = self.imgs[index]
x_path = self.pics[index]
y_path = self.masks[index]
origin_x = Image.open(x_path)
origin_y = Image.open(y_path)
# origin_x = cv2.imread(x_path)
# origin_y = cv2.imread(y_path,cv2.COLOR_BGR2GRAY)
if self.transform is not None:
img_x = self.transform(origin_x)
if self.target_transform is not None:
img_y = self.target_transform(origin_y)
return img_x, img_y,x_path,y_path
def __len__(self):
return len(self.pics)
class esophagusDataset(data.Dataset):
def __init__(self, state, transform=None, target_transform=None):
self.state = state
self.train_root = r"E:\datasets\data_sta_all\train_data"
self.val_root = r"E:\datasets\data_sta_all\test_data"
self.test_root = self.val_root
self.pics,self.masks = self.getDataPath()
self.transform = transform
self.target_transform = target_transform
def getDataPath(self):
assert self.state =='train' or self.state == 'val' or self.state == 'test'
if self.state == 'train':
root = self.train_root
if self.state == 'val':
root = self.val_root
if self.state == 'test':
root = self.test_root
pics = []
masks = []
n = len(os.listdir(root)) // 2 # 因为数据集中一套训练数据包含有训练图和mask图,所以要除2
for i in range(n):
img = os.path.join(root, "%05d.png" % i) # liver is %03d
mask = os.path.join(root, "%05d_mask.png" % i)
pics.append(img)
masks.append(mask)
#imgs.append((img, mask))
return pics,masks
def __getitem__(self, index):
#x_path, y_path = self.imgs[index]
x_path = self.pics[index]
y_path = self.masks[index]
# origin_x = Image.open(x_path)
# origin_y = Image.open(y_path)
origin_x = cv2.imread(x_path)
origin_y = cv2.imread(y_path,cv2.COLOR_BGR2GRAY)
if self.transform is not None:
img_x = self.transform(origin_x)
if self.target_transform is not None:
img_y = self.target_transform(origin_y)
return img_x, img_y,x_path,y_path
def __len__(self):
return len(self.pics)
class dsb2018CellDataset(data.Dataset):
def __init__(self, state, transform=None, target_transform=None):
self.state = state
self.aug = True
self.root = r'E:\codes\pytorch-nested-unet-master\pytorch-nested-unet-master\input\dsb2018_256'
self.img_paths = None
self.mask_paths = None
self.train_img_paths, self.val_img_paths = None,None
self.train_mask_paths, self.val_mask_paths = None,None
self.pics,self.masks = self.getDataPath()
self.transform = transform
self.target_transform = target_transform
def getDataPath(self):
self.img_paths = glob(self.root + '\images\*')
self.mask_paths = glob(self.root + '\masks\*')
self.train_img_paths, self.val_img_paths, self.train_mask_paths, self.val_mask_paths = \
train_test_split(self.img_paths, self.mask_paths, test_size=0.2, random_state=41)
assert self.state == 'train' or self.state == 'val' or self.state == 'test'
if self.state == 'train':
return self.train_img_paths,self.train_mask_paths
if self.state == 'val':
return self.val_img_paths,self.val_mask_paths
if self.state == 'test':
return self.val_img_paths,self.val_mask_paths #因数据集没有测试集,所以用验证集代替
def __getitem__(self, index):
pic_path = self.pics[index]
mask_path = self.masks[index]
# origin_x = Image.open(x_path)
# origin_y = Image.open(y_path)
pic = cv2.imread(pic_path)
mask = cv2.imread(mask_path,cv2.COLOR_BGR2GRAY)
pic = pic.astype('float32') / 255
mask = mask.astype('float32') / 255
# if self.aug:
# if random.uniform(0, 1) > 0.5:
# pic = pic[:, ::-1, :].copy()
# mask = mask[:, ::-1].copy()
# if random.uniform(0, 1) > 0.5:
# pic = pic[::-1, :, :].copy()
# mask = mask[::-1, :].copy()
if self.transform is not None:
img_x = self.transform(pic)
if self.target_transform is not None:
img_y = self.target_transform(mask)
return img_x, img_y,pic_path,mask_path
def __len__(self):
return len(self.pics)
class CornealDataset(data.Dataset):
def __init__(self, state, transform=None, target_transform=None):
self.state = state
self.aug = True
self.root = r'E:\datasets\CORN\CORN\Corneal nerve curivilinear segmentation\Corneal nerve curivilinear segmentation'
self.img_paths = None
self.mask_paths = None
self.train_img_paths, self.val_img_paths,self.test_img_paths = None,None,None
self.train_mask_paths, self.val_mask_paths,self.test_mask_paths = None,None,None
self.pics,self.masks = self.getDataPath()
self.transform = transform
self.target_transform = target_transform
def getDataPath(self):
self.train_img_paths = glob(self.root + r'\training\train_images\*')
self.train_mask_paths = glob(self.root + r'\training\train_mask\*')
self.val_img_paths = glob(self.root + r'\val\val_images\*')
self.val_mask_paths = glob(self.root + r'\val\val_mask\*')
self.test_img_paths = glob(self.root + r'\test\test_images\*')
self.test_mask_paths = glob(self.root + r'\test\test_mask\*')
# self.train_img_paths, self.val_img_paths, self.train_mask_paths, self.val_mask_paths = \
# train_test_split(self.img_paths, self.mask_paths, test_size=0.2, random_state=41)
assert self.state == 'train' or self.state == 'val' or self.state == 'test'
if self.state == 'train':
return self.train_img_paths,self.train_mask_paths
if self.state == 'val':
return self.val_img_paths,self.val_mask_paths
if self.state == 'test':
return self.test_img_paths,self.test_mask_paths
def __getitem__(self, index):
pic_path = self.pics[index]
mask_path = self.masks[index]
# origin_x = Image.open(x_path)
# origin_y = Image.open(y_path)
pic = cv2.imread(pic_path)
mask = cv2.imread(mask_path,cv2.COLOR_BGR2GRAY)
pic = pic.astype('float32') / 255
mask = mask.astype('float32') / 255
# if self.aug:
# if random.uniform(0, 1) > 0.5:
# pic = pic[:, ::-1, :].copy()
# mask = mask[:, ::-1].copy()
# if random.uniform(0, 1) > 0.5:
# pic = pic[::-1, :, :].copy()
# mask = mask[::-1, :].copy()
if self.transform is not None:
img_x = self.transform(pic)
if self.target_transform is not None:
img_y = self.target_transform(mask)
return img_x, img_y,pic_path,mask_path
def __len__(self):
return len(self.pics)
class DriveEyeDataset(data.Dataset):
def __init__(self, state, transform=None, target_transform=None):
self.state = state
self.aug = True
self.root = r'E:\datasets\DRIVE\DRIVE'
self.pics, self.masks = self.getDataPath()
self.img_paths = None
self.mask_paths = None
self.train_img_paths, self.val_img_paths,self.test_img_paths = None,None,None
self.train_mask_paths, self.val_mask_paths,self.test_mask_paths = None,None,None
self.transform = transform
self.target_transform = target_transform
def getDataPath(self):
self.train_img_paths = glob(self.root + r'\training\images\*')
self.train_mask_paths = glob(self.root + r'\training\1st_manual\*')
self.val_img_paths = glob(self.root + r'\test\images\*')
self.val_mask_paths = glob(self.root + r'\test\1st_manual\*')
self.test_img_paths = self.val_img_paths
self.test_mask_paths = self.val_mask_paths
assert self.state == 'train' or self.state == 'val' or self.state == 'test'
if self.state == 'train':
return self.train_img_paths, self.train_mask_paths
if self.state == 'val':
return self.val_img_paths, self.val_mask_paths
if self.state == 'test':
return self.test_img_paths, self.test_mask_paths
def __getitem__(self, index):
imgx,imgy=(576,576)
pic_path = self.pics[index]
mask_path = self.masks[index]
# origin_x = Image.open(x_path)
# origin_y = Image.open(y_path)
#print(pic_path)
pic = cv2.imread(pic_path)
mask = cv2.imread(mask_path,cv2.COLOR_BGR2GRAY)
if mask == None:
mask = imageio.mimread(mask_path)
mask = np.array(mask)[0]
pic = cv2.resize(pic,(imgx,imgy))
mask = cv2.resize(mask, (imgx, imgy))
pic = pic.astype('float32') / 255
mask = mask.astype('float32') / 255
# if self.aug:
# if random.uniform(0, 1) > 0.5:
# pic = pic[:, ::-1, :].copy()
# mask = mask[:, ::-1].copy()
# if random.uniform(0, 1) > 0.5:
# pic = pic[::-1, :, :].copy()
# mask = mask[::-1, :].copy()
if self.transform is not None:
img_x = self.transform(pic)
if self.target_transform is not None:
img_y = self.target_transform(mask)
return img_x, img_y,pic_path,mask_path
def __len__(self):
return len(self.pics)
class IsbiCellDataset(data.Dataset):
def __init__(self, state, transform=None, target_transform=None):
self.state = state
self.aug = True
self.root = r'E:\datasets\isbi'
self.img_paths = None
self.mask_paths = None
self.train_img_paths, self.val_img_paths,self.test_img_paths = None,None,None
self.train_mask_paths, self.val_mask_paths,self.test_mask_paths = None,None,None
self.pics,self.masks = self.getDataPath()
self.transform = transform
self.target_transform = target_transform
def getDataPath(self):
self.img_paths = glob(self.root + r'\train\images\*')
self.mask_paths = glob(self.root + r'\train\label\*')
# self.val_img_paths = glob(self.root + r'\val\val_images\*')
# self.val_mask_paths = glob(self.root + r'\val\val_mask\*')
# self.test_img_paths = glob(self.root + r'\test\test_images\*')
# self.test_mask_paths = glob(self.root + r'\test\test_mask\*')
self.train_img_paths, self.val_img_paths, self.train_mask_paths, self.val_mask_paths = \
train_test_split(self.img_paths, self.mask_paths, test_size=0.2, random_state=41)
self.test_img_paths, self.test_mask_paths = self.val_img_paths,self.val_mask_paths
assert self.state == 'train' or self.state == 'val' or self.state == 'test'
if self.state == 'train':
return self.train_img_paths,self.train_mask_paths
if self.state == 'val':
return self.val_img_paths,self.val_mask_paths
if self.state == 'test':
return self.test_img_paths,self.test_mask_paths
def __getitem__(self, index):
pic_path = self.pics[index]
mask_path = self.masks[index]
# origin_x = Image.open(x_path)
# origin_y = Image.open(y_path)
pic = cv2.imread(pic_path)
mask = cv2.imread(mask_path,cv2.COLOR_BGR2GRAY)
pic = pic.astype('float32') / 255
mask = mask.astype('float32') / 255
# if self.aug:
# if random.uniform(0, 1) > 0.5:
# pic = pic[:, ::-1, :].copy()
# mask = mask[:, ::-1].copy()
# if random.uniform(0, 1) > 0.5:
# pic = pic[::-1, :, :].copy()
# mask = mask[::-1, :].copy()
if self.transform is not None:
img_x = self.transform(pic)
if self.target_transform is not None:
img_y = self.target_transform(mask)
return img_x, img_y,pic_path,mask_path
def __len__(self):
return len(self.pics)
class LungKaggleDataset(data.Dataset):
def __init__(self, state, transform=None, target_transform=None):
self.state = state
self.aug = True
self.root = r'E:\Datasets\finding-lungs-in-ct-data'
self.img_paths = None
self.mask_paths = None
self.train_img_paths, self.val_img_paths,self.test_img_paths = None,None,None
self.train_mask_paths, self.val_mask_paths,self.test_mask_paths = None,None,None
self.pics,self.masks = self.getDataPath()
self.transform = transform
self.target_transform = target_transform
def getDataPath(self):
self.img_paths = glob(self.root + r'\2d_images\*')
self.mask_paths = glob(self.root + r'\2d_masks\*')
self.train_img_paths, self.val_img_paths, self.train_mask_paths, self.val_mask_paths = \
train_test_split(self.img_paths, self.mask_paths, test_size=0.2, random_state=41)
self.test_img_paths, self.test_mask_paths = self.val_img_paths, self.val_mask_paths
assert self.state == 'train' or self.state == 'val' or self.state == 'test'
if self.state == 'train':
return self.train_img_paths, self.train_mask_paths
if self.state == 'val':
return self.val_img_paths, self.val_mask_paths
if self.state == 'test':
return self.test_img_paths, self.test_mask_paths
def __getitem__(self, index):
pic_path = self.pics[index]
mask_path = self.masks[index]
# origin_x = Image.open(x_path)
# origin_y = Image.open(y_path)
pic = cv2.imread(pic_path)
mask = cv2.imread(mask_path,cv2.COLOR_BGR2GRAY)
pic = pic.astype('float32') / 255
mask = mask.astype('float32') / 255
# if self.aug:
# if random.uniform(0, 1) > 0.5:
# pic = pic[:, ::-1, :].copy()
# mask = mask[:, ::-1].copy()
# if random.uniform(0, 1) > 0.5:
# pic = pic[::-1, :, :].copy()
# mask = mask[::-1, :].copy()
if self.transform is not None:
img_x = self.transform(pic)
if self.target_transform is not None:
img_y = self.target_transform(mask)
return img_x, img_y,pic_path,mask_path
def __len__(self):
return len(self.pics)