# 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