-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataLoader.py
198 lines (176 loc) · 6.43 KB
/
dataLoader.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
import os
import numpy as np
from glob import glob
from PIL import Image
import torch
from torchvision.transforms import Compose, CenterCrop, Normalize, ToTensor
from transform import ReLabel, ToLabel, Scale, HorizontalFlip, VerticalFlip, ColorJitter
import random
def makedirs(path):
if not os.path.exists(path):
os.makedirs(path)
class Dataset(torch.utils.data.Dataset):
def __init__(self, root):
self.size = (180, 135)
self.root = root
if not os.path.exists(self.root):
raise Exception("[!] {} not exists.".format(root))
self.img_resize = Compose(
[
Scale(self.size, Image.BILINEAR),
ColorJitter(brightness=0, contrast=0, saturation=0, hue=0),
]
)
self.label_resize = Compose(
[
Scale(self.size, Image.NEAREST),
]
)
self.img_transform = Compose(
[
ToTensor(),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
]
)
self.hsv_transform = Compose(
[
ToTensor(),
]
)
self.label_transform = Compose(
[
ToLabel(),
ReLabel(255, 1),
]
)
# sort file names
self.input_paths = sorted(
glob(os.path.join(self.root, "{}/*.jpg".format("ISIC-2017_Training_Data")))
)
self.label_paths = sorted(
glob(
os.path.join(
self.root, "{}/*.png".format("ISIC-2017_Training_Part1_GroundTruth")
)
)
)
self.name = os.path.basename(root)
if len(self.input_paths) == 0 or len(self.label_paths) == 0:
raise Exception("No images/labels are found in {}".format(self.root))
def __getitem__(self, index):
image = Image.open(self.input_paths[index]).convert("RGB")
# image_hsv = Image.open(self.input_paths[index]).convert('HSV')
label = Image.open(self.label_paths[index]).convert("P")
image = self.img_resize(image)
# image_hsv = self.img_resize(image_hsv)
label = self.label_resize(label)
# brightness_factor = 1 + random.uniform(-0.4,0.4)
# contrast_factor = 1 + random.uniform(-0.4,0.4)
# saturation_factor = 1 + random.uniform(-0.4,0.4)
# hue_factor = random.uniform(-0.1,0.1)
# gamma = 1 + random.uniform(-0.1,0.1)
# randomly flip images
if random.random() > 0.5:
image = HorizontalFlip()(image)
# image_hsv = HorizontalFlip()(image_hsv)
label = HorizontalFlip()(label)
if random.random() > 0.5:
image = VerticalFlip()(image)
# image_hsv = VerticalFlip()(image_hsv)
label = VerticalFlip()(label)
# randomly crop image to size 128*128
w, h = image.size
th, tw = (128, 128)
x1 = random.randint(0, w - tw)
y1 = random.randint(0, h - th)
if w == tw and h == th:
image = image
# image_hsv = image_hsv
label = label
else:
if random.random() > 0.5:
image = image.resize((128, 128), Image.BILINEAR)
# image_hsv = image_hsv.resize((128,128),Image.BILINEAR)
label = label.resize((128, 128), Image.NEAREST)
else:
image = image.crop((x1, y1, x1 + tw, y1 + th))
# image_hsv = image_hsv.crop((x1, y1, x1 + tw, y1 + th))
label = label.crop((x1, y1, x1 + tw, y1 + th))
# angle = random.randint(-20, 20)
# image = image.rotate(angle, resample=Image.BILINEAR)
# image_hsv = image_hsv.rotate(angle, resample=Image.BILINEAR)
# label = label.rotate(angle, resample=Image.NEAREST)
image = self.img_transform(image)
# image_hsv = self.hsv_transform(image_hsv)
# image = torch.cat([image,image_hsv],0)
label = self.label_transform(label)
return image, label
def __len__(self):
return len(self.input_paths)
class Dataset_val(torch.utils.data.Dataset):
def __init__(self, root):
size = (128, 128)
self.root = root
if not os.path.exists(self.root):
raise Exception("[!] {} not exists.".format(root))
self.img_transform = Compose(
[
Scale(size, Image.BILINEAR),
ToTensor(),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
]
)
self.hsv_transform = Compose(
[
Scale(size, Image.BILINEAR),
ToTensor(),
]
)
self.label_transform = Compose(
[
Scale(size, Image.NEAREST),
ToLabel(),
ReLabel(255, 1),
]
)
# sort file names
self.input_paths = sorted(
glob(os.path.join(self.root, "{}/*.jpg".format("ISIC-2017_Test_v2_Data")))
)
self.label_paths = sorted(
glob(
os.path.join(
self.root, "{}/*.png".format("ISIC-2017_Test_v2_Part1_GroundTruth")
)
)
)
self.name = os.path.basename(root)
if len(self.input_paths) == 0 or len(self.label_paths) == 0:
raise Exception("No images/labels are found in {}".format(self.root))
def __getitem__(self, index):
image = Image.open(self.input_paths[index]).convert("RGB")
# image_hsv = Image.open(self.input_paths[index]).convert('HSV')
label = Image.open(self.label_paths[index]).convert("P")
if self.img_transform is not None:
image = self.img_transform(image)
# image_hsv = self.hsv_transform(image_hsv)
else:
image = image
# image_hsv = image_hsv
if self.label_transform is not None:
label = self.label_transform(label)
else:
label = label
# image = torch.cat([image,image_hsv],0)
return image, label
def __len__(self):
return len(self.input_paths)
def loader(dataset, batch_size, num_workers=8, shuffle=True):
input_images = dataset
input_loader = torch.utils.data.DataLoader(
dataset=input_images,
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
)
return input_loader