-
Notifications
You must be signed in to change notification settings - Fork 6
/
dataset.py
297 lines (230 loc) · 10.7 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
import os
import cv2
import random
import itertools
import numpy as np
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import torch
from torch.utils import data
from torch.utils.data import Dataset
from torch.utils.data.sampler import Sampler
from torchvision import transforms
import torchvision.transforms.functional as tf
from utils.sampler import TwoStreamBatchSampler
################# Dataset for Seg
class MyDataSet_seg(data.Dataset):
def __init__(self, root_path, list_path, root_path_coarsemask=None, crop_size=(224, 224), max_iters=None, label=True, fold=None):
self.root_path = root_path
self.root_path_coarsemask = root_path_coarsemask
self.list_path = list_path
self.crop_w, self.crop_h = crop_size
self.label = label
self.ids = os.listdir(os.path.join(self.root_path, 'Images'))
self.img_ids = [f'/Images/{i} /Annotations/{i}' for i in self.ids]
if not max_iters==None:
self.img_ids = self.img_ids * int(np.ceil(float(max_iters) / len(self.img_ids)))
self.files = []
if self.label:
for index, name in enumerate(self.img_ids):
img_file = name[0:name.find(' ')]
label_file = name[name.find(' ')+1:]
self.files.append({
"img": img_file,
"label": label_file,
"name": name
})
else:
for name in self.img_ids:
img_file = name[0:name.find(' ')]
# label_file = name[name.find(' ')+1:]
self.files.append({
"img": img_file,
"label": img_file,
"name": name
})
self.train_augmentation = transforms.Compose(
[
transforms.RandomVerticalFlip(p=0.5),
transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor(),
transforms.ToPILImage(),
transforms.Resize(self.crop_w)
])
self.train_coarsemask_augmentation = transforms.Compose(
[transforms.RandomAffine(degrees=10, translate=(0, 0.1), scale=(0.9, 1.1), shear=5.729578),
transforms.RandomVerticalFlip(p=0.5),
transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor(),
transforms.ToPILImage(),
transforms.Resize(self.crop_w)
])
self.train_gt_augmentation = transforms.Compose(
[
# transforms.RandomAffine(degrees=10, translate=(0, 0.1), scale=(0.9, 1.1), shear=5.729578),
transforms.RandomVerticalFlip(p=0.5),
transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor(),
transforms.ToPILImage(),
transforms.Resize(self.crop_w)
])
def __len__(self):
return len(self.files)
def __getitem__(self, index):
datafiles = self.files[index]
image = Image.open(self.root_path + datafiles["img"]).convert('RGB')
if self.label:
label = Image.open(self.root_path + datafiles["label"])
else:
label = Image.open(self.root_path + datafiles["label"]).convert('L')
is_crop = [0,1]
random.shuffle(is_crop)
if is_crop[0] == 0:
[WW, HH] = image.size
p_center = [int(WW / 2), int(HH / 2)]
crop_num = np.array(range(30, int(np.mean(p_center) / 2), 30))
random.shuffle(crop_num)
crop_p = crop_num[0]
rectangle = (crop_p, crop_p, WW - crop_p, HH - crop_p)
image = image.crop(rectangle)
label = label.crop(rectangle)
image = image.resize((self.crop_w, self.crop_h), Image.BICUBIC)
label = label.resize((self.crop_w, self.crop_h), Image.NEAREST)
else:
image = image.resize((self.crop_w, self.crop_h), Image.BICUBIC)
label = label.resize((self.crop_w, self.crop_h), Image.NEAREST)
seed = np.random.randint(2147483647)
random.seed(seed)
image = self.train_augmentation(image)
random.seed(seed)
label = self.train_gt_augmentation(label)
image = np.array(image) / 255.
image = image.transpose((2, 0, 1))
image = image.astype(np.float32)
label = np.array(label)
label = np.float32(label > 0)
name = datafiles["img"].split('/')[-1]
return image.copy(), label.copy(), name
class MyValDataSet_seg(data.Dataset):
def __init__(self, root_path, list_path, root_path_coarsemask=None, crop_size=(224, 224)):
self.root_path = root_path
self.root_path_coarsemask = root_path_coarsemask
self.list_path = list_path
self.crop_h, self.crop_w = crop_size
self.ids = os.listdir(os.path.join(self.root_path, 'Images'))
self.img_ids = [f'/Images/{i} /Annotations/{i}' for i in self.ids]
self.files = []
for name in self.img_ids:
img_file = name[0:name.find(' ')]
label_file = name[name.find(' ')+1:]
self.files.append({
"img": img_file,
"label": label_file,
"name": name
})
def __len__(self):
return len(self.files)
def __getitem__(self, index):
datafiles = self.files[index]
image = Image.open(self.root_path + datafiles["img"]).convert('RGB')
label = Image.open(self.root_path + datafiles["label"])
image = image.resize((self.crop_h, self.crop_w), Image.BICUBIC)
label = label.resize((self.crop_h, self.crop_w), Image.NEAREST)
image = np.array(image) / 255.
image = image.transpose(2, 0, 1)
image = image.astype(np.float32)
label = np.array(label)
name = datafiles["img"].split('/')[0]
return image.copy(), label.copy(), name
class MyTestDataSet_seg(data.Dataset):
def __init__(self, root_path, list_path, root_path_coarsemask=None, crop_size=(224, 224), fold=None):
self.root_path = root_path
self.root_path_coarsemask = root_path_coarsemask
self.list_path = list_path
self.crop_h, self.crop_w = crop_size
self.ids = os.listdir(os.path.join(self.root_path, 'Images'))
self.img_ids = [f'/Images/{i} /Annotations/{i}' for i in self.ids]
self.files = []
for index, name in enumerate(self.img_ids):
if fold is not None:
if not (index >= fold * 50 and index < (fold + 1) * 50):
continue
img_file = name[0:name.find(' ')]
label_file = name[name.find(' ')+1:]
self.files.append({
"img": img_file,
"label": label_file,
"name": name
})
def __len__(self):
return len(self.files)
def __getitem__(self, index):
datafiles = self.files[index]
image = Image.open(self.root_path + datafiles["img"]).convert('RGB')
label = Image.open(self.root_path + datafiles["label"])
image0 = image.resize((self.crop_h, self.crop_w), Image.BICUBIC)
image0 = np.array(image0) / 255.
image0 = image0.transpose(2, 0, 1).astype(np.float32)
image1 = image.resize((self.crop_h + 32, self.crop_w + 32), Image.BICUBIC)
image1 = np.array(image1) / 255.
image1 = image1.transpose(2, 0, 1).astype(np.float32)
image2 = image.resize((self.crop_h + 64, self.crop_w + 64), Image.BICUBIC)
image2 = np.array(image2) / 255.
image2 = image2.transpose(2, 0, 1).astype(np.float32)
label0 = label.resize((self.crop_h, self.crop_w), Image.NEAREST)
label0 = np.array(label0)
label1 = label.resize((self.crop_h + 32, self.crop_w + 32), Image.NEAREST)
label1 = np.array(label1)
label2 = label.resize((self.crop_h+64, self.crop_w+64), Image.NEAREST)
label2 = np.array(label2)
# name = datafiles["img"][7:23]
name = datafiles["img"].split('/')[-1]
return image0.copy(), image1.copy(), image2.copy(), label0.copy(), name
def get_data(args):
data_str = args.data
if 'isic' in data_str.lower():
############# Load training data
data_train_root = 'root for /ISIC2017/Training'
data_train_add_root = 'root for /ISIC2017/Training_addition'
train_dataset_label = MyDataSet_seg(data_train_root, None, crop_size=(args.w, args.h))
train_dataset_unlabel = MyDataSet_seg(data_train_add_root, None, crop_size=(args.w, args.h),
label=False)
train_data = torch.utils.data.ConcatDataset([train_dataset_label, train_dataset_unlabel])
labeled_idxs = list(range(args.label_data))
unlabeled_idxs = list(range(args.label_data, args.label_data + args.unlabel_data))
batch_sampler = TwoStreamBatchSampler(
labeled_idxs, unlabeled_idxs, args.batch_size, int(args.batch_size / 2))
trainloader = data.DataLoader(train_data, batch_sampler=batch_sampler, num_workers=8, pin_memory=True)
############# Load val data
data_val_root = 'root for /ISIC2017/Validation'
valloader = data.DataLoader(MyValDataSet_seg(data_val_root, None, crop_size=(args.w, args.h)),
batch_size=1, shuffle=False,
num_workers=8,
pin_memory=True)
############# Load testing data
data_test_root = 'root for /ISIC2017/Testing'
testloader = data.DataLoader(
MyTestDataSet_seg(data_test_root, None, crop_size=(args.w, args.h)), batch_size=1,
shuffle=False,
num_workers=8,
pin_memory=True)
return {
'trainloader': trainloader,
'valloader': valloader,
'testloader': testloader
}
elif 'ph' in args.data_str.lower():
data_test_ph2_root = 'root for /PH2Dataset/PH2Dataset/PH2 Dataset images/'
# data_test_root_mask = 'Coarse_masks/Testing_EnhancedSN/'
data_test_list = 'root for /ISIC/ph2_test.txt'
testloader = data.DataLoader(
MyTestDataSet_seg(data_test_ph2_root, None, crop_size=(args.w, args.h)), batch_size=1,
shuffle=False,
num_workers=8,
pin_memory=True)
return {
'trainloader': None,
'valloader': None,
'testloader': testloader
}