Issue 1:
System Information

OpenCV versions tested: 4.5.5, 4.7.0

Operating System / Platform: Ubuntu 18.04

Device: NVIDIA Jetson TX2 DevKit

CUDA version: 10.2

CUDNN version: 8.2.1

Detailed description

Hi,

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?

This is the execution times obtained for both versions of OpenCV:

Test 1

Device: TX2 DevKit
CUDA version: 10.2
CUDNN version: 8.2.1
OpenCV version: 4.7.0

Version
Model 1
Model 2
Model 3
Model 4

Input Size
(112, 112)
(112, 112)
(112, 112)
(112, 112)

Model Architecture
Resnet100
MobileFaceNet
Resnet100
Resnet18

Jetson CPU
702
20.5
699
167

Jetson GPU
91.7
10.5
91.6
52.2

Test 2

Device: TX2 DevKit
CUDA version: 10.2
CUDNN version: 8.2.1
OpenCV version: 4.5.5

Version
Model 1
Model 2
Model 3
Model 4

Input Size
(112, 112)
(112, 112)
(112, 112)
(112, 112)

Model Architecture
Resnet100
MobileFaceNet
Resnet100
Resnet18

Jetson CPU
1088
23.1
1096
257

Jetson GPU
60.9
5.34
60.7
19.9

Note: 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.

Steps to reproduce

You can use this piece of code to reproduce this issue/loss of performance:

#include <thread>
#include <fstream>

#include <opencv2/imgproc.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>

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<cv::Mat> 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<cv::String> unconnectedOutLayerNames = net.getUnconnectedOutLayersNames();

std::vector<cv::Mat> 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<double, std::milli> ms_doubleTimeLoadModelPlusInference = timeLoadModelPlusInference2 - timeLoadModelPlusInference1;

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

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

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

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

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

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

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 => 4.0.1.25

Operating System / Platform => Windows 64 bit

Compiler => MSC v.1900 64 bit (AMD64)

Detailed description

Traceback (most recent call last):
File "thresh.py", line 24, in <module>
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)

Steps to reproduce

The error arises in the rectangle() function.

Here is the small script in which i am trying to use it.

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)

Error occuring on this line:-

cv2.rectangle(mask, (0, req_height), (image.shape[1], height), (0, 255, 0), 5, 2)

I have tried passing in one argument after the color attribute.

I also have tried naming the attributes i.e:-

---> color=(0,255,0), thickness=5 etc.

Let me know if I have missed out any information which should have been given.