Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The ONNX model with higher batch sizes is producing incorrect embeddings. #585

Open
aqibriaz95 opened this issue Oct 2, 2024 · 0 comments

Comments

@aqibriaz95
Copy link

aqibriaz95 commented Oct 2, 2024

I have trained the OSNet_1x model on the Market dataset and successfully converted it to an ONNX file with a single batch size. The model works as expected when generating embeddings for the same person, with the Euclidean distance confirming the match (e.g., a distance of 15.36). However, when I convert the model to higher batch sizes, such as 14 or 100, using the same set of images, the results are significantly different. The distance increases drastically (e.g., 247.5) and suggests incorrect embeddings.

I’ve tested it with multiple image sets, and in every case, the single-batch ONNX produces correct results with small distances for matching images, while the higher batch size ONNX returns random embeddings with incorrect distances, regardless of whether the images match or not.
Following is the code I used to convert into ONNX.
`f```
rom torchreid import models
import torch
import torch.onnx
from torchreid.utils import (
check_isfile, load_pretrained_weights, compute_model_complexity
)

OSNet = models.build_model('osnet_x1_0', 1,pretrained="model.pth.tar-60", loss='softmax')
load_pretrained_weights(OSNet, "model.pth.tar-60")

OSNet.eval()
#OSNet.load_state_dict(torch.load("model.pth.tar-60"))
dynamic_axes = {
'input': {
0: 'batch'
},
'output': {
0: 'batch'
}
}

onnx_path = 'osnet_1x_trained_100b_new.onnx'
torch.onnx.export(OSNet, # model being run
torch.randn((14,3,256,128)), # model input (or a tuple for multiple inputs)
onnx_path, # where to save the model (can be a file or file-like object)
#export_params=True, # store the trained parameter weights inside the model file
opset_version=12, # the ONNX version to export the model to
do_constant_folding=True, # whether to execute constant folding for optimization
input_names = ['input'], # the model's input names
output_names = ['output'],
dynamic_axes=dynamic_axes
# the model's output names
)

`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant