Skip to content

CUDA Remap gives incorrect result or crashes for 1 row/column src Mats #3412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
anklebreaker opened this issue Jan 7, 2023 · 4 comments · Fixed by #3418
Closed

CUDA Remap gives incorrect result or crashes for 1 row/column src Mats #3412

anklebreaker opened this issue Jan 7, 2023 · 4 comments · Fixed by #3418

Comments

@anklebreaker
Copy link

anklebreaker commented Jan 7, 2023

System information (version)

OpenCV & OpenCV Contrib 4.7.0
CUDA 11.7
Python 3.9.5
Ubuntu 20.04 x86_64
NVIDIA T4

Detailed description

When calling cv2.cuda.remap() with a Mat that has one row, an output of all 0s is produced (sometimes there are negligible floating points values) or the destination Mat is not overwritten. When doing the similar cpu operation in cv2.remap(), the correct output is produced. An example of this use case is using remap as lookup table for larger tables or floating point values.

Changing the src size to at least two rows produces the correct result.

Changing the src size to one column crashes with following error message: cv2.error: OpenCV(4.7.0) /home/ubuntu/opencv_contrib-4.7.0/modules/cudev/include/opencv2/cudev/ptr2d/texture.hpp:167: error: (-217:Gpu API call) invalid argument in function 'create'. Similarly changing to 2 columns produced correct result.

This was tested across several combinations of borderMode, interpolation, and channel number and all reproduced the same behavior.

This case had previously worked in OpenCV 4.5.5 before I updated to 4.7.0. Tested only in Python.

Steps to reproduce

Some toy code to reproduce the problem

        # wrong result
        test_src, test_x, test_y, test_dst = cv2.cuda_GpuMat(1, 32766, cv2.CV_32FC2), cv2.cuda_GpuMat(100, 100, cv2.CV_32FC1), cv2.cuda_GpuMat(100, 100, cv2.CV_32FC1), cv2.cuda_GpuMat(100, 100, cv2.CV_32FC2)
        test_src.upload(np.ones((1, 32766), dtype=np.float32) * 13.3)
        test_x.upload(np.ones((100, 100), dtype=np.float32) * np.pi)
        test_y.upload(np.zeros((100, 100), dtype=np.float32))
        cv2.cuda.remap(test_src, test_x, test_y, cv2.INTER_NEAREST, dst=test_dst, borderMode=cv2.BORDER_REPLICATE)
        test_out = np.zeros((100, 100), dtype=np.float32) + 42
        test_dst.download(dst=test_out)
        print(test_out)
        
        # correct result
        cv2.remap(np.ones((1, 32766), dtype=np.float32) * 13.3, np.ones((100, 100), dtype=np.float32) * np.pi, np.zeros((100, 100), dtype=np.float32), cv2.INTER_NEAREST, dst=test_out, borderMode=cv2.BORDER_REPLICATE)
        print(test_out)
        
        # correct result
        test_src, test_x, test_y, test_dst = cv2.cuda_GpuMat(2, 32766, cv2.CV_32FC2), cv2.cuda_GpuMat(100, 100, cv2.CV_32FC1), cv2.cuda_GpuMat(100, 100, cv2.CV_32FC1), cv2.cuda_GpuMat(100, 100, cv2.CV_32FC2)
        test_src.upload(np.ones((2, 32766), dtype=np.float32) * 13.3)
        test_x.upload(np.ones((100, 100), dtype=np.float32) * np.pi)
        test_y.upload(np.zeros((100, 100), dtype=np.float32))
        cv2.cuda.remap(test_src, test_x, test_y, cv2.INTER_NEAREST, dst=test_dst, borderMode=cv2.BORDER_REPLICATE)
        test_out = np.zeros((100, 100), dtype=np.float32) + 42
        test_dst.download(dst=test_out)
        print(test_out)
Issue submission checklist
  • [ x ] I report the issue, it's not a question
  • [ x ] I checked the problem with documentation, FAQ, open issues,
    forum.opencv.org, Stack Overflow, etc and have not found any solution
  • [ x ] I updated to the latest OpenCV version and the issue is still there
  • [ x ] There is reproducer code and related data files: videos, images, onnx, etc
@cudawarped
Copy link
Contributor

If you need a quick fix you could pass a non default CUDA stream to remap to avoid using the texture implementation.

@cudawarped
Copy link
Contributor

Does this patch #3418 fix your issue?

@anklebreaker
Copy link
Author

Yes, this did fix it. Thanks for looking into it!

@cudawarped
Copy link
Contributor

That's great can you close the issue then please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants