-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-ex02-v4.py
312 lines (237 loc) · 8.83 KB
/
test-ex02-v4.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
from __future__ import print_function, division
import os
import torch
import pandas as pd
from skimage import io, transform, filters, exposure
from skimage.util import random_noise
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import json
import Image
import ImageDraw
# Ignore warnings
import warnings
warnings.filterwarnings("ignore")
class CityScapeDataset(Dataset):
"""CityScape dataset"""
def __init__(self, root_dir_img, root_dir_gt, gt_type, transform=None):
"""
Args :
roto_dir_img (string) : Directory to real images
root_dir_gt (string) : Directory to ground truth data of the images
gt_type (String) : Either "gtCoarse" or "gtFine"
transform (callable, optoonal) : Optional transform to be applied on a sample
"""
self.root_dir_img = root_dir_img
self.root_dir_gt = root_dir_gt
self.transform = transform
self.gt_type = gt_type
tmp = []
for cityfolder in os.listdir(self.root_dir_img):
for filename_ori in os.listdir(os.path.join(self.root_dir_img, cityfolder)):
# print(filename_ori)
filename_general = filename_ori.replace("leftImg8bit.png", "")
tmp.append([filename_general, cityfolder])
self.idx_mapping = tmp
def __len__(self):
return len(self.idx_mapping)
def __getitem__(self, idx):
# idx is translated to city folder and
# variable for syntax shortening
rt_im = self.root_dir_img
rt_gt = self.root_dir_gt
fn = self.idx_mapping[idx][0] # filename
cf = self.idx_mapping[idx][1] # city folder
gtt = self.gt_type
# complete path for each file
img_real_fn = os.path.join(rt_im, cf, fn + "leftImg8bit.png")
img_color_fn = os.path.join(rt_gt, cf, fn + gtt + "_color.png")
img_polygon_fn = os.path.join(rt_gt, cf, fn + gtt + "_polygons.json")
# read the file
img_real = io.imread(img_real_fn)
img_color = io.imread(img_color_fn)
with open(img_polygon_fn) as f:
img_polygon = json.load(f)
f.close()
# creating sample tuple
sample = {
'image': img_real,
'gt_color': img_color,
'gt_polygon': img_polygon
}
# transform the sample (if any)
if self.transform:
sample = self.transform(sample)
return sample
class ToTensor(object):
"""Convert ndarrays in sample into Tensors"""
def __call__(self, sample):
image = sample['image']
gt_color = sample['gt_color']
gt_polygon = sample['gt_polygon']
return {
'image': torch.from_numpy(image),
'gt_color': torch.from_numpy(gt_color),
'gt_polygon': gt_polygon
}
class OnlyRoads(object):
""" Recreate ground truth only for road class and non-road class."""
def __call__(self, sample):
image = sample['image']
gt_color = sample['gt_color']
gt_polygon = pd.DataFrame(sample['gt_polygon'])
h, w = gt_polygon['imgHeight'][0], gt_polygon['imgWidth'][0]
polygon_road = []
for item in gt_polygon.itertuples(index=True):
label = getattr(item, 'objects')['label']
if label == 'road':
polygon = getattr(item, 'objects')['polygon']
tmp = []
for i in polygon:
tmp.append((i[0], i[1]))
polygon_road.append(tmp)
poly = Image.new('RGB', (w, h), (0, 0, 0))
pdraw = ImageDraw.Draw(poly)
for pl in polygon_road:
pdraw.polygon(pl, fill=(255, 0, 0))
poly2 = np.array(poly)
return {
'image': image,
'gt_color': poly2,
'gt_polygon': sample['gt_polygon']
}
class Rescale(object):
"""Rescale the image in a sample to a given size.
Args:
output_size (tuple or int): Desired output size. If tuple, output is
matched to output_size. If int, smaller of image edges is matched
to output_size keeping aspect ratio the same.
"""
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
self.output_size = output_size
def __call__(self, sample):
image = sample['image']
gt_color = sample['gt_color']
gt_polygon = sample['gt_polygon']
h, w = image.shape[:2]
if isinstance(self.output_size, int):
if h > w:
new_h, new_w = self.output_size * h / w, self.output_size
else:
new_h, new_w = self.output_size, self.output_size * w / h
else:
new_h, new_w = self.output_size
new_h, new_w = int(new_h), int(new_w)
img = transform.resize(image, (new_h, new_w), order=0)
gt_col = transform.resize(gt_color, (new_h, new_w), order=0)
return {'image': img,
'gt_color': gt_col,
'gt_polygon': gt_polygon}
class Rotate(object):
"""Rotate an image to the desired angle.
Args:
rotate_val (int): Desired rotation value, in degree.
"""
def __init__(self, rotate_val):
assert isinstance(rotate_val, (int))
self.rotate_val = rotate_val
def __call__(self, sample):
image = sample['image']
gt_color = sample['gt_color']
gt_polygon = sample['gt_polygon']
img = transform.rotate(image, self.rotate_val, resize=True, order=0)
gt_col = transform.rotate(gt_color, self.rotate_val, resize=True, order=0)
return {'image': img,
'gt_color': gt_col,
'gt_polygon': gt_polygon}
class FlipLR(object):
"""Flip the image left to right"""
def __call__(self, sample):
image = sample['image']
gt_color = sample['gt_color']
gt_polygon = sample['gt_polygon']
img = np.fliplr(image).copy()
gt_col = np.fliplr(gt_color).copy()
return {'image': img,
'gt_color': gt_col,
'gt_polygon': gt_polygon}
class Blur(object):
"""Blur an image, simulation of rainy or foggy weather.
Args:
blur_val (int): Desired blur value.
"""
def __init__(self, blur_val):
assert isinstance(blur_val, (int))
self.blur_val = blur_val
def __call__(self, sample):
image = sample['image']
gt_color = sample['gt_color']
gt_polygon = sample['gt_polygon']
img = filters.gaussian(image, sigma=self.blur_val)
return {'image': img,
'gt_color': gt_color,
'gt_polygon': gt_polygon}
class ContrastSet(object):
"""Change a contrast of an image, simulation of very light/dark condition.
Args:
val (tuple): Desired stretch range of the distribution.
"""
def __init__(self, val):
assert isinstance(val, (tuple))
self.val = val
def __call__(self, sample):
image = sample['image']
gt_color = sample['gt_color']
gt_polygon = sample['gt_polygon']
img = exposure.rescale_intensity(image, (self.val[0], self.val[1]))
return {'image': img,
'gt_color': gt_color,
'gt_polygon': gt_polygon}
# ------------------------------------------------------------
compose_tf = transforms.Compose([
OnlyRoads(),
Rescale(100),
Rotate(0),
FlipLR(),
Blur(1),
ContrastSet((0, 3)),
ToTensor()
])
city_dataset = CityScapeDataset(root_dir_img='../../../data/cityscape-mini/leftImg8bit/train',
root_dir_gt='../../../data/cityscape-mini/gtFine/train',
gt_type='gtFine', transform=compose_tf
)
print(len(city_dataset))
# for i in range(len(city_dataset)):
# sample = city_dataset[i]
# print(i, sample['image'].shape,
# sample['gt_color'].shape)
# plt.imshow(sample['image'])
# plt.pause(1)
# plt.imshow(sample['gt_color'])
# plt.pause(1)
train_loader = torch.utils.data.DataLoader(city_dataset,
batch_size=1, shuffle=True,
num_workers=4, pin_memory=True)
print(len(train_loader))
def imshow(inp, title=None):
"""Imshow for Tensor."""
inp = inp.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
plt.imshow(inp)
if title is not None:
plt.title(title)
plt.pause(10) # pause a bit so that plots are updated
# Get a batch of training data
# inputs, classes = next(iter(train_loader))
inputs = next(iter(train_loader))
print(inputs)
# Make a grid from batch
# out = torchvision.utils.make_grid(inputs)
# imshow(output)