pytorch model flops and parameters count

1. flops caculate:

import re
def get_num_gen(gen):
        return sum(1 for x in gen)
def flops_layer(layer):
        """
        Calculate the number of flops for given a string information of layer.
        We extract only resonable numbers and use them.

        Args:
            layer (str) : example
                Linear (512 -> 1000)
                Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
                BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True)
        """
        # print(layer)
        idx_type_end = layer.find('(')
        type_name = layer[:idx_type_end]

        params = re.findall('[^a-z](\d+)', layer)
        flops = 1

        if layer.find('Linear') >= 0:
                C1 = int(params[0])
                C2 = int(params[1])
                flops = C1 * C2

        elif layer.find('Conv2d') >= 0:
                C1 = int(params[0])
                C2 = int(params[1])
                K1 = int(params[2])
                K2 = int(params[3])

                # image size
                H = 32
                W = 32
                flops = C1 * C2 * K1 * K2 * H * W

        #     print(type_name, flops)
        return flops
def calculate_flops(gen):
        """
        Calculate the flops given a generator of pytorch model.
        It only compute the flops of forward pass.

        Example:
            >>> net = torchvision.models.resnet18()
            >>> calculate_flops(net.children())
        """
        flops = 0;

        for child in gen:
                num_children = get_num_gen(child.children())

                # leaf node
                if num_children == 0:
                        flops += flops_layer(str(child))

                else:
                        flops += calculate_flops(child.children())

        return flops
flops = calculate_flops(net.children())
print(flops / 10**9, 'G')
exit()

2. parameters count calulate:

params = list(net.parameters())
k = 0
for i in params:
    l = 1
    print("该层的权重维度:" + str(list(i.size())))
    for j in i.size():
        l *= j
    print("该层参数量:" + str(l))
    k = k + l
print("总参数数量和:" + str(k))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值