Deepstream-mdx-perception-app (retail, `-m 3`) reads the probs/embeddings output tensors swapped → wrong product names and out-of-bounds label read

**• Hardware Platform:** Jetson AGX Orin

**• DeepStream Version:** 9.1

**• JetPack Version:** 7.x (L4T R39.2.1)

**• TensorRT Version:** 10.16.2

**• Issue Type:** bug

**• Source:** `NVIDIA/DeepStream`, branch `DeepStream-9.1` (commit `0a63ef8`), `src/apps/tao_apps/apps/tao_others/deepstream-mdx-perception-app`

**• Model:** `retail_object_recognition` `deployable_v2.0` (`FANDualHead_Base_NVIN_op16.onnx`) with `recognitionv2_name_list.txt`, on top of `retail_object_detection` binary DINO

### Summary

In retail dual-head mode (`-m 3`), `generate_event_msg_meta()` assumes output layer **0 = embeddings** and output layer **1 = probs**. On this platform the SGIE engine exposes them in the opposite order. The app then takes the argmax over the **448-dim embedding** and uses it as a product index, and stores the **315 probabilities** as the embedding vector.

The result is a plausible but wrong product name on every object, with no error printed. Because `labels[]` holds only 320 entries, an argmax between 320 and 447 also **reads past the end of the array**.

### How to reproduce

1. Follow `apps/tao_others/README.md` and `download_models.sh` for the retail models (unmodified configs; `output-blob-names: probs;embeddings`).

2. Run the sample exactly as in the README:

```

./deepstream-mdx-perception-app -c ../../../configs/app/retail_object_detection_recognition.yml -m 3 --tiledtext

```

3. Watch the labels on `sample/streams/retail_object_h264.mp4`. The README shows `lipton_noodle_soup` for the red noodle soup box. We get `KV135_Claratin_ND24_30ct`, and the Fudge Brownies box gets `KV040_PorkLoin_Backribs`. Both products are in the 315-name list. Building the SGIE in FP32 gives the same wrong names, so precision is not the cause.

### Root cause

`nvinfer` fills `output_layers_info` in **engine binding order** (`nvdsinfer_context_impl.cpp`, the loop over `getLayerInfo(i)` that pushes non-input layers into `m_OutputLayerInfo`), not in `output-blob-names` order. `nvinfer`'s own engine table for this SGIE shows:

```

INFO: [FullDims Engine Info]: layers num: 3

0 INPUT kFLOAT input 3x224x224

1 OUTPUT kFLOAT probs 315

2 OUTPUT kFLOAT embeddings 448

```

So output 0 is `probs` and output 1 is `embeddings`, while the app reads them the other way round:

```c

NvDsInferDims embedding_dims = tensor_meta->output_layers_info[0].inferDims; // actually probs

…

/* Access the 1-indexed output layer to get probs */

getDimsCHWFromDims(dims, tensor_meta->output_layers_info[1].inferDims); // actually embeddings (448)

dual_head_classes = dims.c; // 448, not 315

float *outputCoverageBuffer = (float *)tensor_meta->out_buf_ptrs_host[1];

…

strcpy(label_info->result_label, labels[label_info->result_class_id]); // labels[320][…]

```

`trtexec --loadEngine` on the same engine lists the bindings in the same order (`probs 16x315`, then `embeddings 16x448`).

### Fix (tested)

The patch below looks the two heads up by layer name instead of assuming a position, and bounds the argmax by the number of names actually loaded. It compiles cleanly against DS 9.1 on the Orin; the only build warnings are the existing `g_memdup` deprecations.

On `retail_object_h264.mp4` the soup now comes out as **`lipton_noodle_soup`**, which matches the README. In the frames we compared, the labels were identical to those of a plain swap of the two indices. Moving objects (motion blur, hand occlusion) still flicker between products, which looks like a normal limit of the classification head.

```diff

-– a/src/apps/tao_apps/apps/tao_others/deepstream-mdx-perception-app/deepstream_mdx_perception_app.c

+++ b/src/apps/tao_apps/apps/tao_others/deepstream-mdx-perception-app/deepstream_mdx_perception_app.c

@@ -88,6 +88,8 @@ typedef enum

// Variable to store labels for FSL dual head model

char labels[MAX_LINES_IN_LABEL_FILE][MAX_CHAR_LENGTH_PER_LINE];

guint dual_head_classes = 315;

+// Number of names actually loaded by get_labels_from_file() (argmax bound).

+guint labels_loaded = 0;

/**

* IMPORTANT Note 2:

@@ -592,30 +594,50 @@ static void generate_event_msg_meta(AppCtx *appCtx, gpointer data,

   meta->type = NVDS_EVENT_MOVING;

   meta->objType = NVDS_OBJECT_TYPE_PRODUCT;

+ /* Look the two heads up by NAME: nvinfer fills output_layers_info in

+ * engine binding order, not in output-blob-names order. */

+ int idx_probs = -1, idx_emb = -1;

+ for (unsigned int i = 0; i < tensor_meta->num_output_layers; i++) {

+ const char *n = tensor_meta->output_layers_info[i].layerName;

+ if (n && !strcmp(n, “probs”)) idx_probs = (int)i;

+ if (n && !strcmp(n, “embeddings”)) idx_emb = (int)i;

+ }

+ if (idx_probs < 0 || idx_emb < 0) {

+ static gboolean warned = FALSE;

+ if (!warned) {

+ g_printerr("[mdx] retail dual head: output layers ‘probs’/‘embeddings’ "

+ “not found by name; skipping\n”);

+ warned = TRUE;

+ }

+ return;

+ }

   NvDsInferDims embedding_dims =

- tensor_meta->output_layers_info[0].inferDims;

+ tensor_meta->output_layers_info[idx_emb].inferDims;

   int numElements = embedding_dims.d\[0\];

   meta->embedding.embedding_vector =

       (float \*)g_malloc0(numElements \* sizeof(float));

   cudaMemcpy(meta->embedding.embedding_vector,

- (float *)(tensor_meta->out_buf_ptrs_dev[0]),

+ (float *)(tensor_meta->out_buf_ptrs_dev[idx_emb]),

              numElements \* sizeof(float), cudaMemcpyDeviceToHost);

   meta->embedding.embedding_length = numElements;

   NvDsInferDimsCHW dims;

- /* Access the 1-indexed output layer to get probs */

- getDimsCHWFromDims(dims, tensor_meta->output_layers_info[1].inferDims);

+ getDimsCHWFromDims(dims, tensor_meta->output_layers_info[idx_probs].inferDims);

   dual_head_classes = dims.c;

+ /* Never index past the names actually loaded into labels[]. */

+ guint n_classes = MIN(dual_head_classes, labels_loaded);

+ n_classes = MIN(n_classes, (guint)MAX_LINES_IN_LABEL_FILE);

   float \*outputCoverageBuffer =

- (float *)tensor_meta->out_buf_ptrs_host[1];

+ (float *)tensor_meta->out_buf_ptrs_host[idx_probs];

   float maxProbability = 0;

   bool attrFound = false;

   NvDsInferAttribute attr;

   /\* Get the output with max probability \*/

- for (unsigned int c = 0; c < dual_head_classes; c++) {

+ for (unsigned int c = 0; c < n_classes; c++) {

     float probability = outputCoverageBuffer\[c\];

     if (probability > 0 && probability > maxProbability) {

       maxProbability = probability;

@@ -1467,6 +1489,8 @@ void get_labels_from_file(gchar *classifier_label_file) {

     "used in "

     "metadata field when a class without label is predicted.\\n");

+ labels_loaded = line_count;

// Close the file

fclose(file);

}

```

Notes on the patch:

- The early `return` sits in the last block of `generate_event_msg_meta()`, before anything is allocated there. It leaves `meta` in the same state as the existing no-tensor path, so nothing leaks.

- In our test the `probs`/`embeddings` lookup always succeeded. The warn-and-skip branch and the `n_classes` bound are defensive and were not triggered.

- `get_labels_from_file()` only warns when the class count and the label count differ, and with the bug above nothing was printed. That fits the code, since `dual_head_classes` still holds its default of 315 when the 315-line label file is checked. That ordering is our reading of the source, not something we traced in a log.

Thanks!

mdx-perception-por-nome.zip (1.6 KB)

Hello @henrique.cerqueira!

Based on the title and content of your topic, it looks like it may receive better visibility and feedback in a different category. We took the liberty of moving it for you.

If this was an incorrect assessment, please send me a direct message.

Disclaimer: this moderation suggestion and message were generated with AI assistance.

Thank you for your feedback; we will investigate the issues in the code/model.
The patch you provided has resolved your issue, correct?

yes, it’s correct.