int8量化的resnet50在ImageNet数据集上的推断结果

本文介绍了将ResNet50模型(除全连接层外)量化到8位精度后,在ImageNet数据集上进行推断的结果。在1个和3个CPU核心上运行,量化模型的fps相比于全精度模型提升了1.x倍,平均延迟减少了30%-50%。尽管量化模型的fps波动和某些batch_size处出现异常fps值,但其性能提升明显。推断代码采用单线程,官方还提供了多线程方法。量化方法为Post-Training Optimization,对权重和激活层进行校准。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将resnet50除全连接层外的所有层均量化为8bit。

结果如图:
在这里插入图片描述
这是使用1个cpu核心的结果。
在这里插入图片描述
这是使用3个cpu核心的结果。

作为对比,这是全精度模型的结果。
在这里插入图片描述
1个cpu核心
在这里插入图片描述
3个cpu核心

从结果对比可以看出,量化模型的fps较全精度模型提升了1.x倍,而平均延迟则分别减少了30%和50%。

将转换后的IR模型在Ubuntu上使用cpu做了推断,fps结果和DL Workbench一致,延迟因为不会测试就没有测。

推断python代码如下:

#!/usr/bin/env python
"""
 Copyright (C) 2018-2020 Intel Corporation

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""
from __future__ import print_function
import sys
import os
from argparse import ArgumentParser, SUPPRESS
import cv2
import numpy as np
import logging as log
from openvino.inference_engine import IECore
import time

def build_argparser():
    parser = ArgumentParser(add_help=False)
    args = parser.add_argument_group('Options')
    args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.')
    args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model.", required=True,
                      type=str)
    args.add_argument("-i", "--input", help="Required. Path to a folder with images or path to an image files",
                      required=True,
                      type=str, nargs="+")
    args.add_argument("-l", "--cpu_extension",
                      help="Optional. Required for CPU custom layers. "
                           "MKLDNN (CPU)-targeted custom layers. Absolute path to a shared library with the"
                           " kernels implementations.", type=str, default=None)
    args.add_argument("-d", "--device"
### YOLOv7 Slim Neck 轻量化改进实现与优化技术 YOLOv7作为一种高效的实时目标检测模型,在实际应用中往往需要进一步的轻量化处理来适应资源受限环境。对于Slim Neck部分,主要通过减少计算量、降低内存占用以及加速推理速度来进行优化。 #### 1. 参数剪枝与稀疏化 参数剪枝是一种有效的方法,可以去除网络中的冗余连接或不重要的权重,从而减小模型大小并加快推断过程。具体到Slim Neck模块,可以通过分析各层的重要性得分,移除那些贡献较小的通道或滤波器[^1]。 ```python import torch.nn.utils.prune as prune def apply_pruning(model): for name, module in model.named_modules(): if isinstance(module, torch.nn.Conv2d): prune.ln_structured( module, name='weight', amount=0.2, # 剪枝比例 n=2, # L2范数 dim=0 # 对输入维度进行操作 ) ``` #### 2. 低秩分解 利用矩阵低秩特性对卷积核实施近似表示,能够显著减少乘法次数。例如采用CP分解或者Tucker分解等方式重构原始张量结构,进而达到压缩效果的同时保持较高精度[^2]。 #### 3. 权重量化 将浮点型权值转换成低位整数形式存储和运算,不仅节省空间还能提高硬件执行效率。通常情况下会选择INT8作为替代方案,并配合相应校准流程确保性能损失最小化[^3]。 ```python from torchvision import models model = models.resnet18(pretrained=True) # Quantization Aware Training setup model.qconfig = torch.quantization.get_default_qat_qconfig('fbgemm') torch.quantization.prepare_qat(model, inplace=True) ``` #### 4. 动态调整特征图分辨率 适当缩小中间层输出尺寸有助于减轻后续阶段负担,特别是在设计Slim Neck时可考虑引入渐进式下采样策略,即随着深度增加逐步降低空间维数而不影响整体表现力[^4]。 #### 5. 使用更高效的基础组件 替换传统卷积操作为Depthwise Separable Convolution等更加紧凑的设计模式;同时探索MobileNet V3提出的EfficientNet架构下的复合缩放法则应用于Neck构建之中[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值