Issue 1:
System information (version)

OpenCV => 4.5.0, 4.5.2, 4.5.4

Operating System / Platform => Linux x86_64 Ubuntu 18.04, 20.04

Compiler => C++/GCC 7.5.0, running OpenCV through python 3.8

Detailed description

Hello!

I'm aware that if CUDA is initialized in the main process, but accessed via a forked child process, then CUDA is not expected to work (see here). This is an issue with CUDA and occurs with multiple CUDA-based packages including OpenCV, numba, pytorch, etc.

However, I'm experiencing an issue with OpenCV DNN DetectionModels when:

CUDA is initialized in the main process

a trivial child process is forked

the model is used within the main process

Specifically, instead of throwing any errors, the model returns whatever the last results were, forever.

Steps to reproduce

I've tested this with multiple OpenCV versions compiled with CUDA using the following python script:

import cv2
import time
from multiprocessing import Process

def func():
for i in range(5):
print(i)
time.sleep(1)

if __name__ == '__main__':
print('Using opencv version', cv2.__version__)
print('loading yolo dnn model')
net = cv2.dnn.readNet('/path/to/yolov4.weights', '/path/to/yolov4.cfg')
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA_FP16)

model = cv2.dnn_DetectionModel(net)
model.setInputParams(size=(512, 512), scale=1 / 255)

print('loading images')
img_with_human = cv2.resize(cv2.imread('/path/to/lena.jpg'), (512, 512))
linux_logo_image = cv2.resize(cv2.imread('/path/to/LinuxLogo.jpg'), (512, 512))

print('Running yolo on the linux logo image:', model.detect(linux_logo_image))
print('Running yolo on image with a human:', model.detect(img_with_human))

print('starting child process')
p1 = Process(target=func)
p1.start()
time.sleep(2)
print('Running yolo on the linux logo image:', model.detect(linux_logo_image))
p1.join()
print('child process completed')
print('Running yolo on the linux logo image:', model.detect(linux_logo_image))

which prints the following:

Using opencv version 4.5.0
loading yolo dnn model
loading images
Running yolo on the linux logo image: ((), (), ())
Running yolo on image with a human: (array([[0],
[0],
[0],
[0],
[0],
[0]], dtype=int32), array([[0.62353516],
[0.6425781 ],
[0.87402344],
[0.89208984],
[0.5029297 ],
[0.5678711 ]], dtype=float32), array([[ 41, 48, 354, 459],
[ 39, 46, 360, 463],
[ 41, 46, 388, 464],
[ 40, 43, 391, 469],
[ 43, 81, 384, 429],
[ 43, 79, 384, 431]], dtype=int32))
starting child process
0
1
2
Running yolo on the linux logo image: (array([[0],
[0],
[0],
[0],
[0],
[0]], dtype=int32), array([[0.62353516],
[0.6425781 ],
[0.87402344],
[0.89208984],
[0.5029297 ],
[0.5678711 ]], dtype=float32), array([[ 41, 48, 354, 459],
[ 39, 46, 360, 463],
[ 41, 46, 388, 464],
[ 40, 43, 391, 469],
[ 43, 81, 384, 429],
[ 43, 79, 384, 431]], dtype=int32))
3
4
child process completed
Running yolo on the linux logo image: (array([[0],
[0],
[0],
[0],
[0],
[0]], dtype=int32), array([[0.62353516],
[0.6425781 ],
[0.87402344],
[0.89208984],
[0.5029297 ],
[0.5678711 ]], dtype=float32), array([[ 41, 48, 354, 459],
[ 39, 46, 360, 463],
[ 41, 46, 388, 464],
[ 40, 43, 391, 469],
[ 43, 81, 384, 429],
[ 43, 79, 384, 431]], dtype=int32))

As we can see, when the model runs in the main process on the LinuxLogo image, it returns no detections. Then when it runs on the image of Lena, it returns some detections. A fairly basic child process is forked, and then in the main process the model runs again on the LinuxLogo image but now returns detections. Even after the child process is joined, the model is still returning detections on the LinuxLogo image.

I don't really understand what's happening under the hood, but somehow the DNN model is being corrupted. I've done similar tests with PyTorch and haven't experienced the same issue, so I'm hoping it's fixable! OpenCV is really fast and I would prefer to use it.

Appreciate your help!

The images I'm using are lena.jpg, LinuxLogo.jpg. And here are the yolo cfg and weights files.

Issue submission checklist

I report the issue, it's not a question

I checked the problem with documentation, FAQ, open issues,

forum.opencv.org, Stack Overflow, etc and have not found any solution

I updated to the latest OpenCV version and the issue is still there

There is reproducer code and related data files: videos, images, onnx, etc

Issue 2:
System information (version)

OpenCV => 2.4.13.6

Operating System / Platform => Windows 10 64 Bit

Compiler => Visual Studio 2017

Detailed description

When compiling my project using x86 config, there were no errors.

When compiling using x64 config, errors were all about type_c.h in opencv.

It seems an issue about <emmintrin.h>, but I don't know what's wrong.

Error message:

Steps to reproduce