Repository navigation
model: Add DEIMv2 to Transformers #44339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
eaef822
init: Add files (v1)
harshaljanjani ddc1bd7
fix: Fix ci/circleci: check_repository_consistency
harshaljanjani 85c7356
feat: Add support and test harness for all variants
harshaljanjani adc4079
fix: Fix ci/circleci: check_repository_consistency
harshaljanjani 81a3d06
Merge branch 'main' into add-deimv2
harshaljanjani 39d300e
refactor: Resolve review comments
harshaljanjani 476d69f
Merge branch 'main' into add-deimv2
harshaljanjani 4ad0dc5
refactor: Resolve second review round
harshaljanjani 16f2d07
nit: Fix copyright year
harshaljanjani 78eaf93
Merge branch 'main' into add-deimv2
harshaljanjani dbe577b
Merge branch 'main' into add-deimv2
harshaljanjani 1259628
Merge branch 'main' into add-deimv2
harshaljanjani 31ee908
refactor: Resolve third review round
harshaljanjani 4a3a877
revert: Adhere to the pattern from yonigozlan
harshaljanjani 558c2af
Merge branch 'main' into add-deimv2
harshaljanjani ada78bf
nit: Clarify the docstring
harshaljanjani 496ce9c
refactor: Resolve fourth review round
harshaljanjani 5a12a56
Merge branch 'main' into add-deimv2
harshaljanjani 85b4079
Merge branch 'main' into add-deimv2
harshaljanjani 422a440
refactor: Closing in on the final set of nits
harshaljanjani f932158
Merge branch 'main' into add-deimv2
harshaljanjani b833ee3
fix: Resolve merge conflicts
harshaljanjani 58a6424
fix: Add loss override and address nits
harshaljanjani 7dd0fb1
nits: Fix minor issues
harshaljanjani 943f4bb
fixup their init weights
vasqu 6213518
Merge branch 'main' into add-deimv2
vasqu fb1f387
fix: Fix loss coupling issue
harshaljanjani 3629f13
Merge branch 'main' into add-deimv2
harshaljanjani a3d9ac5
Merge branch 'main' into add-deimv2
vasqu b9becfe
fix date
vasqu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <!--Copyright 2026 The HuggingFace Team. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
|
|
||
| ⚠️ Note that this file is in Markdown but contains specific syntax for our doc-builder (similar to MDX) that may not be | ||
| rendered properly in your Markdown viewer. | ||
|
|
||
| --> | ||
| *This model was released on 2025-09-25 and added to Hugging Face Transformers on 2026-04-27.* | ||
|
|
||
| # DEIMv2 | ||
|
|
||
| ## Overview | ||
|
|
||
| DEIMv2 (DETR with Improved Matching v2) was proposed in [DEIMv2: Real-Time Object Detection Meets DINOv3](https://huggingface.co/papers/2509.20787) by Shihua Huang, Yongjie Hou, Longfei Liu, Xuanlong Yu, and Xi Shen. | ||
|
|
||
| The abstract from the paper is the following: | ||
|
|
||
| *Driven by the simple and effective Dense O2O, DEIM demonstrates faster convergence and enhanced performance. In this work, we extend it with DINOv3 features, resulting in DEIMv2. DEIMv2 spans eight model sizes from X to Atto, covering GPU, edge, and mobile deployment. For the X, L, M, and S variants, we adopt DINOv3-pretrained / distilled backbones and introduce a Spatial Tuning Adapter (STA), which efficiently converts DINOv3's single-scale output into multi-scale features and complements strong semantics with fine-grained details to enhance detection. For ultra-lightweight models (Nano, Pico, Femto, and Atto), we employ HGNetv2 with depth and width pruning to meet strict resource budgets. Together with a simplified decoder and an upgraded Dense O2O, this unified design enables DEIMv2 to achieve a superior performance-cost trade-off across diverse scenarios, establishing new state-of-the-art results. Notably, our largest model, DEIMv2-X, achieves 57.8 AP with only 50.3M parameters, surpassing prior X-scale models that require over 60M parameters for just 56.5 AP. On the compact side, DEIMv2-S is the first sub-10M model (9.71M) to exceed the 50 AP milestone on COCO, reaching 50.9 AP. Even the ultra-lightweight DEIMv2-Pico, with just 1.5M parameters, delivers 38.5 AP-matching YOLOv10-Nano (2.3M) with ~50% fewer parameters.* | ||
|
|
||
| ## Usage | ||
|
|
||
| ```python | ||
| from transformers import AutoImageProcessor, AutoModelForObjectDetection | ||
| from transformers.image_utils import load_image | ||
|
|
||
| url = "http://images.cocodataset.org/val2017/000000039769.jpg" | ||
| image = load_image(url) | ||
|
|
||
| image_processor = AutoImageProcessor.from_pretrained("harshaljanjani/DEIMv2_HGNetv2_N_COCO_Transformers") | ||
| model = AutoModelForObjectDetection.from_pretrained("harshaljanjani/DEIMv2_HGNetv2_N_COCO_Transformers", device_map="auto") | ||
|
|
||
| inputs = image_processor(images=image, return_tensors="pt").to(model.device) | ||
| outputs = model(**inputs) | ||
|
|
||
| results = image_processor.post_process_object_detection( | ||
| outputs, threshold=0.5, target_sizes=[image.size[::-1]] | ||
| ) | ||
|
|
||
| for result in results: | ||
| for score, label, box in zip(result["scores"], result["labels"], result["boxes"]): | ||
| box = [round(i, 2) for i in box.tolist()] | ||
| print(f"Detected {model.config.id2label[label.item()]} with confidence {round(score.item(), 3)} at location {box}") | ||
| ``` | ||
|
|
||
| ## Deimv2Config | ||
|
|
||
| [[autodoc]] Deimv2Config | ||
|
|
||
| ## Deimv2Model | ||
|
|
||
| [[autodoc]] Deimv2Model | ||
| - forward | ||
|
|
||
| ## Deimv2ForObjectDetection | ||
|
|
||
| [[autodoc]] Deimv2ForObjectDetection | ||
| - forward |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| # Copyright 2026 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| import torch | ||
| import torch.nn.functional as F | ||
|
|
||
| from ..utils import is_vision_available | ||
| from .loss_d_fine import DFineLoss, _set_aux_loss, _set_aux_loss2 | ||
| from .loss_for_object_detection import box_iou | ||
|
|
||
|
|
||
| if is_vision_available(): | ||
| from transformers.image_transforms import center_to_corners_format | ||
|
|
||
|
|
||
| class Deimv2Loss(DFineLoss): | ||
| def __init__(self, config): | ||
| super().__init__(config) | ||
| self.weight_dict = { | ||
| "loss_mal": config.weight_loss_mal, | ||
| "loss_bbox": config.weight_loss_bbox, | ||
| "loss_giou": config.weight_loss_giou, | ||
| "loss_fgl": config.weight_loss_fgl, | ||
| "loss_ddf": config.weight_loss_ddf, | ||
| } | ||
| self.losses = ["mal", "boxes", "local"] | ||
| self.mal_alpha = config.mal_alpha | ||
| self.use_dense_one_to_one = config.use_dense_one_to_one | ||
|
|
||
| def loss_labels_mal(self, outputs, targets, indices, num_boxes): | ||
| """Compute the Matching Aware Loss (MAL), which uses IoU-weighted soft labels | ||
| instead of hard one-hot targets, with focal-style weighting controlled by `mal_alpha`. | ||
| """ | ||
| idx = self._get_source_permutation_idx(indices) | ||
|
|
||
| src_boxes = outputs["pred_boxes"][idx] | ||
| target_boxes = torch.cat([t["boxes"][i] for t, (_, i) in zip(targets, indices)], dim=0) | ||
| ious, _ = box_iou(center_to_corners_format(src_boxes), center_to_corners_format(target_boxes)) | ||
| ious = torch.diag(ious).detach() | ||
|
|
||
| src_logits = outputs["logits"] | ||
| target_classes_original = torch.cat([t["class_labels"][i] for t, (_, i) in zip(targets, indices)]) | ||
| target_classes = torch.full( | ||
| src_logits.shape[:2], self.num_classes, dtype=torch.int64, device=src_logits.device | ||
| ) | ||
| target_classes[idx] = target_classes_original | ||
| target = F.one_hot(target_classes, num_classes=self.num_classes + 1)[..., :-1] | ||
|
|
||
| target_score_original = torch.zeros_like(target_classes, dtype=src_logits.dtype) | ||
| target_score_original[idx] = ious.to(target_score_original.dtype) | ||
| target_score = target_score_original.unsqueeze(-1) * target | ||
|
|
||
| pred_score = F.sigmoid(src_logits).detach() | ||
| target_score = target_score.pow(self.gamma) | ||
| if self.mal_alpha is not None: | ||
| weight = self.mal_alpha * pred_score.pow(self.gamma) * (1 - target) + target | ||
| else: | ||
| weight = pred_score.pow(self.gamma) * (1 - target) + target | ||
|
|
||
| loss = F.binary_cross_entropy_with_logits(src_logits, target_score, weight=weight, reduction="none") | ||
| loss = loss.mean(1).sum() * src_logits.shape[1] / num_boxes | ||
| return {"loss_mal": loss} | ||
|
|
||
| def _get_dense_o2o_indices(self, indices, indices_aux_list): | ||
|
vasqu marked this conversation as resolved.
Resolved
|
||
| results = [] | ||
| for indices_aux in indices_aux_list: | ||
| indices = [ | ||
| (torch.cat([idx1[0], idx2[0]]), torch.cat([idx1[1], idx2[1]])) | ||
| for idx1, idx2 in zip(indices.copy(), indices_aux.copy()) | ||
| ] | ||
|
|
||
| for index in [torch.cat([idx[0][:, None], idx[1][:, None]], 1) for idx in indices]: | ||
| unique, counts = torch.unique(index, return_counts=True, dim=0) | ||
| count_sort_indices = torch.argsort(counts, descending=True) | ||
| unique_sorted = unique[count_sort_indices] | ||
| column_to_row = {} | ||
| for idx_pair in unique_sorted: | ||
| row_idx, col_idx = idx_pair[0].item(), idx_pair[1].item() | ||
| if row_idx not in column_to_row: | ||
| column_to_row[row_idx] = col_idx | ||
| final_rows = torch.tensor(list(column_to_row.keys()), device=index.device) | ||
| final_cols = torch.tensor(list(column_to_row.values()), device=index.device) | ||
| results.append((final_rows.long(), final_cols.long())) | ||
| return results | ||
|
|
||
| def get_loss(self, loss, outputs, targets, indices, num_boxes): | ||
| loss_map = { | ||
| "cardinality": self.loss_cardinality, | ||
| "local": self.loss_local, | ||
| "boxes": self.loss_boxes, | ||
| "focal": self.loss_labels_focal, | ||
| "vfl": self.loss_labels_vfl, | ||
| "mal": self.loss_labels_mal, | ||
| } | ||
| if loss not in loss_map: | ||
| raise ValueError(f"Loss {loss} not supported") | ||
| return loss_map[loss](outputs, targets, indices, num_boxes) | ||
|
|
||
| def forward(self, outputs, targets): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add some small comments? Similar as in other losses so we can follow slightly easier
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copied over from the other losses + a few unique ones. |
||
| """ | ||
| This performs the loss computation. | ||
|
|
||
| Args: | ||
| outputs (`dict`, *optional*): | ||
| Dictionary of tensors, see the output specification of the model for the format. | ||
| targets (`list[dict]`, *optional*): | ||
| List of dicts, such that `len(targets) == batch_size`. The expected keys in each dict depends on the | ||
| losses applied, see each loss' doc. | ||
| """ | ||
| if not self.use_dense_one_to_one: | ||
| return super().forward(outputs, targets) | ||
|
|
||
| # Retrieve the matching between the outputs of the last layer and the targets | ||
| outputs_without_aux = {k: v for k, v in outputs.items() if "auxiliary_outputs" not in k} | ||
| indices = self.matcher(outputs_without_aux, targets) | ||
|
|
||
| # Compute the average number of target boxes across all nodes, for normalization purposes | ||
| num_boxes = sum(len(t["class_labels"]) for t in targets) | ||
| num_boxes = torch.as_tensor([num_boxes], dtype=torch.float, device=next(iter(outputs.values())).device) | ||
| num_boxes = torch.clamp(num_boxes, min=1).item() | ||
|
|
||
| # Handle auxiliary outputs matching | ||
| cached_indices = [] | ||
| indices_aux_list = [] | ||
| if "auxiliary_outputs" in outputs: | ||
| for auxiliary_outputs in outputs["auxiliary_outputs"]: | ||
| aux_indices = self.matcher(auxiliary_outputs, targets) | ||
| cached_indices.append(aux_indices) | ||
| indices_aux_list.append(aux_indices) | ||
|
|
||
| # Dense one-to-one matching | ||
| indices_go = self._get_dense_o2o_indices(indices, indices_aux_list) | ||
| num_boxes_go = sum(len(x[0]) for x in indices_go) | ||
| num_boxes_go = torch.as_tensor([num_boxes_go], dtype=torch.float, device=next(iter(outputs.values())).device) | ||
| num_boxes_go = torch.clamp(num_boxes_go, min=1).item() | ||
|
|
||
| # Compute all the requested losses | ||
| losses = {} | ||
| for loss in self.losses: | ||
| use_union = loss in ("boxes", "local") | ||
| indices_in = indices_go if use_union else indices | ||
| num_boxes_in = num_boxes_go if use_union else num_boxes | ||
| l_dict = self.get_loss(loss, outputs, targets, indices_in, num_boxes_in) | ||
| l_dict = {k: l_dict[k] * self.weight_dict[k] for k in l_dict if k in self.weight_dict} | ||
| losses.update(l_dict) | ||
|
|
||
| # In case of auxiliary losses, we repeat this process with the output of each intermediate layer. | ||
| if "auxiliary_outputs" in outputs: | ||
| for i, auxiliary_outputs in enumerate(outputs["auxiliary_outputs"]): | ||
| for loss in self.losses: | ||
| use_union = loss in ("boxes", "local") | ||
| indices_in = indices_go if use_union else cached_indices[i] | ||
| num_boxes_in = num_boxes_go if use_union else num_boxes | ||
| l_dict = self.get_loss(loss, auxiliary_outputs, targets, indices_in, num_boxes_in) | ||
| l_dict = {k: l_dict[k] * self.weight_dict[k] for k in l_dict if k in self.weight_dict} | ||
| l_dict = {k + f"_aux_{i}": v for k, v in l_dict.items()} | ||
| losses.update(l_dict) | ||
|
|
||
| # In case of cdn auxiliary losses. For deimv2 | ||
| if "dn_auxiliary_outputs" in outputs: | ||
| if "denoising_meta_values" not in outputs: | ||
| raise ValueError( | ||
| "The output must have the 'denoising_meta_values` key. " | ||
| "Please, ensure that 'outputs' includes a 'denoising_meta_values' entry." | ||
| ) | ||
| dn_indices = self.get_cdn_matched_indices(outputs["denoising_meta_values"], targets) | ||
| dn_num_boxes = num_boxes * outputs["denoising_meta_values"]["dn_num_group"] | ||
| for i, auxiliary_outputs in enumerate(outputs["dn_auxiliary_outputs"]): | ||
| for loss in self.losses: | ||
| l_dict = self.get_loss(loss, auxiliary_outputs, targets, dn_indices, dn_num_boxes) | ||
| l_dict = {k: l_dict[k] * self.weight_dict[k] for k in l_dict if k in self.weight_dict} | ||
| l_dict = {k + f"_dn_{i}": v for k, v in l_dict.items()} | ||
| losses.update(l_dict) | ||
|
|
||
| return losses | ||
|
|
||
|
|
||
| def Deimv2ForObjectDetectionLoss( | ||
| logits, | ||
| labels, | ||
| device, | ||
| pred_boxes, | ||
| config, | ||
| outputs_class=None, | ||
| outputs_coord=None, | ||
| enc_topk_logits=None, | ||
| enc_topk_bboxes=None, | ||
| denoising_meta_values=None, | ||
| predicted_corners=None, | ||
| initial_reference_points=None, | ||
| **kwargs, | ||
| ): | ||
| criterion = Deimv2Loss(config) | ||
| criterion.to(device) | ||
|
|
||
| outputs_loss = {"logits": logits, "pred_boxes": pred_boxes.clamp(min=0, max=1)} | ||
| auxiliary_outputs = None | ||
|
|
||
| if config.auxiliary_loss: | ||
| if denoising_meta_values is not None: | ||
| dn_out_coord, normal_out_coord = torch.split( | ||
| outputs_coord.clamp(min=0, max=1), denoising_meta_values["dn_num_split"], dim=2 | ||
| ) | ||
| dn_out_class, normal_out_class = torch.split(outputs_class, denoising_meta_values["dn_num_split"], dim=2) | ||
| # https://github.com/Intellindust-AI-Lab/DEIMv2/blob/main/engine/deim/deim_decoder.py#L562-L571 | ||
| # The original splits denoising queries in the decoder; here it happens in the loss since the decoder returns unsplit tensors. | ||
| _, normal_logits = torch.split(logits, denoising_meta_values["dn_num_split"], dim=1) | ||
| _, normal_pred_boxes = torch.split(pred_boxes, denoising_meta_values["dn_num_split"], dim=1) | ||
| dn_out_corners, out_corners = torch.split(predicted_corners, denoising_meta_values["dn_num_split"], dim=2) | ||
| dn_out_refs, out_refs = torch.split(initial_reference_points, denoising_meta_values["dn_num_split"], dim=2) | ||
|
|
||
| outputs_loss["logits"] = normal_logits | ||
| outputs_loss["pred_boxes"] = normal_pred_boxes.clamp(min=0, max=1) | ||
| else: | ||
| normal_out_coord = outputs_coord.clamp(min=0, max=1) | ||
| normal_out_class = outputs_class | ||
| out_corners = predicted_corners | ||
| out_refs = initial_reference_points | ||
|
|
||
| auxiliary_outputs = _set_aux_loss2( | ||
| normal_out_class[:, :-1].transpose(0, 1), | ||
| normal_out_coord[:, :-1].transpose(0, 1), | ||
| out_corners[:, :-1].transpose(0, 1), | ||
| out_refs[:, :-1].transpose(0, 1), | ||
| out_corners[:, -1], | ||
| normal_out_class[:, -1], | ||
| ) | ||
|
|
||
| outputs_loss["auxiliary_outputs"] = auxiliary_outputs | ||
| outputs_loss["auxiliary_outputs"].extend( | ||
| _set_aux_loss([enc_topk_logits], [enc_topk_bboxes.clamp(min=0, max=1)]) | ||
| ) | ||
|
|
||
| if denoising_meta_values is not None: | ||
| dn_auxiliary_outputs = _set_aux_loss2( | ||
| dn_out_class.transpose(0, 1), | ||
| dn_out_coord.transpose(0, 1), | ||
| dn_out_corners.transpose(0, 1), | ||
| dn_out_refs.transpose(0, 1), | ||
| dn_out_corners[:, -1], | ||
| dn_out_class[:, -1], | ||
| ) | ||
| outputs_loss["dn_auxiliary_outputs"] = dn_auxiliary_outputs | ||
| outputs_loss["denoising_meta_values"] = denoising_meta_values | ||
|
|
||
| loss_dict = criterion(outputs_loss, labels) | ||
|
|
||
| loss = sum(loss_dict.values()) | ||
| return loss, loss_dict, auxiliary_outputs | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.