Tensor copy assertion error when cuda on

Hello,

I use a YOLOv5 C++ implementation based on OpenCV 4.6.0.
On CPU it runs without any issue, but when I switch to CUDA I get this error:

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.6.0-dev) /home/username/Desktop/Projects/opencv/modules/dnn/src/layers/../cuda4dnn/primitives/../csl/tensor_ops.hpp:43: error: (-215:Assertion failed) is_shape_same(dest, src) in function 'copy'

The error occurs at the line of code:

net.forward(outputs, net.getUnconnectedOutLayersNames());

I ran a sample with CUDA to verify the cmake installation, and it works fine on both CPU and GPU.

The YOLOv5 C++ implementation used is here.

Any suggestions/ideas are very much appreciated for why on CPU it works fine but when I run on GPU, the data copied between the src and dest tensors doesn’t have the same shape anymore.

I have the same problem

OpenCV: terminate handler is called! The last OpenCV error is:
OpenCV(4.6.0-dev) Error: Assertion failed (is_shape_same(dest, src)) in cv::dnn::cuda4dnn::csl::tensor_ops::copy, file I:\Opencv\opencv\modules\dnn\src\cuda4dnn\primitives\../csl/tensor_ops.hpp, line 43

Have you solved the problem now

Unfortunately no solution so far.

I can see someone opened an issue on github however. Hopefully this leads to more answers.

I had the same error and fixed it by installing a previous version of OpenCV: OpenCV 4.5.5.

See also this Git issue: OpenCV(4.6.0-dev) Error: Assertion failed (is_shape_same(dest, src)) · Issue #22586 · opencv/opencv · GitHub.