最近找到一些计算FLOPs的文章,奈何全是水文,讲都讲不清楚,完完全全的究极缝合怪。因此,这里准备彻底搞懂。
2023.3.12更新:
FLOPS:全称:FLoating point Operations Per Second的缩写,即 每秒浮点运算次数,或表示为计算速度。是一个衡量 硬件性能的指标。通俗点讲 显卡算力,对应英伟达官网的那些:GPU算力排行榜。
这才是本文的重点,FLOPs:FLoating point OPerationS 即 浮点计算次数,包含乘法和加法,只和模型有关,可以用来衡量其复杂度。多提一嘴,论文里面的FLOPs有的计算也并不明确,包括很多 Github 开源代码里面采用的 MACs,也就是考虑一次乘+加法运算为一次 MAC,粗略换算的话:FLOPs = 2 × MAC \text{FLOPs} = 2\times\text{MAC}FLOPs =2 ×MAC。建议发表的论文还是按照 FLOPs 来给出,因为我看的大部分文章都是用的这个,而不是 MACs。
2.1 2D 卷积运算
就单纯的 2D 卷积而言,举例:
Conv2d ( C o u t , C i n , k e r n e l = K , s t r i d e = S , p a d d i n g = P , b i a s = F a l s e ) \text{Conv2d}(C_{out}, C_{in}, kernel= K, stride= S, padding= P, bias=False)Conv2d (C o u t ,C in ,k er n e l =K ,s t r i d e =S ,p a dd in g =P ,bia s =F a l se )
输入 Feature map:( B , C i n , W i n , H i n ) (B, C_{in}, W_{in}, H_{in})(B ,C in ,W in ,H in ),输出 Feature map:( B , C o u t , W o u t , H o u t ) (B, C_{out}, W_{out}, H_{out})(B ,C o u t ,W o u t ,H o u t ),计算如下:
FLOPs = ( 2 × C i n × K 2 − 1 ) × W o u t × H o u t × C o u t \text{FLOPs}=\left(2\times{C_{in}}\times{K}^2-1\right)\times{W_{out}}\times{H_{out}}\times{C_{out}}FLOPs =(2 ×C in ×K 2 −1 )×W o u t ×H o u t ×C o u t
注意 (.) 里面的 -1 ,如果 bias = True,则不需要 -1。将 (.) 拆分为 乘法和加法:
FLOPs = [ ( C i n × K 2 ) + ( C i n × K 2 − 1 ) ] × W o u t × H o u t × C o u t \text{FLOPs}=\left[\left({C_{in}}\times{K}^2\right)+\left({C_{in}}\times{K}^2-1\right)\right]\times{W_{out}}\times{H_{out}}\times{C_{out}}FLOPs =[(C in ×K 2 )+(C in ×K 2 −1 )]×W o u t ×H o u t ×C o u t
第一个 (.) 里面是乘法,第二个是加法,如果 n 个数相加,做 n – 1 次加法运算,因此当 bias = True 时,刚好和 -1 抵消掉。
参数量的计算要简单些:Paras = K × K × C i n × C o u t + C o u t \text{Paras}=K\times{K}\times{C_{in}}\times{C_{out}}+C_{out}Paras =K ×K ×C in ×C o u t +C o u t
同样注意:如果 bias = True,+ C o u t +C_{out}+C o u t ,如果 bias = False,去掉 + C o u t +C_{out}+C o u t 。
2.2 全连接层
线性全连接层,举例:
L i n e r ( C i n , C o u t , b i a s = F a l s e ) Liner(C_{in}, C_{out}, bias = False)L in er (C in ,C o u t ,bia s =F a l se ),输入 sequence :( B , n u m , C i n ) (B, num, C_{in})(B ,n u m ,C in ),输出 sequence:( B , n u m , C o u t ) (B, num, C_{out})(B ,n u m ,C o u t ),计算如下:
$FLOPs = ( 2 × C i n − 1 ) × C o u t \text{FLOPs}=\left(2\times{C_{in}}-1\right)\times{C_{out}}FLOPs =(2 ×C in −1 )×C o u t
其中 2 2 2 代表乘法和加法。同上,当 bias = False 时,-1,bias = True时,无 -1。
全连接层参数:Paras = C i n × C o u t + C o u t \text{Paras}={C_{in}}\times{C_{out}}+C_{out}Paras =C in ×C o u t +C o u t
同样注意:当 bias = True,+ C o u t +C_{out}+C o u t ,当 bias = False,去掉 + C o u t +C_{out}+C o u t 。
2.3 BatchNorm2D 层
对于每一个通道来说,可学习的参数有 2 个,动量 γ \gamma γ、动量偏移 β \beta β。
2.4 激活层
对于 ReLU 来说,由于其本身性质,不涉及 MAC 运算,因此只考虑 FLOPs。而FLOPs 相对来说较小,所以一般不计算或者想其他办法计算。提一嘴,在推理时哪会用得到sigmoid呢。
激活层没有参数。
3.1 thop
pip install thop
使用举例:
import torch
from torchvision.models import resnet50
from thop import profile
model = resnet50()
input = torch.randn(1, 3, 224, 224)
macs, params = profile(model, inputs=(input, ))
print("MACs=", str(macs / 1e9) + '{}'.format("G"))
print("MACs=", str(macs / 1e6) + '{}'.format("M"))
自定义计算规则举例:
import torch
from thop import profile
class YourModule(nn.Module):
def count_your_model(model, x, y):
input = torch.randn(1, 3, 224, 224)
macs, params = profile(model, inputs=(input,),
custom_ops={YourModule: count_your_model})
print("MACs=", str(macs / 1e9) + '{}'.format("G"))
print("MACs=", str(macs / 1e6) + '{}'.format("M"))
优点:对于某个层的调试来说,很方便,比如 nn.Conv2D。
缺点:自定义的层,如 nn.Sequential()、nn.ModuleList() 这些容器层计算不了,需要自定义规则。
3.2 ptflops
pip install ptflops
使用举例:
import torch
from torchvision.models import resnet50
from ptflops import get_model_complexity_info
model = resnet50()
macs, params = get_model_complexity_info(model, (3, 200, 280), as_strings=True,
print_per_layer_stat=True, verbose=True)
print("MACs=", str(macs / 1e9) + '{}'.format("G"))
print("MACs=", str(macs / 1e6) + '{}'.format("M"))
优点:对于某个层的调试来说,很方便,比如 nn.Conv2D这些。另外 print_per_layer_stat = True 可以打印每一层的结构
缺点:自定义的层,如 nn.Sequential()、nn.ModuleList() 这些容器层计算不了。另外输入没有 batch维度,给出 shape 即可。
3.3 其他
写在后面
CSDN 灌水的人太多了,关键很多是错的,无语~~
Original: https://blog.csdn.net/qq_38929105/article/details/123847385
Author: 乄洛尘
Title: 关于 FLOPS、FLOPs、参数量的相关计算
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/717028/
转载文章受原作者版权保护。转载请注明原作者出处!