forked from Tacode/PSPNet_VOC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoc_loader.py
281 lines (248 loc) · 10.3 KB
/
voc_loader.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
import os
from os.path import join as pjoin
import collections
import json
import torch
import numpy as np
import scipy.misc as m
import scipy.io as io
import matplotlib.pyplot as plt
import glob
from PIL import Image
from tqdm import tqdm
from torch.utils import data
from torchvision import transforms
def get_data_path(name):
"""Extract path to data from config file.
Args:
name (str): The name of the dataset.
Returns:
(str): The path to the root directory containing the dataset.
"""
js = open("config.json").read()
data = json.loads(js)
return os.path.expanduser(data[name]["data_path"])
class pascalVOCLoader(data.Dataset):
"""Data loader for the Pascal VOC semantic segmentation dataset.
Annotations from both the original VOC data (which consist of RGB images
in which colours map to specific classes) and the SBD (Berkely) dataset
(where annotations are stored as .mat files) are converted into a common
`label_mask` format. Under this format, each mask is an (M,N) array of
integer values from 0 to 21, where 0 represents the background class.
The label masks are stored in a new folder, called `pre_encoded`, which
is added as a subdirectory of the `SegmentationClass` folder in the
original Pascal VOC data layout.
A total of five data splits are provided for working with the VOC data:
train: The original VOC 2012 training data - 1464 images
val: The original VOC 2012 validation data - 1449 images
trainval: The combination of `train` and `val` - 2913 images
train_aug: The unique images present in both the train split and
training images from SBD: - 8829 images (the unique members
of the result of combining lists of length 1464 and 8498)
train_aug_val: The original VOC 2012 validation data minus the images
present in `train_aug` (This is done with the same logic as
the validation set used in FCN PAMI paper, but with VOC 2012
rather than VOC 2011) - 904 images
"""
def __init__(
self,
root,
split="train_aug",
is_transform=False,
img_size=224,
augmentations=None,
img_norm=True,
):
self.root = os.path.expanduser(root)
self.split = split
self.is_transform = is_transform
self.augmentations = augmentations
self.img_norm = img_norm
self.n_classes = 21
self.mean = np.array([104.00699, 116.66877, 122.67892])
self.files = collections.defaultdict(list)
self.img_size = (
img_size if isinstance(img_size, tuple) else (img_size, img_size)
)
for split in ["train", "val", "trainval"]:
path = pjoin(self.root, "ImageSets/Segmentation", split + ".txt")
file_list = tuple(open(path, "r"))
file_list = [id_.rstrip() for id_ in file_list]
self.files[split] = file_list
self.setup_annotations()
self.tf = transforms.Compose([transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])
def __len__(self):
return len(self.files[self.split])
def __getitem__(self, index):
im_name = self.files[self.split][index]
im_path = pjoin(self.root, "JPEGImages", im_name + ".jpg")
lbl_path = pjoin(self.root, "SegmentationClass/pre_encoded", im_name + ".png")
im = Image.open(im_path)
lbl = Image.open(lbl_path)
if self.augmentations is not None:
im, lbl = self.augmentations(im, lbl)
if self.is_transform:
im, lbl = self.transform(im, lbl)
cls = self.getcls(lbl)
return im, lbl, cls
def getcls(self,lbl):
vector_lbl = lbl.contiguous().view(1,-1).numpy()
set_lbl = set(np.squeeze(vector_lbl,0))
cls = np.zeros((21),dtype=int)
for s in set_lbl:
cls[int(s)] = 1
cls = torch.from_numpy(np.array(cls)).long()
return cls
def transform(self, img, lbl):
if self.img_size == ('same', 'same'):
pass
else:
img = img.resize((self.img_size[0], self.img_size[1])) # uint8 with RGB mode
lbl = lbl.resize((self.img_size[0], self.img_size[1]))
img = self.tf(img)
lbl = torch.from_numpy(np.array(lbl)).long()
lbl[lbl == 255] = 0
return img, lbl
def get_pascal_labels(self):
"""Load the mapping that associates pascal classes with label colors
Returns:
np.ndarray with dimensions (21, 3)
"""
return np.asarray(
[
[0, 0, 0],
[128, 0, 0],
[0, 128, 0],
[128, 128, 0],
[0, 0, 128],
[128, 0, 128],
[0, 128, 128],
[128, 128, 128],
[64, 0, 0],
[192, 0, 0],
[64, 128, 0],
[192, 128, 0],
[64, 0, 128],
[192, 0, 128],
[64, 128, 128],
[192, 128, 128],
[0, 64, 0],
[128, 64, 0],
[0, 192, 0],
[128, 192, 0],
[0, 64, 128],
]
)
def encode_segmap(self, mask):
"""Encode segmentation label images as pascal classes
Args:
mask (np.ndarray): raw segmentation label image of dimension
(M, N, 3), in which the Pascal classes are encoded as colours.
Returns:
(np.ndarray): class map with dimensions (M,N), where the value at
a given location is the integer denoting the class index.
"""
mask = mask.astype(int)
label_mask = np.zeros((mask.shape[0], mask.shape[1]), dtype=np.int16)
for ii, label in enumerate(self.get_pascal_labels()):
label_mask[np.where(np.all(mask == label, axis=-1))[:2]] = ii
label_mask = label_mask.astype(int)
return label_mask
def decode_segmap(self, label_mask, plot=False):
"""Decode segmentation class labels into a color image
Args:
label_mask (np.ndarray): an (M,N) array of integer values denoting
the class label at each spatial location.
plot (bool, optional): whether to show the resulting color image
in a figure.
Returns:
(np.ndarray, optional): the resulting decoded color image.
"""
label_colours = self.get_pascal_labels()
r = label_mask.copy()
g = label_mask.copy()
b = label_mask.copy()
for ll in range(0, self.n_classes):
r[label_mask == ll] = label_colours[ll, 0]
g[label_mask == ll] = label_colours[ll, 1]
b[label_mask == ll] = label_colours[ll, 2]
rgb = np.zeros((label_mask.shape[0], label_mask.shape[1], 3))
rgb[:, :, 0] = r / 255.0
rgb[:, :, 1] = g / 255.0
rgb[:, :, 2] = b / 255.0
if plot:
plt.imshow(rgb)
plt.show()
else:
return rgb
def setup_annotations(self):
"""Sets up Berkley annotations by adding image indices to the
`train_aug` split and pre-encode all segmentation labels into the
common label_mask format (if this has not already been done). This
function also defines the `train_aug` and `train_aug_val` data splits
according to the description in the class docstring
"""
sbd_path = get_data_path("sbd")
target_path = pjoin(self.root, "SegmentationClass/pre_encoded")
if not os.path.exists(target_path):
os.makedirs(target_path)
path = pjoin(sbd_path, "dataset/train.txt")
sbd_train_list = tuple(open(path, "r"))
sbd_train_list = [id_.rstrip() for id_ in sbd_train_list]
train_aug = self.files["train"] + sbd_train_list
# keep unique elements (stable)
train_aug = [
train_aug[i] for i in sorted(np.unique(train_aug, return_index=True)[1])
]
self.files["train_aug"] = train_aug
set_diff = set(self.files["val"]) - set(train_aug) # remove overlap
self.files["train_aug_val"] = list(set_diff)
pre_encoded = glob.glob(pjoin(target_path, "*.png"))
expected = np.unique(self.files["train_aug"] + self.files["val"]).size
if len(pre_encoded) != expected:
print("Pre-encoding segmentation masks...")
for ii in tqdm(sbd_train_list):
lbl_path = pjoin(sbd_path, "dataset/cls", ii + ".mat")
data = io.loadmat(lbl_path)
lbl = data["GTcls"][0]["Segmentation"][0].astype(np.int32)
lbl = m.toimage(lbl, high=lbl.max(), low=lbl.min())
m.imsave(pjoin(target_path, ii + ".png"), lbl)
for ii in tqdm(self.files["trainval"]):
fname = ii + ".png"
lbl_path = pjoin(self.root, "SegmentationClass", fname)
lbl = self.encode_segmap(m.imread(lbl_path))
lbl = m.toimage(lbl, high=lbl.max(), low=lbl.min())
m.imsave(pjoin(target_path, fname), lbl)
assert expected == 9733, "unexpected dataset sizes"
# Leave code for debugging purposes
# import ptsemseg.augmentations as aug
# if __name__ == '__main__':
# # local_path = '/home/meetshah1995/datasets/VOCdevkit/VOC2012/'
# bs = 4
# augs = aug.Compose([aug.RandomRotate(10), aug.RandomHorizontallyFlip()])
# dst = pascalVOCLoader(root=local_path, is_transform=True, augmentations=augs)
# trainloader = data.DataLoader(dst, batch_size=bs)
# for i, data in enumerate(trainloader):
# imgs, labels = data
# imgs = imgs.numpy()[:, ::-1, :, :]
# imgs = np.transpose(imgs, [0,2,3,1])
# f, axarr = plt.subplots(bs, 2)
# for j in range(bs):
# axarr[j][0].imshow(imgs[j])
# axarr[j][1].imshow(dst.decode_segmap(labels.numpy()[j]))
# plt.show()
# a = raw_input()
# if a == 'ex':
# break
# else:
# plt.close()
if __name__ == "__main__":
local_path = '/media/tyl/File/dataSets/VOCdevkit/VOC2012'
dst = pascalVOCLoader(root=local_path,is_transform=True,augmentations=None)
trainloader = data.DataLoader(dst,batch_size=2)
for i,(imgs,labels,cls) in enumerate(trainloader):
print(cls.shape,imgs.shape)
if i == 1:
break