-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvoc_loader.py
226 lines (189 loc) · 7.02 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
#!/usr/bin/env python
import collections
import os.path as osp
import numbers
import random
import math
import numpy as np
import PIL.Image
import scipy.io
import torch
from torch.utils import data
from torchvision import transforms
class RandomCropGenerator(object):
def __call__(self, img):
self.x1 = random.uniform(0, 1)
self.y1 = random.uniform(0, 1)
return img
class RandomCrop(object):
def __init__(self, size, padding=0, gen=None):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
self.padding = padding
self._gen = gen
def __call__(self, img):
if self.padding > 0:
img = ImageOps.expand(img, border=self.padding, fill=0)
w, h = img.size
th, tw = self.size
if w == tw and h == th:
return img
if self._gen is not None:
x1 = math.floor(self._gen.x1 * (w - tw))
y1 = math.floor(self._gen.y1 * (h - th))
else:
x1 = random.randint(0, w - tw)
y1 = random.randint(0, h - th)
return img.crop((x1, y1, x1 + tw, y1 + th))
class VOCClassSegBase(data.Dataset):
class_names = np.array([
'background',
'aeroplane',
'bicycle',
'bird',
'boat',
'bottle',
'bus',
'car',
'cat',
'chair',
'cow',
'diningtable',
'dog',
'horse',
'motorbike',
'person',
'potted plant',
'sheep',
'sofa',
'train',
'tv/monitor',
])
mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
def __init__(self, root, split='train', transform=False):
self.root = root
self.split = split
self._transform = transform
# VOC2011 and others are subset of VOC2012
dataset_dir = osp.join(self.root, 'VOC/VOCdevkit/VOC2012')
print(dataset_dir)
self.files = collections.defaultdict(list)
for split in ['train', 'val']:
imgsets_file = osp.join(
dataset_dir, 'ImageSets/Segmentation/%s.txt' % split)
for did in open(imgsets_file):
did = did.strip()
img_file = osp.join(dataset_dir, 'JPEGImages/%s.jpg' % did)
lbl_file = osp.join(
dataset_dir, 'SegmentationClass/%s.png' % did)
self.files[split].append({
'img': img_file,
'lbl': lbl_file,
})
print(len(self.files["train"]))
def __len__(self):
return len(self.files[self.split])
def __getitem__(self, index):
data_file = self.files[self.split][index]
# load image
img_file = data_file['img']
img_pil = PIL.Image.open(img_file)
gen = RandomCropGenerator()
onlyBgPatch = True
while onlyBgPatch:
transform_img = transforms.Compose([
gen,
RandomCrop(128, gen=gen),
transforms.Resize([128, 128])])
img = np.array(transform_img(img_pil),dtype=np.uint8)
transform_mask = transforms.Compose([
RandomCrop(128, gen=gen)])
# load label
lbl_file = data_file['lbl']
lbl_pil = PIL.Image.open(lbl_file)
lbl_cropped = transform_mask(lbl_pil)
lbl = np.array(transform_mask(lbl_pil), dtype=np.int32)
lbl[lbl == 255] = -1
unique_vals = np.unique(lbl)
if len(unique_vals) > 2:
onlyBgPatch = False
for i in unique_vals:
percentage_covered = np.sum(lbl==i) / (128*128)
if percentage_covered >= 0.9:
onlyBgPatch = True
break
if self._transform:
return self.transform(img, lbl)
else:
return img, lbl
def transform(self, img, lbl):
img = img[:, :, ::-1] # RGB -> BGR
img = img.astype(np.float64)
img -= self.mean_bgr
img = img.transpose(2, 0, 1)
img = torch.from_numpy(img).float()
lbl = torch.from_numpy(lbl).long()
return img, lbl
def untransform(self, img, lbl):
img = img.numpy()
img = img.transpose(1, 2, 0)
img += self.mean_bgr
img = img.astype(np.uint8)
img = img[:, :, ::-1]
lbl = lbl.numpy()
return img, lbl
class VOC2011ClassSeg(VOCClassSegBase):
def __init__(self, root, split='train', transform=False):
super(VOC2011ClassSeg, self).__init__(
root, split=split, transform=transform)
pkg_root = osp.join(osp.dirname(osp.realpath(__file__)), '..')
imgsets_file = osp.join(
pkg_root, 'ext/fcn.berkeleyvision.org',
'data/pascal/seg11valid.txt')
dataset_dir = osp.join(self.root, 'VOC/VOCdevkit/VOC2012')
for did in open(imgsets_file):
did = did.strip()
img_file = osp.join(dataset_dir, 'JPEGImages/%s.jpg' % did)
lbl_file = osp.join(dataset_dir, 'SegmentationClass/%s.png' % did)
self.files['seg11valid'].append({'img': img_file, 'lbl': lbl_file})
class VOC2012ClassSeg(VOCClassSegBase):
url = 'http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar' # NOQA
def __init__(self, root, split='train', transform=False):
super(VOC2012ClassSeg, self).__init__(
root, split=split, transform=transform)
class SBDClassSeg(VOCClassSegBase):
# XXX: It must be renamed to benchmark.tar to be extracted.
url = 'http://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/semantic_contours/benchmark.tgz' # NOQA
def __init__(self, root, split='train', transform=False):
self.root = root
self.split = split
self._transform = transform
dataset_dir = osp.join(self.root, 'VOC/benchmark_RELEASE/dataset')
self.files = collections.defaultdict(list)
for split in ['train', 'val']:
imgsets_file = osp.join(dataset_dir, '%s.txt' % split)
for did in open(imgsets_file):
did = did.strip()
img_file = osp.join(dataset_dir, 'img/%s.jpg' % did)
lbl_file = osp.join(dataset_dir, 'cls/%s.mat' % did)
self.files[split].append({
'img': img_file,
'lbl': lbl_file,
})
def __getitem__(self, index):
data_file = self.files[self.split][index]
# load image
img_file = data_file['img']
img = PIL.Image.open(img_file)
img = np.array(img, dtype=np.uint8)
# load label
lbl_file = data_file['lbl']
mat = scipy.io.loadmat(lbl_file)
lbl = mat['GTcls'][0]['Segmentation'][0].astype(np.int32)
lbl[lbl == 255] = -1
if self._transform:
return self.transform(img, lbl)
else:
return img, lbl