<h5 dir="auto">System information (version)</h5>
<ul dir="auto">
<li>OpenCV =&gt; 4.1.0</li>
<li>Operating System / Platform =&gt; Windows 64 Bit</li>
<li>Compiler =&gt; Visual Studio 2017</li>
<li>Gpu Model-&gt; RTX 2080 - 2 and 3 Devices tested</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">Running minmax on multiple gpus will cause GPU shared memory allocation to increase until out of memory error occurs.<br>
The problem occurs on driver version 441.22, but not on driver version 431.60.<br>
A number of other methods were tested and only minmax produces a shared memory leak.</p>
<h5 dir="auto">Steps to reproduce</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="```.cpp"><pre class="notranslate"><code class="notranslate">```.cpp
</code></pre></div>
<p dir="auto">#include <br>
#include "opencv2/imgproc.hpp"<br>
#include "opencv2/cudafilters.hpp"<br>
#include "opencv2/cudaimgproc.hpp"<br>
#include "opencv2/cudaarithm.hpp"</p>
<p dir="auto">using namespace std;<br>
using namespace cv;</p>
<p dir="auto">int main()<br>
{<br>
double min = 0;<br>
double max = 0;<br>
int numDevices = cuda::getCudaEnabledDeviceCount();<br>
cuda::GpuMat * img = new cuda::GpuMat[numDevices];</p>
<p dir="auto">for (int i = 0; i &lt; numDevices; i++)<br>
{<br>
cuda::setDevice(i);<br>
img[i] = cuda::GpuMat(500, 500, CV_32F);<br>
cuda::printShortCudaDeviceInfo(i);<br>
}</p>
<p dir="auto">for (int i = 0; ; i++)<br>
{<br>
cuda::setDevice(i % numDevices);<br>
cuda::minMax(img[i%numDevices], &amp;min, &amp;max);<br>
}<br>
}</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="```"><pre class="notranslate"><code class="notranslate">```
</code></pre></div>
<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt;3.2</li>
<li>Operating System / Platform =&gt; Windows 64 Bit</li>
<li>Compiler =&gt; Visual Studio 2017</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">the shape of outputs of <code class="notranslate">RegionLayer</code> is computed as following:<br>
</p><div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/opencv/opencv/blob/76eb38976103c86610f73d87e46b7cb5a74f0017/modules/dnn/src/layers/region_layer.cpp#L94">opencv/modules/dnn/src/layers/region_layer.cpp</a>
    </p>
    <p class="mb-0 color-fg-muted">
         Line 94
      in
      <a data-pjax="true" class="commit-tease-sha" href="/opencv/opencv/commit/76eb38976103c86610f73d87e46b7cb5a74f0017">76eb389</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L94" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="94"></td>
          <td id="LC94" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> outputs = std::vector&lt;MatShape&gt;(<span class="pl-c1">1</span>, <span class="pl-c1">shape</span>(inputs[<span class="pl-c1">0</span>][<span class="pl-c1">1</span>] * inputs[<span class="pl-c1">0</span>][<span class="pl-c1">2</span>] * anchors, inputs[<span class="pl-c1">0</span>][<span class="pl-c1">3</span>] / anchors)); </td>
        </tr>
    </tbody></table>
  </div>
</div>
<p></p>
<p dir="auto">that's does not matter if we input only one image using <code class="notranslate">blobFromImage</code>, but if we input multiple images using <code class="notranslate">blobFromImages</code> then the shape of outputs is not right, and it should changed to</p>
<div class="highlight highlight-source-c++ notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="outputs = std::vector&lt;MatShape&gt;(1, shape(inputs[0][0], inputs[0][1] * inputs[0][2] * anchors, inputs[0][3] / anchors));"><pre class="notranslate">outputs = std::vector&lt;MatShape&gt;(<span class="pl-c1">1</span>, shape(inputs[<span class="pl-c1">0</span>][<span class="pl-c1">0</span>], inputs[<span class="pl-c1">0</span>][<span class="pl-c1">1</span>] * inputs[<span class="pl-c1">0</span>][<span class="pl-c1">2</span>] * anchors, inputs[<span class="pl-c1">0</span>][<span class="pl-c1">3</span>] / anchors));</pre></div>
<p dir="auto">when forward this layer, we should also forward all inputs:<br>
</p><div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/opencv/opencv/blob/76eb38976103c86610f73d87e46b7cb5a74f0017/modules/dnn/src/layers/region_layer.cpp#L218-L222">opencv/modules/dnn/src/layers/region_layer.cpp</a>
    </p>
    <p class="mb-0 color-fg-muted">
        Lines 218 to 222
      in
      <a data-pjax="true" class="commit-tease-sha" href="/opencv/opencv/commit/76eb38976103c86610f73d87e46b7cb5a74f0017">76eb389</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L218" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="218"></td>
          <td id="LC218" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-k">const</span> <span class="pl-k">float</span> *srcData = inpBlob.<span class="pl-smi">ptr</span>&lt;<span class="pl-k">float</span>&gt;(); </td>
        </tr>

        <tr class="border-0">
          <td id="L219" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="219"></td>
          <td id="LC219" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-k">float</span> *dstData = outBlob.<span class="pl-smi">ptr</span>&lt;<span class="pl-k">float</span>&gt;(); </td>
        </tr>

        <tr class="border-0">
          <td id="L220" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="220"></td>
          <td id="LC220" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">  </td>
        </tr>

        <tr class="border-0">
          <td id="L221" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="221"></td>
          <td id="LC221" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-c"><span class="pl-c">//</span> logistic activation for t0, for each grid cell (X x Y x Anchor-index)</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L222" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="222"></td>
          <td id="LC222" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-k">for</span> (<span class="pl-k">int</span> i = <span class="pl-c1">0</span>; i &lt; rows*cols*anchors; ++i) { </td>
        </tr>
    </tbody></table>
  </div>
</div>
<p></p>
<p dir="auto">should change to</p>
<div class="highlight highlight-source-c++ notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="           const float *srcData = inpBlob.ptr&lt;float&gt;();
           float *dstData = outBlob.ptr&lt;float&gt;();
           CV_Assert(inpBlob.size[0] == outBlob.size[0]);

           for (int n = 0; n &lt; inpBlob.size[0]; n++)
           {
               // logistic activation for t0, for each grid cell (X x Y x Anchor-index)
               for (int i = 0; i &lt; rows * cols * anchors; ++i) {
               ......
               srcData += inpBlob.step1(0);
               dstData += outBlob.step1(0);
           }"><pre class="notranslate">           <span class="pl-k">const</span> <span class="pl-k">float</span> *srcData = inpBlob.ptr&lt;<span class="pl-k">float</span>&gt;();
           <span class="pl-k">float</span> *dstData = outBlob.ptr&lt;<span class="pl-k">float</span>&gt;();
           <span class="pl-en">CV_Assert</span>(inpBlob.size[<span class="pl-c1">0</span>] == outBlob.size[<span class="pl-c1">0</span>]);

           <span class="pl-k">for</span> (<span class="pl-k">int</span> n = <span class="pl-c1">0</span>; n &lt; inpBlob.size[<span class="pl-c1">0</span>]; n++)
           {
               <span class="pl-c"><span class="pl-c">//</span> logistic activation for t0, for each grid cell (X x Y x Anchor-index)</span>
               <span class="pl-k">for</span> (<span class="pl-k">int</span> i = <span class="pl-c1">0</span>; i &lt; rows * cols * anchors; ++i) {
               ......
               srcData += inpBlob.<span class="pl-c1">step1</span>(<span class="pl-c1">0</span>);
               dstData += outBlob.<span class="pl-c1">step1</span>(<span class="pl-c1">0</span>);
           }</pre></div>