122. Extracting Inference Results Using C

A semantic segmentation model outputs a tensor shaped

[Batch_size,Channel(Number of Classes),Img_Height,Img_Width]

(If using Pytorch), but if you convert that to a TensorRT engine for faster inference, the output is flattened to a 1d array. Therefore the shaping being,

[(Batch_size)X(Channel)X(Img_Height)X(Img_Width),]

Considering this, if you want to get the index of a certain pixel(x,y) in channel (c) the equation would be

Index = c*Img_Width*Img_Height + y*Img_Width + x

I’m deploying models on edge devices, so I converted the model to a TensorRT engine file and had to use C to get the inference results.

Here is how I did it. I was using a binary segmentation model,my image size was 512*512, and the channel for the class I wanted was the 2nd channel.

//Definition
int i;
FILE *filePtr;
filePtr = fopen("frame_res.txt", "w");

//Since I want the inference results from the 2nd channel
// my for loop's starting index would be 512*512
// and end at 512*512*2(Because there are only 2 channels)
for (i = 512 * 512; i < 512 * 512 * 2; i++)
{
    results = inference_results[i];

    //print each results to frame_res.txt
    fprintf(filePtr, "%.3g,", results); 
}