Inference not correct on exported onnx model
See original GitHub issueInstructions To Reproduce the Issue:
To reproduce this behaviour please also refer to the following notebook: https://github.com/randombenj/detectron2onnx-inference/blob/master/detectron2-onnx.ipynb
When using the Caffe2Tracer
to export to an onnx trace model, the model does not perform as expected (with the
default trained weights). The inference behaviour is different than when running the inference with the DefaultPredictor
.
Configure model and weights:
model_config = "COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml"
cfg = get_cfg()
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
cfg.merge_from_file(model_zoo.get_config_file(model_config))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(model_config)
cfg.MODEL.DEVICE = 'cpu'
Load and prepare image:
im = cv2.imread('datasets/coco/val2017/000000023937.jpg')
batch_size, channels, height, width = 1, 3, 480, 640
im = cv2.resize(im, (width, height))
image = torch.as_tensor(im.astype("float32")).view(batch_size, height, width, channels)
# NWHC -> NCHW
image = image.permute(0, 3, 1, 2)
# img_info for inference
image_info = torch.tensor(
[width, height, 1.0]
).to(dtype=torch.float32).view(batch_size, 3)
Run inference using DefaultPredictor
:
predictor = DefaultPredictor(cfg)
outputs = predictor(im)
Export to onnx
:
torch_model = build_model(cfg)
DetectionCheckpointer(torch_model).resume_or_load(cfg.MODEL.WEIGHTS)
torch_model.eval()
tracer = Caffe2Tracer(
cfg,
torch_model,
[{"image": image[0]}]
)
Run onnx
inference:
model = onnx.load("model.onnx") # or use the `onnx_model` from above
onnx.checker.check_model(model)
W = {
model.graph.input[0].name: image.data.numpy(),
model.graph.input[1].name: image_info.data.numpy()
}
rep = backend.prepare(model)
onnx_outputs = rep.run(W)
- Am I doing something wrong with the tracing (input samples), or in the onnx inference?
- Could this behaviour be due to tracing and not scripting export?
Expected behavior:
I would not expect such a big difference in inference accuracy, otherwise, the export is kind of pointless.
Environment:
Paste the output of the following command:
--------------------- ---------------------------------------------------------
sys.platform linux
Python 3.6.9 (default, Jan 26 2021, 15:33:00) [GCC 8.4.0]
numpy 1.19.2
detectron2 0.3 @/home/users/benj/Work/detectron2/detectron2
Compiler GCC 7.5
CUDA compiler not available
DETECTRON2_ENV_MODULE <not set>
PyTorch 1.8.0 @/usr/local/lib/python3.6/dist-packages/torch
PyTorch debug build False
GPU available False
Pillow 7.2.0
torchvision 0.9.0 @/usr/local/lib/python3.6/dist-packages/torchvision
fvcore 0.1.3.post20210310
cv2 4.4.0
--------------------- ---------------------------------------------------------
PyTorch built with:
- GCC 7.3
- C++ Version: 201402
- Intel(R) Math Kernel Library Version 2020.0.0 Product Build 20191122 for Intel(R) 64 architecture applications
- Intel(R) MKL-DNN v1.7.0 (Git Hash 7aed236906b1f7a05c0917e5257a1af05e9ff683)
- OpenMP 201511 (a.k.a. OpenMP 4.5)
- NNPACK is enabled
- CPU capability usage: AVX2
- Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=10.2, CUDNN_VERSION=7.6.5, CXX_COMPILER=/opt/rh/devtoolset-7/root/usr/bin/c++, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -DNDEBUG -DUSE_KINETO -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Wno-stringop-overflow, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, TORCH_VERSION=1.8.0, USE_CUDA=ON, USE_CUDNN=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON,
Issue Analytics
- State:
- Created 3 years ago
- Comments:10
Top Results From Across the Web
How to improve the accuracy and recall of exported ONNX ...
How to improve the accuracy and recall of exported ONNX model? · 1. Are we doing something wrong on the inference side from...
Read more >How to Convert a PyTorch Model to ONNX in 5 Minutes
The input and names that you would like to use for the exported model. Let's start by ensuring that the model is in...
Read more >Onnx inference does not correctly Yolov5
I want to inference the trained model in C++ using Opencv ( dnn::readnet ) so I tried both commands of below: python export.py...
Read more >Exporting transformers models
Converting an ONNX model using the transformers.onnx package ... onnx/bert-base-cased/model.onnx. This export can now be used in the ONNX inference runtime:.
Read more >Convert a PyTorch Model to ONNX and OpenVINO™ IR
When the conversion is successful, the last line of the output will read: ONNX model exported to fastseg1024.onnx. if not onnx_path.exists(): dummy_input ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the hint with the resizing, turns out this was actually what caused the difference in accuracy. For anyone stumbling upon this issue here is how you can run
caffe2
andonnx
inference:Now I get the exact same inference as with the
DefaultPredictor
hello, I’m using
backend.prepare
to run inference, however, there is a error in the termial.Code:
Terminal As following:
I have no idea how that came about , can you offer some help?