Susan19900316 / yolov5_tensorrt_int8

yolov5 tensorrt int8量化方法汇总
55 stars 13 forks source link

导出ptq量化导出onnx后onnx推理的结果和pt推理的结果差异很大 #12

Open shupinghu opened 4 months ago

shupinghu commented 4 months ago

为了方便测试,我参考作者的代码写了一个分类网络的ptq代码,然后在转onnx的时候对输出结果进行了对比,发现差异很大,不知道您这边有没有遇到过这个问题

复现步骤,使用cifar数据集,所以复现很快:

  1. python fp32_train.py
  2. python ptq.py(注意修改加载模型的路径)

aa.zip

Susan19900316 commented 4 months ago

我看你用的是qat的方式做的量化,68行有打印训练后的test_accuracy,与原始变化大吗?我没对比过onnx与pytorch的结果,我使用时是pytorch转onnx转tensorrt,对比pytorch与tensorrt的结果,pytorch_quantization这个是为转tensorrt设计的,我不确定onnx对这块是否有效

shupinghu commented 4 months ago

test_accuracy差异不大。主要是如果onnx这一步推理结果都差很多了,tensorrt差异不会更大吗? the output of torch model is tensor([[ 1.2588, -0.9620, 0.6842, 1.5087, -0.1065, -1.0762, 2.5894, 0.4193, -4.7289, -0.8528]], device='cuda:0', grad_fn=) the output of onnx model is [array([[ 1.2327292 , -0.9246984 , 0.6782162 , 1.5033406 , -0.15788133,​ -1.0333301 , 2.577277 , 0.41833997, -4.706562 , -0.8618131 ]],​ dtype=float32)]

Susan19900316 commented 4 months ago

你是分类模型吧,你测试对比accuracy就行了,纯看模型输出结果每个值的差异,意义不大

shupinghu commented 4 months ago

我只是搞了个例程先对比一下,后续要尝试其他的模型。那我去算算accuracy吧