Skip to content

Add ONNX export and a CI check - #46

Open
sarlinpe wants to merge 1 commit into
mainfrom
sarlinpe/onnx
Open

sarlinpe wants to merge 1 commit into
mainfrom
sarlinpe/onnx

Conversation

@sarlinpe

Copy link
Copy Markdown
Member

This PR adds the ONNX export of GeoCalib's perspective field network used by COLMAP, a verification script against the PyTorch implementation, and a CI workflow.

Contents

  • onnx/export.py exports GeoCalib's feed-forward perspective field estimation network (MSCAN backbone + LowLevelEncoder + PerspectiveDecoder):
    • Input: image (1, 3, H, W) float32 in [0, 1] with dynamic height and width divisible by 32.
    • Outputs: up_field (1, 2, H, W), up_confidence (1, 1, H, W), latitude_field (1, 1, H, W), and latitude_confidence (1, 1, H, W).
  • No duplicated model code or monkey-patching:
    • GeoCalib.predict_fields in geocalib/geocalib.py encapsulates the feed-forward perspective field prediction and is shared between GeoCalib.forward and PerspectiveFieldsONNX.
    • NMF2D.freeze_bases (GeoCalib.freeze_nmf_bases) fixes the initial NMF basis to a deterministic buffer (None by default, so default PyTorch behavior is unchanged).
    • LightHamHead.forward uses scale_factor=float(2**i) (and identity for i == 0), which is bit-for-bit identical in PyTorch while supporting dynamic spatial dimensions natively in ONNX.
  • onnx/check.py checks any exported ONNX model against a PyTorch checkpoint (--weights) across the 4 asset images in landscape, portrait (rot90), and square aspect ratios:
    • Compares the dense up_field, latitude_field, up_confidence, and latitude_confidence predictions (<= 6.3e-6 max error on CPU).
    • Compares the camera (vfov, radial distortion k1) and gravity (roll, pitch) optimized by LMOptimizer from the ONNX-predicted perspective fields vs. PyTorch (<= 2.7e-5 deg on CPU).
  • CI (.github/workflows/onnx_export.yml):
    • Runs on changes to geocalib/, onnx/, requirements.txt, or pyproject.toml.
    • Exports geocalib_distorted.onnx on CPU and checks it against PyTorch with onnx/check.py.
  • README: new "ONNX export" section.

COLMAP integration: colmap/colmap#4797.

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