ncnn icon indicating copy to clipboard operation
ncnn copied to clipboard

Model is not giving correct prediction scores after conversion

Open nahla-naz opened this issue 1 year ago • 1 comments

Hi,

I created a Pytorch model which is giving >80% accuracy on windows. The prediction scores are within the range of 0 and 1. However, after I convert it to NCNN, it is giving 0 and 1 as scores. I managed to solve this problem by adding a softmax layer: out = F.softmax(out, dim=1) Now, the prediction scores are between 0 and 1. But the accuracy is very low.

How can I make the accuracy of NCNN model same as that of Pytorch model?

I converted the model from .pth to .onnx using below method and converted .onnx to .param and .bin using convertmodel.com(without simplifying or optimising).

model=torch.load('model.pth',map_location=torch.device('cpu')) model.eval() input_tensor = torch.randn(1, 3, 80, 80 )
exportModel = model torch.onnx.export(exportModel, input_tensor, 'path' + ('modelNEW.onnx'), export_params=True, input_names=["data"], output_names=["softmax"])

Kindly help. Thanks.

nahla-naz avatar Jun 20 '24 09:06 nahla-naz

针对onnx模型转换的各种问题,推荐使用最新的pnnx工具转换到ncnn In view of various problems in onnx model conversion, it is recommended to use the latest pnnx tool to convert your model to ncnn

pip install pnnx
pnnx model.onnx inputshape=[1,3,224,224]

详细参考文档 Detailed reference documentation https://github.com/pnnx/pnnx https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx#how-to-use-pnnx

nihui avatar Aug 05 '24 09:08 nihui