Skip to content

fix: remove redundant slice in fine matching for onnx inference#337

Open
johnnysluckydays wants to merge 1 commit into
zju3dv:masterfrom
johnnysluckydays:master
Open

fix: remove redundant slice in fine matching for onnx inference#337
johnnysluckydays wants to merge 1 commit into
zju3dv:masterfrom
johnnysluckydays:master

Conversation

@johnnysluckydays

@johnnysluckydays johnnysluckydays commented Apr 15, 2026

Copy link
Copy Markdown

Fix: Remove redundant slice in fine_matching.get_fine_match to support ONNX export with dynamic match counts

Problem 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:

2026-04-15 07:12:03.730711457 [E:onnxruntime:, sequential_executor.cc:572 ExecuteKernel] Non-zero status code 
 returned while running Add node. Name:'/fine_matching/Add_2' Status Message:   
/onnxruntime_src/onnxruntime/core/providers/cpu/math/element_wise_ops.h:560 void 
onnxruntime::BroadcastIterator::Append(ptrdiff_t, ptrdiff_t) axis == 1 || axis == largest was false. 
Attempting to broadcast   an axis by a dimension other than 1. 2408 by 2707  

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'])] inside FineMatching.get_fine_match:

mkpts1_f = data['mkpts1_c'] + (coords_normed * (W // 2) * scale1)[:len(data['mconf'])]

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.

@johnnysluckydays johnnysluckydays changed the title fix: remove redundant slice in fine matching for onnx export fix: remove redundant slice in fine matching for onnx inference Apr 15, 2026
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

Successfully merging this pull request may close these issues.

1 participant