Flops 计算以及代码快速查看网络参数、浮点数

详细计算:卷积神经网络CNN中的参数量(parameters)和计算量(FLOPs ) - 知乎

FLOPS: 全大写,指每秒浮点运算次数,可以理解为计算的速度。是衡量硬件性能的一个指标。(硬件) 

FLOPs: s小写,指浮点运算数,理解为计算量。可以用来衡量算法/模型的复杂度。(模型) 在论文中常用GFLOPs(1 GFLOPs = 10^9 FLOPs)

安装方式: 

pip install torchstat

 使用方法:

from torchstat import stat
import torchvision.models as models

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

用来计算pytorch构建的网络的参数,空间大小,MAdd,FLOPs等指标; 

使用torchsummary 计算参数:

pip install torchsummary

import torch
import torchvision.models as models
model = models.alexnet()
from torchsummary import summary
summary(model, (3, 224, 224))

工具:fvcore/docs/flop_count.md at main · facebookresearch/fvcore · GitHub

  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
统计网络参数FLOPs深度学习中常用的操作,可以通过以下代码来实现: ```python import torch from torchsummary import summary # 定义模型 class Net(torch.nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = torch.nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1) self.conv2 = torch.nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1) self.pool = torch.nn.MaxPool2d(kernel_size=2, stride=2) self.fc1 = torch.nn.Linear(64*8*8, 128) self.fc2 = torch.nn.Linear(128, 10) def forward(self, x): x = torch.nn.functional.relu(self.conv1(x)) x = self.pool(x) x = torch.nn.functional.relu(self.conv2(x)) x = self.pool(x) x = x.view(-1, 64*8*8) x = torch.nn.functional.relu(self.fc1(x)) x = self.fc2(x) return x # 统计参数FLOPs model = Net() input_data = torch.randn(1, 3, 32, 32) summary(model, input_data) # 输出结果 ''' ---------------------------------------------------------------- Layer (type) Output Shape Param # ================================================================ Conv2d-1 [-1, 32, 32, 32] 896 MaxPool2d-2 [-1, 32, 16, 16] 0 Conv2d-3 [-1, 64, 16, 16] 18,496 MaxPool2d-4 [-1, 64, 8, 8] 0 Linear-5 [-1, 128] 524,416 Linear-6 [-1, 10] 1,290 ================================================================ Total params: 545,098 Trainable params: 545,098 Non-trainable params: 0 ---------------------------------------------------------------- Input size (MB): 0.01 Forward/backward pass size (MB): 0.50 Params size (MB): 2.08 Estimated Total Size (MB): 2.59 ---------------------------------------------------------------- FLOPs(2D): 111,411,200 FLOPs(3D): 18,564,864 ---------------------------------------------------------------- ''' ``` 这里使用了 `torchsummary` 库来统计模型参数FLOPs,在代码中 `Net` 类定义了一个简单的卷积神经网络,`summary` 函数可以输出模型各层的参数FLOPs。其中, `FLOPs(2D)` 表示网络的二维FLOPs,即在输入数据为二维时的FLOPs, `FLOPs(3D)` 表示网络的三维FLOPs,即在输入数据为三维(如3D卷积神经网络)时的FLOPs
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐亦亦乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值