forked from motokimura/yolo_v1_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.py
308 lines (248 loc) · 12.6 KB
/
detect.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import torch
from torch.autograd import Variable
import torchvision.transforms as transforms
import os
import cv2
import numpy as np
from darknet import DarkNet
from yolo_v1 import YOLOv1
# VOC class names and BGR color.
VOC_CLASS_BGR = {
'aeroplane': (128, 0, 0),
'bicycle': (0, 128, 0),
'bird': (128, 128, 0),
'boat': (0, 0, 128),
'bottle': (128, 0, 128),
'bus': (0, 128, 128),
'car': (128, 128, 128),
'cat': (64, 0, 0),
'chair': (192, 0, 0),
'cow': (64, 128, 0),
'diningtable': (192, 128, 0),
'dog': (64, 0, 128),
'horse': (192, 0, 128),
'motorbike': (64, 128, 128),
'person': (192, 128, 128),
'pottedplant': (0, 64, 0),
'sheep': (128, 64, 0),
'sofa': (0, 192, 0),
'train': (128, 192, 0),
'tvmonitor': (0, 64, 128)
}
def visualize_boxes(image_bgr, boxes, class_names, probs, name_bgr_dict=None, line_thickness=2):
if name_bgr_dict is None:
name_bgr_dict = VOC_CLASS_BGR
image_boxes = image_bgr.copy()
for box, class_name, prob in zip(boxes, class_names, probs):
# Draw box on the image.
left_top, right_bottom = box
left, top = int(left_top[0]), int(left_top[1])
right, bottom = int(right_bottom[0]), int(right_bottom[1])
bgr = name_bgr_dict[class_name]
cv2.rectangle(image_boxes, (left, top), (right, bottom), bgr, thickness=line_thickness)
# Draw text on the image.
text = '%s %.2f' % (class_name, prob)
size, baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.5, thickness=2)
text_w, text_h = size
x, y = left, top
x1y1 = (x, y)
x2y2 = (x + text_w + line_thickness, y + text_h + line_thickness + baseline)
cv2.rectangle(image_boxes, x1y1, x2y2, bgr, -1)
cv2.putText(image_boxes, text, (x + line_thickness, y + 2*baseline + line_thickness),
cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.4, color=(255, 255, 255), thickness=1, lineType=8)
return image_boxes
class YOLODetector:
def __init__(self,
model_path, class_name_list=None, mean_rgb=[122.67891434, 116.66876762, 104.00698793],
conf_thresh=0.1, prob_thresh=0.1, nms_thresh=0.5,
gpu_id=0):
os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_id)
use_gpu = torch.cuda.is_available()
assert use_gpu, 'Current implementation does not support CPU mode. Enable CUDA.'
# Load YOLO model.
print("Loading YOLO model...")
darknet = DarkNet(conv_only=True, bn=True, init_weight=True)
darknet.features = torch.nn.DataParallel(darknet.features)
self.yolo = YOLOv1(darknet.features)
self.yolo.conv_layers = torch.nn.DataParallel(self.yolo.conv_layers)
self.yolo.load_state_dict(torch.load(model_path))
self.yolo.cuda()
print("Done loading!")
self.yolo.eval()
self.S = self.yolo.feature_size
self.B = self.yolo.num_bboxes
self.C = self.yolo.num_classes
self.class_name_list = class_name_list if (class_name_list is not None) else list(VOC_CLASS_BGR.keys())
assert len(self.class_name_list) == self.C
self.mean = np.array(mean_rgb, dtype=np.float32)
assert self.mean.shape == (3,)
self.conf_thresh = conf_thresh
self.prob_thresh = prob_thresh
self.nms_thresh = nms_thresh
self.to_tensor = transforms.ToTensor()
# Warm up.
dummy_input = Variable(torch.zeros((1, 3, 448, 448)))
dummy_input = dummy_input.cuda()
for i in range(10):
self.yolo(dummy_input)
def detect(self, image_bgr, image_size=448):
""" Detect objects from given image.
Args:
image_bgr: (numpy array) input image in BGR ids_sorted, sized [h, w, 3].
image_size: (int) image width and height to which input image is resized.
Returns:
boxes_detected: (list of tuple) box corner list like [((x1, y1), (x2, y2))_obj1, ...]. Re-scaled for original input image size.
class_names_detected: (list of str) list of class name for each detected boxe.
probs_detected: (list of float) list of probability(=confidence x class_score) for each detected box.
"""
h, w, _ = image_bgr.shape
img = cv2.resize(image_bgr, dsize=(image_size, image_size), interpolation=cv2.INTER_LINEAR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # assuming the model is trained with RGB images.
img = (img - self.mean) / 255.0
img = self.to_tensor(img) # [image_size, image_size, 3] -> [3, image_size, image_size]
img = img[None, :, :, :] # [3, image_size, image_size] -> [1, 3, image_size, image_size]
img = Variable(img)
img = img.cuda()
with torch.no_grad():
pred_tensor = self.yolo(img)
pred_tensor = pred_tensor.cpu().data
pred_tensor = pred_tensor.squeeze(0) # squeeze batch dimension.
# Get detected boxes_detected, labels, confidences, class-scores.
boxes_normalized_all, class_labels_all, confidences_all, class_scores_all = self.decode(pred_tensor)
if boxes_normalized_all.size(0) == 0:
return [], [], [] # if no box found, return empty lists.
# Apply non maximum supression for boxes of each class.
boxes_normalized, class_labels, probs = [], [], []
for class_label in range(len(self.class_name_list)):
mask = (class_labels_all == class_label)
if torch.sum(mask) == 0:
continue # if no box found, skip that class.
boxes_normalized_masked = boxes_normalized_all[mask]
class_labels_maked = class_labels_all[mask]
confidences_masked = confidences_all[mask]
class_scores_masked = class_scores_all[mask]
ids = self.nms(boxes_normalized_masked, confidences_masked)
boxes_normalized.append(boxes_normalized_masked[ids])
class_labels.append(class_labels_maked[ids])
probs.append(confidences_masked[ids] * class_scores_masked[ids])
boxes_normalized = torch.cat(boxes_normalized, 0)
class_labels = torch.cat(class_labels, 0)
probs = torch.cat(probs, 0)
# Postprocess for box, labels, probs.
boxes_detected, class_names_detected, probs_detected = [], [], []
for b in range(boxes_normalized.size(0)):
box_normalized = boxes_normalized[b]
class_label = class_labels[b]
prob = probs[b]
x1, x2 = w * box_normalized[0], w * box_normalized[2] # unnormalize x with image width.
y1, y2 = h * box_normalized[1], h * box_normalized[3] # unnormalize y with image height.
boxes_detected.append(((x1, y1), (x2, y2)))
class_label = int(class_label) # convert from LongTensor to int.
class_name = self.class_name_list[class_label]
class_names_detected.append(class_name)
prob = float(prob) # convert from Tensor to float.
probs_detected.append(prob)
return boxes_detected, class_names_detected, probs_detected
def decode(self, pred_tensor):
""" Decode tensor into box coordinates, class labels, and probs_detected.
Args:
pred_tensor: (tensor) tensor to decode sized [S, S, 5 x B + C], 5=(x, y, w, h, conf)
Returns:
boxes: (tensor) [[x1, y1, x2, y2]_obj1, ...]. Normalized from 0.0 to 1.0 w.r.t. image width/height, sized [n_boxes, 4].
labels: (tensor) class labels for each detected boxe, sized [n_boxes,].
confidences: (tensor) objectness confidences for each detected box, sized [n_boxes,].
class_scores: (tensor) scores for most likely class for each detected box, sized [n_boxes,].
"""
S, B, C = self.S, self.B, self.C
boxes, labels, confidences, class_scores = [], [], [], []
cell_size = 1.0 / float(S)
conf = pred_tensor[:, :, 4].unsqueeze(2) # [S, S, 1]
for b in range(1, B):
conf = torch.cat((conf, pred_tensor[:, :, 5*b + 4].unsqueeze(2)), 2)
conf_mask = conf > self.conf_thresh # [S, S, B]
# TBM, further optimization may be possible by replacing the following for-loops with tensor operations.
for i in range(S): # for x-dimension.
for j in range(S): # for y-dimension.
class_score, class_label = torch.max(pred_tensor[j, i, 5*B:], 0)
for b in range(B):
conf = pred_tensor[j, i, 5*b + 4]
prob = conf * class_score
if float(prob) < self.prob_thresh:
continue
# Compute box corner (x1, y1, x2, y2) from tensor.
box = pred_tensor[j, i, 5*b : 5*b + 4]
x0y0_normalized = torch.FloatTensor([i, j]) * cell_size # cell left-top corner. Normalized from 0.0 to 1.0 w.r.t. image width/height.
xy_normalized = box[:2] * cell_size + x0y0_normalized # box center. Normalized from 0.0 to 1.0 w.r.t. image width/height.
wh_normalized = box[2:] # Box width and height. Normalized from 0.0 to 1.0 w.r.t. image width/height.
box_xyxy = torch.FloatTensor(4) # [4,]
box_xyxy[:2] = xy_normalized - 0.5 * wh_normalized # left-top corner (x1, y1).
box_xyxy[2:] = xy_normalized + 0.5 * wh_normalized # right-bottom corner (x2, y2).
# Append result to the lists.
boxes.append(box_xyxy)
labels.append(class_label)
confidences.append(conf)
class_scores.append(class_score)
if len(boxes) > 0:
boxes = torch.stack(boxes, 0) # [n_boxes, 4]
labels = torch.stack(labels, 0) # [n_boxes, ]
confidences = torch.stack(confidences, 0) # [n_boxes, ]
class_scores = torch.stack(class_scores, 0) # [n_boxes, ]
else:
# If no box found, return empty tensors.
boxes = torch.FloatTensor(0, 4)
labels = torch.LongTensor(0)
confidences = torch.FloatTensor(0)
class_scores = torch.FloatTensor(0)
return boxes, labels, confidences, class_scores
def nms(self, boxes, scores):
""" Apply non maximum supression.
Args:
Returns:
"""
threshold = self.nms_thresh
x1 = boxes[:, 0] # [n,]
y1 = boxes[:, 1] # [n,]
x2 = boxes[:, 2] # [n,]
y2 = boxes[:, 3] # [n,]
areas = (x2 - x1) * (y2 - y1) # [n,]
_, ids_sorted = scores.sort(0, descending=True) # [n,]
ids = []
while ids_sorted.numel() > 0:
# Assume `ids_sorted` size is [m,] in the beginning of this iter.
i = ids_sorted.item() if (ids_sorted.numel() == 1) else ids_sorted[0]
ids.append(i)
if ids_sorted.numel() == 1:
break # If only one box is left (i.e., no box to supress), break.
inter_x1 = x1[ids_sorted[1:]].clamp(min=x1[i]) # [m-1, ]
inter_y1 = y1[ids_sorted[1:]].clamp(min=y1[i]) # [m-1, ]
inter_x2 = x2[ids_sorted[1:]].clamp(max=x2[i]) # [m-1, ]
inter_y2 = y2[ids_sorted[1:]].clamp(max=y2[i]) # [m-1, ]
inter_w = (inter_x2 - inter_x1).clamp(min=0) # [m-1, ]
inter_h = (inter_y2 - inter_y1).clamp(min=0) # [m-1, ]
inters = inter_w * inter_h # intersections b/w/ box `i` and other boxes, sized [m-1, ].
unions = areas[i] + areas[ids_sorted[1:]] - inters # unions b/w/ box `i` and other boxes, sized [m-1, ].
ious = inters / unions # [m-1, ]
# Remove boxes whose IoU is higher than the threshold.
ids_keep = (ious <= threshold).nonzero().squeeze() # [m-1, ]. Because `nonzero()` adds extra dimension, squeeze it.
if ids_keep.numel() == 0:
break # If no box left, break.
ids_sorted = ids_sorted[ids_keep+1] # `+1` is needed because `ids_sorted[0] = i`.
return torch.LongTensor(ids)
if __name__ == '__main__':
# Paths to input/output images.
image_path = 'data/test_samples/009046.jpg'
out_path = 'result.png'
# Path to the yolo weight.
model_path = 'weights/yolo/model_best.pth'
# GPU device on which yolo is loaded.
gpu_id = 0
# Load model.
yolo = YOLODetector(model_path, gpu_id=gpu_id, conf_thresh=0.1, prob_thresh=0.1, nms_thresh=0.35)
# Load image.
image = cv2.imread(image_path)
# Detect objects.
boxes, class_names, probs = yolo.detect(image)
# Visualize.
image_boxes = visualize_boxes(image, boxes, class_names, probs)
# Output detection result as an image.
cv2.imwrite(out_path, image_boxes)