spmallick / learnopencv

Learn OpenCV : C++ and Python Examples
https://www.learnopencv.com/
20.67k stars 11.52k forks source link

Reshape issue while running net.forward() #832

Open ArushikaBansal opened 1 year ago

ArushikaBansal commented 1 year ago

While running notebook 'multi-person-openpose.ipynb' encountered an issue in the code

t = time.time() net = cv2.dnn.readNetFromCaffe(protoFile, weightfile) net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV) net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)

if device == "cpu": net.setPreferableBackend(cv2.dnn.DNN_TARGET_CPU) print("Using CPU device") elif device == "gpu": net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA) print("Using GPU device")

Fix the input Height and get the width according to the Aspect Ratio inHeight = 368 inWidth = int((inHeight/frameHeight)*frameWidth)

inpBlob = cv2.dnn.blobFromImage(image1, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False)

net.setInput(inpBlob) output = net.forward() print("Time Taken = {}".format(time.time() - t))


error Traceback (most recent call last)

in () 21 # inpBlob = np.squeeze(inpBlob,0) 22 net.setInput(inpBlob) ---> 23 output = net.forward() 24 # print("Time Taken = {}".format(time.time() - t)) error: OpenCV(4.7.0) /io/opencv/modules/core/src/matrix.cpp:1240: error: (-209:Sizes of input arguments do not match) Requested and source matrices have different count of elements in function 'reshape' Please help me to solve this issue. Thanks in advance :)
brmarkus commented 1 year ago

Looks like the code doesn't match to the model (anylonger). Which version of the code/notebook/repo and which version of the model do you use? Do you use the original model of the sample, or used your own retrieved one? Can you add a few print-log-messages of the provided input-shape and of the expected input-shape? You might check the model via a model-viewer like "Netron" and check the expected input-shape.

ArushikaBansal commented 1 year ago

I have simply run the notebook below: https://github.com/spmallick/learnopencv/blob/master/OpenPose-Multi-Person/multi-person-openpose.ipynb with protoFile and weightFile as given. I kept the input also the same. The Openpose vesion is 4.7.0

brmarkus commented 1 year ago

Now I tried it under MS-Win10 in a Python virt-env using thhe Jupyter notebook as well. I got a cv2.error: Unknown C++ exception with the instruction net.setInput(inpBlob).

Via Google search I found e.g. this pointer: "https://stackoverflow.com/questions/75334101/layeroutputs-net-forwardoutput-layers-names-cv2-error-unknown-c-exception"

Yes, I had manually installed pip3 install opencv-python, and now manually installed pip3 install opencv-python==4.6.0.66.

With this version I got the Jupyter notebook to run successfully:

image

image

image