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

这篇具有很好参考价值的文章主要介绍了计算模型的GFLOPs和参数量 & 举例VGG16和DETR。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

近期忙于写论文,分享一下论文中表格数据的计算方法。

目录

一、FLOPS、FLOPs和GFLOPs的概念

二、计算VGG16的GFLOPs和参数量

三、计算DETR的GFLOPs和参数量

四、整理数据表格


一、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

参数量: 138357544.0
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
torch.set_grad_enabled(False)

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)
    parser.add_argument('--lr', default=1e-4, type=float)
    parser.add_argument('--lr_backbone', default=1e-5, type=float)
    parser.add_argument('--batch_size', default=1, type=int)
    parser.add_argument('--weight_decay', default=1e-4, type=float)
    # parser.add_argument('--epochs', default=300, type=int)
    parser.add_argument('--epochs', default=100, type=int)
    parser.add_argument('--lr_drop', default=200, type=int)
    parser.add_argument('--clip_max_norm', default=0.1, type=float,
                        help='gradient clipping max norm')

    # Model parameters
    parser.add_argument('--frozen_weights', type=str, default=None,
                        help="Path to the pretrained model. If set, only the mask head will be trained")
    # * Backbone
    parser.add_argument('--backbone', default='resnet50', type=str,
                        help="Name of the convolutional backbone to use")
    parser.add_argument('--dilation', action='store_true',
                        help="If true, we replace stride with dilation in the last convolutional block (DC5)")
    parser.add_argument('--position_embedding', default='sine', type=str, choices=('sine', 'learned'),
                        help="Type of positional embedding to use on top of the image features")

    # * Transformer
    parser.add_argument('--enc_layers', default=6, type=int,
                        help="Number of encoding layers in the transformer")
    parser.add_argument('--dec_layers', default=6, type=int,
                        help="Number of decoding layers in the transformer")
    parser.add_argument('--dim_feedforward', default=2048, type=int,
                        help="Intermediate size of the feedforward layers in the transformer blocks")
    parser.add_argument('--hidden_dim', default=256, type=int,
                        help="Size of the embeddings (dimension of the transformer)")
    parser.add_argument('--dropout', default=0.1, type=float,
                        help="Dropout applied in the transformer")
    parser.add_argument('--nheads', default=8, type=int,
                        help="Number of attention heads inside the transformer's attentions")
    parser.add_argument('--num_queries', default=40, type=int,
                        help="Number of query slots")  # 论文中对象查询为100
    parser.add_argument('--pre_norm', action='store_true')

    # * Segmentation
    parser.add_argument('--masks', action='store_true',
                        help="Train segmentation head if the flag is provided")

    # Loss
    parser.add_argument('--no_aux_loss', dest='aux_loss', action='store_false',
                        help="Disables auxiliary decoding losses (loss at each layer)")
    # * Matcher
    parser.add_argument('--set_cost_class', default=1, type=float,
                        help="Class coefficient in the matching cost")
    parser.add_argument('--set_cost_bbox', default=5, type=float,
                        help="L1 box coefficient in the matching cost")
    parser.add_argument('--set_cost_giou', default=2, type=float,
                        help="giou box coefficient in the matching cost")
    # * Loss coefficients
    parser.add_argument('--mask_loss_coef', default=1, type=float)
    parser.add_argument('--dice_loss_coef', default=1, type=float)
    parser.add_argument('--bbox_loss_coef', default=5, type=float)
    parser.add_argument('--giou_loss_coef', default=2, type=float)
    parser.add_argument('--eos_coef', default=0.1, type=float,
                        help="Relative classification weight of the no-object class")

    # dataset parameters
    parser.add_argument('--dataset_file', default='coco')
    parser.add_argument('--coco_path', default='', type=str)
    parser.add_argument('--coco_panoptic_path', type=str)
    parser.add_argument('--remove_difficult', action='store_true')

    parser.add_argument('--output_dir', default='E:\project_yd\paper_sci_one_yd\Transformer\DETR\detr\\runs\\train',
                        help='path where to save, empty for no saving')
    parser.add_argument('--device', default='cuda',
                        help='device to use for training / testing')
    parser.add_argument('--seed', default=42, type=int)

    # ============================================================================= #
    parser.add_argument('--resume', default='', help='resume from checkpoint')
    # ============================================================================= #

    parser.add_argument('--start_epoch', default=0, type=int, metavar='N',
                        help='start epoch')
    parser.add_argument('--eval', action='store_true')
    parser.add_argument('--num_workers', default=2, type=int)

    # distributed training parameters
    parser.add_argument('--world_size', default=1, type=int,
                        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'
    state_dict = torch.load(url)
    # print(state_dict)

    # 加载模型参数,以字典的形式表示
    model.load_state_dict(state_dict['model'])
    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

参数量: 36739785.0
FLOPS: 100937364480.0

四、整理数据表格

Model GFLOPs Params
VGG16 15.47 13.84 M
DETR 100.94 36.74 M

 

>>> 如有疑问,欢迎评论区一起探讨!文章来源地址https://www.toymoban.com/news/detail-406817.html

到了这里,关于计算模型的GFLOPs和参数量 & 举例VGG16和DETR的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 基于VGG-16+Android+Python的智能车辆驾驶行为分析—深度学习算法应用(含全部工程源码)+数据集+模型(四)

    本项目采用VGG-16网络模型,使用Kaggle开源数据集,旨在提取图片中的用户特征,最终在移动端实现对不良驾驶行为的识别功能。 首先,通过使用VGG-16网络模型,本项目能够深入学习和理解驾驶场景图像中的特征。VGG-16是一种深度卷积神经网络,特别适用于图像识别任务,通过

    2024年02月03日
    浏览(34)
  • 深度学习应用篇-计算机视觉-图像分类[2]:LeNet、AlexNet、VGG、GoogleNet、DarkNet模型结构、实现、模型特点详细介绍

    LeNet是最早的卷积神经网络之一 [1] ,其被提出用于识别手写数字和机器印刷字符。1998年,Yann LeCun第一次将LeNet卷积神经网络应用到图像分类上,在手写数字识别任务中取得了巨大成功。算法中阐述了图像中像素特征之间的相关性能够由参数共享的卷积操作所提取,同时使用

    2024年02月08日
    浏览(30)
  • vgg16-pytorch

    基于pytorch实现VGG16模型 刚听完土哥的入门pytorch,试着写一个不完善的vgg16 VGG16具体的架构: VGG16模型构建: 卷积池化后尺寸计算公式: 引入库: dilation默认为1,计算第一个卷积的步长和填充: 图片为3通道,输出后维64通道,卷积核为3x3,第一个卷积层为: 在第一个池化这里

    2024年02月16日
    浏览(28)
  • 训练YOLOv9-S(注意:官方还没有提供YOLOv9-S的网络,我这是根据网络博客进行的步骤,按照0.33、0.50比例调整网络大小,参数量15.60M,计算量67.7GFLOPs)

    重点参考的链接:YOLOv9改进 | 提供YOLOv9全系列支持YOLOv9n、YOLOv9s、V9m、V9l、V9x的修改方式(全网独家首发) 改前的 yolov9.yaml 参数量58.35M,计算量267.1GFLOPs 改后的 yolov9-S.yaml 参数量15.60M,计算量67.7GFLOPs 这是修改 前 调用的 yolo.py 测试的 yolov9.yaml 的打印网络情况,包含参数量、

    2024年03月27日
    浏览(51)
  • 经典卷积神经网络——VGG16

    我们都知道Alexnet是卷积神经网络的开山之作,但是由于卷积核太大,移动步长大,无填充,所以14年提出的VGG网络解决了这一问题 VGG网络由牛津大学在2014年ImageNet挑战赛本地和分类追踪分别获得了第一名和第二名。研究卷积网络深度对其影响在大规模图像识别设置中的准确性

    2024年02月03日
    浏览(53)
  • 计算GMAC和GFLOPS

    GMAC 代表“Giga Multiply-Add Operations per Second”(每秒千兆乘法累加运算),是用于衡量深度学习模型计算效率的指标。它表示每秒在模型中执行的乘法累加运算的数量,以每秒十亿 (giga) 表示。 乘法累加 (MAC) 运算是许多数学计算中的基本运算,包括矩阵乘法、卷积和深度学习中

    2024年02月06日
    浏览(15)
  • 深度学习12. CNN经典网络 VGG16

    VGG(Visual Geometry Group)是一个视觉几何组在2014年提出的深度卷积神经网络架构。 VGG在2014年ImageNet图像分类竞赛亚军,定位竞赛冠军;VGG网络采用连续的小卷积核(3x3)和池化层构建深度神经网络,网络深度可以达到16层或19层,其中VGG16和VGG19最为著名。 VGG16和VGG19网络架构非

    2023年04月09日
    浏览(26)
  • VGG16详细原理(含tensorflow版源码)

            VGG16是一个经典的卷积神经网络模型,由牛津大学计算机视觉组(Visual Geometry Group)提出,用于参加2014年的ImageNet图像分类比赛。VGG16的名称来源于网络中包含的16个卷积层,其基本结构如下: 输入层:接收大小为224x224的RGB图像。 卷积层:共13个卷积层,每个卷积

    2024年02月05日
    浏览(28)
  • pytorch实战3:基于pytorch复现VGG16

    前言 ​ 最近在看经典的卷积网络架构,打算自己尝试复现一下,在此系列文章中,会参考很多文章,有些已经忘记了出处,所以就不贴链接了,希望大家理解。 ​ 完整的代码在最后。 本系列必须的基础 ​ python基础知识、CNN原理知识、pytorch基础知识 本系列的目的 ​ 一是

    2024年02月08日
    浏览(30)
  • 基于FPGA的VGG16卷积神经网络加速器

    文章搬运自本人知乎 VGG在2014年由牛津大学Visual GeometryGroup提出,获得该年lmageNet竞赛中Localization Task(定位任务)第一名和 Classification Task (分类任务)第二名。与AlexNet相比,VGG使用了3个3x3卷积核来代替7x7卷积核,使用了2个3x3卷积核来代替5x5卷积核,从而在保证具有相同感知野的

    2024年02月06日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包