import torch
from thop import profile
import torchvision.models as models
import copy
from torch.quantization.quantize_fx import prepare_qat_fx,convert_fx,fuse_fx
import torch.nn as nn
resnet18_model = models.resnet34(pretrained=True)
tensorrt_qconfig = torch.ao.quantization.QConfig(
activation= torch.ao.quantization.FakeQuantize.with_args(observer= torch.ao.quantization.MinMaxObserver,
dtype=torch.qint8, qscheme=torch.per_tensor_symmetric, reduce_range=False),
weight= torch.ao.quantization.FakeQuantize.with_args(observer= torch.ao.quantization.PerChannelMinMaxObserver,
dtype=torch.qint8, qscheme=torch.per_channel_symmetric, reduce_range=False),
)
def qat_version_model(model):
# print(tensorrt_qconfig )
qconfig_dict = {
# Global Config
"": tensorrt_qconfig ,
# # Disable by layer-name
# "module_name": [(m, None) for m in disable_layers],
# Or disable by layer-type
}
example_inputs = (torch.randn(1, 3, 32, 32),)#通过一个例子获取graph 的实例
model_to_quantize = copy.deepcopy(model)
model_fp32_prepared = prepare_qat_fx(
model_to_quantize, qconfig_dict, example_inputs )
return model_fp32_prepared
qat_model=qat_version_model(resnet18_model)
model=convert_fx(qat_model)
input1=torch.ones(1, 3, 32, 32)
torch.onnx.export(model, (input1), “quantized_model.onnx”,
input_names=[‘input1’], output_names=[‘result’],
opset_version=13, export_params=True)
There are no QDQ operators in the exported onnx file,how to solve?