(转载)CNN 模型所需的计算力(FLOPs)和参数(parameters)数量计算

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}

 

扫描二维码关注公众号,回复: 6263632 查看本文章

全联接层: (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

参考:李珂

附:Pytorch计算FLOPs的代码:

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

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

猜你喜欢

转载自www.cnblogs.com/king-lps/p/10904552.html