# 计算模型的GFLOPs和参数量 & 举例VGG16和DETR

## 一、FLOPS、FLOPs和GFLOPs的概念

• FLOPS：注意S是大写，是 “每秒所执行的浮点运算次数”（floating-point operations per second）的缩写。它常被用来估算电脑的执行效能，尤其是在使用到大量浮点运算的科学计算领域中。正因为FLOPS字尾的那个S，代表秒，而不是复数，所以不能省略掉。
• FLOPs：注意s小写，是floating point operations的缩写（s表复数），意指浮点运算数，理解为计算量。可以用来衡量算法/模型的复杂度。
• GFLOPs：一个GFLOPs等于每秒十亿（=10^9）次的浮点运算

## 二、计算VGG16的GFLOPs和参数量

``````from thop import profile
import torch
import torchvision.models as models

model = models.vgg16()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
input = torch.zeros((1, 3, 224, 224)).to(device)
flops, params = profile(model.to(device), inputs=(input,))

print("参数量：", params)
print("FLOPS：", flops)``````

>>>output

FLOPS： 15470314496.0

## 三、计算DETR的GFLOPs和参数量

1. 首先，访问网址：GitHub - facebookresearch/detr: End-to-End Object Detection with Transformers
2. 然后，下载DETR源码压缩包，调通源码。
3. 最后，把下面的代码封装到py文件中，放到DETR源码的根目录即可。
``````import os
import time

from PIL import Image
import matplotlib.pyplot as plt

import torch
import torchvision.transforms as T

from models import build_model
import argparse

from torch.nn.functional import dropout,linear,softmax

def get_args_parser():
parser = argparse.ArgumentParser('Set transformer detector', add_help=False)

# Model parameters
help="Path to the pretrained model. If set, only the mask head will be trained")
# * Backbone
help="Name of the convolutional backbone to use")
help="If true, we replace stride with dilation in the last convolutional block (DC5)")
help="Type of positional embedding to use on top of the image features")

# * Transformer
help="Number of encoding layers in the transformer")
help="Number of decoding layers in the transformer")
help="Intermediate size of the feedforward layers in the transformer blocks")
help="Size of the embeddings (dimension of the transformer)")
help="Dropout applied in the transformer")
help="Number of attention heads inside the transformer's attentions")
help="Number of query slots")  # 论文中对象查询为100

# * Segmentation
help="Train segmentation head if the flag is provided")

# Loss
help="Disables auxiliary decoding losses (loss at each layer)")
# * Matcher
help="Class coefficient in the matching cost")
help="L1 box coefficient in the matching cost")
help="giou box coefficient in the matching cost")
# * Loss coefficients
help="Relative classification weight of the no-object class")

# dataset parameters

help='path where to save, empty for no saving')
help='device to use for training / testing')

# ============================================================================= #
# ============================================================================= #

help='start epoch')

# distributed training parameters
help='number of distributed processes')
parser.add_argument('--dist_url', default='env://', help='url used to set up distributed training')
return parser

if __name__ == '__main__':
parser = argparse.ArgumentParser('DETR training and evaluation script', parents=[get_args_parser()])
args = parser.parse_args()

# 建立模型
model, criterion, postprocessors = build_model(args)
model.to('cuda:0')
url = r'detr-r50-dc5-f0fb7ef5.pth'
# print(state_dict)

# 加载模型参数，以字典的形式表示
model.eval()  # 把字符串类型转换成字典类型

# ==================================================== #
from thop import profile
import torchsummary

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
input = torch.zeros((1, 3, 800, 1422)).to(device)
flops, params = profile(model.to(device), inputs=(input,))

print("参数量：", params)
print("FLOPS：", flops)
# ==================================================== #``````

>>> output

FLOPS： 100937364480.0

## 四、整理数据表格

Model GFLOPs Params
VGG16 15.47 13.84 M
DETR 100.94 36.74 M

>>> 如有疑问，欢迎评论区一起探讨！

THE END