king-lps

FLOPS:注意全大写,是floating point operations per second的缩写,意指每秒浮点运算次数,理解为计算速度。是一个衡量硬件性能的指标。

FLOPs:注意s小写,是floating point operations的缩写(s表复数),意指浮点运算数,理解为计算量。可以用来衡量算法/模型的复杂度。

网上打字很容易全小写,造成混淆,本问题针对模型,应指的是FLOPs。

以下答案不考虑activation function的运算。

卷积层:(2\times C_{i} \times K^{2}-1)\times H\times W\times C_{o}

Ci=input channel, k=kernel size, HW=output feature map size, Co=output channel.

2是因为一个MAC算2个operations。

不考虑bias时有-1,有bias时没有-1。

上面针对一个input feature map,没考虑batch size。

理解上面这个公式分两步,括号内是第一步,计算出output feature map的一个pixel,然后再乘以HWCo拓展到整个output feature map。括号内的部分又可以分为两步, (2 \cdot C_{i}\cdot K^{2}-1)=(C_{i}\cdot K^{2})+(C_{i}\cdot K^{2}-1) ,第一项是乘法运算数,第二项是加法运算数,因为n个数相加,要加n-1次,所以不考虑bias,会有一个-1,如果考虑bias,刚好中和掉,括号内变为 2 \cdot C_{i}\cdot K^{2}

 

全联接层: (2\times I-1)\times O

I=input neuron numbers, O=output neuron numbers.

2是因为一个MAC算2个operations。

不考虑bias时有-1,有bias时没有-1。

分析同理,括号内是一个输出神经元的计算量,拓展到O了输出神经元。

参考:chen liu

 

 

对于一个卷积层,假设其大小为 h \times w \times c \times n (其中c为#input channel, n为#output channel),输出的feature map尺寸为 H\' \times W\' ,则该卷积层的

  • #paras = n \times (h \times w \times c + 1)
  • #FLOPS= H\' \times W\' \times n \times(h \times w \times c + 1)

即#FLOPS= H\' \times W\' \times #paras

参考:李珂

 

Model_size = 4*params  模型大小为参数量的4倍

 

附:Pytorch计算FLOPs的代码:

http://link.zhihu.com/?target=https%3A//github.com/Lyken17/pytorch-OpCounter

https://github.com/sovrasov/flops-counter.pytorch

神器(pytorch):

pytorch-OpCounter 用法:(pytorch版本>=1.0)

from torchvision.models import resnet50
from thop import profile
model = resnet50()
flops, params = profile(model, input_size=(1, 3, 224,224))

 

 

torchstat 用法:

from torchstat import stat
import torchvision.models as models

model = model.alexnet()
stat(model, (3, 224, 224))

 

flopth 用法:

from flopth import flopth
print(flopth(net, in_size=[3,112,112]))

 

ptflops用法:

from ptflops import get_model_complexity_info
flops, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True, print_per_layer_stat=True)
print(\'Flops:  \' + flops)
print(\'Params: \' + params)

 

自己计算参数量:

print(\'Total params: %.2fM\' % (sum(p.numel() for p in net.parameters())/1000000.0))

 

 需要注意的是:params只与你定义的网络结构有关,和forward的任何操作无关。即定义好了网络结构,参数就已经决定了。FLOPs和不同的层运算结构有关。如果forward时在同一层(同一名字命名的层)多次运算,FLOPs不会增加。

 

 

参考:

轻量级神经网络“巡礼”(一)—— ShuffleNetV2

 

分类:

技术点:

相关文章:

  • 2021-12-04
  • 2022-01-22
  • 2022-01-23
  • 2021-06-20
  • 2021-10-14
  • 2021-10-04
  • 2022-01-16
  • 2021-12-22
猜你喜欢
  • 2021-10-14
  • 2021-10-14
  • 2021-06-18
  • 2021-10-07
  • 2021-12-10
  • 2022-01-03
  • 2021-08-09
相关资源
相似解决方案