Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
6aed0a7c
Unverified
提交
6aed0a7c
authored
8月 13, 2022
作者:
Glenn Jocher
提交者:
GitHub
8月 13, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
GFLOPs computation fix for classification models (#8954)
* GFLOPs computation fix for classification models Improved robustness in reading input channel count * Update torch_utils.py * Update torch_utils.py
上级
f1214f23
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
8 行增加
和
9 行删除
+8
-9
torch_utils.py
utils/torch_utils.py
+8
-9
没有找到文件。
utils/torch_utils.py
浏览文件 @
6aed0a7c
...
@@ -199,12 +199,11 @@ def sparsity(model):
...
@@ -199,12 +199,11 @@ def sparsity(model):
def
prune
(
model
,
amount
=
0.3
):
def
prune
(
model
,
amount
=
0.3
):
# Prune model to requested global sparsity
# Prune model to requested global sparsity
import
torch.nn.utils.prune
as
prune
import
torch.nn.utils.prune
as
prune
print
(
'Pruning model... '
,
end
=
''
)
for
name
,
m
in
model
.
named_modules
():
for
name
,
m
in
model
.
named_modules
():
if
isinstance
(
m
,
nn
.
Conv2d
):
if
isinstance
(
m
,
nn
.
Conv2d
):
prune
.
l1_unstructured
(
m
,
name
=
'weight'
,
amount
=
amount
)
# prune
prune
.
l1_unstructured
(
m
,
name
=
'weight'
,
amount
=
amount
)
# prune
prune
.
remove
(
m
,
'weight'
)
# make permanent
prune
.
remove
(
m
,
'weight'
)
# make permanent
print
(
'
%.3
g global sparsity'
%
sparsity
(
model
)
)
LOGGER
.
info
(
f
'Model pruned to {sparsity(model):.3g} global sparsity'
)
def
fuse_conv_and_bn
(
conv
,
bn
):
def
fuse_conv_and_bn
(
conv
,
bn
):
...
@@ -230,7 +229,7 @@ def fuse_conv_and_bn(conv, bn):
...
@@ -230,7 +229,7 @@ def fuse_conv_and_bn(conv, bn):
return
fusedconv
return
fusedconv
def
model_info
(
model
,
verbose
=
False
,
img
_size
=
640
):
def
model_info
(
model
,
verbose
=
False
,
img
sz
=
640
):
# Model information. img_size may be int or list, i.e. img_size=640 or img_size=[640, 320]
# Model information. img_size may be int or list, i.e. img_size=640 or img_size=[640, 320]
n_p
=
sum
(
x
.
numel
()
for
x
in
model
.
parameters
())
# number parameters
n_p
=
sum
(
x
.
numel
()
for
x
in
model
.
parameters
())
# number parameters
n_g
=
sum
(
x
.
numel
()
for
x
in
model
.
parameters
()
if
x
.
requires_grad
)
# number gradients
n_g
=
sum
(
x
.
numel
()
for
x
in
model
.
parameters
()
if
x
.
requires_grad
)
# number gradients
...
@@ -242,12 +241,12 @@ def model_info(model, verbose=False, img_size=640):
...
@@ -242,12 +241,12 @@ def model_info(model, verbose=False, img_size=640):
(
i
,
name
,
p
.
requires_grad
,
p
.
numel
(),
list
(
p
.
shape
),
p
.
mean
(),
p
.
std
()))
(
i
,
name
,
p
.
requires_grad
,
p
.
numel
(),
list
(
p
.
shape
),
p
.
mean
(),
p
.
std
()))
try
:
# FLOPs
try
:
# FLOPs
from
thop
import
profile
p
=
next
(
model
.
parameters
())
stride
=
max
(
int
(
model
.
stride
.
max
()),
32
)
if
hasattr
(
model
,
'stride'
)
else
32
stride
=
max
(
int
(
model
.
stride
.
max
()),
32
)
if
hasattr
(
model
,
'stride'
)
else
32
# max stride
im
g
=
torch
.
zeros
((
1
,
model
.
yaml
.
get
(
'ch'
,
3
),
stride
,
stride
),
device
=
next
(
model
.
parameters
())
.
device
)
# inpu
t
im
=
torch
.
zeros
((
1
,
p
.
shape
[
1
],
stride
,
stride
),
device
=
p
.
device
)
# input image in BCHW forma
t
flops
=
profile
(
deepcopy
(
model
),
inputs
=
(
img
,),
verbose
=
False
)[
0
]
/
1E9
*
2
# stride GFLOPs
flops
=
thop
.
profile
(
deepcopy
(
model
),
inputs
=
(
im
,),
verbose
=
False
)[
0
]
/
1E9
*
2
# stride GFLOPs
img
_size
=
img_size
if
isinstance
(
img_size
,
list
)
else
[
img_size
,
img_size
]
# expand if int/float
img
sz
=
imgsz
if
isinstance
(
imgsz
,
list
)
else
[
imgsz
,
imgsz
]
# expand if int/float
fs
=
',
%.1
f GFLOPs'
%
(
flops
*
img_size
[
0
]
/
stride
*
img_size
[
1
]
/
stride
)
# 640x640 GFLOPs
fs
=
f
', {flops * imgsz[0] / stride * imgsz[1] / stride:.1f} GFLOPs'
# 640x640 GFLOPs
except
Exception
:
except
Exception
:
fs
=
''
fs
=
''
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论