Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
0dc725e3
Unverified
提交
0dc725e3
authored
9月 16, 2021
作者:
Glenn Jocher
提交者:
GitHub
9月 16, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor `forward()` method profiling (#4816)
上级
621b6d5b
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
19 行增加
和
22 行删除
+19
-22
yolo.py
models/yolo.py
+19
-22
没有找到文件。
models/yolo.py
浏览文件 @
0dc725e3
...
...
@@ -98,7 +98,6 @@ class Model(nn.Module):
self
.
model
,
self
.
save
=
parse_model
(
deepcopy
(
self
.
yaml
),
ch
=
[
ch
])
# model, savelist
self
.
names
=
[
str
(
i
)
for
i
in
range
(
self
.
yaml
[
'nc'
])]
# default names
self
.
inplace
=
self
.
yaml
.
get
(
'inplace'
,
True
)
# LOGGER.info([x.shape for x in self.forward(torch.zeros(1, ch, 64, 64))])
# Build strides, anchors
m
=
self
.
model
[
-
1
]
# Detect()
...
...
@@ -110,7 +109,6 @@ class Model(nn.Module):
check_anchor_order
(
m
)
self
.
stride
=
m
.
stride
self
.
_initialize_biases
()
# only run once
# LOGGER.info('Strides: %s' % m.stride.tolist())
# Init weights, biases
initialize_weights
(
self
)
...
...
@@ -119,47 +117,33 @@ class Model(nn.Module):
def
forward
(
self
,
x
,
augment
=
False
,
profile
=
False
,
visualize
=
False
):
if
augment
:
return
self
.
forward_augment
(
x
)
# augmented inference, None
return
self
.
forward_once
(
x
,
profile
,
visualize
)
# single-scale inference, train
return
self
.
_
forward_augment
(
x
)
# augmented inference, None
return
self
.
_
forward_once
(
x
,
profile
,
visualize
)
# single-scale inference, train
def
forward_augment
(
self
,
x
):
def
_
forward_augment
(
self
,
x
):
img_size
=
x
.
shape
[
-
2
:]
# height, width
s
=
[
1
,
0.83
,
0.67
]
# scales
f
=
[
None
,
3
,
None
]
# flips (2-ud, 3-lr)
y
=
[]
# outputs
for
si
,
fi
in
zip
(
s
,
f
):
xi
=
scale_img
(
x
.
flip
(
fi
)
if
fi
else
x
,
si
,
gs
=
int
(
self
.
stride
.
max
()))
yi
=
self
.
forward_once
(
xi
)[
0
]
# forward
yi
=
self
.
_
forward_once
(
xi
)[
0
]
# forward
# cv2.imwrite(f'img_{si}.jpg', 255 * xi[0].cpu().numpy().transpose((1, 2, 0))[:, :, ::-1]) # save
yi
=
self
.
_descale_pred
(
yi
,
fi
,
si
,
img_size
)
y
.
append
(
yi
)
return
torch
.
cat
(
y
,
1
),
None
# augmented inference, train
def
forward_once
(
self
,
x
,
profile
=
False
,
visualize
=
False
):
def
_
forward_once
(
self
,
x
,
profile
=
False
,
visualize
=
False
):
y
,
dt
=
[],
[]
# outputs
for
m
in
self
.
model
:
if
m
.
f
!=
-
1
:
# if not from previous layer
x
=
y
[
m
.
f
]
if
isinstance
(
m
.
f
,
int
)
else
[
x
if
j
==
-
1
else
y
[
j
]
for
j
in
m
.
f
]
# from earlier layers
if
profile
:
c
=
isinstance
(
m
,
Detect
)
# copy input as inplace fix
o
=
thop
.
profile
(
m
,
inputs
=
(
x
.
copy
()
if
c
else
x
,),
verbose
=
False
)[
0
]
/
1E9
*
2
if
thop
else
0
# FLOPs
t
=
time_sync
()
for
_
in
range
(
10
):
m
(
x
.
copy
()
if
c
else
x
)
dt
.
append
((
time_sync
()
-
t
)
*
100
)
if
m
==
self
.
model
[
0
]:
LOGGER
.
info
(
f
"{'time (ms)':>10s} {'GFLOPs':>10s} {'params':>10s} {'module'}"
)
LOGGER
.
info
(
f
'{dt[-1]:10.2f} {o:10.2f} {m.np:10.0f} {m.type}'
)
self
.
_profile_one_layer
(
m
,
x
,
dt
)
x
=
m
(
x
)
# run
y
.
append
(
x
if
m
.
i
in
self
.
save
else
None
)
# save output
if
visualize
:
feature_visualization
(
x
,
m
.
type
,
m
.
i
,
save_dir
=
visualize
)
if
profile
:
LOGGER
.
info
(
'
%.1
fms total'
%
sum
(
dt
))
return
x
def
_descale_pred
(
self
,
p
,
flips
,
scale
,
img_size
):
...
...
@@ -179,6 +163,19 @@ class Model(nn.Module):
p
=
torch
.
cat
((
x
,
y
,
wh
,
p
[
...
,
4
:]),
-
1
)
return
p
def
_profile_one_layer
(
self
,
m
,
x
,
dt
):
c
=
isinstance
(
m
,
Detect
)
# is final layer, copy input as inplace fix
o
=
thop
.
profile
(
m
,
inputs
=
(
x
.
copy
()
if
c
else
x
,),
verbose
=
False
)[
0
]
/
1E9
*
2
if
thop
else
0
# FLOPs
t
=
time_sync
()
for
_
in
range
(
10
):
m
(
x
.
copy
()
if
c
else
x
)
dt
.
append
((
time_sync
()
-
t
)
*
100
)
if
m
==
self
.
model
[
0
]:
LOGGER
.
info
(
f
"{'time (ms)':>10s} {'GFLOPs':>10s} {'params':>10s} {'module'}"
)
LOGGER
.
info
(
f
'{dt[-1]:10.2f} {o:10.2f} {m.np:10.0f} {m.type}'
)
if
c
:
LOGGER
.
info
(
f
"{sum(dt):10.2f} {'-':>10s} {'-':>10s} Total"
)
def
_initialize_biases
(
self
,
cf
=
None
):
# initialize biases into Detect(), cf is class frequency
# https://arxiv.org/abs/1708.02002 section 3.3
# cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论