-
Notifications
You must be signed in to change notification settings - Fork 2
/
datasets.py
289 lines (221 loc) · 9.17 KB
/
datasets.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
import math
from pathlib import Path
import numpy as np
import torch
from PIL import Image
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms, utils
from torchvision.transforms import functional as F
import transformations
from helpers import natural_keys, pad_image, to_int
IMAGENET_MEAN = [0.485, 0.456, 0.406]
IMAGENET_STD = [0.229, 0.224, 0.225]
VALIDATION_ID_THRESHOLD = 90
class RoadsDatasetTrain(Dataset):
"""Road segmentation datset"""
def __init__(
self,
patch_size,
large_patch_size,
number_patch_per_image,
image_initial_size,
root_dir,
):
self.root_dir = Path(root_dir)
self.img_dir = self.root_dir / "images"
self.gt_dir = self.root_dir / "groundtruth"
self.img_names = [x.name for x in self.img_dir.glob("**/*.png") if x.is_file()]
self.img_names = [x.name for x in self.img_dir.glob("**/*.png") if x.is_file()]
# keep only image with id >= 90
self.img_names = [
x
for x in self.img_names
if int(x.split("_")[1].split(".")[0]) < VALIDATION_ID_THRESHOLD
]
self.large_patch_size = large_patch_size
self.number_patch_per_image = number_patch_per_image
self.image_initial_size = image_initial_size
self.patch_size = patch_size
# self.img_transform = transforms.Compose(
# [
# transformations.ToPILImage(),
## TODO: Put right parameters for pad to get to 480x480
# transformations.Pad((self.large_patch_size - self.patch_size)//2)
# ]
# )
self.transforms = self._get_transforms()
def __len__(self):
return self.number_patch_per_image * len(self.img_names)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
l_p_s = self.large_patch_size
n_p_p_i = self.number_patch_per_image
n_s_p_p_i = self.image_initial_size // self.patch_size
p_s = self.patch_size
l_p_s = self.large_patch_size
image_index = idx // n_p_p_i
patch_index = idx % n_p_p_i
padding = (l_p_s - p_s) // 2
y = ((patch_index % n_s_p_p_i) * p_s) + padding
x = ((patch_index // n_s_p_p_i) * p_s) + padding
image_sample = self._get_image_and_gt(image_index)
image = image_sample["image"]
groundtruth = image_sample["groundtruth"]
small_image = F.crop(image, x - padding, y - padding, l_p_s, l_p_s)
small_groundtruth = F.crop(groundtruth, x - padding, y - padding, l_p_s, l_p_s)
sample = {"image": small_image, "groundtruth": small_groundtruth}
transformation = transformations.ToTensor()
sample = transformation(sample)
return sample
def _get_image_and_gt(self, image_index):
"""
Will get the image and grond truth corresponding to index and will transform them according to self.img_transform
"""
image_name = self.img_names[image_index]
image = Image.open(self.img_dir / image_name)
groundtruth = Image.open(self.gt_dir / image_name)
image_sample = {"image": image, "groundtruth": groundtruth}
if self.transforms is not None:
image_sample = self.transforms(image_sample)
return image_sample
def _get_transforms(self):
padding = (self.large_patch_size - self.patch_size) // 2
im_size1 = self.image_initial_size + 2 * padding
im_size2 = self.image_initial_size
rot_padding = math.ceil(math.ceil((im_size1 * math.sqrt(2)) - im_size2) / 2)
transforms_list = [
transformations.Pad(padding, padding_mode="symmetric"),
transformations.RandomHorizontalFlip(p=1),
transformations.RandomVerticalFlip(p=1),
transformations.Pad(rot_padding, padding_mode="symmetric"),
transformations.RandomRotation(degrees=90),
transformations.CenterCrop(self.image_initial_size + 2 * padding),
transformations.ColorJitter(),
transformations.ToTensor(),
# TODO: Right params for normalize, can be the mean and std of imageNet
transformations.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD),
transformations.ToPILImage(),
]
return transforms.Compose(transforms_list)
class RoadsDatasetTest(Dataset):
"""Road segmentation dataset for test time"""
def __init__(
self,
patch_size,
large_patch_size,
number_patch_per_image,
image_initial_size,
root_dir,
):
self.root_dir = Path(root_dir)
self.img_names = [str(x) for x in self.root_dir.glob("**/*.png") if x.is_file()]
# Sort images to in a human readable way
self.img_names.sort(key=natural_keys)
self.patch_size = patch_size
self.large_patch_size = large_patch_size
self.number_patch_per_image = number_patch_per_image
self.image_initial_size = image_initial_size
def __len__(self):
return self.number_patch_per_image * len(self.img_names)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
l_p_s = self.large_patch_size
n_p_p_i = self.number_patch_per_image
n_s_p_p_i = self.image_initial_size // self.patch_size
p_s = self.patch_size
image_index = idx // n_p_p_i
patch_index = idx % n_p_p_i
padding = (l_p_s - p_s) // 2
y = ((patch_index % n_s_p_p_i) * p_s) + padding
x = ((patch_index // n_s_p_p_i) * p_s) + padding
image = self._extract_image(self, image_index)
small_image = F.crop(image, x - padding, y - padding, l_p_s, l_p_s)
x = (x - padding) // p_s
y = (y - padding) // p_s
sample = small_image
transformation = transforms.Compose([transforms.ToTensor()])
sample = transformation(sample)
sample = {"id": image_index, "x": x, "y": y, "image": sample}
return sample
def _extract_image(self, image_index):
padding = (self.large_patch_size - self.patch_size) // 2
image_name = self.img_names[image_index]
image = Image.open(self.img_dir / image_name)
transformation = transforms.Compose(
[
# ImageNet normalization for now
transforms.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD),
transforms.Pad(padding, padding_mode="symmetric"),
]
)
return transformation(image)
class RoadsDatasetValidation(Dataset):
"""Road segmentation dataset for test time"""
def __init__(
self,
patch_size,
large_patch_size,
number_patch_per_image,
image_initial_size,
root_dir,
):
self.root_dir = Path(root_dir)
self.img_names = [str(x) for x in self.root_dir.glob("**/*.png") if x.is_file()]
# keep only image with id >= 90
self.img_names = [
x
for x in self.img_names
if int(x.split("_")[1].split(".")[0]) >= VALIDATION_ID_THRESHOLD
]
self.patch_size = patch_size
self.large_patch_size = large_patch_size
self.number_patch_per_image = number_patch_per_image
self.image_initial_size = image_initial_size
def __len__(self):
return self.number_patch_per_image * len(self.images)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
l_p_s = self.large_patch_size
n_p_p_i = self.number_patch_per_image
n_s_p_p_i = self.image_initial_size // self.patch_size
p_s = self.patch_size
image_index = idx // n_p_p_i
patch_index = idx % n_p_p_i
padding = (l_p_s - p_s) // 2
y = ((patch_index % n_s_p_p_i) * p_s) + padding
x = ((patch_index // n_s_p_p_i) * p_s) + padding
image_sample = self._extract_image(self, image_index)
image = image_sample["image"]
groundtruth = image_sample["groundtruth"]
small_image = F.crop(image, x - padding, y - padding, l_p_s, l_p_s)
small_groundtruth = F.crop(groundtruth, x - padding, y - padding, l_p_s, l_p_s)
x = (x - padding) // p_s
y = (y - padding) // p_s
transformation = transforms.ToTensor()
image = transformation(image)
groundtruth = transformation(groundtruth)
sample = {
"id": image_index,
"x": x,
"y": y,
"image": small_image,
"groundtruth": groundtruth,
}
return sample
def _extract_image(self, image_index):
padding = (self.large_patch_size - self.patch_size) // 2
image_name = self.img_names[image_index]
image = Image.open(self.img_dir / image_name)
groundtruth = Image.open(self.gt_dir / image_name)
sample = {"image": image, "groundtruth": groundtruth}
transformation = transforms.Compose(
[
# ImageNet normalization for now
transformations.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD),
transforms.Pad(padding, padding_mode="symmetric"),
]
)
return transformation(sample)