Skip to content

Add decoder-only deformable neck to Hungarian DETR detector#4

Open
TNodeCode with Copilot wants to merge 10 commits into
mainfrom
copilot/add-dinov3-feature-extractor
Open

Add decoder-only deformable neck to Hungarian DETR detector#4
TNodeCode with Copilot wants to merge 10 commits into
mainfrom
copilot/add-dinov3-feature-extractor

Conversation

Copilot AI commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

The Hungarian DETR path was consuming backbone features too directly, without a deformable-transformer-style neck. This update introduces a decoder-only deformable neck (no encoder stack) and makes neck depth configurable for DINO backbone outputs.

  • Decoder-only deformable neck

    • Added DeformableDecoderNeck and DeformableDecoderLayer in hungarian_head.py.
    • The neck builds multi-scale memory from backbone features, applies level embeddings + positional encoding, and refines learnable queries through a decoder stack only.
  • Head integration

    • HungarianDetectionHead now routes backbone features through the new neck before class/box prediction.
    • Removed the prior direct TransformerDecoder path that operated on raw projected features.
  • Configurability surfaced in detector API

    • DINOv2HungarianDetector now forwards:
      • num_decoder_layers (neck depth)
      • num_feature_levels (multi-scale memory levels)
    • replace_head(...) preserves these settings when rebuilding the head for class-count changes.
  • Behavioral intent

    • Keeps the backbone as the encoder and uses decoder-only processing in the neck, aligning with deformable DETR-style query decoding for detector heads.
detector = DINOv2HungarianDetector(
    num_classes=10,
    model_name="facebook/dinov2-base",
    hidden_dim=256,
    num_queries=100,
    num_decoder_layers=8,   # configurable neck depth
    num_feature_levels=4,   # configurable multi-scale levels
    device="cuda",
)

Copilot AI changed the title [WIP] Add DinoV3 feature extractor and integrate into framework Add DINOv2 ViT and ConvNext feature extractors with RetinaNet detectors Jun 19, 2026
Copilot AI requested a review from TNodeCode June 19, 2026 18:49
@TNodeCode
TNodeCode marked this pull request as ready for review June 19, 2026 18:55
@TNodeCode

Copy link
Copy Markdown
Owner

Can you also add a custom Detektion head that uses hungarian matching for matching detections and ground truth?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR integrates HuggingFace Transformers DINOv2 backbones (ViT and ConvNeXt) into the project and wires them into torchvision RetinaNet detectors, enabling training/inference with these newer feature extractors.

Changes:

  • Added feature_extractors.py implementing DINOv2 ViT/ConvNeXt extractors plus RetinaNet-compatible backbone adapters (5-level pyramids).
  • Added two new AbstractDetector implementations using those backbones with torchvision’s RetinaNet.
  • Updated dependencies to include transformers.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.

File Description
requirements.txt Adds transformers dependency required by the new DINOv2/ConvNeXt backbones.
feature_extractors.py Introduces DINOv2-based feature extractors and backbone adapters that emit RetinaNet-compatible feature pyramids.
detectors.py Adds new RetinaNet detector classes that use the new DINOv2 ViT/ConvNeXt backbones.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread feature_extractors.py Outdated
Comment thread feature_extractors.py
Comment on lines +77 to +95
captured = OrderedDict()
hooks = []

if self.output_patches:
# key= default-argument captures current string at definition time
hooks.append(
self.model.embeddings.register_forward_hook(
lambda m, i, o, key="embeddings": captured.update({key: o})
)
)
for layer_idx in self.layers:
# key=layer_idx default-argument captures the current int at definition time
hooks.append(
self.model.encoder.layer[layer_idx].register_forward_hook(
lambda m, i, o, key=layer_idx: captured.update({key: o})
)
)

z = self.model(x)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then use output_hidden_states=True

Copilot AI changed the title Add DINOv2 ViT and ConvNext feature extractors with RetinaNet detectors Add DETR-style Hungarian matching detection head for DINOv2 Jun 19, 2026
Copilot AI changed the title Add DETR-style Hungarian matching detection head for DINOv2 Add mkdocs usage docs for dataset layout, training flow, and inference Jun 19, 2026
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI changed the title Add mkdocs usage docs for dataset layout, training flow, and inference Add decoder-only deformable neck to Hungarian DETR detector Jun 20, 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.

3 participants