-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicleDC.py
383 lines (318 loc) · 12.7 KB
/
VehicleDC.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# coding: utf-8
import os
import sys
import re
import time
import pickle
import shutil
import random
import argparse
from darknet_util import *
from darknet import Darknet
from preprocess import prep_image, process_img, inp_to_image
from dataset import color_attrs, direction_attrs, type_attrs
import torch
import torchvision
import paramiko
import cv2
import numpy as np
import PIL
from PIL import Image
from matplotlib import pyplot as plt
from matplotlib.widgets import Cursor
from matplotlib.image import AxesImage
from scipy.spatial.distance import cityblock
from tqdm import tqdm
# -------------------------------------
# for matplotlib to displacy chinese characters correctly
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
use_cuda = True # True
os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
device = torch.device(
'cuda: 0' if torch.cuda.is_available() and use_cuda else 'cpu')
if use_cuda:
torch.manual_seed(0)
torch.cuda.manual_seed_all(0)
print('=> device: ', device)
local_model_path = './checkpoints/epoch_39.pth'
local_car_cfg_path = './car.cfg'
local_car_det_weights_path = './car_detect.weights'
class Cls_Net(torch.nn.Module):
"""
vehicle multilabel classification model
"""
def __init__(self, num_cls, input_size):
"""
network definition
:param is_freeze:
"""
torch.nn.Module.__init__(self)
# output channels
self._num_cls = num_cls
# input image size
self.input_size = input_size
# delete original FC and add custom FC
self.features = torchvision.models.resnet18(pretrained=True)
del self.features.fc
# print('feature extractor:\n', self.features)
self.features = torch.nn.Sequential(
*list(self.features.children()))
self.fc = torch.nn.Linear(512 ** 2, num_cls) # 输出类别数
# print('=> fc layer:\n', self.fc)
def forward(self, X):
"""
:param X:
:return:
"""
N = X.size()[0]
X = self.features(X) # extract features
X = X.view(N, 512, 1 ** 2)
X = torch.bmm(X, torch.transpose(X, 1, 2)) / (1 ** 2) # Bi-linear CNN
X = X.view(N, 512 ** 2)
X = torch.sqrt(X + 1e-5)
X = torch.nn.functional.normalize(X)
X = self.fc(X)
assert X.size() == (N, self._num_cls)
return X
# ------------------------------------- vehicle detection model
class Car_Classifier(object):
"""
vehicle detection model mabager
"""
def __init__(self,
num_cls,
model_path=local_model_path):
"""
load model and initialize
"""
# define model and load weights
self.net = Cls_Net(num_cls=num_cls, input_size=224).to(device)
# self.net = torch.nn.DataParallel(Net(num_cls=20, input_size=224),
# device_ids=[0]).to(device)
self.net.load_state_dict(torch.load(model_path))
print('=> vehicle classifier loaded from %s' % model_path)
# set model to eval mode
self.net.eval()
# test data transforms
self.transforms = torchvision.transforms.Compose([
torchvision.transforms.Resize(size=224),
torchvision.transforms.CenterCrop(size=224),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225))
])
# split each label
self.color_attrs = color_attrs
print('=> color_attrs:\n', self.color_attrs)
self.direction_attrs = direction_attrs
print('=> direction attrs:\n', self.direction_attrs)
self.type_attrs = type_attrs
print('=> type_attrs:\n', self.type_attrs)
def get_predict(self, output):
"""
get prediction from output
"""
# get each label's prediction from output
output = output.cpu() # fetch data from gpu
pred_color = output[:, :9]
pred_direction = output[:, 9:11]
pred_type = output[:, 11:]
color_idx = pred_color.max(1, keepdim=True)[1]
direction_idx = pred_direction.max(1, keepdim=True)[1]
type_idx = pred_type.max(1, keepdim=True)[1]
pred = torch.cat((color_idx, direction_idx, type_idx), dim=1)
return pred
def pre_process(self, image):
"""
image formatting
:rtype: PIL.JpegImagePlugin.JpegImageFile
"""
# image data formatting
if type(image) == np.ndarray:
if image.shape[2] == 3: # turn all 3 channels to RGB format
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
elif image.shape[2] == 1: # turn 1 channel to RGB
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
# turn numpy.ndarray into PIL.Image
image = Image.fromarray(image)
elif type(image) == PIL.JpegImagePlugin.JpegImageFile:
if image.mode == 'L' or image.mode == 'I': # turn 8bits or 32bits into 3 channels RGB
image = image.convert('RGB')
return image
def predict(self, img):
"""
predict vehicle attributes by classifying
:return: vehicle color, direction and type
"""
# image pre-processing
img = self.transforms(img)
img = img.view(1, 3, 224, 224)
# put image data into device
img = img.to(device)
# calculating inference
output = self.net.forward(img)
# get result
# self.get_predict_ce, return pred to host side(cpu)
pred = self.get_predict(output)
color_name = self.color_attrs[pred[0][0]]
direction_name = self.direction_attrs[pred[0][1]]
type_name = self.type_attrs[pred[0][2]]
return color_name, direction_name, type_name
class Car_DC():
def __init__(self,
src_dir,
dst_dir,
car_cfg_path=local_car_cfg_path,
car_det_weights_path=local_car_det_weights_path,
inp_dim=768,
prob_th=0.2,
nms_th=0.4,
num_classes=1):
"""
model initialization
"""
# super parameters
self.inp_dim = inp_dim
self.prob_th = prob_th
self.nms_th = nms_th
self.num_classes = num_classes
self.dst_dir = dst_dir
# clear dst_dir
if os.path.exists(self.dst_dir):
for x in os.listdir(self.dst_dir):
if x.endswith('.jpg'):
os.remove(self.dst_dir + '/' + x)
else:
os.makedirs(self.dst_dir)
# initialize vehicle detection model
self.detector = Darknet(car_cfg_path)
self.detector.load_weights(car_det_weights_path)
# set input dimension of image
self.detector.net_info['height'] = self.inp_dim
self.detector.to(device)
self.detector.eval() # evaluation mode
print('=> car detection model initiated.')
# initiate multilabel classifier
self.classifier = Car_Classifier(num_cls=19,
model_path=local_model_path)
# initiate imgs_path
self.imgs_path = [os.path.join(src_dir, x) for x in os.listdir(
src_dir) if x.endswith('.jpg')]
def cls_draw_bbox(self, output, orig_img):
"""
1. predict vehicle's attributes based on bbox of vehicle
2. draw bbox to orig_img
"""
labels = []
pt_1s = []
pt_2s = []
# 1
for det in output:
# rectangle points
pt_1 = tuple(det[1:3].int()) # the left-up point
pt_2 = tuple(det[3:5].int()) # the right down point
pt_1s.append(pt_1)
pt_2s.append(pt_2)
# turn BGR back to RGB
ROI = Image.fromarray(
orig_img[pt_1[1]: pt_2[1],
pt_1[0]: pt_2[0]][:, :, ::-1])
# ROI.show()
# call classifier to predict
car_color, car_direction, car_type = self.classifier.predict(ROI)
label = str(car_color + ' ' + car_direction + ' ' + car_type)
labels.append(label)
print('=> predicted label: ', label)
# 2
color = (0, 215, 255)
for i, det in enumerate(output):
pt_1 = pt_1s[i]
pt_2 = pt_2s[i]
# draw bounding box
cv2.rectangle(orig_img, pt_1, pt_2, color, thickness=2)
# get str text size
txt_size = cv2.getTextSize(
label, cv2.FONT_HERSHEY_PLAIN, 2, 2)[0]
# pt_2 = pt_1[0] + txt_size[0] + 3, pt_1[1] + txt_size[1] + 5
pt_2 = pt_1[0] + txt_size[0] + 3, pt_1[1] - txt_size[1] - 5
# draw text background rect
cv2.rectangle(orig_img, pt_1, pt_2, color, thickness=-1) # text
# draw text
cv2.putText(orig_img, labels[i], (pt_1[0], pt_1[1]), # pt_1[1] + txt_size[1] + 4
cv2.FONT_HERSHEY_PLAIN, 2, [225, 255, 255], 2)
def process_predict(self,
prediction,
prob_th,
num_cls,
nms_th,
inp_dim,
orig_img_size):
"""
processing detections
"""
scaling_factor = min([inp_dim / float(x)
for x in orig_img_size]) # W, H scaling factor
output = post_process(prediction,
prob_th,
num_cls,
nms=True,
nms_conf=nms_th,
CUDA=True) # post-process such as nms
if type(output) != int:
output[:, [1, 3]] -= (inp_dim - scaling_factor *
orig_img_size[0]) / 2.0 # x, w
output[:, [2, 4]] -= (inp_dim - scaling_factor *
orig_img_size[1]) / 2.0 # y, h
output[:, 1:5] /= scaling_factor
for i in range(output.shape[0]):
output[i, [1, 3]] = torch.clamp(
output[i, [1, 3]], 0.0, orig_img_size[0])
output[i, [2, 4]] = torch.clamp(
output[i, [2, 4]], 0.0, orig_img_size[1])
return output
def detect_classify(self):
"""
detect and classify
"""
for x in self.imgs_path:
# read image data
img = Image.open(x)
img2det = process_img(img, self.inp_dim)
img2det = img2det.to(device) # put image data to device
# vehicle detection
prediction = self.detector.forward(img2det, CUDA=True)
# calculating scaling factor
orig_img_size = list(img.size)
output = self.process_predict(prediction,
self.prob_th,
self.num_classes,
self.nms_th,
self.inp_dim,
orig_img_size)
orig_img = cv2.cvtColor(np.asarray(
img), cv2.COLOR_RGB2BGR) # RGB => BGR
if type(output) != int:
self.cls_draw_bbox(output, orig_img)
dst_path = self.dst_dir + '/' + os.path.split(x)[1]
if not os.path.exists(dst_path):
cv2.imwrite(dst_path, orig_img)
# -----------------------------------------------------------
parser = argparse.ArgumentParser(description='Detect and classify cars.')
parser.add_argument('-src-dir',
type=str,
default='./test_imgs',
help='source directory of images')
parser.add_argument('-dst-dir',
type=str,
default='./test_result',
help='destination directory of images to store results.')
if __name__ == '__main__':
# ---------------------------- Car detect and classify
# DR_model = Car_DC(src_dir='./test_imgs',
# dst_dir='./test_result')
# DR_model.detect_classify()
args = parser.parse_args()
DR_model = Car_DC(src_dir=args.src_dir, dst_dir=args.dst_dir)
DR_model.detect_classify()