fix: remove redundant slice in fine matching for onnx inference#337
Open
johnnysluckydays wants to merge 1 commit into
Open
fix: remove redundant slice in fine matching for onnx inference#337johnnysluckydays wants to merge 1 commit into
johnnysluckydays wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Remove redundant slice in
fine_matching.get_fine_matchto support ONNX export with dynamic match countsProblem Description
When exporting LoFTR to ONNX and performing inference on image pairs with varying numbers of coarse matches, the following shape-mismatch error frequently occurs like:
The exported ONNX model works for images that produce the exact same number of matches as during export, but fails for any other image pair.
Root Cause Analysis
The error originates from the slice operation
[:len(data['mconf'])]insideFineMatching.get_fine_match:During training, CoarseMatching augments the coarse matches with ground-truth padding samples, causing a mismatch between the lengths of coords_normed (which includes all padded samples) and mkpts1_c (which only contains valid predictions). The slice is necessary to align them for loss computation.
During inference (i.e., model.eval()), the training branch is not executed. All coarse match tensors (b_ids, i_ids, j_ids, mkpts0_c, mkpts1_c, mconf) are derived from the same set of predicted matches and therefore have identical first-dimension lengths. The slice operation becomes redundant.
When exporting to ONNX using torch.onnx.export (tracing mode), the slice index len(data['mconf']) is evaluated once on the export-time inputs and hard-coded as a constant (e.g., 2408). For any new image pair with a different number of coarse matches (e.g., 2707), the slice produces a tensor of the wrong length, leading to a shape broadcast error in the subsequent Add node.