From 7c2e467a274c2b58bcc2bb6f38c68e4c1d42d0b2 Mon Sep 17 00:00:00 2001 From: hlista Date: Fri, 28 Aug 2026 16:20:07 -0500 Subject: [PATCH 1/4] fix: remove obsolete reshaped_input_sizes from SAM2 post-processing --- .../image_util/segment_anything/segment_anything_2_pipeline.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py b/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py index c7b6bf6b39b..63e21fc5c72 100644 --- a/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py +++ b/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py @@ -100,8 +100,7 @@ def segment( # Post-process the masks to get the final segmentation masks = self._sam2_processor.post_process_masks( masks=outputs.pred_masks, - original_sizes=processed_inputs.original_sizes, - reshaped_input_sizes=processed_inputs.reshaped_input_sizes, + original_sizes=processed_inputs.original_sizes ) # There should be only one batch. From bc4b369a9ee8878f61a615e52e2e46a2277f7635 Mon Sep 17 00:00:00 2001 From: hlista Date: Fri, 28 Aug 2026 22:16:46 -0500 Subject: [PATCH 2/4] test: add segmentation pipeline regression tests for SAM and SAM2 --- .../segment_anything_2_pipeline.py | 3 +- .../test_segment_anything_2_pipeline.py | 53 +++++++++++++++++++ .../test_segment_anything_pipeline.py | 52 ++++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 tests/backend/image_util/segment_anything/test_segment_anything_2_pipeline.py create mode 100644 tests/backend/image_util/segment_anything/test_segment_anything_pipeline.py diff --git a/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py b/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py index 63e21fc5c72..591921fa5ca 100644 --- a/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py +++ b/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py @@ -99,8 +99,7 @@ def segment( # Post-process the masks to get the final segmentation masks = self._sam2_processor.post_process_masks( - masks=outputs.pred_masks, - original_sizes=processed_inputs.original_sizes + masks=outputs.pred_masks, original_sizes=processed_inputs.original_sizes ) # There should be only one batch. diff --git a/tests/backend/image_util/segment_anything/test_segment_anything_2_pipeline.py b/tests/backend/image_util/segment_anything/test_segment_anything_2_pipeline.py new file mode 100644 index 00000000000..390cb51ec9f --- /dev/null +++ b/tests/backend/image_util/segment_anything/test_segment_anything_2_pipeline.py @@ -0,0 +1,53 @@ +import torch +from PIL import Image +from transformers.models.sam2.configuration_sam2 import Sam2Config +from transformers.models.sam2.image_processing_sam2 import Sam2ImageProcessor +from transformers.models.sam2.modeling_sam2 import Sam2Model +from transformers.models.sam2.processing_sam2 import Sam2Processor + +from invokeai.backend.image_util.segment_anything.segment_anything_2_pipeline import ( + SegmentAnything2Pipeline, +) + +from invokeai.backend.image_util.segment_anything.shared import ( + SAMInput, + SAMPoint, + SAMPointLabel, +) + +def test_segment_anything_2_pipeline_segment(): + width = 96 + height = 64 + + image = Image.new("RGB", (width, height)) + + model = Sam2Model(Sam2Config()) + processor = Sam2Processor( + image_processor=Sam2ImageProcessor(), + ) + + pipeline = SegmentAnything2Pipeline( + sam2_model=model, + sam2_processor=processor, + ) + + inputs = [ + SAMInput( + points=[ + SAMPoint( + x=width // 2, + y=height // 2, + label=SAMPointLabel.positive, + ) + ] + ) + ] + + with torch.inference_mode(): + masks = pipeline.segment( + image=image, + inputs=inputs, + ) + + assert masks.dtype == torch.bool + assert masks.shape == (1, 3, height, width) \ No newline at end of file diff --git a/tests/backend/image_util/segment_anything/test_segment_anything_pipeline.py b/tests/backend/image_util/segment_anything/test_segment_anything_pipeline.py new file mode 100644 index 00000000000..1060c50c49d --- /dev/null +++ b/tests/backend/image_util/segment_anything/test_segment_anything_pipeline.py @@ -0,0 +1,52 @@ +import torch +from PIL import Image +from transformers.models.sam.configuration_sam import SamConfig +from transformers.models.sam.image_processing_sam import SamImageProcessor +from transformers.models.sam.modeling_sam import SamModel +from transformers.models.sam.processing_sam import SamProcessor + +from invokeai.backend.image_util.segment_anything.segment_anything_pipeline import ( + SegmentAnythingPipeline, +) +from invokeai.backend.image_util.segment_anything.shared import ( + SAMInput, + SAMPoint, + SAMPointLabel, +) + +def test_segment_anything_pipeline_segment(): + width = 96 + height = 64 + + image = Image.new("RGB", (width, height)) + + model = SamModel(SamConfig()) + processor = SamProcessor( + image_processor=SamImageProcessor(), + ) + + pipeline = SegmentAnythingPipeline( + sam_model=model, + sam_processor=processor, + ) + + inputs = [ + SAMInput( + points=[ + SAMPoint( + x=width // 2, + y=height // 2, + label=SAMPointLabel.positive, + ) + ] + ) + ] + + with torch.inference_mode(): + masks = pipeline.segment( + image=image, + inputs=inputs, + ) + + assert masks.dtype == torch.bool + assert masks.shape == (1, 3, height, width) \ No newline at end of file From 173966e8169df3fc75895ac6ac3e9b01c40c4d60 Mon Sep 17 00:00:00 2001 From: hlista Date: Fri, 28 Aug 2026 22:30:44 -0500 Subject: [PATCH 3/4] docs: update SAM2 pipeline comments and docstring --- .../image_util/segment_anything/segment_anything_2_pipeline.py | 3 +-- .../segment_anything/test_segment_anything_2_pipeline.py | 2 +- .../segment_anything/test_segment_anything_pipeline.py | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py b/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py index 591921fa5ca..5f0655e9633 100644 --- a/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py +++ b/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py @@ -3,7 +3,6 @@ import torch from PIL import Image -# Import SAM2 components - these should be available in transformers 4.56.0+ from transformers.models.sam2 import Sam2Model from transformers.models.sam2.processing_sam2 import Sam2Processor @@ -19,7 +18,7 @@ def __init__(self, sam2_model: Sam2Model, sam2_processor: Sam2Processor): Args: sam2_model: The SAM2 model - sam2_processor: The SAM2 processor (can be Sam2Processor or Sam2VideoProcessor) + sam2_processor: The SAM2 processor used to preprocess inputs and post-process masks """ self._sam2_model = sam2_model self._sam2_processor = sam2_processor diff --git a/tests/backend/image_util/segment_anything/test_segment_anything_2_pipeline.py b/tests/backend/image_util/segment_anything/test_segment_anything_2_pipeline.py index 390cb51ec9f..b2245a443dc 100644 --- a/tests/backend/image_util/segment_anything/test_segment_anything_2_pipeline.py +++ b/tests/backend/image_util/segment_anything/test_segment_anything_2_pipeline.py @@ -50,4 +50,4 @@ def test_segment_anything_2_pipeline_segment(): ) assert masks.dtype == torch.bool - assert masks.shape == (1, 3, height, width) \ No newline at end of file + assert masks.shape == (1, 3, height, width) diff --git a/tests/backend/image_util/segment_anything/test_segment_anything_pipeline.py b/tests/backend/image_util/segment_anything/test_segment_anything_pipeline.py index 1060c50c49d..988a425ccd8 100644 --- a/tests/backend/image_util/segment_anything/test_segment_anything_pipeline.py +++ b/tests/backend/image_util/segment_anything/test_segment_anything_pipeline.py @@ -49,4 +49,4 @@ def test_segment_anything_pipeline_segment(): ) assert masks.dtype == torch.bool - assert masks.shape == (1, 3, height, width) \ No newline at end of file + assert masks.shape == (1, 3, height, width) From 2451e250bce5abd1aab4b7d51e7f4c58a51d084d Mon Sep 17 00:00:00 2001 From: hlista Date: Sat, 29 Aug 2026 02:34:17 -0500 Subject: [PATCH 4/4] fixing ruff formating issues in segm pipeline --- .../image_util/segment_anything/segment_anything_2_pipeline.py | 1 - .../segment_anything/test_segment_anything_2_pipeline.py | 2 +- .../segment_anything/test_segment_anything_pipeline.py | 1 + 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py b/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py index 5f0655e9633..845c825e3e8 100644 --- a/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py +++ b/invokeai/backend/image_util/segment_anything/segment_anything_2_pipeline.py @@ -2,7 +2,6 @@ import torch from PIL import Image - from transformers.models.sam2 import Sam2Model from transformers.models.sam2.processing_sam2 import Sam2Processor diff --git a/tests/backend/image_util/segment_anything/test_segment_anything_2_pipeline.py b/tests/backend/image_util/segment_anything/test_segment_anything_2_pipeline.py index b2245a443dc..9aa4f454dd1 100644 --- a/tests/backend/image_util/segment_anything/test_segment_anything_2_pipeline.py +++ b/tests/backend/image_util/segment_anything/test_segment_anything_2_pipeline.py @@ -8,13 +8,13 @@ from invokeai.backend.image_util.segment_anything.segment_anything_2_pipeline import ( SegmentAnything2Pipeline, ) - from invokeai.backend.image_util.segment_anything.shared import ( SAMInput, SAMPoint, SAMPointLabel, ) + def test_segment_anything_2_pipeline_segment(): width = 96 height = 64 diff --git a/tests/backend/image_util/segment_anything/test_segment_anything_pipeline.py b/tests/backend/image_util/segment_anything/test_segment_anything_pipeline.py index 988a425ccd8..736e592abfa 100644 --- a/tests/backend/image_util/segment_anything/test_segment_anything_pipeline.py +++ b/tests/backend/image_util/segment_anything/test_segment_anything_pipeline.py @@ -14,6 +14,7 @@ SAMPointLabel, ) + def test_segment_anything_pipeline_segment(): width = 96 height = 64