Pytorch中对于模型的参数量和计算量的统计

背景:接前面的想法,安装thop第三方库后,就要利用它来进行模型参数量和计算量啦!!!!

1、安装Thop

哈哈哈哈,这一步我就不在这里赘述了,大家可以去看看我的另一篇博客。传送门-------》thop安装避坑,点击即达

2、代码使用

1)直接输出

import thop
from thop import profile
# 定义好的网络模型
input = torch.randn(1, 3, 224, 224)
flops, params = profile(Model,inputs=(input, ))
print('flops: ', flops, 'params: ', params)

这样的直接输出会让数据可比较性很弱,还需要自己转化单位【有点麻烦,不太推荐!】
在这里插入图片描述

2)使用thop.clever_format

from thop import clever_format
# 定义好的网络模型
input = torch.randn(1, 3, 224, 224)
flops, params = profile(Model,inputs=(input, ))
flops, params = clever_format([flops, params], "%.3f")  #这个就是与上面的差异哈!!!
print('flops: ', flops, 'params: ', params)

加上这样简单的一句话后,真的输入结果看上去太舒服了。
在这里插入图片描述

你可能感兴趣的:(python零散知识总结,python,pytorch,可视化)