forked from IshitaTakeshi/RoadDamageDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
road_damage_dataset.py
188 lines (143 loc) · 6.71 KB
/
road_damage_dataset.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
import numpy as np
import os
import xml.etree.ElementTree as ET
import chainer
from chainercv.utils import read_image
from chainercv.transforms import random_flip
from chainercv.links.model.ssd import random_distort
from chainer.links.model.vision import resnet
from utils import roaddamage_label_names, generate_background_bbox
class RoadDamageDataset(chainer.dataset.DatasetMixin):
"""Bounding box dataset for RoadDamageDataset.
The index corresponds to each image.
When queried by an index, if :obj:`return_difficult == False`,
this dataset returns a corresponding
:obj:`img, bbox, label`, a tuple of an image, bounding boxes and labels.
This is the default behaviour.
If :obj:`return_difficult == True`, this dataset returns corresponding
:obj:`img, bbox, label, difficult`. :obj:`difficult` is a boolean array
that indicates whether bounding boxes are labeled as difficult or not.
The bounding boxes are packed into a two dimensional tensor of shape
:math:`(R, 4)`, where :math:`R` is the number of bounding boxes in
the image. The second axis represents attributes of the bounding box.
They are :math:`(y_{min}, x_{min}, y_{max}, x_{max})`, where the
four attributes are coordinates of the top left and the bottom right
vertices.
The labels are packed into a one dimensional tensor of shape :math:`(R,)`.
:math:`R` is the number of bounding boxes in the image.
The class name of the label :math:`l` is :math:`l` th element of
:obj:`roadddamage_label_names`.
The array :obj:`difficult` is a one dimensional boolean array of shape
:math:`(R,)`. :math:`R` is the number of bounding boxes in the image.
If :obj:`use_difficult` is :obj:`False`, this array is
a boolean array with all :obj:`False`.
The type of the image, the bounding boxes and the labels are as follows.
* :obj:`img.dtype == numpy.float32`
* :obj:`bbox.dtype == numpy.float32`
* :obj:`label.dtype == numpy.int32`
* :obj:`difficult.dtype == numpy.bool`
Args:
data_dir (string): Path to the root of the training data. If this is
:obj:`auto`, this class will automatically download data for you
under :obj:`$CHAINER_DATASET_ROOT/pfnet/chainercv/voc`.
split ({'train', 'val', 'trainval', 'test'}): Select a split of the
dataset. :obj:`test` split is only available for
2007 dataset.
"""
def __init__(self, data_dir, split='train'):
if split not in ['train', 'trainval', 'val']:
raise ValueError(
"split must be either of 'train', 'traival', or 'val'"
)
id_list_file = os.path.join(
data_dir, 'ImageSets/Main/{0}.txt'.format(split))
self.ids = [id_.strip() for id_ in open(id_list_file)]
self.data_dir = data_dir
def __len__(self):
return len(self.ids)
def get_example(self, i):
"""Returns the i-th example.
Returns a color image and bounding boxes. The image is in CHW format.
The returned image is RGB.
Args:
i (int): The index of the example.
Returns:
tuple of an image and bounding boxes
"""
id_ = self.ids[i]
anno = ET.parse(
os.path.join(self.data_dir, 'Annotations', id_ + '.xml'))
bbox = []
label = []
for obj in anno.findall('object'):
bndbox_anno = obj.find('bndbox')
# Ignore if the label is not listed
name = obj.find('name').text.strip()
if name not in roaddamage_label_names:
continue
label.append(roaddamage_label_names.index(name))
# subtract 1 to make pixel indexes 0-based
bbox.append([
int(bndbox_anno.find(tag).text) - 1
for tag in ('ymin', 'xmin', 'ymax', 'xmax')])
bbox = np.array(bbox).astype(np.int32)
label = np.array(label).astype(np.int32)
# Load an image
img_file = os.path.join(self.data_dir, 'JPEGImages', id_ + '.jpg')
img = read_image(img_file, color=True)
return img, bbox, label
class RoadDamageClassificationDataset(RoadDamageDataset):
def __init__(self, data_dir, split, background_probability=None):
"""
Generates images for road damage classification.
This dataset returns :obj:`image, label`, a tuple of an image and its
label. The image is basically of a damage part, but in a certain
probability which can be specified by `background_probability`,
a random background image is returned.
Args:
background_probability (float64): Probability to generate
a background image.
The default value is 1 / (number of damage categories + 1).
"""
super(RoadDamageClassificationDataset, self).__init__(
data_dir, split)
self.background_probability = background_probability
if background_probability is None:
self.background_probability = 1 / (len(roaddamage_label_names) + 1)
def _generate_damage(self, image, bboxes, labels):
index = np.random.randint(len(labels))
label = labels[index]
ymin, xmin, ymax, xmax = bboxes[index]
damage = image[:, ymin:ymax, xmin:xmax]
background = np.zeros(image.shape)
background[:, ymin:ymax, xmin:xmax] = damage
image = resnet.prepare(background)
return image, label
def _generate_background(self, image, bboxes):
_, H, W = image.shape
bbox = generate_background_bbox((H, W), (224, 224), bboxes)
ymin, xmin, ymax, xmax = bbox
image = resnet.prepare(image[:, ymin:ymax, xmin:xmax])
label = len(roaddamage_label_names) + 1
return image, label
def _data_augumentation(self, image):
image = random_distort(image)
image = random_flip(image, x_random=True)
return image
def get_example(self, i):
image, bboxes, labels =\
super(RoadDamageClassificationDataset, self).get_example(i)
if len(labels) == 0 or np.random.rand() < self.background_probability:
# generate_background
try:
image, label = self._generate_background(image, bboxes)
image = self._data_augumentation(image)
return image, label
except RuntimeError:
# return damage if failed to generate background
image, label = self._generate_damage(image, bboxes, labels)
image = self._data_augumentation(image)
return image, label
image, label = self._generate_damage(image, bboxes, labels)
image = self._data_augumentation(image)
return image, label