Issue when loading ONNX model with OpenCV - opencv

I trained a custom YOLOv7 object detection model in Google Colab. I exported it to .onnx file using this command:
python export.py --weights runs/train/exp/weights/best.pt --grid --end2end --simplify --topk-all 100 --iou-thres 0.65 --conf-thres 0.3 --img-size 640 640 --max-wh 640
This is my code to load the model:
import cv2
net = cv2.dnn.readNet('runs/train/exp/weights/best.onnx')
When I run the code above, I get this error:
error: OpenCV(4.6.0) /io/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1040: error: (-2:Unspecified error) in function 'handleNode'
> Node [NonMaxSuppression#ai.onnx]:(onnx_node!NonMaxSuppression_370) parse error: OpenCV(4.6.0) /io/opencv/modules/dnn/src/layer_internals.hpp:110: error: (-2:Unspecified error) Can't create layer "onnx_node!NonMaxSuppression_370" of type "NonMaxSuppression" in function 'getLayerInstance'
However, this python code loads the model successfully:
import onnx
onnx_model = onnx.load('runs/train/exp/weights/best.onnx')
onnx.checker.check_model(onnx_model)
So seems like the issue is in OpenCV?
The .onnx file is in the correct directory.
Enviroment:
opencv-python: 4.6.0.66
torch: 1.11.0
onnx: 1.11.0
CUDA: 11.2
My PyTorch was previously version 1.12.0, but I got similar issues too. Then I read somewhere that downgrading to 1.11.0 should fix my problem. It didn't.
I downloaded a pre-exported .onnx file from google and it loaded successfully using the same code. Why can't I do it now?

Related

OpenCV test failed in Yocto-Jethro

I have added meta-openembedded recipes in the Yocto project, at Jethro branch OpenCV version is 2.4.
After building while testing the sample applications provided by opencv-samples_2.4.bb gives these samples :
adaptiveskindetector contours facedetect find_obj_calonder morphology mushroom pyramid_segmentation
bgfg_codebook convert_cascade fback_c find_obj_ferns motempl one_way_sample smiledetect
blobtrack_sample delaunay find_obj latentsvmdetect mser_sample polar_transforms tree_engine
but while testing I got this error :
OpenCV Error: Unspecified error (The function is not implemented.
Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you
are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then
re-run cmake or configure script) in cvNamedWindow, file
/home/ymj/opencv/modules/highgui/src/window.cpp, line 516
I have added in local.conf:
IMAGE_INSTALL_append += " opencv opencv-samples python-opencv"
Is anything more needed to get full-fledged opencv operations.

cv2.dnn.readNetFromDarknet error: (-212:Parsing error) Unsupported activation: relu in function 'cv::dnn::darknet::ReadDarknetFromCfgStream'

I tried to run the Openpose on darknet with weights and cfg downloaded from this place: https://github.com/lincolnhard/openpose-darknet
This is the error when I tried to create a net in Opencv
modelConfiguration = path to cfg file
modelWeights = path to weights file
darknet = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
error Traceback (most recent call last)
in ()
----> 1 darknet = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\dnn\src\darknet\darknet_io.cpp:552: error: (-212:Parsing error) Unsupported activation: relu in function 'cv::dnn::darknet::ReadDarknetFromCfgStream'
The fix is either you install the latest master branch of openCV or OpenCV version 3.4.XX.XX Only these branches support yolo4.
You can install another version of OpenCV by the following snippet:
!pip install opencv-contrib-python==3.4.13.47 --force-reinstall
as you konw, opnecv 4.0.0 does not support the relu activation now.
I found some project faced the same problem, but he adjusted opencv for darknet, Support CNN "relu" and maxpool "VALID", you can reference it.
https://github.com/chineseocr/opencv-for-darknet
hope it will solve your problem.

YoloV3 : undefined symbol: _ZN9_IplImageC1ERKN2cv3MatE when Build with OpenCV

I use https://github.com/AlexeyAB/darknet to run Yolo V3 and darknet is built using OpenCV 3.4.0.
When I tried to run this command ./darknet detector train data/obj.data cfg/objyolov3.cfg darknet53.conv.74 to train my own dataset, with OPENCV = 0 training works successfully, but when I build with OPENCV=1, I got this following error :
Loading weights from darknet53.conv.74...
seen 64
Done!
Learning Rate: 0.001, Momentum: 0.9, Decay: 0.0005
If error occurs - run training with flag: -dont_show
Resizing
896 x 896
./darknet: symbol lookup error: ./darknet: undefined symbol: _ZN9_IplImageC1ERKN2cv3MatE
The chart image showed up but closed immediately, adding -dont_show also not helping. I couldn't find similar problem on the internet, any solution for this?
I know this is very rare to encounter this problem. But I managed to solve it after discussed it with the repo's author. Insted building darknet using make, it works when I built darknet using cmake.
Edit CMakeLists.txt in darknet folder and add this line : SET(OpenCV_DIR /home/<Change this to your OPENCV path installation>/OpenCV-3.4.0/share/OpenCV/)
cmake .
make
Original github issue : https://github.com/AlexeyAB/darknet/issues/2489

Opencv Python-3.5.2 Face Recognition

I am working on Face Recognition. My code was working fine but the results were not that good so I did a little revision and along with some amendements, I updated my opencv-python from 3.4.3 to 3.4.4 and same for the opencv-contrib-python. And now I am getting syntax errors.
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Error: cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
And also,
cv2.face.LBPHFaceRecognizer_create()
AttributeError: module 'cv2.cv2' has no attribute 'face'
I didn't get any Errors like this before but after updating, I am getting these. What's the solution.
You can go back to the previous version and try.
pip uninstall opencv-python
pip install opencv-python==3.4.3
the same as opencv-contrib-python.
Then see if your code works.

Build OpenCV binding in Haskell for OpenCV 2.4

I tried to install cv library via cabal (cabal install cv).
But it brings me up an error:
Configuring CV-0.3.7...
Building CV-0.3.7...
Preprocessing library CV-0.3.7...
c2hs: C header contains errors:
dist/build/CV/Video.chs.h:22: (column 39) [ERROR] >>> Unknown identifier!
Cannot find a definition for `CV_CAP_PROP_WHITE_BALANCE_BLUE_U' in the header file.
cabal: Error: some packages failed to install:
CV-0.3.7 failed during the building phase. The exception was:
ExitFailure 1
I have read that it's problem is, that my install opencv is newer than the haskell-library needs it.
My opencv:
local/opencv 2.4.12.2-2
Open Source Computer Vision Library
So how can I fix this error now? Thanks for your help!

Resources