Classification Post-processing Pipeline
The classification post processing pipeline is a relatively lightweight one with sorting being the only operation happening in it.
The exact post-processing operations are:
Sorting the probabilities to get the top N -> Print the top N classes with the confidence scores
Since the network outputs the class probabilities (0-1) for all the classes supported by the network, we must first sort those in the descending order and take out the top-N from it. These operations will be done using PyTorch math and the results will be logged to the stdout.
1# We assume that everything other than probabilities will be a CVCUDA tensor.
2# probabilities has to be a torch tensor because we need to perform a few
3# math operations on it. Even if the TensorRT backend was used to run inference
4# it would have generated output as Torch.tensor
5
6actual_batch_size = probabilities.shape[0]
7
8# Sort output scores in descending order
9_, indices = torch.sort(probabilities, descending=True)
10
11probabilities = probabilities.cpu().numpy()
12indices = indices.cpu().numpy()
13
14# tag: Display Top N Results
15for img_idx in range(actual_batch_size):
16 self.logger.info(
17 "Classification probabilities for the image: %d of %d"
18 % (img_idx + 1, actual_batch_size)
19 )
20
21 # Display Top N Results
22 for idx in indices[img_idx][:top_n]:
23 self.logger.info(
24 "\t%s: %2.3f%%"
25 % (labels[idx], round(probabilities[img_idx][idx] * 100.0, 3)),
26 )
27