<h3 dir="auto">System Information</h3>
<p dir="auto">OpenCV versions tested: 4.5.5, 4.7.0<br>
Operating System / Platform: Ubuntu 18.04<br>
Device: NVIDIA Jetson TX2 DevKit<br>
CUDA version: 10.2<br>
CUDNN version: 8.2.1</p>
<h3 dir="auto">Detailed description</h3>
<p dir="auto">Hi,</p>
<p dir="auto">I was using OpenCV 4.5.5, backend CUDA on a NVIDIA Jetson TX2 Devkit with the specs defined above. A couple of days I decided to update to OpenCV 4.7.0 to check if I had some boost in performance for the models I'm currently using. However what I did saw was a performance loss (in terms of execution time) for the majority of the models. Do you know what is the reason for this loss of performance?</p>
<p dir="auto">This is the execution times obtained for both versions of OpenCV:</p>


<h2 dir="auto">Test 1</h2><ul dir="auto"><li>Device: <strong>TX2 DevKit</strong></li><li><strong>CUDA </strong>version: <strong>10.2</strong></li><li><strong>CUDNN</strong> version: <strong>8.2.1</strong></li><li><strong>OpenCV </strong>version:<strong> 4.7.0</strong></li></ul><p dir="auto"><br></p>
<table role="table">
<thead>
<tr>
<th>Version</th>
<th>Model 1</th>
<th>Model 2</th>
<th>Model 3</th>
<th>Model 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>Input Size</td>
<td>(112, 112)</td>
<td>(112, 112)</td>
<td>(112, 112)</td>
<td>(112, 112)</td>
</tr>
<tr>
<td>Model Architecture</td>
<td>Resnet100</td>
<td>MobileFaceNet</td>
<td>Resnet100</td>
<td>Resnet18</td>
</tr>
<tr>
<td>Jetson CPU</td>
<td>702</td>
<td>20.5</td>
<td>699</td>
<td>167</td>
</tr>
<tr>
<td>Jetson GPU</td>
<td><strong>91.7</strong></td>
<td><strong>10.5</strong></td>
<td><strong>91.6</strong></td>
<td><strong>52.2</strong></td>
</tr>
</tbody>
</table>





<h2 dir="auto">Test 2</h2><ul dir="auto"><li>Device: <strong>TX2 DevKit</strong></li><li><strong>CUDA </strong>version: <strong>10.2</strong></li><li><strong>CUDNN</strong> version: <strong>8.2.1</strong></li><li><strong>OpenCV </strong>version:<strong> 4.5.5</strong></li></ul><p dir="auto"><br></p>
<table role="table">
<thead>
<tr>
<th>Version</th>
<th>Model 1</th>
<th>Model 2</th>
<th>Model 3</th>
<th>Model 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>Input Size</td>
<td>(112, 112)</td>
<td>(112, 112)</td>
<td>(112, 112)</td>
<td>(112, 112)</td>
</tr>
<tr>
<td>Model Architecture</td>
<td>Resnet100</td>
<td>MobileFaceNet</td>
<td>Resnet100</td>
<td>Resnet18</td>
</tr>
<tr>
<td>Jetson CPU</td>
<td>1088</td>
<td>23.1</td>
<td>1096</td>
<td>257</td>
</tr>
<tr>
<td>Jetson GPU</td>
<td><strong>60.9</strong></td>
<td><strong>5.34</strong></td>
<td><strong>60.7</strong></td>
<td><strong>19.9</strong></td>
</tr>
</tbody>
</table>



<p dir="auto"><strong>Note:</strong> Both tests were built with the same OpenCV flags and requirements, the only thing that changed was the version of both opencv and opencv_contrib. Moreover, all the execution times presented in those tables are in ms.</p>
<h3 dir="auto">Steps to reproduce</h3>
<p dir="auto">You can use this piece of code to reproduce this issue/loss of performance:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#include &lt;thread&gt;
#include &lt;fstream&gt;

#include &lt;opencv2/imgproc.hpp&gt;
#include &lt;opencv2/dnn.hpp&gt;
#include &lt;opencv2/core.hpp&gt;
#include &lt;opencv2/highgui.hpp&gt;

int main(int argc, char** argv)
{
    auto imageToTest = argv[1];
    auto modelToTest = argv[2];
    int modelInputWidth = atoi(argv[3]);
    int modelInputHeight = atoi(argv[4]);

    cv::Size currSize = cv::Size(modelInputWidth, modelInputHeight);
    std::string modelToTestOnnx = modelToTest;
    std::string imagefilename = imageToTest;
    unsigned int num_inferences = 100;

    cv::dnn::Net net = cv::dnn::readNetFromONNX(modelToTestOnnx);

    net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
    net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);

    cv::Mat img = cv::imread(imagefilename, cv::IMREAD_ANYCOLOR);
    cv::Mat resized;
    cv::resize(img, resized, currSize);
	
	std::vector&lt;cv::Mat&gt; imgBatch = { resized };
    bool swaprbchannels = false;
    cv::Mat blob = cv::dnn::blobFromImages(imgBatch, 1.0f / 255.0f, cv::Size(), cv::Scalar(), swaprbchannels, false, CV_32F);

    net.setInput(blob);

    std::vector&lt;cv::String&gt; unconnectedOutLayerNames = net.getUnconnectedOutLayersNames();

    std::vector&lt;cv::Mat&gt; outputs;
    outputs.clear();

    auto timeLoadModelPlusInference1 = std::chrono::high_resolution_clock::now();

    net.forward(outputs, unconnectedOutLayerNames);

    auto timeLoadModelPlusInference2 = std::chrono::high_resolution_clock::now();

    std::chrono::duration&lt;double, std::milli&gt; ms_doubleTimeLoadModelPlusInference = timeLoadModelPlusInference2 - timeLoadModelPlusInference1;

    std::cout &lt;&lt; &quot;Execution time (load model + inference): &quot; &lt;&lt; ms_doubleTimeLoadModelPlusInference.count() &lt;&lt; std::endl; // in ms

    auto time1 = std::chrono::high_resolution_clock::now();

    try {
        for (size_t i = 0; i &lt; num_inferences; i++)
		net.forward(outputs, unconnectedOutLayerNames);
    }
    catch (std::exception&amp; ex)
    {
        std::cout &lt;&lt; ex.what() &lt;&lt; std::endl;
    }

    auto time2 = std::chrono::high_resolution_clock::now();

    std::chrono::duration&lt;double, std::milli&gt; ms_double = time2 - time1;
    std::cout &lt;&lt; &quot;Execution time inference only: &quot; &lt;&lt; ms_double.count() / num_inferences &lt;&lt; std::endl; // in ms

    std::cout &lt;&lt; &quot;Outputs Size: &quot; &lt;&lt; outputs[0].size[0] &lt;&lt; &quot;x&quot; &lt;&lt; outputs[0].size[1] &lt;&lt; std::endl;
    std::cout &lt;&lt; &quot;Outputs value: &quot; &lt;&lt; outputs[0] &lt;&lt; std::endl;
}
"><pre class="notranslate"><code class="notranslate">#include &lt;thread&gt;
#include &lt;fstream&gt;

#include &lt;opencv2/imgproc.hpp&gt;
#include &lt;opencv2/dnn.hpp&gt;
#include &lt;opencv2/core.hpp&gt;
#include &lt;opencv2/highgui.hpp&gt;

int main(int argc, char** argv)
{
    auto imageToTest = argv[1];
    auto modelToTest = argv[2];
    int modelInputWidth = atoi(argv[3]);
    int modelInputHeight = atoi(argv[4]);

    cv::Size currSize = cv::Size(modelInputWidth, modelInputHeight);
    std::string modelToTestOnnx = modelToTest;
    std::string imagefilename = imageToTest;
    unsigned int num_inferences = 100;

    cv::dnn::Net net = cv::dnn::readNetFromONNX(modelToTestOnnx);

    net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
    net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);

    cv::Mat img = cv::imread(imagefilename, cv::IMREAD_ANYCOLOR);
    cv::Mat resized;
    cv::resize(img, resized, currSize);
	
	std::vector&lt;cv::Mat&gt; imgBatch = { resized };
    bool swaprbchannels = false;
    cv::Mat blob = cv::dnn::blobFromImages(imgBatch, 1.0f / 255.0f, cv::Size(), cv::Scalar(), swaprbchannels, false, CV_32F);

    net.setInput(blob);

    std::vector&lt;cv::String&gt; unconnectedOutLayerNames = net.getUnconnectedOutLayersNames();

    std::vector&lt;cv::Mat&gt; outputs;
    outputs.clear();

    auto timeLoadModelPlusInference1 = std::chrono::high_resolution_clock::now();

    net.forward(outputs, unconnectedOutLayerNames);

    auto timeLoadModelPlusInference2 = std::chrono::high_resolution_clock::now();

    std::chrono::duration&lt;double, std::milli&gt; ms_doubleTimeLoadModelPlusInference = timeLoadModelPlusInference2 - timeLoadModelPlusInference1;

    std::cout &lt;&lt; "Execution time (load model + inference): " &lt;&lt; ms_doubleTimeLoadModelPlusInference.count() &lt;&lt; std::endl; // in ms

    auto time1 = std::chrono::high_resolution_clock::now();

    try {
        for (size_t i = 0; i &lt; num_inferences; i++)
		net.forward(outputs, unconnectedOutLayerNames);
    }
    catch (std::exception&amp; ex)
    {
        std::cout &lt;&lt; ex.what() &lt;&lt; std::endl;
    }

    auto time2 = std::chrono::high_resolution_clock::now();

    std::chrono::duration&lt;double, std::milli&gt; ms_double = time2 - time1;
    std::cout &lt;&lt; "Execution time inference only: " &lt;&lt; ms_double.count() / num_inferences &lt;&lt; std::endl; // in ms

    std::cout &lt;&lt; "Outputs Size: " &lt;&lt; outputs[0].size[0] &lt;&lt; "x" &lt;&lt; outputs[0].size[1] &lt;&lt; std::endl;
    std::cout &lt;&lt; "Outputs value: " &lt;&lt; outputs[0] &lt;&lt; std::endl;
}

</code></pre></div>
<h3 dir="auto">Issue submission checklist</h3>
<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, 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" checked=""> 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; 4.0.1.25</li>
<li>Operating System / Platform =&gt; Windows 64 bit</li>
<li>Compiler =&gt; MSC v.1900 64 bit (AMD64)</li>
</ul>
<h5 dir="auto">Detailed description</h5>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;thresh.py&quot;, line 24, in &lt;module&gt;
    cropped_image = region_of_interest(thresh_img)
  File &quot;thresh.py&quot;, line 13, in region_of_interest
    cv2.rectangle(mask, (0, req_height), (image.shape[1], height), (0, 255, 0), 5, 2)
TypeError: an integer is required (got type tuple)"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "thresh.py", line 24, in &lt;module&gt;
    cropped_image = region_of_interest(thresh_img)
  File "thresh.py", line 13, in region_of_interest
    cv2.rectangle(mask, (0, req_height), (image.shape[1], height), (0, 255, 0), 5, 2)
TypeError: an integer is required (got type tuple)
</code></pre></div>
<h5 dir="auto">Steps to reproduce</h5>

<p dir="auto">The error arises in the rectangle() function.</p>
<p dir="auto">Here is the small script in which i am trying to use it.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy as np
import cv2

def thresh(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    _, th = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    return th

def region_of_interest(image):
    height = image.shape[0]
    mask = np.zeros_like(image, np.uint8)
    req_height = 0.4*height
    cv2.rectangle(mask, (0, req_height), (image.shape[1], height), (0, 255, 0), 5, 2)
    masked_image = cv2.bitwise_and(image, mask)
    f_image = cv2.bitwise_not(masked_image)

    return f_image

image = cv2.imread('image2.jpg')

copy_img = np.copy(image)
thresh_img = thresh(copy_img)
cropped_image = region_of_interest(thresh_img)

cv2.imshow('image', cropped_image)
cv2.waitKey(0)"><pre class="notranslate"><code class="notranslate">import numpy as np
import cv2

def thresh(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    _, th = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    return th

def region_of_interest(image):
    height = image.shape[0]
    mask = np.zeros_like(image, np.uint8)
    req_height = 0.4*height
    cv2.rectangle(mask, (0, req_height), (image.shape[1], height), (0, 255, 0), 5, 2)
    masked_image = cv2.bitwise_and(image, mask)
    f_image = cv2.bitwise_not(masked_image)

    return f_image

image = cv2.imread('image2.jpg')

copy_img = np.copy(image)
thresh_img = thresh(copy_img)
cropped_image = region_of_interest(thresh_img)

cv2.imshow('image', cropped_image)
cv2.waitKey(0)
</code></pre></div>
<p dir="auto">Error occuring on this line:-</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content=" cv2.rectangle(mask, (0, req_height), (image.shape[1], height), (0, 255, 0), 5, 2)"><pre class="notranslate"><code class="notranslate"> cv2.rectangle(mask, (0, req_height), (image.shape[1], height), (0, 255, 0), 5, 2)
</code></pre></div>
<p dir="auto">I have tried passing in one argument after the color attribute.<br>
I also have tried naming the attributes i.e:-<br>
---&gt; color=(0,255,0), thickness=5 etc.</p>
<p dir="auto">Let me know if I have missed out any information which should have been given.</p>