-
Notifications
You must be signed in to change notification settings - Fork 10
/
cityscapes.py
204 lines (151 loc) · 6.76 KB
/
cityscapes.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
import torch
import numpy as np
from torch.utils.data import Dataset
import os
import random
from torchvision.transforms import Compose, CenterCrop, Normalize, Resize, Pad, ToTensor, ToPILImage
from PIL import Image, ImageOps
EXTENSIONS = ['.jpg', '.png']
from PIL import Image
def load_image(file):
return Image.open(file)
def is_image(filename):
return any(filename.endswith(ext) for ext in EXTENSIONS)
def is_label(filename):
return filename.endswith("_labelTrainIds.png")
def image_path(root, basename, extension):
return os.path.join(root, f'{basename}{extension}')
def image_path_city(root, name):
return os.path.join(root, f'{name}')
def image_basename(filename):
return os.path.basename(os.path.splitext(filename)[0])
def colormap_cityscapes(n):
cmap = np.zeros([n, 3]).astype(np.uint8)
cmap[0, :] = np.array([128, 64, 128])
cmap[1, :] = np.array([244, 35, 232])
cmap[2, :] = np.array([70, 70, 70])
cmap[3, :] = np.array([102, 102, 156])
cmap[4, :] = np.array([190, 153, 153])
cmap[5, :] = np.array([153, 153, 153])
cmap[6, :] = np.array([250, 170, 30])
cmap[7, :] = np.array([220, 220, 0])
cmap[8, :] = np.array([107, 142, 35])
cmap[9, :] = np.array([152, 251, 152])
cmap[10, :] = np.array([70, 130, 180])
cmap[11, :] = np.array([220, 20, 60])
cmap[12, :] = np.array([255, 0, 0])
cmap[13, :] = np.array([0, 0, 142])
cmap[14, :] = np.array([0, 0, 70])
cmap[15, :] = np.array([0, 60, 100])
cmap[16, :] = np.array([0, 80, 100])
cmap[17, :] = np.array([0, 0, 230])
cmap[18, :] = np.array([119, 11, 32])
cmap[19, :] = np.array([0, 0, 0])
return cmap
def colormap(n):
cmap = np.zeros([n, 3]).astype(np.uint8)
for i in np.arange(n):
r, g, b = np.zeros(3)
for j in np.arange(8):
r = r + (1 << (7 - j)) * ((i & (1 << (3 * j))) >> (3 * j))
g = g + (1 << (7 - j)) * ((i & (1 << (3 * j + 1))) >> (3 * j + 1))
b = b + (1 << (7 - j)) * ((i & (1 << (3 * j + 2))) >> (3 * j + 2))
cmap[i, :] = np.array([r, g, b])
return cmap
class Relabel:
def __init__(self, olabel, nlabel):
self.olabel = olabel
self.nlabel = nlabel
def __call__(self, tensor):
assert (isinstance(tensor, torch.LongTensor) or isinstance(tensor,
torch.ByteTensor)), 'tensor needs to be LongTensor'
tensor[tensor == self.olabel] = self.nlabel
return tensor
class ToLabel:
def __call__(self, image):
return torch.from_numpy(np.array(image)).long().unsqueeze(0)
class Colorize:
def __init__(self, n=22):
# self.cmap = colormap(256)
self.cmap = colormap_cityscapes(256)
self.cmap[n] = self.cmap[-1]
self.cmap = torch.from_numpy(self.cmap[:n])
def __call__(self, gray_image):
size = gray_image.size()
# print(size)
color_image = torch.ByteTensor(3, size[1], size[2]).fill_(0)
# color_image = torch.ByteTensor(3, size[0], size[1]).fill_(0)
# for label in range(1, len(self.cmap)):
for label in range(0, len(self.cmap)):
mask = gray_image[0] == label
# mask = gray_image == label
color_image[0][mask] = self.cmap[label][0]
color_image[1][mask] = self.cmap[label][1]
color_image[2][mask] = self.cmap[label][2]
return color_image
# Augmentations - different function implemented to perform random augments on both image and target
class MyCoTransform(object):
def __init__(self, enc, augment=True, height=512):
self.enc = enc
self.augment = augment
self.height = height
pass
def __call__(self, input, target):
# do something to both images
input = Resize(self.height, Image.BILINEAR)(input)
target = Resize(self.height, Image.NEAREST)(target)
if (self.augment):
# Random hflip
hflip = random.random()
if (hflip < 0.5):
input = input.transpose(Image.FLIP_LEFT_RIGHT)
target = target.transpose(Image.FLIP_LEFT_RIGHT)
# Random translation 0-2 pixels (fill rest with padding
transX = random.randint(-2, 2)
transY = random.randint(-2, 2)
input = ImageOps.expand(input, border=(transX, transY, 0, 0), fill=0)
target = ImageOps.expand(target, border=(transX, transY, 0, 0), fill=255) # pad label filling with 255
input = input.crop((0, 0, input.size[0] - transX, input.size[1] - transY))
target = target.crop((0, 0, target.size[0] - transX, target.size[1] - transY))
input = ToTensor()(input)
if (self.enc):
target = Resize(int(self.height / 8), Image.NEAREST)(target)
target = ToLabel()(target)
target = Relabel(255, 19)(target)
return input, target
class cityscapes(Dataset):
def __init__(self, root, co_transform=None, subset='train'):
self.images_root = os.path.join(root, 'leftImg8bit/')
self.labels_root = os.path.join(root, 'gtFine/')
self.images_root += subset
self.labels_root += subset
# print (self.images_root)
# self.filenames = [image_basename(f) for f in os.listdir(self.images_root) if is_image(f)]
# print(os.walk(self.images_root))
# for root, dirs, files in os.walk(self.images_root, topdown=True):
# for name in files:
# print(os.path.join(root, name))
# for name in dirs:
# print(os.path.join(root, name))
self.filenames = [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(self.images_root)) for f in
fn if is_image(f)]
self.filenames.sort()
# print(self.filenames)
# [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(".")) for f in fn]
# self.filenamesGt = [image_basename(f) for f in os.listdir(self.labels_root) if is_image(f)]
self.filenamesGt = [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(self.labels_root)) for f in
fn if is_label(f)]
self.filenamesGt.sort()
self.co_transform = co_transform # ADDED THIS
def __getitem__(self, index):
filename = self.filenames[index]
filenameGt = self.filenamesGt[index]
with open(image_path_city(self.images_root, filename), 'rb') as f:
image = load_image(f).convert('RGB')
with open(image_path_city(self.labels_root, filenameGt), 'rb') as f:
label = load_image(f).convert('P')
if self.co_transform is not None:
image, label = self.co_transform(image, label)
return image, label
def __len__(self):
return len(self.filenames)