-
Notifications
You must be signed in to change notification settings - Fork 394
/
Copy pathpascal.py
280 lines (241 loc) · 9 KB
/
pascal.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
"""
Copyright 2017-2018 Fizyr (https://fizyr.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from generators.common import Generator
import os
import numpy as np
from six import raise_from
import cv2
import xml.etree.ElementTree as ET
voc_classes = {
'aeroplane': 0,
'bicycle': 1,
'bird': 2,
'boat': 3,
'bottle': 4,
'bus': 5,
'car': 6,
'cat': 7,
'chair': 8,
'cow': 9,
'diningtable': 10,
'dog': 11,
'horse': 12,
'motorbike': 13,
'person': 14,
'pottedplant': 15,
'sheep': 16,
'sofa': 17,
'train': 18,
'tvmonitor': 19
}
def _findNode(parent, name, debug_name=None, parse=None):
if debug_name is None:
debug_name = name
result = parent.find(name)
if result is None:
raise ValueError('missing element \'{}\''.format(debug_name))
if parse is not None:
try:
return parse(result.text)
except ValueError as e:
raise_from(ValueError('illegal value for \'{}\': {}'.format(debug_name, e)), None)
return result
class PascalVocGenerator(Generator):
"""
Generate data for a Pascal VOC dataset.
See http://host.robots.ox.ac.uk/pascal/VOC/ for more information.
"""
def __init__(
self,
data_dir,
set_name,
classes=voc_classes,
image_extension='.jpg',
skip_truncated=False,
skip_difficult=False,
**kwargs
):
"""
Initialize a Pascal VOC data generator.
Args:
data_dir: the path of directory which contains ImageSets directory
set_name: test|trainval|train|val
classes: class names tos id mapping
image_extension: image filename ext
skip_truncated:
skip_difficult:
**kwargs:
"""
self.data_dir = data_dir
self.set_name = set_name
self.classes = classes
self.image_names = [l.strip().split(None, 1)[0] for l in
open(os.path.join(data_dir, 'ImageSets', 'Main', set_name + '.txt')).readlines()]
self.image_extension = image_extension
self.skip_truncated = skip_truncated
self.skip_difficult = skip_difficult
# class ids to names mapping
self.labels = {}
for key, value in self.classes.items():
self.labels[value] = key
super(PascalVocGenerator, self).__init__(**kwargs)
def size(self):
"""
Size of the dataset.
"""
return len(self.image_names)
def num_classes(self):
"""
Number of classes in the dataset.
"""
return len(self.classes)
def has_label(self, label):
"""
Return True if label is a known label.
"""
return label in self.labels
def has_name(self, name):
"""
Returns True if name is a known class.
"""
return name in self.classes
def name_to_label(self, name):
"""
Map name to label.
"""
return self.classes[name]
def label_to_name(self, label):
"""
Map label to name.
"""
return self.labels[label]
def image_aspect_ratio(self, image_index):
"""
Compute the aspect ratio for an image with image_index.
"""
path = os.path.join(self.data_dir, 'JPEGImages', self.image_names[image_index] + self.image_extension)
image = cv2.imread(path)
h, w = image.shape[:2]
return float(w) / float(h)
def load_image(self, image_index):
"""
Load an image at the image_index.
"""
path = os.path.join(self.data_dir, 'JPEGImages', self.image_names[image_index] + self.image_extension)
image = cv2.imread(path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image
def __parse_annotation(self, element):
"""
Parse an annotation given an XML element.
"""
truncated = _findNode(element, 'truncated', parse=int)
difficult = _findNode(element, 'difficult', parse=int)
class_name = _findNode(element, 'name').text
if class_name not in self.classes:
raise ValueError('class name \'{}\' not found in classes: {}'.format(class_name, list(self.classes.keys())))
box = np.zeros((4,))
label = self.name_to_label(class_name)
bndbox = _findNode(element, 'bndbox')
box[0] = _findNode(bndbox, 'xmin', 'bndbox.xmin', parse=float) - 1
box[1] = _findNode(bndbox, 'ymin', 'bndbox.ymin', parse=float) - 1
box[2] = _findNode(bndbox, 'xmax', 'bndbox.xmax', parse=float) - 1
box[3] = _findNode(bndbox, 'ymax', 'bndbox.ymax', parse=float) - 1
return truncated, difficult, box, label
def __parse_annotations(self, xml_root):
"""
Parse all annotations under the xml_root.
"""
annotations = {'labels': np.empty((0,), dtype=np.int32),
'bboxes': np.empty((0, 4))}
for i, element in enumerate(xml_root.iter('object')):
try:
truncated, difficult, box, label = self.__parse_annotation(element)
except ValueError as e:
raise_from(ValueError('could not parse object #{}: {}'.format(i, e)), None)
if truncated and self.skip_truncated:
continue
if difficult and self.skip_difficult:
continue
annotations['bboxes'] = np.concatenate([annotations['bboxes'], [box]])
annotations['labels'] = np.concatenate([annotations['labels'], [label]])
return annotations
def load_annotations(self, image_index):
"""
Load annotations for an image_index.
"""
filename = self.image_names[image_index] + '.xml'
try:
tree = ET.parse(os.path.join(self.data_dir, 'Annotations', filename))
return self.__parse_annotations(tree.getroot())
except ET.ParseError as e:
raise_from(ValueError('invalid annotations file: {}: {}'.format(filename, e)), None)
except ValueError as e:
raise_from(ValueError('invalid annotations file: {}: {}'.format(filename, e)), None)
if __name__ == '__main__':
train_generator = PascalVocGenerator(
'datasets/voc_trainval/VOC2007',
'train',
phi=0,
skip_difficult=True,
batch_size=1,
misc_effect=None,
visual_effect=None,
)
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
anchors = train_generator.anchors
for batch_inputs, batch_targets in train_generator:
image = batch_inputs[0][0]
image[..., 0] *= std[0]
image[..., 1] *= std[1]
image[..., 2] *= std[2]
image[..., 0] += mean[0]
image[..., 1] += mean[1]
image[..., 2] += mean[2]
image *= 255.
regression = batch_targets[0][0]
valid_ids = np.where(regression[:, -1] == 1)[0]
boxes = anchors[valid_ids]
deltas = regression[valid_ids]
class_ids = np.argmax(batch_targets[1][0][valid_ids], axis=-1)
mean_ = [0, 0, 0, 0]
std_ = [0.2, 0.2, 0.2, 0.2]
width = boxes[:, 2] - boxes[:, 0]
height = boxes[:, 3] - boxes[:, 1]
x1 = boxes[:, 0] + (deltas[:, 0] * std_[0] + mean_[0]) * width
y1 = boxes[:, 1] + (deltas[:, 1] * std_[1] + mean_[1]) * height
x2 = boxes[:, 2] + (deltas[:, 2] * std_[2] + mean_[2]) * width
y2 = boxes[:, 3] + (deltas[:, 3] * std_[3] + mean_[3]) * height
for x1_, y1_, x2_, y2_, class_id in zip(x1, y1, x2, y2, class_ids):
x1_, y1_, x2_, y2_ = int(x1_), int(y1_), int(x2_), int(y2_)
cv2.rectangle(image, (x1_, y1_), (x2_, y2_), (0, 255, 0), 2)
class_name = train_generator.labels[class_id]
label = class_name
ret, baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.3, 1)
cv2.rectangle(image, (x1_, y2_ - ret[1] - baseline), (x1_ + ret[0], y2_), (255, 255, 255), -1)
cv2.putText(image, label, (x1_, y2_ - baseline), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
cv2.imshow('image', image.astype(np.uint8)[..., ::-1])
cv2.waitKey(0)
# 36864, 46080, 48384, 48960, 49104
# if first_valid_id < 36864:
# stride = 8
# elif 36864 <= first_valid_id < 46080:
# stride = 16
# elif 46080 <= first_valid_id < 48384:
# stride = 32
# elif 48384 <= first_valid_id < 48960:
# stride = 64
# else:
# stride = 128
pass