mmcv
mmcv copied to clipboard
Initialization error in function 'allocate' when loading mmcv and using OpenCV cv2.cuda.GpuMat()
I am having the following error:
cv2.error: OpenCV(4.7.0) /home/brian/Downloads/tmp/opencv-build-python3.7/opencv/modules/core/src/cuda/gpu_mat.cu:116: error: (-217:Gpu API call) initialization error in function 'allocate'
when I call the upload function of an GpuMat object of OpenCV every time I import mmcv. If I do not import it, everything works fine.
I have built OpenCV (4.7.0) with CUDA in my system (Ubuntu 22.04) with an RTX GeForce 2080 Ti. I am using CUDA 11.7 and I also built mmengine (latest version) and mmcv (v1.7.1). I can use mmcv perfectly fine (and OpenCV if I do not import mmcv). But when trying to use both, I got that error. Any suggestion of what could I have done wrong?
Did you find any solution to this?
Did you find any solution to this?
Hi Jorgro, actually I didn't. The error happened when I loaded mmcv and opencv in different processes (because I used multiprocessing). For the record, I had the same code running with multiple threads instead of processes and it worked fine. The workaround I found was to import mmcv inside the function that was run by my process. Honestly, I don't remember what was my reasoning for that, but it worked. Hope it helps :)
In my case, I was also using multiprocessing.
Explanation for anyone else experiencing this issue: The error happens because multiprocessing uses fork()
by default and a CUDA context can't be shared between two different processes (https://stackoverflow.com/questions/22950047/cuda-initialization-error-after-fork/22950549#22950549).
My solution was to use mp.set_start_method('spawn', force=True)
in the beginning of the program. Thus this problem is not related to mmcv or opencv themselves, but rather how they are used in multiprocessing.
If you want to read further on this issue:
- https://forums.developer.nvidia.com/t/python-opencv-multiprocessing-doesnt-work-with-cuda/180195
- https://stackoverflow.com/questions/22950047/cuda-initialization-error-after-fork/22950549#22950549