Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
7af1b4c2
Unverified
提交
7af1b4c2
authored
9月 10, 2021
作者:
Glenn Jocher
提交者:
GitHub
9月 10, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improved `detect.py` timing (#4741)
* Improved detect.py timing * Eliminate 1 time_sync() call * Inference-only time * dash * #Save section * Cleanup
上级
c5360f6e
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
22 行增加
和
18 行删除
+22
-18
detect.py
detect.py
+14
-10
val.py
val.py
+8
-8
没有找到文件。
detect.py
浏览文件 @
7af1b4c2
...
@@ -8,7 +8,6 @@ Usage:
...
@@ -8,7 +8,6 @@ Usage:
import
argparse
import
argparse
import
sys
import
sys
import
time
from
pathlib
import
Path
from
pathlib
import
Path
import
cv2
import
cv2
...
@@ -123,8 +122,9 @@ def run(weights='yolov5s.pt', # model.pt path(s)
...
@@ -123,8 +122,9 @@ def run(weights='yolov5s.pt', # model.pt path(s)
# Run inference
# Run inference
if
pt
and
device
.
type
!=
'cpu'
:
if
pt
and
device
.
type
!=
'cpu'
:
model
(
torch
.
zeros
(
1
,
3
,
*
imgsz
)
.
to
(
device
)
.
type_as
(
next
(
model
.
parameters
())))
# run once
model
(
torch
.
zeros
(
1
,
3
,
*
imgsz
)
.
to
(
device
)
.
type_as
(
next
(
model
.
parameters
())))
# run once
t0
=
time
.
time
()
dt
,
seen
=
[
0.0
,
0.0
,
0.0
],
0
for
path
,
img
,
im0s
,
vid_cap
in
dataset
:
for
path
,
img
,
im0s
,
vid_cap
in
dataset
:
t1
=
time_sync
()
if
onnx
:
if
onnx
:
img
=
img
.
astype
(
'float32'
)
img
=
img
.
astype
(
'float32'
)
else
:
else
:
...
@@ -133,9 +133,10 @@ def run(weights='yolov5s.pt', # model.pt path(s)
...
@@ -133,9 +133,10 @@ def run(weights='yolov5s.pt', # model.pt path(s)
img
=
img
/
255.0
# 0 - 255 to 0.0 - 1.0
img
=
img
/
255.0
# 0 - 255 to 0.0 - 1.0
if
len
(
img
.
shape
)
==
3
:
if
len
(
img
.
shape
)
==
3
:
img
=
img
[
None
]
# expand for batch dim
img
=
img
[
None
]
# expand for batch dim
t2
=
time_sync
()
dt
[
0
]
+=
t2
-
t1
# Inference
# Inference
t1
=
time_sync
()
if
pt
:
if
pt
:
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
pred
=
model
(
img
,
augment
=
augment
,
visualize
=
visualize
)[
0
]
pred
=
model
(
img
,
augment
=
augment
,
visualize
=
visualize
)[
0
]
...
@@ -162,17 +163,20 @@ def run(weights='yolov5s.pt', # model.pt path(s)
...
@@ -162,17 +163,20 @@ def run(weights='yolov5s.pt', # model.pt path(s)
pred
[
...
,
2
]
*=
imgsz
[
1
]
# w
pred
[
...
,
2
]
*=
imgsz
[
1
]
# w
pred
[
...
,
3
]
*=
imgsz
[
0
]
# h
pred
[
...
,
3
]
*=
imgsz
[
0
]
# h
pred
=
torch
.
tensor
(
pred
)
pred
=
torch
.
tensor
(
pred
)
t3
=
time_sync
()
dt
[
1
]
+=
t3
-
t2
# 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
()
dt
[
2
]
+=
time_sync
()
-
t3
# Second-stage classifier (optional)
# Second-stage classifier (optional)
if
classify
:
if
classify
:
pred
=
apply_classifier
(
pred
,
modelc
,
img
,
im0s
)
pred
=
apply_classifier
(
pred
,
modelc
,
img
,
im0s
)
# Process predictions
# Process predictions
for
i
,
det
in
enumerate
(
pred
):
# detections per image
for
i
,
det
in
enumerate
(
pred
):
# per image
seen
+=
1
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
else
:
else
:
...
@@ -209,8 +213,8 @@ def run(weights='yolov5s.pt', # model.pt path(s)
...
@@ -209,8 +213,8 @@ def run(weights='yolov5s.pt', # model.pt path(s)
if
save_crop
:
if
save_crop
:
save_one_box
(
xyxy
,
imc
,
file
=
save_dir
/
'crops'
/
names
[
c
]
/
f
'{p.stem}.jpg'
,
BGR
=
True
)
save_one_box
(
xyxy
,
imc
,
file
=
save_dir
/
'crops'
/
names
[
c
]
/
f
'{p.stem}.jpg'
,
BGR
=
True
)
# Print time (inference
+ NMS
)
# Print time (inference
-only
)
print
(
f
'{s}Done. ({t
2 - t1
:.3f}s)'
)
print
(
f
'{s}Done. ({t
3 - t2
:.3f}s)'
)
# Stream results
# Stream results
im0
=
annotator
.
result
()
im0
=
annotator
.
result
()
...
@@ -237,15 +241,15 @@ def run(weights='yolov5s.pt', # model.pt path(s)
...
@@ -237,15 +241,15 @@ def run(weights='yolov5s.pt', # model.pt path(s)
vid_writer
[
i
]
=
cv2
.
VideoWriter
(
save_path
,
cv2
.
VideoWriter_fourcc
(
*
'mp4v'
),
fps
,
(
w
,
h
))
vid_writer
[
i
]
=
cv2
.
VideoWriter
(
save_path
,
cv2
.
VideoWriter_fourcc
(
*
'mp4v'
),
fps
,
(
w
,
h
))
vid_writer
[
i
]
.
write
(
im0
)
vid_writer
[
i
]
.
write
(
im0
)
# Print results
t
=
tuple
(
x
/
seen
*
1E3
for
x
in
dt
)
# speeds per image
print
(
f
'Speed:
%.1
fms pre-process,
%.1
fms inference,
%.1
fms NMS per image at shape {(1, 3, *imgsz)}'
%
t
)
if
save_txt
or
save_img
:
if
save_txt
or
save_img
:
s
=
f
"
\n
{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}"
if
save_txt
else
''
s
=
f
"
\n
{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}"
if
save_txt
else
''
print
(
f
"Results saved to {colorstr('bold', save_dir)}{s}"
)
print
(
f
"Results saved to {colorstr('bold', save_dir)}{s}"
)
if
update
:
if
update
:
strip_optimizer
(
weights
)
# update model (to fix SourceChangeWarning)
strip_optimizer
(
weights
)
# update model (to fix SourceChangeWarning)
print
(
f
'Done. ({time.time() - t0:.3f}s)'
)
def
parse_opt
():
def
parse_opt
():
parser
=
argparse
.
ArgumentParser
()
parser
=
argparse
.
ArgumentParser
()
...
...
val.py
浏览文件 @
7af1b4c2
...
@@ -154,22 +154,22 @@ def run(data,
...
@@ -154,22 +154,22 @@ def run(data,
names
=
{
k
:
v
for
k
,
v
in
enumerate
(
model
.
names
if
hasattr
(
model
,
'names'
)
else
model
.
module
.
names
)}
names
=
{
k
:
v
for
k
,
v
in
enumerate
(
model
.
names
if
hasattr
(
model
,
'names'
)
else
model
.
module
.
names
)}
class_map
=
coco80_to_coco91_class
()
if
is_coco
else
list
(
range
(
1000
))
class_map
=
coco80_to_coco91_class
()
if
is_coco
else
list
(
range
(
1000
))
s
=
(
'
%20
s'
+
'
%11
s'
*
6
)
%
(
'Class'
,
'Images'
,
'Labels'
,
'P'
,
'R'
,
'mAP@.5'
,
'mAP@.5:.95'
)
s
=
(
'
%20
s'
+
'
%11
s'
*
6
)
%
(
'Class'
,
'Images'
,
'Labels'
,
'P'
,
'R'
,
'mAP@.5'
,
'mAP@.5:.95'
)
p
,
r
,
f1
,
mp
,
mr
,
map50
,
map
,
t0
,
t1
,
t2
=
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
dt
,
p
,
r
,
f1
,
mp
,
mr
,
map50
,
map
=
[
0.0
,
0.0
,
0.0
],
0.0
,
0.0
,
0.0
,
0.0
,
0.0
,
0.0
,
0.0
loss
=
torch
.
zeros
(
3
,
device
=
device
)
loss
=
torch
.
zeros
(
3
,
device
=
device
)
jdict
,
stats
,
ap
,
ap_class
=
[],
[],
[],
[]
jdict
,
stats
,
ap
,
ap_class
=
[],
[],
[],
[]
for
batch_i
,
(
img
,
targets
,
paths
,
shapes
)
in
enumerate
(
tqdm
(
dataloader
,
desc
=
s
)):
for
batch_i
,
(
img
,
targets
,
paths
,
shapes
)
in
enumerate
(
tqdm
(
dataloader
,
desc
=
s
)):
t
_
=
time_sync
()
t
1
=
time_sync
()
img
=
img
.
to
(
device
,
non_blocking
=
True
)
img
=
img
.
to
(
device
,
non_blocking
=
True
)
img
=
img
.
half
()
if
half
else
img
.
float
()
# uint8 to fp16/32
img
=
img
.
half
()
if
half
else
img
.
float
()
# uint8 to fp16/32
img
/=
255.0
# 0 - 255 to 0.0 - 1.0
img
/=
255.0
# 0 - 255 to 0.0 - 1.0
targets
=
targets
.
to
(
device
)
targets
=
targets
.
to
(
device
)
nb
,
_
,
height
,
width
=
img
.
shape
# batch size, channels, height, width
nb
,
_
,
height
,
width
=
img
.
shape
# batch size, channels, height, width
t
=
time_sync
()
t
2
=
time_sync
()
t0
+=
t
-
t_
dt
[
0
]
+=
t2
-
t1
# Run model
# Run model
out
,
train_out
=
model
(
img
,
augment
=
augment
)
# inference and training outputs
out
,
train_out
=
model
(
img
,
augment
=
augment
)
# inference and training outputs
t1
+=
time_sync
()
-
t
dt
[
1
]
+=
time_sync
()
-
t2
# Compute loss
# Compute loss
if
compute_loss
:
if
compute_loss
:
...
@@ -178,9 +178,9 @@ def run(data,
...
@@ -178,9 +178,9 @@ def run(data,
# Run NMS
# Run NMS
targets
[:,
2
:]
*=
torch
.
Tensor
([
width
,
height
,
width
,
height
])
.
to
(
device
)
# to pixels
targets
[:,
2
:]
*=
torch
.
Tensor
([
width
,
height
,
width
,
height
])
.
to
(
device
)
# to pixels
lb
=
[
targets
[
targets
[:,
0
]
==
i
,
1
:]
for
i
in
range
(
nb
)]
if
save_hybrid
else
[]
# for autolabelling
lb
=
[
targets
[
targets
[:,
0
]
==
i
,
1
:]
for
i
in
range
(
nb
)]
if
save_hybrid
else
[]
# for autolabelling
t
=
time_sync
()
t
3
=
time_sync
()
out
=
non_max_suppression
(
out
,
conf_thres
,
iou_thres
,
labels
=
lb
,
multi_label
=
True
,
agnostic
=
single_cls
)
out
=
non_max_suppression
(
out
,
conf_thres
,
iou_thres
,
labels
=
lb
,
multi_label
=
True
,
agnostic
=
single_cls
)
t2
+=
time_sync
()
-
t
dt
[
2
]
+=
time_sync
()
-
t3
# Statistics per image
# Statistics per image
for
si
,
pred
in
enumerate
(
out
):
for
si
,
pred
in
enumerate
(
out
):
...
@@ -247,7 +247,7 @@ def run(data,
...
@@ -247,7 +247,7 @@ def run(data,
print
(
pf
%
(
names
[
c
],
seen
,
nt
[
c
],
p
[
i
],
r
[
i
],
ap50
[
i
],
ap
[
i
]))
print
(
pf
%
(
names
[
c
],
seen
,
nt
[
c
],
p
[
i
],
r
[
i
],
ap50
[
i
],
ap
[
i
]))
# Print speeds
# Print speeds
t
=
tuple
(
x
/
seen
*
1E3
for
x
in
(
t0
,
t1
,
t2
)
)
# speeds per image
t
=
tuple
(
x
/
seen
*
1E3
for
x
in
dt
)
# speeds per image
if
not
training
:
if
not
training
:
shape
=
(
batch_size
,
3
,
imgsz
,
imgsz
)
shape
=
(
batch_size
,
3
,
imgsz
,
imgsz
)
print
(
f
'Speed:
%.1
fms pre-process,
%.1
fms inference,
%.1
fms NMS per image at shape {shape}'
%
t
)
print
(
f
'Speed:
%.1
fms pre-process,
%.1
fms inference,
%.1
fms NMS per image at shape {shape}'
%
t
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论