Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
2e538443
Unverified
提交
2e538443
authored
7月 24, 2021
作者:
Glenn Jocher
提交者:
GitHub
7月 24, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ONNX inference update (#4073)
上级
39ef6c7a
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
32 行增加
和
22 行删除
+32
-22
detect.py
detect.py
+32
-22
没有找到文件。
detect.py
浏览文件 @
2e538443
...
@@ -64,18 +64,23 @@ def run(weights='yolov5s.pt', # model.pt path(s)
...
@@ -64,18 +64,23 @@ def run(weights='yolov5s.pt', # model.pt path(s)
half
&=
device
.
type
!=
'cpu'
# half precision only supported on CUDA
half
&=
device
.
type
!=
'cpu'
# half precision only supported on CUDA
# Load model
# Load model
model
=
attempt_load
(
weights
,
map_location
=
device
)
# load FP32 model
w
=
weights
[
0
]
if
isinstance
(
weights
,
list
)
else
weights
stride
=
int
(
model
.
stride
.
max
())
# model stride
classify
,
pt
,
onnx
=
False
,
w
.
endswith
(
'.pt'
),
w
.
endswith
(
'.onnx'
)
# inference type
stride
,
names
=
64
,
[
f
'class{i}'
for
i
in
range
(
1000
)]
# assign defaults
if
pt
:
model
=
attempt_load
(
weights
,
map_location
=
device
)
# load FP32 model
stride
=
int
(
model
.
stride
.
max
())
# model stride
names
=
model
.
module
.
names
if
hasattr
(
model
,
'module'
)
else
model
.
names
# get class names
if
half
:
model
.
half
()
# to FP16
if
classify
:
# second-stage classifier
modelc
=
load_classifier
(
name
=
'resnet50'
,
n
=
2
)
# initialize
modelc
.
load_state_dict
(
torch
.
load
(
'resnet50.pt'
,
map_location
=
device
)[
'model'
])
.
to
(
device
)
.
eval
()
elif
onnx
:
check_requirements
((
'onnx'
,
'onnxruntime'
))
import
onnxruntime
session
=
onnxruntime
.
InferenceSession
(
w
,
None
)
imgsz
=
check_img_size
(
imgsz
,
s
=
stride
)
# check image size
imgsz
=
check_img_size
(
imgsz
,
s
=
stride
)
# check image size
names
=
model
.
module
.
names
if
hasattr
(
model
,
'module'
)
else
model
.
names
# get class names
if
half
:
model
.
half
()
# to FP16
# Second-stage classifier
classify
=
False
if
classify
:
modelc
=
load_classifier
(
name
=
'resnet50'
,
n
=
2
)
# initialize
modelc
.
load_state_dict
(
torch
.
load
(
'resnet50.pt'
,
map_location
=
device
)[
'model'
])
.
to
(
device
)
.
eval
()
# Dataloader
# Dataloader
if
webcam
:
if
webcam
:
...
@@ -89,31 +94,36 @@ def run(weights='yolov5s.pt', # model.pt path(s)
...
@@ -89,31 +94,36 @@ def run(weights='yolov5s.pt', # model.pt path(s)
vid_path
,
vid_writer
=
[
None
]
*
bs
,
[
None
]
*
bs
vid_path
,
vid_writer
=
[
None
]
*
bs
,
[
None
]
*
bs
# Run inference
# Run inference
if
device
.
type
!=
'cpu'
:
if
pt
and
device
.
type
!=
'cpu'
:
model
(
torch
.
zeros
(
1
,
3
,
imgsz
,
imgsz
)
.
to
(
device
)
.
type_as
(
next
(
model
.
parameters
())))
# run once
model
(
torch
.
zeros
(
1
,
3
,
imgsz
,
imgsz
)
.
to
(
device
)
.
type_as
(
next
(
model
.
parameters
())))
# run once
t0
=
time
.
time
()
t0
=
time
.
time
()
for
path
,
img
,
im0s
,
vid_cap
in
dataset
:
for
path
,
img
,
im0s
,
vid_cap
in
dataset
:
img
=
torch
.
from_numpy
(
img
)
.
to
(
device
)
if
pt
:
img
=
img
.
half
()
if
half
else
img
.
float
()
# uint8 to fp16/32
img
=
torch
.
from_numpy
(
img
)
.
to
(
device
)
img
=
img
.
half
()
if
half
else
img
.
float
()
# uint8 to fp16/32
elif
onnx
:
img
=
img
.
astype
(
'float32'
)
img
/=
255.0
# 0 - 255 to 0.0 - 1.0
img
/=
255.0
# 0 - 255 to 0.0 - 1.0
if
img
.
ndimension
(
)
==
3
:
if
len
(
img
.
shape
)
==
3
:
img
=
img
.
unsqueeze
(
0
)
img
=
img
[
None
]
# expand for batch dim
# Inference
# Inference
t1
=
time_sync
()
t1
=
time_sync
()
pred
=
model
(
img
,
if
pt
:
augment
=
augment
,
visualize
=
increment_path
(
save_dir
/
Path
(
path
)
.
stem
,
mkdir
=
True
)
if
visualize
else
False
visualize
=
increment_path
(
save_dir
/
Path
(
path
)
.
stem
,
mkdir
=
True
)
if
visualize
else
False
)[
0
]
pred
=
model
(
img
,
augment
=
augment
,
visualize
=
visualize
)[
0
]
elif
onnx
:
pred
=
torch
.
tensor
(
session
.
run
([
session
.
get_outputs
()[
0
]
.
name
],
{
session
.
get_inputs
()[
0
]
.
name
:
img
}))
#
Apply
NMS
# NMS
pred
=
non_max_suppression
(
pred
,
conf_thres
,
iou_thres
,
classes
,
agnostic_nms
,
max_det
=
max_det
)
pred
=
non_max_suppression
(
pred
,
conf_thres
,
iou_thres
,
classes
,
agnostic_nms
,
max_det
=
max_det
)
t2
=
time_sync
()
t2
=
time_sync
()
#
Apply Classifier
#
Second-stage classifier (optional)
if
classify
:
if
classify
:
pred
=
apply_classifier
(
pred
,
modelc
,
img
,
im0s
)
pred
=
apply_classifier
(
pred
,
modelc
,
img
,
im0s
)
# Process
dete
ctions
# Process
predi
ctions
for
i
,
det
in
enumerate
(
pred
):
# detections per image
for
i
,
det
in
enumerate
(
pred
):
# detections per image
if
webcam
:
# batch_size >= 1
if
webcam
:
# batch_size >= 1
p
,
s
,
im0
,
frame
=
path
[
i
],
f
'{i}: '
,
im0s
[
i
]
.
copy
(),
dataset
.
count
p
,
s
,
im0
,
frame
=
path
[
i
],
f
'{i}: '
,
im0s
[
i
]
.
copy
(),
dataset
.
count
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论