forked from feiyuhuahuo/Yolact_minimal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_with_onnx.py
152 lines (123 loc) · 6.57 KB
/
detect_with_onnx.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import onnxruntime as ort
import argparse
import cv2
import time
import re
import torch.utils.data as data
import pdb
from config import get_config
from utils.coco import COCODetection, detect_onnx_collate
from utils import timer
from utils.output_utils import nms_numpy, after_nms_numpy, draw_img
from utils.common_utils import ProgressBar
from utils.augmentations import val_aug
parser = argparse.ArgumentParser(description='YOLACT Detection.')
parser.add_argument('--weight', default='onnx_files/res101_coco.onnx', type=str)
parser.add_argument('--image', default=None, type=str, help='The folder of images for detecting.')
parser.add_argument('--video', default=None, type=str, help='The path of the video to evaluate.')
parser.add_argument('--img_size', type=int, default=550, help='The image size for validation.')
parser.add_argument('--traditional_nms', default=False, action='store_true', help='Whether to use traditional nms.')
parser.add_argument('--hide_mask', default=False, action='store_true', help='Hide masks in results.')
parser.add_argument('--hide_bbox', default=False, action='store_true', help='Hide boxes in results.')
parser.add_argument('--hide_score', default=False, action='store_true', help='Hide scores in results.')
parser.add_argument('--cutout', default=False, action='store_true', help='Cut out each object and save.')
parser.add_argument('--save_lincomb', default=False, action='store_true', help='Show the generating process of masks.')
parser.add_argument('--no_crop', default=False, action='store_true',
help='Do not crop the output masks with the predicted bounding box.')
parser.add_argument('--real_time', default=False, action='store_true', help='Show the detection results real-timely.')
parser.add_argument('--visual_thre', default=0.3, type=float,
help='Detections with a score under this threshold will be removed.')
args = parser.parse_args()
args.cfg = re.findall(r'res.+_[a-z]+', args.weight)[0]
cfg = get_config(args, mode='detect')
sess = ort.InferenceSession(cfg.weight)
input_name = sess.get_inputs()[0].name
# detect images
if cfg.image is not None:
dataset = COCODetection(cfg, mode='detect')
data_loader = data.DataLoader(dataset, 1, num_workers=4, shuffle=False,
pin_memory=True, collate_fn=detect_onnx_collate)
ds = len(data_loader)
assert ds > 0, 'No .jpg images found.'
progress_bar = ProgressBar(40, ds)
timer.reset()
for i, (img, img_origin, img_name) in enumerate(data_loader):
if i == 1:
timer.start()
img_h, img_w = img_origin.shape[0:2]
with timer.counter('forward'):
class_p, box_p, coef_p, proto_p, anchors = sess.run(None, {input_name: img})
with timer.counter('nms'):
ids_p, class_p, box_p, coef_p, proto_p = nms_numpy(class_p, box_p, coef_p, proto_p, anchors, cfg)
with timer.counter('after_nms'):
ids_p, class_p, boxes_p, masks_p = after_nms_numpy(ids_p, class_p, box_p, coef_p,
proto_p, img_h, img_w, cfg)
with timer.counter('save_img'):
img_numpy = draw_img(ids_p, class_p, boxes_p, masks_p, img_origin, cfg, img_name=img_name)
cv2.imwrite(f'results/images/{img_name}', img_numpy)
aa = time.perf_counter()
if i > 0:
batch_time = aa - temp
timer.add_batch_time(batch_time)
temp = aa
if i > 0:
t_t, t_d, t_f, t_nms, t_an, t_si = timer.get_times(['batch', 'data', 'forward',
'nms', 'after_nms', 'save_img'])
fps, t_fps = 1 / (t_d + t_f + t_nms + t_an), 1 / t_t
bar_str = progress_bar.get_bar(i + 1)
print(f'\rTesting: {bar_str} {i + 1}/{ds}, fps: {fps:.2f} | total fps: {t_fps:.2f} | '
f't_t: {t_t:.3f} | t_d: {t_d:.3f} | t_f: {t_f:.3f} | t_nms: {t_nms:.3f} | '
f't_after_nms: {t_an:.3f} | t_save_img: {t_si:.3f}', end='')
print('\nFinished, saved in: results/images.')
# detect videos
elif cfg.video is not None:
vid = cv2.VideoCapture(cfg.video)
target_fps = round(vid.get(cv2.CAP_PROP_FPS))
frame_width = round(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = round(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
num_frames = round(vid.get(cv2.CAP_PROP_FRAME_COUNT))
name = cfg.video.split('/')[-1]
video_writer = cv2.VideoWriter(f'results/videos/{name}', cv2.VideoWriter_fourcc(*"mp4v"), target_fps,
(frame_width, frame_height))
progress_bar = ProgressBar(40, num_frames)
timer.reset()
t_fps = 0
for i in range(num_frames):
if i == 1:
timer.start()
frame_origin = vid.read()[1]
img_h, img_w = frame_origin.shape[0:2]
frame_trans = val_aug(frame_origin, cfg.img_size)[None, :]
with timer.counter('forward'):
class_p, box_p, coef_p, proto_p, anchors = sess.run(None, {input_name: frame_trans})
with timer.counter('nms'):
ids_p, class_p, box_p, coef_p, proto_p = nms_numpy(class_p, box_p, coef_p, proto_p, anchors, cfg)
with timer.counter('after_nms'):
ids_p, class_p, boxes_p, masks_p = after_nms_numpy(ids_p, class_p, box_p, coef_p,
proto_p, img_h, img_w, cfg)
with timer.counter('save_img'):
frame_numpy = draw_img(ids_p, class_p, boxes_p, masks_p, frame_origin, cfg, fps=t_fps)
if cfg.real_time:
cv2.imshow('Detection', frame_numpy)
cv2.waitKey(1)
else:
video_writer.write(frame_numpy)
aa = time.perf_counter()
if i > 0:
batch_time = aa - temp
timer.add_batch_time(batch_time)
temp = aa
if i > 0:
t_t, t_d, t_f, t_nms, t_an, t_si = timer.get_times(['batch', 'data', 'forward',
'nms', 'after_nms', 'save_img'])
fps, t_fps = 1 / (t_d + t_f + t_nms + t_an), 1 / t_t
bar_str = progress_bar.get_bar(i + 1)
print(f'\rDetecting: {bar_str} {i + 1}/{num_frames}, fps: {fps:.2f} | total fps: {t_fps:.2f} | '
f't_t: {t_t:.3f} | t_d: {t_d:.3f} | t_f: {t_f:.3f} | t_nms: {t_nms:.3f} | '
f't_after_nms: {t_an:.3f} | t_save_img: {t_si:.3f}', end='')
if not cfg.real_time:
print(f'\n\nFinished, saved in: results/videos/{name}')
vid.release()
video_writer.release()