Skip to content

test: add regression test for RFDETRSegNano PE interpolation at custom resolution#1026

Draft
Copilot wants to merge 2 commits intodevelopfrom
copilot/fix-segmentation-training-error
Draft

test: add regression test for RFDETRSegNano PE interpolation at custom resolution#1026
Copilot wants to merge 2 commits intodevelopfrom
copilot/fix-segmentation-training-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 4, 2026

RFDETRSegNano(resolution=1008, num_classes=2) raised RuntimeError: size mismatch for backbone.0.encoder.encoder.embeddings.position_embeddings when loading pretrain weights — the checkpoint's 26×26 PE grid (default 312px resolution) wasn't being interpolated to match the model's 84×84 grid (1008px / patch_size=12).

The fix was already present in the current codebase: inference._build_model_context routes through load_pretrain_weightsinterpolate_position_embeddings, and ModelConfig._sync_pe_with_resolution correctly propagates positional_encoding_size = resolution // patch_size when a custom resolution is supplied. However, there was no end-to-end test covering the segmentation model path.

Changes

  • tests/training/test_load_pretrain_weights.py — adds test_rfdetr_seg_nano_loads_default_pe_checkpoint_at_custom_resolution to TestL1FacadePEInterpolationEndToEnd, mirroring the existing RFDETRNano test for the segmentation variant:
    1. Builds a default-resolution RFDETRSegNano (no pretrain weights, 90 classes) and saves its state dict as a checkpoint with args.segmentation_head=True, args.patch_size=12
    2. Reloads at resolution=624 (num_classes=2), triggering PE interpolation from 26×26 to 52×52
    3. Asserts the backbone position_embeddings tensor shape is [1, 2705, 384] (52²+1 tokens)
# Before fix (v1.6.5.post0): _load_pretrain_weights_into in detr.py skipped interpolation
# RuntimeError: size mismatch for backbone.0.encoder.encoder.embeddings.position_embeddings:
#   copying a param with shape torch.Size([1, 677, 384]) from checkpoint,
#   the shape in current model is torch.Size([1, 7057, 384]).

# Now works:
model = RFDETRSegNano(resolution=1008, num_classes=2)
model.train(dataset_dir=..., epochs=10, batch_size=1)

…tom resolution

Adds test_rfdetr_seg_nano_loads_default_pe_checkpoint_at_custom_resolution to
TestL1FacadePEInterpolationEndToEnd. This mirrors the existing RFDETRNano test
but covers the segmentation model case reported in issue #1010.

The test saves a default-resolution RFDETRSegNano state_dict as a checkpoint
(simulating rf-detr-seg-nano.pt) and loads it at a higher resolution, verifying
that PE interpolation succeeds and the backbone's position_embeddings tensor has
the correct shape for the new resolution.

The fix (routing through load_pretrain_weights → interpolate_position_embeddings)
was already present in inference._build_model_context; this test pins the wiring
so a future refactor cannot reintroduce a divergent loader path that silently
skips PE interpolation for segmentation variants.

Agent-Logs-Url: https://github.com/roboflow/rf-detr/sessions/c6ff38d8-bd65-4f52-a9ec-e9bc91adfea2

Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix segmentation training error in RFDETRSegNano test: add regression test for RFDETRSegNano PE interpolation at custom resolution May 4, 2026
Copilot finished work on behalf of Borda May 4, 2026 08:11
Copilot AI requested a review from Borda May 4, 2026 08:11
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.

v1.6.5.post0: Segmentation training is still facing error: Error(s) in loading state_dict for LWDETR

2 participants