I have a custom image preprocessing neural network model. It takes input of 1920x1080x3
and outputs 1920x1080x3
(It basically does preprocessing, so input is an image and output is an image).
I successfully tested code in Python
and in ONNX
.
However, I want to run the code in tensorRT C++
. So, I took this code from github, and modifying it run my .engine
file.
I read the image using
image = cv::imread(path);
cv::resize(image, image_resize, cv::Size(1920,1080), cv::INTER_LINEAR);
cv::cvtColor(image_resize, image_cvtColor, cv::COLOR_BGR2RGB);
then created blob and copied it to GPU memory for inference,
cv::dnn::blobFromImage(tmp, out, 1 / (255.f), cv::Size(), cv::Scalar(0, 0, 0), false, false, CV_32F);
I read back the result using,
// cv::Mat image = cv::Mat(width, height, CV_8UC4, (unsigned*)data);
cv::Mat output1 = cv::Mat(num_rows, num_cols, CV_32F, static_cast<float*>(this->host_ptrs[0]+(0))); //CV_32F
cv::Mat output2 = cv::Mat(num_rows, num_cols, CV_32F, static_cast<float*>(this->host_ptrs[0]+(7680))); //CV_32F
cv::Mat output3 = cv::Mat(num_rows, num_cols, CV_32F, static_cast<float*>(this->host_ptrs[0]+(7680*2))); //CV_32F
// Scale and convert floating-point data to 8-bit unsigned integers
cv::Mat output1_8U, output2_8U, output3_8U;
output1.convertTo(output1_8U, CV_8U, 255.0);
output2.convertTo(output2_8U, CV_8U, 255.0);
output3.convertTo(output3_8U, CV_8U, 255.0);
// Combine the channels into a single RGB image
std::vector<cv::Mat> channels = {output3_8U, output2_8U, output1_8U}; // BGR order
cv::Mat rgbImage;
cv::merge(channels, rgbImage);
// Display the combined RGB image
cv::namedWindow("win_name1", cv::WINDOW_NORMAL);
cv::imshow("win_name1", rgbImage);
cv::waitKey(0);
I think, I am doing something wrong here. I am getting output in grayscale
. Am I doing any mistake in NxCxHxW
to NxHxWxC
?
My input image is like this.
Ideally, I should get sharpened output, but I am getting output like this.
How to read pixels from static_cast<float*>(this->host_ptrs[0]+(0))
.
Please provide complete information as applicable to your setup. Ideally I should read 6220800
(1920x1080x3
) pixels from this address.
TensorRT Version : 8.4.1
GPU Type : A5000
Nvidia Driver Version :
CUDA Version : 11.4
CUDNN Version :
Operating System + Version : Ubuntu 20.04
Python Version (if applicable) : No, running on C++
TensorFlow Version (if applicable) : NA
PyTorch Version (if applicable) : NA
Baremetal or Container (if container which image + tag) : NA