-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathinference.py
285 lines (232 loc) · 8.95 KB
/
inference.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
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
"""
Run inference on images, videos, directories, streams, etc.
"""
import argparse
import os
import sys
from pathlib import Path
import glob
import cv2
import torch
FILE = Path(__file__).resolve()
ROOT = FILE.parents[0] # YOLOv5 root directory
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT)) # add ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
from models.common import DetectMultiBackend
from utils.datasets import LoadImages
from utils.general import LOGGER, check_img_size, check_requirements, non_max_suppression, print_args, scale_coords
from utils.torch_utils import select_device, time_sync
import torch
import torch.nn as nn
from torchvision.models import resnet18, vgg11
import numpy as np
from script.Dataset import generate_bins, DetectedObject
from library.Math import *
from library.Plotting import *
from script import Model, ClassAverages
from script.Model import ResNet, ResNet18, VGG11
# model factory to choose model
model_factory = {
'resnet': resnet18(pretrained=True),
'resnet18': resnet18(pretrained=True),
# 'vgg11': vgg11(pretrained=True)
}
regressor_factory = {
'resnet': ResNet,
'resnet18': ResNet18,
'vgg11': VGG11
}
class Bbox:
def __init__(self, box_2d, class_):
self.box_2d = box_2d
self.detected_class = class_
def detect3d(
reg_weights,
model_select,
source,
calib_file,
show_result,
save_result,
output_path
):
# Directory
imgs_path = sorted(glob.glob(str(source) + '/*'))
calib = str(calib_file)
# load model
base_model = model_factory[model_select]
regressor = regressor_factory[model_select](model=base_model).cuda()
# load weight
checkpoint = torch.load(reg_weights)
regressor.load_state_dict(checkpoint['model_state_dict'])
regressor.eval()
averages = ClassAverages.ClassAverages()
angle_bins = generate_bins(2)
# loop images
for i, img_path in enumerate(imgs_path):
# read image
img = cv2.imread(img_path)
# Run detection 2d
dets = detect2d(
weights='yolov5s.pt',
source=img_path,
data='data/coco128.yaml',
imgsz=[640, 640],
device=0,
classes=[0, 2, 3, 5]
)
for det in dets:
if not averages.recognized_class(det.detected_class):
continue
try:
detectedObject = DetectedObject(img, det.detected_class, det.box_2d, calib)
except:
continue
theta_ray = detectedObject.theta_ray
input_img = detectedObject.img
proj_matrix = detectedObject.proj_matrix
box_2d = det.box_2d
detected_class = det.detected_class
input_tensor = torch.zeros([1,3,224,224]).cuda()
input_tensor[0,:,:,:] = input_img
# predict orient, conf, and dim
[orient, conf, dim] = regressor(input_tensor)
orient = orient.cpu().data.numpy()[0, :, :]
conf = conf.cpu().data.numpy()[0, :]
dim = dim.cpu().data.numpy()[0, :]
dim += averages.get_item(detected_class)
argmax = np.argmax(conf)
orient = orient[argmax, :]
cos = orient[0]
sin = orient[1]
alpha = np.arctan2(sin, cos)
alpha += angle_bins[argmax]
alpha -= np.pi
# plot 3d detection
plot3d(img, proj_matrix, box_2d, dim, alpha, theta_ray)
if show_result:
cv2.imshow('3d detection', img)
cv2.waitKey(0)
if save_result and output_path is not None:
try:
os.mkdir(output_path)
except:
pass
cv2.imwrite(f'{output_path}/{i:03d}.png', img)
@torch.no_grad()
def detect2d(
weights,
source,
data,
imgsz,
device,
classes
):
# array for boundingbox detection
bbox_list = []
# Directories
source = str(source)
# Load model
device = select_device(device)
model = DetectMultiBackend(weights, device=device, dnn=False, data=data)
stride, names, pt, jit, onnx, engine = model.stride, model.names, model.pt, model.jit, model.onnx, model.engine
imgsz = check_img_size(imgsz, s=stride) # check image size
# Dataloader
dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt)
# Run inference
model.warmup(imgsz=(1, 3, *imgsz), half=False) # warmup
dt, seen = [0.0, 0.0, 0.0], 0
for path, im, im0s, vid_cap, s in dataset:
t1 = time_sync()
im = torch.from_numpy(im).to(device)
im = im.float()
im /= 255 # 0 - 255 to 0.0 - 1.0
if len(im.shape) == 3:
im = im[None] # expand for batch dim
t2 = time_sync()
dt[0] += t2 - t1
# Inference
pred = model(im, augment=False, visualize=False)
t3 = time_sync()
dt[1] += t3 - t2
# NMS
pred = non_max_suppression(prediction=pred, classes=classes)
dt[2] += time_sync() - t3
# Second-stage classifier (optional)
# pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)
# Process predictions
for i, det in enumerate(pred): # per image
seen += 1
p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)
p = Path(p) # to Path
s += '%gx%g ' % im.shape[2:] # print string
if len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round()
# Print results
for c in det[:, -1].unique():
n = (det[:, -1] == c).sum() # detections per class
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
# Write results
for *xyxy, conf, cls in reversed(det):
xyxy_ = (torch.tensor(xyxy).view(1,4)).view(-1).tolist()
xyxy_ = [int(x) for x in xyxy_]
top_left, bottom_right = (xyxy_[0], xyxy_[1]), (xyxy_[2], xyxy_[3])
bbox = [top_left, bottom_right]
c = int(cls) # integer class
label = names[c]
bbox_list.append(Bbox(bbox, label))
# Print time (inference-only)
LOGGER.info(f'{s}Done. ({t3 - t2:.3f}s)')
# Print results
t = tuple(x / seen * 1E3 for x in dt) # speeds per image
LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {(1, 3, *imgsz)}' % t)
return bbox_list
def plot3d(
img,
proj_matrix,
box_2d,
dimensions,
alpha,
theta_ray,
img_2d=None
):
# the math! returns X, the corners used for constraint
location, X = calc_location(dimensions, proj_matrix, box_2d, alpha, theta_ray)
orient = alpha + theta_ray
if img_2d is not None:
plot_2d_box(img_2d, box_2d)
plot_3d_box(img, proj_matrix, orient, dimensions, location) # 3d boxes
return location
def parse_opt():
parser = argparse.ArgumentParser()
parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s.pt', help='model path(s)')
parser.add_argument('--source', type=str, default=ROOT / 'eval/image_2', help='file/dir/URL/glob, 0 for webcam')
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='(optional) dataset.yaml path')
parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--classes', default=[0, 2, 3, 5], nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')
parser.add_argument('--reg_weights', type=str, default='weights/epoch_10.pkl', help='Regressor model weights')
parser.add_argument('--model_select', type=str, default='resnet', help='Regressor model list: resnet, vgg, eff')
parser.add_argument('--calib_file', type=str, default=ROOT / 'eval/camera_cal/calib_cam_to_cam.txt', help='Calibration file or path')
parser.add_argument('--show_result', action='store_true', help='Show Results with imshow')
parser.add_argument('--save_result', action='store_true', help='Save result')
parser.add_argument('--output_path', type=str, default=ROOT / 'output', help='Save output pat')
opt = parser.parse_args()
opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand
print_args(FILE.stem, opt)
return opt
def main(opt):
detect3d(
reg_weights=opt.reg_weights,
model_select=opt.model_select,
source=opt.source,
calib_file=opt.calib_file,
show_result=opt.show_result,
save_result=opt.save_result,
output_path=opt.output_path
)
if __name__ == "__main__":
opt = parse_opt()
main(opt)