Skip to content

fix(seg): filter unlabeled images and fix mask-loss DDP graph disconnect#323

Open
imagra93 wants to merge 2 commits into
LibreYOLO:devfrom
imagra93:fix/seg-ddp-unlabeled-images
Open

fix(seg): filter unlabeled images and fix mask-loss DDP graph disconnect#323
imagra93 wants to merge 2 commits into
LibreYOLO:devfrom
imagra93:fix/seg-ddp-unlabeled-images

Conversation

@imagra93
Copy link
Copy Markdown
Contributor

@imagra93 imagra93 commented Jun 3, 2026

Problem

DDP segmentation training (static_graph=True) crashes when the dataset contains unlabeled images. Two independent code paths contribute:

  1. Disconnected gradient in loss_masks — when a rank's batch has
    zero Hungarian matches, the factored mask-head branch returns
    torch.tensor([], device=device). This tensor has no grad_fn, so
    spatial_features, query_features, and bias are silently dropped
    from the backward graph on that rank, violating static_graph=True.

  2. Unlabeled images in the dataloader — images with no annotation
    file inflate the dataset and can produce all-unlabeled batches on some
    ranks, making the no-match case structurally inevitable.

Fixes #291

Solution

libreyolo/models/rfdetr/loss.py
Return zero losses routed through the mask-head tensors instead of a
disconnected empty tensor:

zero = spatial_features.sum() * 0.0 + query_features.sum() * 0.0 + bias * 0.0
return {"loss_mask_ce": zero, "loss_mask_dice": zero}

Every mask-head parameter participates in backward (with zero gradient),
satisfying DDP's reducer regardless of batch content.

libreyolo/data/dataset.py

New filter_empty_annotations: bool = False parameter on YOLODataset.
When True, zero-annotation images are dropped at construction time. A
warning is logged (main process only) with the count of dropped images.
Raises ValueError if the entire dataset is unlabeled.

libreyolo/training/trainer.py

filter_empty_annotations=load_segments passed to all three
YOLODataset construction sites, enabling the filter automatically for
every segmentation training path.

@imagra93
Copy link
Copy Markdown
Contributor Author

imagra93 commented Jun 3, 2026

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@imagra93 imagra93 marked this pull request as ready for review June 3, 2026 10:01
@EHxuban11
Copy link
Copy Markdown
Contributor

I'm concerned that this PR is dropping the unlabelled images basically (often called background images)d, but background images are useful in order to avoid the model to detect false positives when there isn't an object in the image.

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.

[Bug] Training error occurs when using the latest compiled code for RF-DETR

2 participants