paddle.flops(net, input_size, custom_ops=None, print_detail=False)[源代码]
作用为:打印网络的基础结构和参数信息。
- net (paddle.nn.Layer|paddle.static.Program) - 网络实例,必须是 paddle.nn.Layer 的子类或者静态图下的 paddle.static.Program。
- input_size (list) - 输入 Tensor 的大小。注意:仅支持 batch_size=1。
- custom_ops (dict,可选) - 字典,用于实现对自定义网络层的统计。字典的 key 为自定义网络层的 class,value 为统计网络层 flops 的函数,函数实现方法见示例代码。此参数仅在
net
为 paddle.nn.Layer 时生效。默认值:None。- print_detail (bool,可选) - bool 值,用于控制是否打印每个网络层的细节。默认值:False。
int,网络模型的计算量。
import paddle
import paddle.nn as nn
class LeNet(nn.Layer):
def __init__(self, num_classes=10):
super().__init__()
self.num_classes = num_classes
self.features = nn.Sequential(
nn.Conv2D(1, 6, 3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2D(2, 2),
nn.Conv2D(6, 16, 5, stride=1, padding=0),
nn.ReLU(),
nn.MaxPool2D(2, 2))
if num_classes > 0:
self.fc = nn.Sequential(
nn.Linear(400, 120),
nn.Linear(120, 84),
nn.Linear(84, 10))
def forward(self, inputs):
x = self.features(inputs)
if self.num_classes > 0:
x = paddle.flatten(x, 1)
x = self.fc(x)
return x
lenet = LeNet()
# m is the instance of nn.Layer, x is the intput of layer, y is the output of layer.
def count_leaky_relu(m, x, y):
x = x[0]
nelements = x.numel()
m.total_ops += int(nelements)
FLOPs = paddle.flops(lenet,
[1, 1, 28, 28],
custom_ops= {nn.LeakyReLU: count_leaky_relu},
print_detail=True)
print(FLOPs)
输出:
<class 'paddle.nn.layer.conv.Conv2D'>'s flops has been counted
paddle.nn.layer.activation.ReLU'>'s flops has been counted
Cannot find suitable count function for <class 'paddle.nn.layer.pooling.MaxPool2D'>. Treat it as zero FLOPs.
<class 'paddle.nn.layer.common.Linear'>'s flops has been counted
+--------------+-----------------+-----------------+--------+--------+
| Layer Name | Input Shape | Output Shape | Params | Flops |
+--------------+-----------------+-----------------+--------+--------+
| conv2d_0 | [1, 1, 28, 28] | [1, 6, 28, 28] | 60 | 47040 |
| re_lu_0 | [1, 6, 28, 28] | [1, 6, 28, 28] | 0 | 0 |
| max_pool2d_0 | [1, 6, 28, 28] | [1, 6, 14, 14] | 0 | 0 |
| conv2d_1 | [1, 6, 14, 14] | [1, 16, 10, 10] | 2416 | 241600 |
| re_lu_1 | [1, 16, 10, 10] | [1, 16, 10, 10] | 0 | 0 |
| max_pool2d_1 | [1, 16, 10, 10] | [1, 16, 5, 5] | 0 | 0 |
| linear_0 | [1, 400] | [1, 120] | 48120 | 48000 |
| linear_1 | [1, 120] | [1, 84] | 10164 | 10080 |
| linear_2 | [1, 84] | [1, 10] | 850 | 840 |
+--------------+-----------------+-----------------+--------+--------+
Total Flops: 347560 Total Params: 61610
347560
import paddle
import paddle.nn as nn
from paddle.vision.models import resnet50
from ppcls.arch.backbone.legendary_models.resnet import ResNet50
testNet = ResNet50()
FLOPs = paddle.flops(testNet,
[1, 3, 224, 224],
print_detail=True)
print(FLOPs)
输出:
W0122 17:22:08.571107 1724177 gpu_resources.cc:61] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 12.2, Runtime API Version: 11.6
W0122 17:22:08.575258 1724177 gpu_resources.cc:91] device: 0, cuDNN Version: 8.4.
<class 'paddle.nn.layer.pooling.AvgPool2D'>'s flops has been counted
paddle.nn.layer.conv.Conv2D'>'s flops has been counted
<class 'paddle.fluid.dygraph.nn.BatchNorm'>'s flops has been counted
paddle.nn.layer.activation.ReLU'>'s flops has been counted
Cannot find suitable count function for <class 'paddle.nn.layer.pooling.MaxPool2D'>. Treat it as zero FLOPs.
<class 'paddle.nn.layer.pooling.AdaptiveAvgPool2D'>'s flops has been counted
Cannot find suitable count function for paddle.fluid.dygraph.nn.Flatten'>. Treat it as zero FLOPs.
paddle.nn.layer.common.Linear'>'s flops has been counted
+-----------------------+-------------------+-------------------+---------+-----------+
| Layer Name | Input Shape | Output Shape | Params | Flops |
+-----------------------+-------------------+-------------------+---------+-----------+
| conv2d_0 | [1, 3, 224, 224] | [1, 64, 112, 112] | 9408 | 118013952 |
| batch_norm_0 | [1, 64, 112, 112] | [1, 64, 112, 112] | 256 | 1605632 |
| re_lu_4 | [1, 64, 112, 112] | [1, 64, 112, 112] | 0 | 0 |
| max_pool2d_0 | [1, 64, 112, 112] | [1, 64, 56, 56] | 0 | 0 |
| conv2d_1 | [1, 64, 56, 56] | [1, 64, 56, 56] | 4096 | 12845056 |
| batch_norm_1 | [1, 64, 56, 56] | [1, 64, 56, 56] | 256 | 401408 |
| re_lu_5 | [1, 64, 56, 56] | [1, 64, 56, 56] | 0 | 0 |
| conv2d_2 | [1, 64, 56, 56] | [1, 64, 56, 56] | 36864 | 115605504 |
| batch_norm_2 | [1, 64, 56, 56] | [1, 64, 56, 56] | 256 | 401408 |
| re_lu_6 | [1, 64, 56, 56] | [1, 64, 56, 56] | 0 | 0 |
| conv2d_3 | [1, 64, 56, 56] | [1, 256, 56, 56] | 16384 | 51380224 |
| batch_norm_3 | [1, 256, 56, 56] | [1, 256, 56, 56] | 1024 | 1605632 |
| conv2d_4 | [1, 64, 56, 56] | [1, 256, 56, 56] | 16384 | 51380224 |
| batch_norm_4 | [1, 256, 56, 56] | [1, 256, 56, 56] | 1024 | 1605632 |
| re_lu_9 | [1, 256, 56, 56] | [1, 256, 56, 56] | 0 | 0 |
| conv2d_5 | [1, 256, 56, 56] | [1, 64, 56, 56] | 16384 | 51380224 |
| batch_norm_5 | [1, 64, 56, 56] | [1, 64, 56, 56] | 256 | 401408 |
| re_lu_10 | [1, 64, 56, 56] | [1, 64, 56, 56] | 0 | 0 |
| conv2d_6 | [1, 64, 56, 56] | [1, 64, 56, 56] | 36864 | 115605504 |
| batch_norm_6 | [1, 64, 56, 56] | [1, 64, 56, 56] | 256 | 401408 |
| re_lu_11 | [1, 64, 56, 56] | [1, 64, 56, 56] | 0 | 0 |
| conv2d_7 | [1, 64, 56, 56] | [1, 256, 56, 56] | 16384 | 51380224 |
| batch_norm_7 | [1, 256, 56, 56] | [1, 256, 56, 56] | 1024 | 1605632 |
| re_lu_13 | [1, 256, 56, 56] | [1, 256, 56, 56] | 0 | 0 |
| conv2d_8 | [1, 256, 56, 56] | [1, 64, 56, 56] | 16384 | 51380224 |
| batch_norm_8 | [1, 64, 56, 56] | [1, 64, 56, 56] | 256 | 401408 |
| re_lu_14 | [1, 64, 56, 56] | [1, 64, 56, 56] | 0 | 0 |
| conv2d_9 | [1, 64, 56, 56] | [1, 64, 56, 56] | 36864 | 115605504 |
| batch_norm_9 | [1, 64, 56, 56] | [1, 64, 56, 56] | 256 | 401408 |
| re_lu_15 | [1, 64, 56, 56] | [1, 64, 56, 56] | 0 | 0 |
| conv2d_10 | [1, 64, 56, 56] | [1, 256, 56, 56] | 16384 | 51380224 |
| batch_norm_10 | [1, 256, 56, 56] | [1, 256, 56, 56] | 1024 | 1605632 |
| re_lu_17 | [1, 256, 56, 56] | [1, 256, 56, 56] | 0 | 0 |
| conv2d_11 | [1, 256, 56, 56] | [1, 128, 56, 56] | 32768 | 102760448 |
| batch_norm_11 | [1, 128, 56, 56] | [1, 128, 56, 56] | 512 | 802816 |
| re_lu_18 | [1, 128, 56, 56] | [1, 128, 56, 56] | 0 | 0 |
| conv2d_12 | [1, 128, 56, 56] | [1, 128, 28, 28] | 147456 | 115605504 |
| batch_norm_12 | [1, 128, 28, 28] | [1, 128, 28, 28] | 512 | 200704 |
| re_lu_19 | [1, 128, 28, 28] | [1, 128, 28, 28] | 0 | 0 |
| conv2d_13 | [1, 128, 28, 28] | [1, 512, 28, 28] | 65536 | 51380224 |
| batch_norm_13 | [1, 512, 28, 28] | [1, 512, 28, 28] | 2048 | 802816 |
| conv2d_14 | [1, 256, 56, 56] | [1, 512, 28, 28] | 131072 | 102760448 |
| batch_norm_14 | [1, 512, 28, 28] | [1, 512, 28, 28] | 2048 | 802816 |
| re_lu_22 | [1, 512, 28, 28] | [1, 512, 28, 28] | 0 | 0 |
| conv2d_15 | [1, 512, 28, 28] | [1, 128, 28, 28] | 65536 | 51380224 |
| batch_norm_15 | [1, 128, 28, 28] | [1, 128, 28, 28] | 512 | 200704 |
| re_lu_23 | [1, 128, 28, 28] | [1, 128, 28, 28] | 0 | 0 |
| conv2d_16 | [1, 128, 28, 28] | [1, 128, 28, 28] | 147456 | 115605504 |
| batch_norm_16 | [1, 128, 28, 28] | [1, 128, 28, 28] | 512 | 200704 |
| re_lu_24 | [1, 128, 28, 28] | [1, 128, 28, 28] | 0 | 0 |
| conv2d_17 | [1, 128, 28, 28] | [1, 512, 28, 28] | 65536 | 51380224 |
| batch_norm_17 | [1, 512, 28, 28] | [1, 512, 28, 28] | 2048 | 802816 |
| re_lu_26 | [1, 512, 28, 28] | [1, 512, 28, 28] | 0 | 0 |
| conv2d_18 | [1, 512, 28, 28] | [1, 128, 28, 28] | 65536 | 51380224 |
| batch_norm_18 | [1, 128, 28, 28] | [1, 128, 28, 28] | 512 | 200704 |
| re_lu_27 | [1, 128, 28, 28] | [1, 128, 28, 28] | 0 | 0 |
| conv2d_19 | [1, 128, 28, 28] | [1, 128, 28, 28] | 147456 | 115605504 |
| batch_norm_19 | [1, 128, 28, 28] | [1, 128, 28, 28] | 512 | 200704 |
| re_lu_28 | [1, 128, 28, 28] | [1, 128, 28, 28] | 0 | 0 |
| conv2d_20 | [1, 128, 28, 28] | [1, 512, 28, 28] | 65536 | 51380224 |
| batch_norm_20 | [1, 512, 28, 28] | [1, 512, 28, 28] | 2048 | 802816 |
| re_lu_30 | [1, 512, 28, 28] | [1, 512, 28, 28] | 0 | 0 |
| conv2d_21 | [1, 512, 28, 28] | [1, 128, 28, 28] | 65536 | 51380224 |
| batch_norm_21 | [1, 128, 28, 28] | [1, 128, 28, 28] | 512 | 200704 |
| re_lu_31 | [1, 128, 28, 28] | [1, 128, 28, 28] | 0 | 0 |
| conv2d_22 | [1, 128, 28, 28] | [1, 128, 28, 28] | 147456 | 115605504 |
| batch_norm_22 | [1, 128, 28, 28] | [1, 128, 28, 28] | 512 | 200704 |
| re_lu_32 | [1, 128, 28, 28] | [1, 128, 28, 28] | 0 | 0 |
| conv2d_23 | [1, 128, 28, 28] | [1, 512, 28, 28] | 65536 | 51380224 |
| batch_norm_23 | [1, 512, 28, 28] | [1, 512, 28, 28] | 2048 | 802816 |
| re_lu_34 | [1, 512, 28, 28] | [1, 512, 28, 28] | 0 | 0 |
| conv2d_24 | [1, 512, 28, 28] | [1, 256, 28, 28] | 131072 | 102760448 |
| batch_norm_24 | [1, 256, 28, 28] | [1, 256, 28, 28] | 1024 | 401408 |
| re_lu_35 | [1, 256, 28, 28] | [1, 256, 28, 28] | 0 | 0 |
| conv2d_25 | [1, 256, 28, 28] | [1, 256, 14, 14] | 589824 | 115605504 |
| batch_norm_25 | [1, 256, 14, 14] | [1, 256, 14, 14] | 1024 | 100352 |
| re_lu_36 | [1, 256, 14, 14] | [1, 256, 14, 14] | 0 | 0 |
| conv2d_26 | [1, 256, 14, 14] | [1, 1024, 14, 14] | 262144 | 51380224 |
| batch_norm_26 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 4096 | 401408 |
| conv2d_27 | [1, 512, 28, 28] | [1, 1024, 14, 14] | 524288 | 102760448 |
| batch_norm_27 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 4096 | 401408 |
| re_lu_39 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 0 | 0 |
| conv2d_28 | [1, 1024, 14, 14] | [1, 256, 14, 14] | 262144 | 51380224 |
| batch_norm_28 | [1, 256, 14, 14] | [1, 256, 14, 14] | 1024 | 100352 |
| re_lu_40 | [1, 256, 14, 14] | [1, 256, 14, 14] | 0 | 0 |
| conv2d_29 | [1, 256, 14, 14] | [1, 256, 14, 14] | 589824 | 115605504 |
| batch_norm_29 | [1, 256, 14, 14] | [1, 256, 14, 14] | 1024 | 100352 |
| re_lu_41 | [1, 256, 14, 14] | [1, 256, 14, 14] | 0 | 0 |
| conv2d_30 | [1, 256, 14, 14] | [1, 1024, 14, 14] | 262144 | 51380224 |
| batch_norm_30 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 4096 | 401408 |
| re_lu_43 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 0 | 0 |
| conv2d_31 | [1, 1024, 14, 14] | [1, 256, 14, 14] | 262144 | 51380224 |
| batch_norm_31 | [1, 256, 14, 14] | [1, 256, 14, 14] | 1024 | 100352 |
| re_lu_44 | [1, 256, 14, 14] | [1, 256, 14, 14] | 0 | 0 |
| conv2d_32 | [1, 256, 14, 14] | [1, 256, 14, 14] | 589824 | 115605504 |
| batch_norm_32 | [1, 256, 14, 14] | [1, 256, 14, 14] | 1024 | 100352 |
| re_lu_45 | [1, 256, 14, 14] | [1, 256, 14, 14] | 0 | 0 |
| conv2d_33 | [1, 256, 14, 14] | [1, 1024, 14, 14] | 262144 | 51380224 |
| batch_norm_33 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 4096 | 401408 |
| re_lu_47 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 0 | 0 |
| conv2d_34 | [1, 1024, 14, 14] | [1, 256, 14, 14] | 262144 | 51380224 |
| batch_norm_34 | [1, 256, 14, 14] | [1, 256, 14, 14] | 1024 | 100352 |
| re_lu_48 | [1, 256, 14, 14] | [1, 256, 14, 14] | 0 | 0 |
| conv2d_35 | [1, 256, 14, 14] | [1, 256, 14, 14] | 589824 | 115605504 |
| batch_norm_35 | [1, 256, 14, 14] | [1, 256, 14, 14] | 1024 | 100352 |
| re_lu_49 | [1, 256, 14, 14] | [1, 256, 14, 14] | 0 | 0 |
| conv2d_36 | [1, 256, 14, 14] | [1, 1024, 14, 14] | 262144 | 51380224 |
| batch_norm_36 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 4096 | 401408 |
| re_lu_51 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 0 | 0 |
| conv2d_37 | [1, 1024, 14, 14] | [1, 256, 14, 14] | 262144 | 51380224 |
| batch_norm_37 | [1, 256, 14, 14] | [1, 256, 14, 14] | 1024 | 100352 |
| re_lu_52 | [1, 256, 14, 14] | [1, 256, 14, 14] | 0 | 0 |
| conv2d_38 | [1, 256, 14, 14] | [1, 256, 14, 14] | 589824 | 115605504 |
| batch_norm_38 | [1, 256, 14, 14] | [1, 256, 14, 14] | 1024 | 100352 |
| re_lu_53 | [1, 256, 14, 14] | [1, 256, 14, 14] | 0 | 0 |
| conv2d_39 | [1, 256, 14, 14] | [1, 1024, 14, 14] | 262144 | 51380224 |
| batch_norm_39 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 4096 | 401408 |
| re_lu_55 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 0 | 0 |
| conv2d_40 | [1, 1024, 14, 14] | [1, 256, 14, 14] | 262144 | 51380224 |
| batch_norm_40 | [1, 256, 14, 14] | [1, 256, 14, 14] | 1024 | 100352 |
| re_lu_56 | [1, 256, 14, 14] | [1, 256, 14, 14] | 0 | 0 |
| conv2d_41 | [1, 256, 14, 14] | [1, 256, 14, 14] | 589824 | 115605504 |
| batch_norm_41 | [1, 256, 14, 14] | [1, 256, 14, 14] | 1024 | 100352 |
| re_lu_57 | [1, 256, 14, 14] | [1, 256, 14, 14] | 0 | 0 |
| conv2d_42 | [1, 256, 14, 14] | [1, 1024, 14, 14] | 262144 | 51380224 |
| batch_norm_42 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 4096 | 401408 |
| re_lu_59 | [1, 1024, 14, 14] | [1, 1024, 14, 14] | 0 | 0 |
| conv2d_43 | [1, 1024, 14, 14] | [1, 512, 14, 14] | 524288 | 102760448 |
| batch_norm_43 | [1, 512, 14, 14] | [1, 512, 14, 14] | 2048 | 200704 |
| re_lu_60 | [1, 512, 14, 14] | [1, 512, 14, 14] | 0 | 0 |
| conv2d_44 | [1, 512, 14, 14] | [1, 512, 7, 7] | 2359296 | 115605504 |
| batch_norm_44 | [1, 512, 7, 7] | [1, 512, 7, 7] | 2048 | 50176 |
| re_lu_61 | [1, 512, 7, 7] | [1, 512, 7, 7] | 0 | 0 |
| conv2d_45 | [1, 512, 7, 7] | [1, 2048, 7, 7] | 1048576 | 51380224 |
| batch_norm_45 | [1, 2048, 7, 7] | [1, 2048, 7, 7] | 8192 | 200704 |
| conv2d_46 | [1, 1024, 14, 14] | [1, 2048, 7, 7] | 2097152 | 102760448 |
| batch_norm_46 | [1, 2048, 7, 7] | [1, 2048, 7, 7] | 8192 | 200704 |
| re_lu_64 | [1, 2048, 7, 7] | [1, 2048, 7, 7] | 0 | 0 |
| conv2d_47 | [1, 2048, 7, 7] | [1, 512, 7, 7] | 1048576 | 51380224 |
| batch_norm_47 | [1, 512, 7, 7] | [1, 512, 7, 7] | 2048 | 50176 |
| re_lu_65 | [1, 512, 7, 7] | [1, 512, 7, 7] | 0 | 0 |
| conv2d_48 | [1, 512, 7, 7] | [1, 512, 7, 7] | 2359296 | 115605504 |
| batch_norm_48 | [1, 512, 7, 7] | [1, 512, 7, 7] | 2048 | 50176 |
| re_lu_66 | [1, 512, 7, 7] | [1, 512, 7, 7] | 0 | 0 |
| conv2d_49 | [1, 512, 7, 7] | [1, 2048, 7, 7] | 1048576 | 51380224 |
| batch_norm_49 | [1, 2048, 7, 7] | [1, 2048, 7, 7] | 8192 | 200704 |
| re_lu_68 | [1, 2048, 7, 7] | [1, 2048, 7, 7] | 0 | 0 |
| conv2d_50 | [1, 2048, 7, 7] | [1, 512, 7, 7] | 1048576 | 51380224 |
| batch_norm_50 | [1, 512, 7, 7] | [1, 512, 7, 7] | 2048 | 50176 |
| re_lu_69 | [1, 512, 7, 7] | [1, 512, 7, 7] | 0 | 0 |
| conv2d_51 | [1, 512, 7, 7] | [1, 512, 7, 7] | 2359296 | 115605504 |
| batch_norm_51 | [1, 512, 7, 7] | [1, 512, 7, 7] | 2048 | 50176 |
| re_lu_70 | [1, 512, 7, 7] | [1, 512, 7, 7] | 0 | 0 |
| conv2d_52 | [1, 512, 7, 7] | [1, 2048, 7, 7] | 1048576 | 51380224 |
| batch_norm_52 | [1, 2048, 7, 7] | [1, 2048, 7, 7] | 8192 | 200704 |
| re_lu_72 | [1, 2048, 7, 7] | [1, 2048, 7, 7] | 0 | 0 |
| adaptive_avg_pool2d_0 | [1, 2048, 7, 7] | [1, 2048, 1, 1] | 0 | 102400 |
| flatten_0 | [1, 2048, 1, 1] | [1, 2048] | 0 | 0 |
| linear_0 | [1, 2048] | [1, 1000] | 2049000 | 2048000 |
+-----------------------+-------------------+-------------------+---------+-----------+
Total Flops: 4111514624 Total Params: 25610152
4111514624
@misc{BibEntry2024Jan,
title = {{flops-API文档-PaddlePaddle深度学习平台}},
year = {2024},
month = jan,
urldate = {2024-01-22},
language = {chinese},
note = {[Online; accessed 22. Jan. 2024]},
url = {https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/flops_cn.html}
}