前言
关于计算量(FLOPs)参数量(Params)的一个直观理解,便是计算量对应时间复杂度,参数量对应空间复杂度,即计算量要看网络执行时间的长短,参数量要看占用显存的量。
计算量: FLOPs,FLOP时指浮点运算次数,s是指秒,即每秒浮点运算次数的意思,考量一个网络模型的计算量的标准。越小越好
参数量: Params,是指网络模型中需要训练的参数总数。越小越好

了解以上概念后,接下来便是如何计算这两个值。
一个很常见的方法便是通过ptflos包来实现。
# -- coding: utf-8 --
import torchvision
from ptflops import get_model_complexity_info
model = torchvision.models.alexnet(pretrained=False)
flops, params = get_model_complexity_info(model, (3, 224, 224), as_strings=True, print_per_layer_stat=True)
print('flops: ', flops, 'params: ', params)
这段代码可以说是即插即用。
DAB-DETR模型
博主以DAB-DETR模型为例,运行时报错,这是由于权重文件于模型配置文件不匹配导致的
权重文件与模型配置不匹配
RuntimeError: Error(s) in loading state_dict for DABDeformableDETR:
size mismatch for input_proj.0.0.weight: copying a param with shape torch.Size([256, 512, 1, 1]) from checkpoint, the shape in current model is torch.Size([256, 128, 1, 1]).
size mismatch for input_proj.1.0.weight: copying a param with shape torch.Size([256, 1024, 1, 1]) from checkpoint, the shape in current model is torch.Size([256, 256, 1, 1]).
size mismatch for input_proj.2.0.weight: copying a param with shape torch.Size([256, 2048, 1, 1]) from checkpoint, the shape in current model is torch.Size([256, 512, 1, 1]).
size mismatch for input_proj.3.0.weight: copying a param with shape torch.Size([256, 2048, 3, 3]) from checkpoint, the shape in current model is torch.Size([256, 512, 3, 3]).
修改num_channels的值即可,原本为【128,256,512】
if return_interm_layers:
# return_layers = {"layer1": "0", "layer2": "1", "layer3": "2", "layer4": "3"}
return_layers = {
"layer2": "0", "layer3": "1", "layer4": "2"}
self.strides = [8, 16, 32]
self.num_channels = [512, 1024, 2048]
推理代码
推理代码如下:几乎所有的DETR类模型的推理代码都是可以通用的。
import json
import os, sys
import torch
import numpy as np
from models import build_DABDETR
from models.dab_deformable_detr import build_dab_deformable_detr

本文介绍了计算量(FLOPs)和参数量(Params)在深度学习中的意义,展示了如何通过代码计算这些值,并详细讲述了在使用DAB-DETR、DN-DETR和YOLO模型时遇到的问题,如权重文件匹配、GPU/CPU运算和参数计算错误的解决方案。
最低0.47元/天 解锁文章
6311

被折叠的 条评论
为什么被折叠?



