tensorrt-cpp-api icon indicating copy to clipboard operation
tensorrt-cpp-api copied to clipboard

Error in blobFromGpuMats

Open ioannkozlitin opened this issue 3 months ago • 0 comments

When I tried to apply your code to MobileNetV3 inference, I discovered that your preprocessing transformation (mean subtraction and division by standard deviation) doesn't work correctly when these values are different for different color channels.

I made some minor adjustments to your code as follows:

     cv::cuda::GpuMat mfloat;
     if (normalize) {
         // [0.f, 1.f]
-        gpu_dst.convertTo(mfloat, CV_32FC3, 1.f / 255.f);
+        gpu_dst.convertTo(mfloat, CV_32F, 1.f / 255.f);
     } else {
         // [0.f, 255.f]
-        gpu_dst.convertTo(mfloat, CV_32FC3);
+        gpu_dst.convertTo(mfloat, CV_32F);
     }
 
     // Apply scaling and mean subtraction
 
-    cv::cuda::subtract(mfloat, cv::Scalar(subVals[0], subVals[1], subVals[2]), mfloat, cv::noArray(), -1);
-    cv::cuda::divide(mfloat, cv::Scalar(divVals[0], divVals[1], divVals[2]), mfloat, 1, -1);
+    for (size_t img = 0; img < batchInput.size(); img++) {
+    std::vector<cv::cuda::GpuMat> mfloat_channels{
+        cv::cuda::GpuMat(1, width, CV_32F, &(mfloat.ptr()[0 + 4 * width * 3 * img])),
+        cv::cuda::GpuMat(1, width, CV_32F, &(mfloat.ptr()[4 * width + 4 * width * 3 * img])),
+        cv::cuda::GpuMat(1, width, CV_32F, &(mfloat.ptr()[4 * width * 2 + 4 * width * 3 * img]))};
+
+        for(size_t i = 0; i < 3; i++)
+        {
+            cv::cuda::subtract(mfloat_channels[i], cv::Scalar(subVals[i]), mfloat_channels[i], cv::noArray(), -1);
+            cv::cuda::divide(mfloat_channels[i], cv::Scalar(divVals[i]), mfloat_channels[i], 1, -1);
+        }
+    }
 
     return mfloat;

You might find a better solution, but this worked for MobileNetV3.

ioannkozlitin avatar Oct 24 '25 08:10 ioannkozlitin