-
Notifications
You must be signed in to change notification settings - Fork 1
/
object_detection.py
201 lines (161 loc) · 8.68 KB
/
object_detection.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
# The steps implemented in the object detection sample code:
# 1. for an image of width and height being (w, h) pixels, resize image to (w', h'), where w/h = w'/h' and w' x h' = 262144
# 2. resize network input size to (w', h')
# 3. pass the image to network and do inference
# (4. if inference speed is too slow for you, try to make w' x h' smaller, which is defined with DEFAULT_INPUT_SIZE (in object_detection.py or ObjectDetection.cs))
import numpy as np
import math
from PIL import Image
class ObjectDetection(object):
"""Class for Custom Vision's exported object detection model
"""
ANCHORS = np.array([[0.573, 0.677], [1.87, 2.06], [3.34, 5.47], [7.88, 3.53], [9.77, 9.17]])
IOU_THRESHOLD = 0.45
DEFAULT_INPUT_SIZE = 512 * 512
def __init__(self, labels, prob_threshold=0.10, max_detections = 20):
"""Initialize the class
Args:
labels ([str]): list of labels for the exported model.
prob_threshold (float): threshold for class probability.
max_detections (int): the max number of output results.
"""
assert len(labels) >= 1, "At least 1 label is required"
self.labels = labels
self.prob_threshold = prob_threshold
self.max_detections = max_detections
def _logistic(self, x):
return np.where(x > 0, 1 / (1 + np.exp(-x)), np.exp(x) / (1 + np.exp(x)))
def _non_maximum_suppression(self, boxes, class_probs, max_detections):
"""Remove overlapping bouding boxes
"""
assert len(boxes) == len(class_probs)
max_detections = min(max_detections, len(boxes))
max_probs = np.amax(class_probs, axis=1)
max_classes = np.argmax(class_probs, axis=1)
areas = boxes[:, 2] * boxes[:, 3]
selected_boxes = []
selected_classes = []
selected_probs = []
while len(selected_boxes) < max_detections:
# Select the prediction with the highest probability.
i = np.argmax(max_probs)
if max_probs[i] < self.prob_threshold:
break
# Save the selected prediction
selected_boxes.append(boxes[i])
selected_classes.append(max_classes[i])
selected_probs.append(max_probs[i])
box = boxes[i]
other_indices = np.concatenate((np.arange(i), np.arange(i + 1, len(boxes))))
other_boxes = boxes[other_indices]
# Get overlap between the 'box' and 'other_boxes'
x1 = np.maximum(box[0], other_boxes[:, 0])
y1 = np.maximum(box[1], other_boxes[:, 1])
x2 = np.minimum(box[0] + box[2], other_boxes[:, 0] + other_boxes[:, 2])
y2 = np.minimum(box[1] + box[3], other_boxes[:, 1] + other_boxes[:, 3])
w = np.maximum(0, x2 - x1)
h = np.maximum(0, y2 - y1)
# Calculate Intersection Over Union (IOU)
overlap_area = w * h
iou = overlap_area / (areas[i] + areas[other_indices] - overlap_area)
# Find the overlapping predictions
overlapping_indices = other_indices[np.where(iou > self.IOU_THRESHOLD)[0]]
overlapping_indices = np.append(overlapping_indices, i)
# Set the probability of overlapping predictions to zero, and udpate max_probs and max_classes.
class_probs[overlapping_indices, max_classes[i]] = 0
max_probs[overlapping_indices] = np.amax(class_probs[overlapping_indices], axis=1)
max_classes[overlapping_indices] = np.argmax(class_probs[overlapping_indices], axis=1)
assert len(selected_boxes) == len(selected_classes) and len(selected_boxes) == len(selected_probs)
return selected_boxes, selected_classes, selected_probs
def _extract_bb(self, prediction_output, anchors):
assert len(prediction_output.shape) == 3
num_anchor = anchors.shape[0]
height, width, channels = prediction_output.shape
assert channels % num_anchor == 0
num_class = int(channels / num_anchor) - 5
assert num_class == len(self.labels)
outputs = prediction_output.reshape((height, width, num_anchor, -1))
# Extract bouding box information
x = (self._logistic(outputs[..., 0]) + np.arange(width)[np.newaxis, :, np.newaxis]) / width
y = (self._logistic(outputs[..., 1]) + np.arange(height)[:, np.newaxis, np.newaxis]) / height
w = np.exp(outputs[..., 2]) * anchors[:, 0][np.newaxis, np.newaxis, :] / width
h = np.exp(outputs[..., 3]) * anchors[:, 1][np.newaxis, np.newaxis, :] / height
# (x,y) in the network outputs is the center of the bounding box. Convert them to top-left.
x = x - w / 2
y = y - h / 2
boxes = np.stack((x, y, w, h), axis=-1).reshape(-1, 4)
# Get confidence for the bounding boxes.
objectness = self._logistic(outputs[..., 4])
# Get class probabilities for the bounding boxes.
class_probs = outputs[..., 5:]
class_probs = np.exp(class_probs - np.amax(class_probs, axis=3)[..., np.newaxis])
class_probs = class_probs / np.sum(class_probs, axis=3)[..., np.newaxis] * objectness[..., np.newaxis]
class_probs = class_probs.reshape(-1, num_class)
assert len(boxes) == len(class_probs)
return (boxes, class_probs)
def _update_orientation(self, image):
"""
corrects image orientation according to EXIF data
image: input PIL image
returns corrected PIL image
"""
exif_orientation_tag = 0x0112
if hasattr(image, '_getexif'):
exif = image._getexif()
if exif != None and exif_orientation_tag in exif:
orientation = exif.get(exif_orientation_tag, 1)
print('Image has EXIF Orientation: {}'.format(str(orientation)))
# orientation is 1 based, shift to zero based and flip/transpose based on 0-based values
orientation -= 1
if orientation >= 4:
image = image.transpose(Image.TRANSPOSE)
if orientation == 2 or orientation == 3 or orientation == 6 or orientation == 7:
image = image.transpose(Image.FLIP_TOP_BOTTOM)
if orientation == 1 or orientation == 2 or orientation == 5 or orientation == 6:
image = image.transpose(Image.FLIP_LEFT_RIGHT)
return image
def predict_image(self, image):
inputs = self.preprocess(image)
prediction_outputs = self.predict(inputs)
return self.postprocess(prediction_outputs)
def preprocess(self, image):
image = image.convert("RGB") if image.mode != "RGB" else image
image = self._update_orientation(image)
ratio = math.sqrt(self.DEFAULT_INPUT_SIZE / image.width / image.height)
new_width = int(image.width * ratio)
new_height = int(image.height * ratio)
new_width = 32 * round(new_width / 32);
new_height = 32 * round(new_height / 32);
image = image.resize((new_width, new_height))
return image
def predict(self, preprocessed_inputs):
"""Evaluate the model and get the output
Need to be implemented for each platforms. i.e. TensorFlow, CoreML, etc.
"""
raise NotImplementedError
def postprocess(self, prediction_outputs):
""" Extract bounding boxes from the model outputs.
Args:
prediction_outputs: Output from the object detection model. (H x W x C)
Returns:
List of Prediction objects.
"""
boxes, class_probs = self._extract_bb(prediction_outputs, self.ANCHORS)
# Remove bounding boxes whose confidence is lower than the threshold.
max_probs = np.amax(class_probs, axis=1)
index, = np.where(max_probs > self.prob_threshold)
index = index[(-max_probs[index]).argsort()]
# Remove overlapping bounding boxes
selected_boxes, selected_classes, selected_probs = self._non_maximum_suppression(boxes[index],
class_probs[index],
self.max_detections)
return [{'probability': round(float(selected_probs[i]), 8),
'tagId': int(selected_classes[i]),
'tagName': self.labels[selected_classes[i]],
'boundingBox': {
'left': round(float(selected_boxes[i][0]), 8),
'top': round(float(selected_boxes[i][1]), 8),
'width': round(float(selected_boxes[i][2]), 8),
'height': round(float(selected_boxes[i][3]), 8)
}
} for i in range(len(selected_boxes))]