<h5 dir="auto">System information (version)</h5>
<ul dir="auto">
<li>OpenCV =&gt; 4.5.0, 4.5.2, 4.5.4</li>
<li>Operating System / Platform =&gt; Linux x86_64 Ubuntu 18.04, 20.04</li>
<li>Compiler =&gt; C++/GCC 7.5.0, running OpenCV through python 3.8</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">Hello!<br>
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 <a href="https://forums.developer.nvidia.com/t/python-opencv-multiprocessing-doesnt-work-with-cuda/180195" rel="nofollow">here</a>). This is an issue with CUDA and occurs with multiple CUDA-based packages including OpenCV, numba, pytorch, etc.</p>
<p dir="auto">However, I'm experiencing an issue with OpenCV DNN DetectionModels when:</p>
<ul dir="auto">
<li>CUDA is initialized in the main process</li>
<li>a trivial child process is forked</li>
<li>the model is used within the main process</li>
</ul>
<p dir="auto">Specifically, instead of throwing any errors, the model returns whatever the last results were, forever.</p>
<h5 dir="auto">Steps to reproduce</h5>
<p dir="auto">I've tested this with multiple OpenCV versions compiled with CUDA using the following python script:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="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))"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">cv2</span>
<span class="pl-k">import</span> <span class="pl-s1">time</span>
<span class="pl-k">from</span> <span class="pl-s1">multiprocessing</span> <span class="pl-k">import</span> <span class="pl-v">Process</span>

<span class="pl-k">def</span> <span class="pl-en">func</span>():
    <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">5</span>):
        <span class="pl-en">print</span>(<span class="pl-s1">i</span>)
        <span class="pl-s1">time</span>.<span class="pl-en">sleep</span>(<span class="pl-c1">1</span>)

<span class="pl-k">if</span> <span class="pl-s1">__name__</span> <span class="pl-c1">==</span> <span class="pl-s">'__main__'</span>:
    <span class="pl-en">print</span>(<span class="pl-s">'Using opencv version'</span>, <span class="pl-s1">cv2</span>.<span class="pl-s1">__version__</span>)
    <span class="pl-en">print</span>(<span class="pl-s">'loading yolo dnn model'</span>)
    <span class="pl-s1">net</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-s1">dnn</span>.<span class="pl-en">readNet</span>(<span class="pl-s">'/path/to/yolov4.weights'</span>, <span class="pl-s">'/path/to/yolov4.cfg'</span>)
    <span class="pl-s1">net</span>.<span class="pl-en">setPreferableBackend</span>(<span class="pl-s1">cv2</span>.<span class="pl-s1">dnn</span>.<span class="pl-v">DNN_BACKEND_CUDA</span>)
    <span class="pl-s1">net</span>.<span class="pl-en">setPreferableTarget</span>(<span class="pl-s1">cv2</span>.<span class="pl-s1">dnn</span>.<span class="pl-v">DNN_TARGET_CUDA_FP16</span>)

    <span class="pl-s1">model</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-en">dnn_DetectionModel</span>(<span class="pl-s1">net</span>)
    <span class="pl-s1">model</span>.<span class="pl-en">setInputParams</span>(<span class="pl-s1">size</span><span class="pl-c1">=</span>(<span class="pl-c1">512</span>, <span class="pl-c1">512</span>), <span class="pl-s1">scale</span><span class="pl-c1">=</span><span class="pl-c1">1</span> <span class="pl-c1">/</span> <span class="pl-c1">255</span>)

    <span class="pl-en">print</span>(<span class="pl-s">'loading images'</span>)
    <span class="pl-s1">img_with_human</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-en">resize</span>(<span class="pl-s1">cv2</span>.<span class="pl-en">imread</span>(<span class="pl-s">'/path/to/lena.jpg'</span>), (<span class="pl-c1">512</span>, <span class="pl-c1">512</span>))
    <span class="pl-s1">linux_logo_image</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-en">resize</span>(<span class="pl-s1">cv2</span>.<span class="pl-en">imread</span>(<span class="pl-s">'/path/to/LinuxLogo.jpg'</span>), (<span class="pl-c1">512</span>, <span class="pl-c1">512</span>))

    <span class="pl-en">print</span>(<span class="pl-s">'Running yolo on the linux logo image:'</span>, <span class="pl-s1">model</span>.<span class="pl-en">detect</span>(<span class="pl-s1">linux_logo_image</span>))
    <span class="pl-en">print</span>(<span class="pl-s">'Running yolo on image with a human:'</span>, <span class="pl-s1">model</span>.<span class="pl-en">detect</span>(<span class="pl-s1">img_with_human</span>))

    <span class="pl-en">print</span>(<span class="pl-s">'starting child process'</span>)
    <span class="pl-s1">p1</span> <span class="pl-c1">=</span> <span class="pl-v">Process</span>(<span class="pl-s1">target</span><span class="pl-c1">=</span><span class="pl-s1">func</span>)
    <span class="pl-s1">p1</span>.<span class="pl-en">start</span>()
    <span class="pl-s1">time</span>.<span class="pl-en">sleep</span>(<span class="pl-c1">2</span>)
    <span class="pl-en">print</span>(<span class="pl-s">'Running yolo on the linux logo image:'</span>, <span class="pl-s1">model</span>.<span class="pl-en">detect</span>(<span class="pl-s1">linux_logo_image</span>))
    <span class="pl-s1">p1</span>.<span class="pl-en">join</span>()
    <span class="pl-en">print</span>(<span class="pl-s">'child process completed'</span>)
    <span class="pl-en">print</span>(<span class="pl-s">'Running yolo on the linux logo image:'</span>, <span class="pl-s1">model</span>.<span class="pl-en">detect</span>(<span class="pl-s1">linux_logo_image</span>))</pre></div>
<p dir="auto">which prints the following:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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))"><pre class="notranslate"><code class="notranslate">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))
</code></pre></div>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">Appreciate your help!</p>
<p dir="auto">The images I'm using are <a href="https://github.com/opencv/opencv/blob/master/samples/data/lena.jpg">lena.jpg</a>, <a href="https://github.com/opencv/opencv/blob/master/samples/data/LinuxLogo.jpg">LinuxLogo.jpg</a>. And here are the <a href="https://github.com/AlexeyAB/darknet/blob/master/cfg/yolov4.cfg">yolo cfg</a> and <a href="https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights">weights</a> files.</p>
<h5 dir="auto">Issue submission checklist</h5>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I report the issue, it's not a question</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I checked the problem with documentation, FAQ, open issues,<br>
forum.opencv.org, Stack Overflow, etc and have not found any solution</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I updated to the latest OpenCV version and the issue is still there</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> There is reproducer code and related data files: videos, images, onnx, etc</li>
</ul>
<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; 2.4.13.6</li>
<li>Operating System / Platform =&gt; Windows 10 64 Bit</li>
<li>Compiler =&gt; Visual Studio 2017</li>
</ul>
<h5 dir="auto">Detailed description</h5>

<p dir="auto">When compiling my project using x86 config,  there were no errors.<br>
When compiling using <strong>x64</strong> config, errors were all about <em>type_c.h</em> in opencv.<br>
It seems an issue about <strong>&lt;emmintrin.h&gt;</strong>, but I don't know what's wrong.<br>
Error message:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/11918823/51451244-5e2b9280-1d6f-11e9-9e13-c394786453f7.png"><img src="https://user-images.githubusercontent.com/11918823/51451244-5e2b9280-1d6f-11e9-9e13-c394786453f7.png" alt="default" style="max-width: 100%;"></a></p>
<h5 dir="auto">Steps to reproduce</h5>