简体   繁体   English

从 pytorch 转换为 ONNX 后,量化的 model 给出负精度

[英]Quantized model gives negative accuracy after conversion from pytorch to ONNX

I'm trying to train a quantize model in pytorch and convert it to ONNX.我正在尝试在 pytorch 中训练量化 model 并将其转换为 ONNX。 I employ the quantized-aware-training technique with help of pytorch_quantization package. I used the below code to convert my model to ONNX:我在 pytorch_quantization package 的帮助下采用量化感知训练技术。我使用以下代码将我的 model 转换为 ONNX:

from pytorch_quantization import nn as quant_nn
from pytorch_quantization import calib
from pytorch_quantization.tensor_quant import QuantDescriptor
from pytorch_quantization import quant_modules
import onnxruntime 
import torch
import torch.utils.data
from torch import nn
import torchvision

def export_onnx(model, onnx_filename, batch_onnx, per_channel_quantization):
    model.eval()
    quant_nn.TensorQuantizer.use_fb_fake_quant = True # We have to shift to pytorch's fake quant ops before exporting the model to ONNX

    if per_channel_quantization:
        opset_version = 13
    else:
        opset_version = 12

    # Export ONNX for multiple batch sizes
    print("Creating ONNX file: " + onnx_filename)
    dummy_input = torch.randn(batch_onnx, 3, 224, 224, device='cuda') #TODO: switch input dims by model
    input_names = ['input']
    output_names = ['Linear[fc]']  ### ResNet34
    dynamic_axes = {'input': {0: 'batch_size'}}

    try:
        torch.onnx.export(model, dummy_input, onnx_filename, input_names=input_names,
                          export_params=True, output_names=output_names, opset_version=opset_version,
                          verbose=True, enable_onnx_checker=False, do_constant_folding=True)

    except ValueError:
        warnings.warn(UserWarning("Per-channel quantization is not yet supported in Pytorch/ONNX RT (requires ONNX opset 13)"))
        print("Failed to export to ONNX")
        return False
    return True

After conversion, I get the following warnings:转换后,我收到以下警告:

warnings.warn("'enable_onnx_checker' is deprecated and ignored. It will be removed in " W0305 12:39:40.472136 140018114328384 tensor_quantizer.py:280] Use Pytorch's native experimental fake quantization. warnings.warn("'enable_onnx_checker' 已弃用并被忽略。它将在 " W0305 12:39:40.472136 140018114328384 tensor_quantizer.py:280] 中删除] 使用 Pytorch 的原生实验假量化。

/usr/local/lib/python3.8/dist-packages/pytorch_quantization/nn/modules/tensor_quantizer.py:285: TracerWarning: Converting a tensor to a Python number might cause the trace to be incorrect. /usr/local/lib/python3.8/dist-packages/pytorch_quantization/nn/modules/tensor_quantizer.py:285: TracerWarning: 将张量转换为 Python 数字可能会导致跟踪不正确。 We can't record the data flow of Python values, so this value will be treated as a constant in the future.我们无法记录Python个值的数据流向,所以以后这个值会被当作常量处理。 This means that the trace might not generalize to other inputs!这意味着跟踪可能不会推广到其他输入!

Also, the accuracy is not valid for ONNX model!此外,精度不适用于 ONNX 模型!

Accuracy summary:
+-----------+-------+
| Stage     |  Top1 |
+-----------+-------+
| Finetuned | 38.03 |
| ONNX      | -1.00 |
+-----------+-------+

More info is here:更多信息在这里:

pytorch 1.10.2+cu102
torchvision 0.11.3+cu102 
TensorRT  8.2.3-1+cuda11.4
ONNX 1.11.0
ONNX Runtime 1.10.0
cuda 11.6
python 3.8

What is the problem with ONNX conversion? ONNX 转换有什么问题?

After some tries, I found that there is a version conflict.经过一些尝试,我发现存在版本冲突。 I changed the versions accordingly:我相应地更改了版本:

onnx == 1.9.0
onnxruntime == 1.8.1
pytorch == 1.9.0+cu111
torchvision == 0.10.0+cu111

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM