-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
323 lines (243 loc) · 10.9 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
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
"""
Based on https://github.com/asanakoy/kaggle_carvana_segmentation
"""
import torch
import torch.utils.data as data
from torch.autograd import Variable as V
from PIL import Image
import cv2
import numpy as np
import os
import scipy.misc as misc
def randomHueSaturationValue(image, hue_shift_limit=(-180, 180),
sat_shift_limit=(-255, 255),
val_shift_limit=(-255, 255), u=0.5):
if np.random.random() < u:
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(image)
hue_shift = np.random.randint(hue_shift_limit[0], hue_shift_limit[1]+1)
hue_shift = np.uint8(hue_shift)
h += hue_shift
sat_shift = np.random.uniform(sat_shift_limit[0], sat_shift_limit[1])
s = cv2.add(s, sat_shift)
val_shift = np.random.uniform(val_shift_limit[0], val_shift_limit[1])
v = cv2.add(v, val_shift)
image = cv2.merge((h, s, v))
#image = cv2.merge((s, v))
image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
return image
def randomShiftScaleRotate(image, mask,
shift_limit=(-0.0, 0.0),
scale_limit=(-0.0, 0.0),
rotate_limit=(-0.0, 0.0),
aspect_limit=(-0.0, 0.0),
borderMode=cv2.BORDER_CONSTANT, u=0.5):
if np.random.random() < u:
height, width, channel = image.shape
angle = np.random.uniform(rotate_limit[0], rotate_limit[1])
scale = np.random.uniform(1 + scale_limit[0], 1 + scale_limit[1])
aspect = np.random.uniform(1 + aspect_limit[0], 1 + aspect_limit[1])
sx = scale * aspect / (aspect ** 0.5)
sy = scale / (aspect ** 0.5)
dx = round(np.random.uniform(shift_limit[0], shift_limit[1]) * width)
dy = round(np.random.uniform(shift_limit[0], shift_limit[1]) * height)
cc = np.math.cos(angle / 180 * np.math.pi) * sx
ss = np.math.sin(angle / 180 * np.math.pi) * sy
rotate_matrix = np.array([[cc, -ss], [ss, cc]])
box0 = np.array([[0, 0], [width, 0], [width, height], [0, height], ])
box1 = box0 - np.array([width / 2, height / 2])
box1 = np.dot(box1, rotate_matrix.T) + np.array([width / 2 + dx, height / 2 + dy])
box0 = box0.astype(np.float32)
box1 = box1.astype(np.float32)
mat = cv2.getPerspectiveTransform(box0, box1)
image = cv2.warpPerspective(image, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
borderValue=(
0, 0,
0,))
mask = cv2.warpPerspective(mask, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
borderValue=(
0, 0,
0,))
return image, mask
def randomHorizontalFlip(image, mask, u=0.5):
if np.random.random() < u:
image = cv2.flip(image, 1)
mask = cv2.flip(mask, 1)
return image, mask
def randomVerticleFlip(image, mask, u=0.5):
if np.random.random() < u:
image = cv2.flip(image, 0)
mask = cv2.flip(mask, 0)
return image, mask
def randomRotate90(image, mask, u=0.5):
if np.random.random() < u:
image=np.rot90(image)
mask=np.rot90(mask)
return image, mask
def default_loader(img_path, mask_path):
img = cv2.imread(img_path)
# print("img:{}".format(np.shape(img)))
img = cv2.resize(img, (448, 448))
mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
mask = 255. - cv2.resize(mask, (448, 448))
img = randomHueSaturationValue(img,
hue_shift_limit=(-30, 30),
sat_shift_limit=(-5, 5),
val_shift_limit=(-15, 15))
img, mask = randomShiftScaleRotate(img, mask,
shift_limit=(-0.1, 0.1),
scale_limit=(-0.1, 0.1),
aspect_limit=(-0.1, 0.1),
rotate_limit=(-0, 0))
img, mask = randomHorizontalFlip(img, mask)
img, mask = randomVerticleFlip(img, mask)
img, mask = randomRotate90(img, mask)
mask = np.expand_dims(mask, axis=2)
#
# print(np.shape(img))
# print(np.shape(mask))
img = np.array(img, np.float32).transpose(2,0,1)/255.0 * 3.2 - 1.6
mask = np.array(mask, np.float32).transpose(2,0,1)/255.0
mask[mask >= 0.5] = 1
mask[mask <= 0.5] = 0
#mask = abs(mask-1)
return img, mask
def default_DRIVE_loader(img_path, mask_path):
img = cv2.imread(img_path)
img = cv2.resize(img, (448, 448))
# mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
mask = np.array(Image.open(mask_path))
mask = cv2.resize(mask, (448, 448))
img = randomHueSaturationValue(img,
hue_shift_limit=(-30, 30),
sat_shift_limit=(-5, 5),
val_shift_limit=(-15, 15))
img, mask = randomShiftScaleRotate(img, mask,
shift_limit=(-0.1, 0.1),
scale_limit=(-0.1, 0.1),
aspect_limit=(-0.1, 0.1),
rotate_limit=(-0, 0))
img, mask = randomHorizontalFlip(img, mask)
img, mask = randomVerticleFlip(img, mask)
img, mask = randomRotate90(img, mask)
mask = np.expand_dims(mask, axis=2)
img = np.array(img, np.float32).transpose(2, 0, 1) / 255.0 * 3.2 - 1.6
mask = np.array(mask, np.float32).transpose(2, 0, 1) / 255.0
mask[mask >= 0.5] = 1
mask[mask <= 0.5] = 0
# mask = abs(mask-1)
return img, mask
def read_ORIGA_datasets(root_path, mode='train'):
images = []
masks = []
if mode == 'train':
read_files = os.path.join(root_path, 'Set_A.txt')
else:
read_files = os.path.join(root_path, 'Set_B.txt')
image_root = os.path.join(root_path, 'images')
gt_root = os.path.join(root_path, 'masks')
for image_name in open(read_files):
image_path = os.path.join(image_root, image_name.split('.')[0] + '.jpg')
label_path = os.path.join(gt_root, image_name.split('.')[0] + '.jpg')
print(image_path, label_path)
images.append(image_path)
masks.append(label_path)
return images, masks
def read_Messidor_datasets(root_path, mode='train'):
images = []
masks = []
if mode == 'train':
read_files = os.path.join(root_path, 'train.txt')
else:
read_files = os.path.join(root_path, 'test.txt')
image_root = os.path.join(root_path, 'save_image')
gt_root = os.path.join(root_path, 'save_mask')
for image_name in open(read_files):
image_path = os.path.join(image_root, image_name.split('.')[0] + '.png')
label_path = os.path.join(gt_root, image_name.split('.')[0] + '.png')
images.append(image_path)
masks.append(label_path)
return images, masks
def read_RIM_ONE_datasets(root_path, mode='train'):
images = []
masks = []
if mode == 'train':
read_files = os.path.join(root_path, 'train_files.txt')
else:
read_files = os.path.join(root_path, 'test_files.txt')
image_root = os.path.join(root_path, 'RIM-ONE-images')
gt_root = os.path.join(root_path, 'RIM-ONE-exp1')
for image_name in open(read_files):
image_path = os.path.join(image_root, image_name.split('.')[0] + '.png')
label_path = os.path.join(gt_root, image_name.split('.')[0] + '-exp1.png')
images.append(image_path)
masks.append(label_path)
return images, masks
def read_DRIVE_datasets(root_path, mode='train'):
images = []
masks = []
image_root = os.path.join(root_path, 'training/images')
gt_root = os.path.join(root_path, 'training/1st_manual')
for image_name in os.listdir(image_root):
image_path = os.path.join(image_root, image_name.split('.')[0] + '.tif')
label_path = os.path.join(gt_root, image_name.split('_')[0] + '_manual1.gif')
images.append(image_path)
masks.append(label_path)
print(images, masks)
return images, masks
def read_Cell_datasets(root_path, mode='train'):
images = []
masks = []
image_root = os.path.join(root_path, 'train-images')
gt_root = os.path.join(root_path, 'train-labels')
for image_name in os.listdir(image_root):
image_path = os.path.join(image_root, image_name)
label_path = os.path.join(gt_root, image_name)
images.append(image_path)
masks.append(label_path)
print(images, masks)
return images, masks
def read_datasets_vessel(root_path, mode='train'):
images = []
masks = []
image_root = os.path.join(root_path, 'training/images')
gt_root = os.path.join(root_path, 'training/mask')
for image_name in os.listdir(image_root):
image_path = os.path.join(image_root, image_name)
label_path = os.path.join(gt_root, image_name)
if cv2.imread(image_path) is not None:
if os.path.exists(image_path) and os.path.exists(label_path):
images.append(image_path)
masks.append(label_path)
print(images[:10], masks[:10])
return images, masks
class ImageFolder(data.Dataset):
def __init__(self,root_path, datasets='Messidor', mode='train'):
self.root = root_path
self.mode = mode
self.dataset = datasets
assert self.dataset in ['RIM-ONE', 'Messidor', 'ORIGA', 'DRIVE', 'Cell', 'Vessel'], \
"the dataset should be in 'Messidor', 'ORIGA', 'RIM-ONE', 'Vessel' "
if self.dataset == 'RIM-ONE':
self.images, self.labels = read_RIM_ONE_datasets(self.root, self.mode)
elif self.dataset == 'Messidor':
self.images, self.labels = read_Messidor_datasets(self.root, self.mode)
elif self.dataset == 'ORIGA':
self.images, self.labels = read_ORIGA_datasets(self.root, self.mode)
elif self.dataset == 'DRIVE':
self.images, self.labels = read_DRIVE_datasets(self.root, self.mode)
elif self.dataset == 'Cell':
self.images, self.labels = read_Cell_datasets(self.root, self.mode)
elif self.dataset == 'GAN_Vessel':
self.images, self.labels = read_datasets_vessel(self.root, self.mode)
else:
print('Default dataset is Messidor')
self.images, self.labels = read_Messidor_datasets(self.root, self.mode)
def __getitem__(self, index):
img, mask = default_DRIVE_loader(self.images[index], self.labels[index])
img = torch.Tensor(img)
mask = torch.Tensor(mask)
return img, mask
def __len__(self):
assert len(self.images) == len(self.labels), 'The number of images must be equal to labels'
return len(self.images)