Skip to content

Restore the rectangular imgsz a checkpoint was trained at (#899) - #901

Merged
EHxuban11 merged 5 commits into
devfrom
899-restore-rect-imgsz
Sep 25, 2026
Merged

EHxuban11 merged 5 commits into
devfrom
899-restore-rect-imgsz

Conversation

@EHxuban11

@EHxuban11 EHxuban11 commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

What: reload a rectangular fine-tune at the imgsz=(h, w) it was trained at.
Why: #899. Checkpoints store imgsz_h/imgsz_w, but the base loader ignored them, so predict/val/export fell back to the family's square default and padded frames again (43.75% padding for a 1920x1080 frame at 640).

  • Base _load_weights restores the pair (square pairs collapse to an int; a half pair raises). Checkpoints without the pair load exactly as before.
  • The live model matches what a reload gives after train(): adopts a rectangular run's size, drops a restored rectangle after a later square run. Natively rectangular families (HRNet, PP-LiteSeg, U-Net) are skipped.
  • Default export() where the family/format pair has no rectangular export (YOLOX, YOLOv7, RTMDet, PicoDet; YOLO9 to ExecuTorch/Paddle/MNN) falls back to a max(h, w) square with a warning instead of raising. Explicit rectangular imgsz still raises as before. CLI export --json reports the exported canvas.
  • predict(tiling=True) tiles and infers at the long side instead of raising.
  • Found while checking for regressions, also fixed: YOLOX val(imgsz=(h, w)) TypeError (already on dev); TTA undid the letterbox at the family default instead of the requested imgsz (already on dev); YOLO9-E2E postprocess defaulted to 640; quantized export(format="pt") crashed on / dropped the pair; Python InferenceProfiler crashed on it; val preprocessor and capture_graph assumed square.

Check: _restore_checkpoint_input_size / _adopt_trained_input_size in models/base/model.py, and _square_fallback_for_restored_rect in export/exporter.py.
Verified: 7677 unit tests pass; the 3 failures (DETR ONNX parity on macOS) also fail on clean dev. New tests fail on dev except the ones pinning unchanged behavior (square checkpoints, explicit rectangular export still raising). CPU probes on yolo9/yolo9_p2/yolo9_e2e/yolox/yolo7/rtmdet/picodet: predict, batch, numpy, val, val(augment=True), info, save/reload, ONNX export, track all pass. End-to-end YOLO9t and YOLOX-n: train at (192, 320), val, reload, retrain square.
Not verified: GPU, non-ONNX export formats end to end (fallback covered by unit tests only), RKNN (resolves imgsz itself; unchanged from dev).
Opened by an agent.

Code provenance

Original code written for this PR; bug fixes to LibreYOLO's own first-party code, no third-party code ported, adapted, or introduced; no GPL/AGPL/LGPL/non-commercial/unknown-license material involved.

RetriggerConfidence Score: 5/5

The PR appears safe to merge; no outstanding finding or new actionable regression was established.

Summary

Restores rectangular checkpoint input sizes for prediction, validation, and export.

  • Synchronizes the live model’s size after training and provides square fallbacks where rectangular export is unsupported.
  • Updates tiling, preprocessing, postprocessing, profiling, quantized checkpoint export, and CLI export reporting to handle the restored size.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Checkpoint imgsz_h and imgsz_w] --> B[Restore model input size]
  B --> C[Predict and validate at restored size]
  B --> D{Export supports rectangle?}
  D -->|Yes| E[Export rectangular canvas]
  D -->|No, default size| F[Warn and export long-side square]
Loading

Reviews (3) · Last reviewed commit: "Merge remote-tracking branch 'origin/dev..."

A model trained with imgsz=(h, w) dual-writes imgsz_h/imgsz_w, but the
base loader ignored them, so predict/val/export went back to the
family's square default. Restore the pair on load, keep the live model
in sync after train(), and harden the paths that assumed a square size:
default export falls back to max(h, w) where rectangular export is not
supported, tiling uses the long side, YOLOX recomputes the val ratio per
axis, TTA undoes the letterbox at the preprocessed canvas, and the
quantized-checkpoint finalize and the inference profiler accept pairs.
Comment thread libreyolo/models/base/model.py
Comment thread libreyolo/models/base/inference.py
@EHxuban11
EHxuban11 merged commit fb7dfc4 into dev Sep 25, 2026
14 checks passed
@EHxuban11
EHxuban11 deleted the 899-restore-rect-imgsz branch September 26, 2026 15:35
EHxuban11 added a commit that referenced this pull request Sep 26, 2026
Restore the rectangular imgsz a checkpoint was trained at (#899)
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