notAI-tech / NudeNet

Lightweight nudity detection
https://nudenet.notai.tech/
GNU Affero General Public License v3.0
1.76k stars 342 forks source link

Can't open ONNX model in OpenCV #99

Open silverark opened 3 years ago

silverark commented 3 years ago

It would be amazing if I could use the onnx detector model in openCV using the DNN module. I'm wondering if anyone here knows what might be the issue with the error message below which is thrown then trying to load the model using net = cv2.dnn.readNetFromONNX("/home/matt/.NudeNet/detector_v2_default_checkpoint.onnx")

[ERROR:0] global /tmp/pip-req-build-dzetuct2/opencv/modules/dnn/src/onnx/onnx_importer.cpp (2108) handleNode DNN/ONNX: ERROR during processing node with 1 inputs and 4 outputs: [Split]:(clipped_boxes/unstack:0)
Traceback (most recent call last):
  File "cv.py", line 5, in <module>
    net = cv2.dnn.readNetFromONNX("/home/matt/.NudeNet/detector_v2_default_checkpoint.onnx")                                                                                     
cv2.error: OpenCV(4.5.2) /tmp/pip-req-build-dzetuct2/opencv/modules/dnn/src/onnx/onnx_importer.cpp:2129: error: (-2:Unspecified error) in function 'handleNode'                  
> Node [Split]:(clipped_boxes/unstack:0) parse error: OpenCV(4.5.2) /tmp/pip-req-build-dzetuct2/opencv/modules/dnn/src/layers/slice_layer.cpp:164: error: (-215:Assertion failed)
inputs.size() == 1 in function 'getMemoryShapes'

I'm loading the model in python using opencv:

import numpy as np
import cv2

print(cv2.__version__)
net = cv2.dnn.readNetFromONNX("/home/matt/.NudeNet/detector_v2_default_checkpoint.onnx")
img = cv2.cvtColor(cv2.imread('./boobs.jpeg'), cv2.COLOR_BGR2GRAY)
blob= cv2.dnn.blobFromImage(img, size=(64,256))
net.setInput(blob)
net.forward()

I'm using the latest version of nudenet, version 4.5.2 of opencv.

I've re-converted the model to use opcode 11 as I saw some people were having issues with the GPU and onnxruntime but that didn't help.

I have opened a issue with opencv too as I'm not sure where the issue actually is!