python----------->>>>>>>>>>>>统计caffe模型的模型参数量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#统计caffe算模型的参数量
from numpy import prod, sum
flops = 0
typenames = ['Convolution', 'BatchNorm']
for layer_name, blob in net.blobs.iteritems():
    if layer_name not in net.layer_dict:
        continue
    if net.layer_dict[layer_name].type in typenames:
        cur_flops = 0.0
        if net.layer_dict[layer_name].type in typenames[:2]:
            cur_flops = (np.product(net.params[layer_name][0].data.shape) * \
                    blob.data.shape[-1] * blob.data.shape[-2])
        else:
            cur_flops = np.product(net.params[layer_name][0].data.shape)
        print(layer_name.ljust(20),
                str(net.params[layer_name][0].data.shape).ljust(20),
                str(blob.data.shape).ljust(20),
                net.layer_dict[layer_name].type.ljust(20), str(cur_flops).ljust(20))
        # InnerProduct
        if len(blob.data.shape) == 2:
            flops += prod(net.params[layer_name][0].data.shape)
        else:
            flops += prod(net.params[layer_name][0].data.shape) * blob.data.shape[2] * blob.data.shape[3]
 
print ('layers num: ' + str(len(net.params.items())))
print ("Total number of parameters: " + str(sum([prod(v[0].data.shape) for k, v in net.params.items()])))
print ("Total number of flops: " + str(flops))

  

posted @   水木清扬  阅读(214)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示