Discrepancy between PyTorch and DeepStream inference when deploying a custom ReID model

Please provide complete information as applicable to your setup.

• Hardware Platform (Jetson / GPU) 4090
• DeepStream Version: 7.0
• JetPack Version (valid for Jetson only)
• TensorRT Version
• NVIDIA GPU Driver Version (valid for GPU only)
• Issue Type( questions, new requirements, bugs)
• How to reproduce the issue ? (This is for bugs. Including which sample app is using, the configuration files content, the command line used and other details for reproducing)
**• Requirement details( This is for new requirement. Including the module name-for which plugin or for which sample application, the function description)
**
I use a ReID model based on fast-reid and have an ONNX model. When I compare the inference outputs between DeepStream and PyTorch, I notice some differences as follows.

    def process_frame_simplified(self,frame,bboxes):
        frame=torch.from_numpy(frame[:,:,::-1].copy()).permute(2,0,1)
        frame=frame/255.0
        
        frame.sub_(self.mean).div_(self.std)
        frame=frame.unsqueeze(0)
        cbboxes=bboxes.copy()

        cbboxes=cbboxes.astype(np.float32)

        newcrops=roi_align(frame,torch.cat([torch.zeros(len(cbboxes),1),torch.from_numpy(cbboxes)],1),(384,128)).to(self.device)
        
        newfeats=self.mgn(newcrops).detach().cpu().numpy()

        return newfeats

The order of operations is: divide by 255 → normalize with std and mean → roi_align (crop and resize).

In DeepStream, I use PGIE to detect human bounding boxes and SGIE to extract embeddings (ReID). However, it seems the pipeline is operating in the following order: crop → resize → color format conversion (RGB) → normalization with std/mean.

After debugging, I found two discrepancies that cause the ReID outputs between PyTorch and DeepStream (for the same bounding box) to differ, with cosine similarity dropping to around 0.87:

  1. The resize operation in OpenCV and nvinfer are not identical (I experimented with parameters like scaling-filter and scaling-compute-hw).

  2. The order of preprocessing operations is different.

Does DeepStream support a pipeline similar to PyTorch, or is there a way to apply /255 and std/mean normalization first on the image before passing it to the SGIE, so that crop and resize and computation happen afterward? Thanks

  1. First Please read the FAQ 15.[DSx_All_App] Debug Tips for DeepStream Accuracy Issue. what is the resolution of source and model input? what is the complete media pipeline in DeepStream? Please share the cfg files of nvsterammux and nvinfer.
  2. Regarding ‘resize’, Since there are many resizing algorithms, Please make sure the same resizing algo, maintain-aspect-ratio and symmetric-padding parameters are used in opencv and nvinfer. Please refer to this link for the explanation of each resizing algo.
  3. Regarding normalization, nvinfer does not support standard deviation normalization. It is recommended to implement the preprocessing algorithm by gst-nvpreprocess plugin. Here is a ready-made sample deepstream_custom_preprocessing_app which leverages opensource roiconvert to do standard deviation normalization in nvdspreprocess plugin. Namely you can set pixel-normalization-factor and offset separately for each channel, as the cfg shown.
# encoding: utf-8
"""
Export FastReID MGN to ONNX with final-only feature and built-in normalization
Author: Adapted by ChatGPT
"""

import os
import sys
import torch
import torch.nn as nn
import torch.nn.functional as F
import onnx
from onnxsim import simplify


# ---- Add fast-reid to sys.path ----
current_file_path = os.path.abspath(__file__)
path_arr = current_file_path.split('/')[:-3]
root_path = '/'.join(path_arr)
sys.path.append(os.path.join(root_path, 'fast-reid'))

from fastreid.modeling.meta_arch import build_model
from fastreid.utils.checkpoint import Checkpointer


# ---- Wrapper class ----
class ReidWrapperFinalOnly(nn.Module):
    def __init__(self, reid_model):
        super(ReidWrapperFinalOnly, self).__init__()
        self.reid = reid_model
        # Normalization params (ImageNet mean/std)
        self.register_buffer('mean', torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1))
        self.register_buffer('std', torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1))

    def mgn(self, crops):
        # crops: (B, 3, 384, 128)
        features = self.reid.backbone(crops)

        b1_feat = self.reid.b1(features)
        b2_feat = self.reid.b2(features)
        b3_feat = self.reid.b3(features)

        # Split b2 into 2 parts
        b21_feat, b22_feat = torch.chunk(b2_feat, 2, dim=2)

        # Split b3 into 3 parts
        b31_feat, b32_feat, b33_feat = torch.chunk(b3_feat, 3, dim=2)

        # Apply pooling heads
        b1_pool = self.reid.b1_head(b1_feat)
        b2_pool = self.reid.b2_head(b2_feat)
        b21_pool = self.reid.b21_head(b21_feat)
        b22_pool = self.reid.b22_head(b22_feat)
        b3_pool = self.reid.b3_head(b3_feat)
        b31_pool = self.reid.b31_head(b31_feat)
        b32_pool = self.reid.b32_head(b32_feat)
        b33_pool = self.reid.b33_head(b33_feat)

        # Concatenate final feature
        pred_feat = torch.cat([
            b1_pool, b2_pool, b3_pool,
            b21_pool, b22_pool,
            b31_pool, b32_pool, b33_pool
        ], dim=1)

        return pred_feat  # shape: (B, 6144)

    def forward(self, x):
        # x expected: [0..255], shape: (B, 3, 384, 128), format: RGB
        x = x / 255.0
        x = (x - self.mean) / self.std
        return self.mgn(x)


def main():
    # ---- Load checkpoint ----
    ckpt_path = '/ssd1/lelong/pytorch/AI_CITY/PoseTrack/ckpt_weight/aic24.pkl'
    reid = torch.load(ckpt_path, map_location='cpu').eval()

    # Wrap with final-only + normalization
    model = ReidWrapperFinalOnly(reid).eval()

    # ---- Dummy input ----
    # dummy_input = torch.rand(1, 3, 384, 128) * 255.0
    dummy_input = torch.randint(0, 256, (1, 3, 384, 128), dtype=torch.float32)

    # ---- Test forward ----
    with torch.no_grad():
        test_output = model(dummy_input)
        print(f"✅ Model output shape: {test_output.shape}")  # Expected: [1, 6144]

    # ---- Export ONNX ----
    onnx_path = "reid_mgn_final_only_norm.onnx"
    torch.onnx.export(
        model,
        dummy_input,
        onnx_path,
        # export_params=True,
        opset_version=17,
        do_constant_folding=True,
        input_names=['input'],
        output_names=['output'],
        dynamic_axes={'input': {0: 'batch'}, 'output': {0: 'batch'}},
        verbose=False,
        keep_initializers_as_inputs=False
    )

    # ---- Simplify model ----
    # model_onnx = onnx.load(onnx_path)
    # model_simp, check = simplify(model_onnx)
    # assert check, "Simplified ONNX model could not be validated"
    # onnx.save(model_simp, onnx_path)

    print(f"\n🎉 ONNX model exported and simplified: {onnx_path}")
    print("📌 Output shape: (B, 6144)")
    print("💡 Normalization (x/255 - mean)/std is included inside ONNX graph.")


if __name__ == '__main__':
    main()

Above is my ONNX export code, with the weights taken from https://drive.google.com/file/d/17qbBmBX7DiT2lOuQ6rGHl8s9deKHkVn2/view?usp=sharing
my inference code to compare onnx with .pt

import os
import cv2
import numpy as np
import onnxruntime as ort

def preprocess_for_onnx(frame, bbox, size=(128, 384)):
    x1, y1, x2, y2 = map(int, bbox)
    x1, y1 = max(0, x1), max(0, y1)
    x2, y2 = min(frame.shape[1], x2), min(frame.shape[0], y2)
    if x2 <= x1 or y2 <= y1:
        crop = np.zeros((size[1], size[0], 3), dtype=np.uint8)
    else:
        crop = frame[y1:y2, x1:x2]
        crop = cv2.resize(crop, size, interpolation=cv2.INTER_AREA)  # (width, height) = (128, 384)

    # BGR -> RGB, HWC -> CHW
    crop = crop[:, :, ::-1].transpose(2, 0, 1)  # RGB, (3, 384, 128)

    crop = crop.astype(np.float32)[None]  # (1, 3, 384, 128)
    return crop

def l2_normalize(x):
    return x / (np.linalg.norm(x, axis=1, keepdims=True))

def read_detections(txt_file):
    """Đọc file detection txt -> list (frame_id, bbox, conf)."""
    dets = []
    with open(txt_file, "r") as f:
        for line in f:
            parts = line.strip().split(",")
            if len(parts) < 7:
                continue
            frame_id, cls, x1, y1, x2, y2, conf = parts
            dets.append((
                int(frame_id),
                [float(x1), float(y1), float(x2), float(y2)],
                float(conf)
            ))
    return dets

def get_frame_from_video(cap, frame_id, one_based=True):
    """Đọc frame thứ frame_id từ video."""
    if one_based:
        frame_id = frame_id - 1
    cap.set(cv2.CAP_PROP_POS_FRAMES, frame_id)
    ret, frame = cap.read()
    if not ret:
        raise ValueError(f"Không đọc được frame {frame_id}")
    return frame

def main():
    # ---- Config ----
    onnx_path = "/ssd1/lelong/pytorch/AI_CITY/PoseTrack/fast-reid/reid_mgn_final_only_norm.onnx"
    reid_file = "/ssd1/lelong/pytorch/AI_CITY/PoseTrack/result/reid/scene_094/camera_0961.npy"
    bbox_file = "/ssd1/lelong/pytorch/AI_CITY/PoseTrack/result/detection/scene_094/camera_0961.txt"
    video_path = "/ssd1/lelong/pytorch/AI_CITY/PoseTrack/dataset/test/scene_094/camera_0961/video.mp4"

    reid_feats_saved = np.load(reid_file, mmap_mode="r")  # (N, 6144) — CHƯA normalized
    detections = read_detections(bbox_file)
    cap = cv2.VideoCapture(video_path)

    # ---- Load ONNX ----
    ort_sess = ort.InferenceSession(
        onnx_path, providers=["CUDAExecutionProvider", "CPUExecutionProvider"]
    )

    idx = 2
    frame_id, bbox, conf = detections[idx]
    feat_saved = reid_feats_saved[idx]  # (6144,) — raw feature

    # Lấy frame tương ứng
    frame = get_frame_from_video(cap, frame_id, one_based=False)

    # Crop + inference
    inp = preprocess_for_onnx(frame, bbox)
    feat_onnx = ort_sess.run(None, {"input": inp})[0]  # (1, 6144)

    feat_onnx = feat_onnx[0]  # (6144,)

    # ✅ Normalize cả hai để tính cosine similarity
    feat_saved_norm = l2_normalize(feat_saved[None, :])[0]
    feat_onnx_norm = l2_normalize(feat_onnx[None, :])[0]
    print(type(feat_onnx_norm[0]), type(feat_saved_norm[0]))

    # ---- Compare ----
    cos_sim = np.dot(feat_saved_norm, feat_onnx_norm)  # ∈ [-1, 1]
    l2_dist = np.linalg.norm(feat_saved - feat_onnx)   # raw L2

    print("Frame ID:", frame_id, "Conf:", conf)
    print("✅ Saved feature shape:", feat_saved.shape)
    print("✅ ONNX feature shape:", feat_onnx.shape)
    print("📌 Cosine similarity (normalized):", cos_sim)
    print("📏 L2 distance (raw):", l2_dist)

    cap.release()

if __name__ == "__main__":
    main()

Cosine similarity (normalized): 0.98391575
📏 L2 distance (raw): 2.4364464

But after config this to nvinfer, this cosine drop to 0.87

Below it my yolo and reid config

[property]
gpu-id=0
net-scale-factor=0.0173520735727919486
offsets=123.675;116.28;103.53
model-color-format=0
model-engine-file=/ds_app/models/engine/yolox_b2_gpu0_fp32.engine
onnx-file=/ds_app/models/onnx/yolox.onnx
labelfile-path=labels.txt
#batch-size=8
network-mode=0
num-detected-classes=1
interval=0
gie-unique-id=1
process-mode=1
network-type=0
cluster-mode=2
maintain-aspect-ratio=1
symmetric-padding=0

scaling-filter=2
scaling-compute-hw=1
#crop-objects-to-roi-boundary=1

#workspace-size=2000
parse-bbox-func-name=NvDsInferParseYolo
#parse-bbox-func-name=NvDsInferParseYoloCuda
custom-lib-path=/nvdsinfer_custom_impl_Yolo11/libnvdsinfer_custom_impl_Yolo.so
engine-create-func-name=NvDsInferYoloCudaEngineGet

[class-attrs-all]
nms-iou-threshold=0.4
pre-cluster-threshold=0.6
topk=300

reid

[property]
gpu-id=0
#net-scale-factor=0.01735207
#offsets=123.675;116.28;103.53
net-scale-factor=1.0
offsets=0.0;0.0;0.0
model-color-format=0
model-engine-file=/ds_app/models/engine/reid_mgn.onnx_b2_gpu0_fp32.engine
onnx-file=/ds_app/models/onnx/reid_mgn.onnx
labelfile-path=labels.txt
batch-size=2
#Integer 0: FP32 1: INT8 2: FP16
network-mode=0
num-detected-classes=1
interval=0
gie-unique-id=2
operate-on-gie-id=1
operate-on-class-ids=0
process-mode=2
network-type=0 # change here 100 -> 0
cluster-mode=2
maintain-aspect-ratio=0
symmetric-padding=0
output-tensor-meta=1


scaling-filter=2
scaling-compute-hw=1
crop-objects-to-roi-boundary=1

#workspace-size=2000
parse-bbox-func-name=NvDsInferParseYolo
#parse-bbox-func-name=NvDsInferParseYoloCuda
custom-lib-path=/nvdsinfer_custom_impl_mgn_v2/libnvdsinfer_custom_impl_Yolo.so
#engine-create-func-name=NvDsInferYoloCudaEngineGet

 

#[class-attrs-all]
#nms-iou-threshold=0.5
#pre-cluster-threshold=0.5
#topk=300





To reduce error, i using std, mean in onnx graph, so in reid model:
net-scale-factor=1.0
offsets=0.0;0.0;0.0

Logic to get output tensor 2048

                       l_user = obj_meta.obj_user_meta_list

                        while l_user is not None:
                        
                            try:
                                # Casting l_user.data to pyds.NvDsUserMeta
                                user_meta=pyds.NvDsUserMeta.cast(l_user.data)
                            except StopIteration:
                                break

                            if (
                                user_meta.base_meta.meta_type
                                != pyds.NvDsMetaType.NVDSINFER_TENSOR_OUTPUT_META
                            ):
                                continue
                            
                            # Converting to tensor metadata
                            # Casting user_meta.user_meta_data to NvDsInferTensorMeta
                            tensor_meta = pyds.NvDsInferTensorMeta.cast(user_meta.user_meta_data)
                            
                            # Get output layer as NvDsInferLayerInfo 
                            layer = pyds.get_nvds_LayerInfo(tensor_meta, 0)

                            # Convert NvDsInferLayerInfo buffer to numpy array
                            ptr = ctypes.cast(pyds.get_ptr(layer.buffer), ctypes.POINTER(ctypes.c_float))
                            embedding = np.ctypeslib.as_array(ptr, shape=(2048,))

I using newstreammux with default settings
Do you need me to provide any additional information?
Thanks

  1. are bbox_file and reid_file the groundthruth data? In python code, the bbox is from bbox_file. the bbox is from yolo model In DeepStream. did you compare the bbox from yolo model and from bbox_file? If you want to compare the reid model output in PyTorch and DeepStream, please make sure the input bbox to reid model is the same.

I try to fix this problem by using triton with python backend, but I get some error when using this with deepstream. I have create new topics here: Deepstream with nvinferserver (python backend triton mode)

Sorry for the late reply, Is this still an DeepStream issue to support? Thanks! do you mean “mean” is 0 and std is 1? if so, You can set net-scale-factor to 1.0 and set offsets to 0.0;0.0;0.0.

There is no update from you for a period, assuming this is not an issue anymore. Hence we are closing this topic. If need further support, please open a new one. Thanks.