Skip to content

Support empty label fields #2471

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

sklum
Copy link

@sklum sklum commented Mar 13, 2025

For my use case, it's valuable to be able to train on images without any labels of any kind as a part of a batch. This change allows that.

Summary by Sourcery

Enhancements:

  • Handle empty label fields during data processing.

Copy link
Contributor

sourcery-ai bot commented Mar 13, 2025

Reviewer's Guide by Sourcery

This pull request modifies the _process_label_fields method in albumentations/core/utils.py to support training on images without any labels. It adds a check to handle empty encoded_labels gracefully, preventing errors during concatenation with data_array.

Sequence diagram for processing label fields with empty labels

sequenceDiagram
  participant A as Albumentations
  participant L as LabelManager
  A->>A: _process_label_fields(data, data_name)
  loop for each label_field in label_fields
    A->>L: process_field(data_name, label_field, data[label_field])
    L-->>A: encoded_labels (empty)
    alt encoded_labels.size > 0
      A->>A: np.hstack((data_array, encoded_labels))
    else encoded_labels.size == 0
      A->>A: data_array (no change)
    end
  end
  A-->>A: return data_array
Loading

File-Level Changes

Change Details Files
Modified the _process_label_fields method to handle empty label fields gracefully.
  • Added a conditional check to ensure encoded_labels has a size greater than zero before concatenating it with data_array.
  • If encoded_labels is empty, data_array is initialized as an empty numpy array to avoid errors during concatenation.
albumentations/core/utils.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @sklum - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a test case that covers the scenario with empty labels.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@ternaus
Copy link
Collaborator

ternaus commented Mar 14, 2025

@sklum

For my use case, it's valuable to be able to train on images without any labels of any kind as a part of a batch. This change allows that.

What do you mean?

You can pass images, and no any other labels to nearly every transform.

@sklum
Copy link
Author

sklum commented Mar 14, 2025

If e.g. the bounding boxes are an empty tensor (i.e. bboxes = tensor([])), the call to hstack will fail with the following error:

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)

@sklum
Copy link
Author

sklum commented Mar 31, 2025

Here's an MRE which should hopefully clarify my use case. Is this not idiomatic?

import albumentations as A
import numpy as np
from torch import tensor


def main():
    img_size = 640

    train_transforms = A.Compose(
        [
            A.LongestMaxSize(max_size=img_size, interpolation=1),
            A.PadIfNeeded(min_height=img_size, min_width=img_size),
        ],
        bbox_params=A.BboxParams(
            format="coco",
            label_fields=["labels", "ids"],
            clip=True,
        ),
    )

    image = np.random.rand(640, 640, 3)

    target = {
        "bbox": tensor(
            [
                [174.2637, 116.5894, 264.3941, 315.8969],
                [1.8412, 40.0401, 602.9775, 462.5326],
            ]
        ),
        "labels": tensor([1, 2]),
    }

    # Works fine
    train_transforms(
        image=image,
        bboxes=target["bbox"],
        labels=target["labels"].tolist(),
        ids=range(len(target["labels"].tolist())),
    )

    # Image has no classes that I'm interested in detecting
    target = {
        "bbox": tensor([]),
        "labels": tensor([]),
    }

    """
    File "/Users/scottklum/Developer/albumentations_mre/.venv/lib/python3.10/site-packages/albumentations/core/utils.py", line 212, in _process_label_fields
    data_array = np.hstack((data_array, encoded_labels))
    File "/Users/scottklum/Developer/albumentations_mre/.venv/lib/python3.10/site-packages/numpy/_core/shape_base.py", line 365, in hstack
        return _nx.concatenate(arrs, 0, dtype=dtype, casting=casting)
    ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimensio
    """
    train_transforms(
        image=image,
        bboxes=target["bbox"],
        labels=target["labels"].tolist(),
        ids=range(len(target["labels"].tolist())),
    )


if __name__ == "__main__":
    main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants