Skip to content
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

Fix Batch Size Mismatch When Using crops_n_layers in mask-generation Pipeline #35530 #35627

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

sambhavnoobcoder
Copy link

@sambhavnoobcoder sambhavnoobcoder commented Jan 11, 2025

Overview

This pull request addresses a batch size mismatch issue that arises when users set the parameter crops_n_layers > 0 in the Segment Anything Model (SAM) powered mask-generation pipeline. Previously, attempting to run the pipeline caused a runtime error, as the model would receive mismatched batch dimensions for image embeddings and input points. fixes issue #35530 . @Rocketknight1 kindly review this at the earliest .

Fixes #35530

The Problem

When segmenting images in "segment everything" mode with cropping enabled (crops_n_layers=1 or more), the pipeline splits the image into multiple crops. It generates a grid of points for each crop to compute masks in parallel. In some scenarios, particularly where the input is not batched before the cropping procedure, the pipeline would produce additional crops as separate batch entries. This led to:
• A mismatch in the batch dimension between the generated image embeddings and the corresponding point embeddings.
• A ValueError reporting different batch sizes, preventing the pipeline from completing the segmentation process.

Proposed Solution

We introduced internal improvements that unify the handling of image crops and points so they remain synchronized:
• Ensured that all crops from a single original image preserve the same batch index.
• Correctly merged the batched embeddings back together, with consistent shapes, so each batch item has the correct number of crops and associated point embeddings.

Approach

  1. Revisited the internal crop generation logic to ensure that each cropped region is tracked as part of the same batch, instead of being treated as separate entries.
  2. Adjusted how point embeddings are grouped per crop, clarifying that they should share the original batch dimension.
  3. Reworked the post-processing step to handle the final combination of crops so the final output remains consistent with the user’s single input image.

Screenshots

Before

Screenshot 2025-01-11 at 9 12 15 PM

After

Screenshot 2025-01-11 at 9 13 13 PM

Prior to this fix, the Before screenshot would visibly show an error stack trace indicating the batch mismatch. The After screenshot demonstrates a smooth run, producing valid masks across cropped regions.

@sambhavnoobcoder sambhavnoobcoder changed the title Fix Batch Size Mismatch When Using crops_n_layers in mask-generation Pipeline #35530 Fix Batch Size Mismatch When Using crops_n_layers in mask-generation Pipeline #35530 Jan 12, 2025
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.

SAM mask-generation - crops_n_layers
1 participant