RGB processing sampleUffSSD is quite not understandable

Hi, sorry if my question is odd, but i really need to understand it to use opencv image with it. So the code is follows:

// RGB preprocessing based on keras pipeline
for (int i = 0, volImg = INPUT_C * INPUT_H * INPUT_W; i < N; ++i)
{
for (unsigned j = 0, volChl = INPUT_H * INPUT_W; j < volChl; ++j)
{
//std::cout << volImg << j * INPUT_C + 0] << std::endl;
std::cout << std::endl;
std::cout << 0 * volChl + j << j * INPUT_C + 0 << std::endl;
std::cout << 1 * volChl + j << j * INPUT_C + 1 << std::endl;
std::cout << 2 * volChl + j << j * INPUT_C + 2 << std::endl;

    data[ 0 * volChl + j] = float(ppms[i].buffer[j * INPUT_C + 0]) - 123.68;
    data[ 1 * volChl + j] = float(ppms[i].buffer[j * INPUT_C + 1]) - 116.779;
    data[ 2 * volChl + j] = float(ppms[i].buffer[j * INPUT_C + 2]) - 103.939;
  }
}

the above code in green is not understandable

taken from :

nvinfer1::ICudaEngine* engine = loadTRTEngine("ssd_engine.engine", gLogger);

// Read a random sample image.
srand(unsigned(time(nullptr)));
// Available images.
std::vector<std::string> imageList = {"dog.ppm", "image1.ppm"};
std::vector<samplesCommon::PPM<INPUT_C, INPUT_H, INPUT_W>> ppms(N);

assert(ppms.size() <= imageList.size());
std::cout << " Num batches  " << N << std::endl;
/***
cv::VideoCapture cap(0);
if(!cap.isOpened())
    return -1;
cv::Mat edges;
cv::namedWindow("edges", 1);

for(;;){
    cv::Mat frame;
    cap >> frame;
    cv::imshow("edges", frame);
    if(cv::waitKey(30) >=0) {
        cv::destroyWindow("edges");
        break;
    }
    
}
**/
for (int i = 0; i < N; ++i)
{
    readPPMFile(locateFile(imageList[i%2]), ppms[i]);
}

vector<float> data(N * INPUT_C * INPUT_H * INPUT_W);

// for (int i = 0, volImg = INPUT_C * INPUT_H * INPUT_W; i < N; ++i)
// {
//     for (int c = 0; c < INPUT_C; ++c)
//     {
//         for (unsigned j = 0, volChl = INPUT_H * INPUT_W; j < volChl; ++j)
//         {
//             data[i * volImg + c * volChl + j] = (2.0 / 255.0) * float(ppms[i].buffer[j * INPUT_C + c]) - 1.0;
//         }
//     }
// }

// RGB preprocessing based on keras pipeline
for (int i = 0, volImg = INPUT_C * INPUT_H * INPUT_W; i < N; ++i)
{
  for (unsigned j = 0, volChl = INPUT_H * INPUT_W; j < volChl; ++j)
  {
    //std::cout << volImg << j * INPUT_C + 0] << std::endl;
    std::cout << std::endl;
    std::cout <<  0 * volChl + j << j * INPUT_C + 0 << std::endl;
    std::cout <<  1 * volChl + j << j * INPUT_C + 1 << std::endl;
    std::cout <<  2 * volChl + j << j * INPUT_C + 2 << std::endl;

    data[ 0 * volChl + j] = float(ppms[i].buffer[j * INPUT_C + 0]) - 123.68;
    data[ 1 * volChl + j] = float(ppms[i].buffer[j * INPUT_C + 1]) - 116.779;
    data[ 2 * volChl + j] = float(ppms[i].buffer[j * INPUT_C + 2]) - 103.939;
  }
}
std::cout << " Data Size  " << data.size() << std::endl;

// Deserialize the engine.
std::cout << “*** deserializing” << std::endl;
IRuntime* runtime = createInferRuntime(gLogger);
assert(runtime != nullptr);
//ICudaEngine* engine = runtime->deserializeCudaEngine(trtModelStream->data(), trtModelStream->size(), nullptr);
assert(engine != nullptr);

IExecutionContext* context = engine->createExecutionContext();
assert(context != nullptr);
//SimpleProfiler profiler("layerTime");
//context->setProfiler(&profiler);
// Host memory for outputs.
vector<float> detectionOut(100000); //(N * detectionOutputParam.keepTopK * 7);
vector<int> keepCount(N);
std::cout << "actually starting inference" << std::endl;
// Run inference.
doInference(*context, &data[0], &detectionOut[0], &keepCount[0], N);
cout << " KeepCount " << keepCount[0] << "\n";
//cout << profiler;

std::string CLASSES[OUTPUT_CLS_SIZE];

populateClassLabels(CLASSES);

for (int p = 0; p < N; ++p)
{
    for (int i = 0; i < keepCount[p]; ++i)
    {
        float* det = &detectionOut[0] + (p * detectionOutputParam.keepTopK + i) * 7;
        if (det[2] < visualizeThreshold)
            continue;

        // Output format for each detection is stored in the below order
        // [image_id, label, confidence, xmin, ymin, xmax, ymax]
        assert((int) det[1] < OUTPUT_CLS_SIZE);
        //std::string storeName = CLASSES[(int) det[1]] + "-" + std::to_string(det[2]) + ".ppm";

      //  printf("Detected %s in the image %d (%s) with confidence %f%% and coordinates (%f,%f),(%f,%f).\n", CLASSES[(int) det[1]].c_str(), int(det[0]),  ppms[p].fileName.c_str(), det[2] * 100.f, det[3] * INPUT_W, det[4] * INPUT_H, det[5] * INPUT_W, det[6] * INPUT_H);

        //samplesCommon::writePPMFileWithBBox(storeName, ppms[p], {det[3] * INPUT_W, det[4] * INPUT_H, det[5] * INPUT_W, det[6] * INPUT_H});
    }
}

// Destroy the engine.
context->destroy();
engine->destroy();
runtime->destroy();

and

inline void readPPMFile(const std::string& filename, samplesCommon::PPM<C, H, W>& ppm)
{
ppm.fileName = filename;
std::ifstream infile(filename, std::ifstream::binary);
assert(infile.is_open() && “Attempting to read from a file that is not open.”);
infile >> ppm.magic >> ppm.w >> ppm.h >> ppm.max;
infile.seekg(1, infile.cur);
infile.read(reinterpret_cast<char*>(ppm.buffer), ppm.w * ppm.h * 3);
}

Hi,

This includes several pre-processing tasks:

1. Subtract mean
2. Convert integer to float
3. Rearrange from NHWC to NCHW

Thanks.

Thank you. :)