Fix image segmentation pipeline errors, resolve backward compatibility issues (#19768)
* Fix panoptic segmentation and pipeline * Update ImageSegmentationPipeline tests and reenable test_small_model_pt * Resolve backward compatibility issues
This commit is contained in:
@@ -81,7 +81,12 @@ class ImageSegmentationPipelineTests(unittest.TestCase, metaclass=PipelineTestCa
|
||||
]
|
||||
|
||||
def run_pipeline_test(self, image_segmenter, examples):
|
||||
outputs = image_segmenter("./tests/fixtures/tests_samples/COCO/000000039769.png", threshold=0.0)
|
||||
outputs = image_segmenter(
|
||||
"./tests/fixtures/tests_samples/COCO/000000039769.png",
|
||||
threshold=0.0,
|
||||
mask_threshold=0,
|
||||
overlap_mask_area_threshold=0,
|
||||
)
|
||||
self.assertIsInstance(outputs, list)
|
||||
n = len(outputs)
|
||||
if isinstance(image_segmenter.model, (MaskFormerForInstanceSegmentation)):
|
||||
@@ -97,15 +102,15 @@ class ImageSegmentationPipelineTests(unittest.TestCase, metaclass=PipelineTestCa
|
||||
dataset = datasets.load_dataset("hf-internal-testing/fixtures_image_utils", "image", split="test")
|
||||
|
||||
# RGBA
|
||||
outputs = image_segmenter(dataset[0]["file"])
|
||||
outputs = image_segmenter(dataset[0]["file"], threshold=0.0, mask_threshold=0, overlap_mask_area_threshold=0)
|
||||
m = len(outputs)
|
||||
self.assertEqual([{"score": ANY(float, type(None)), "label": ANY(str), "mask": ANY(Image.Image)}] * m, outputs)
|
||||
# LA
|
||||
outputs = image_segmenter(dataset[1]["file"])
|
||||
outputs = image_segmenter(dataset[1]["file"], threshold=0.0, mask_threshold=0, overlap_mask_area_threshold=0)
|
||||
m = len(outputs)
|
||||
self.assertEqual([{"score": ANY(float, type(None)), "label": ANY(str), "mask": ANY(Image.Image)}] * m, outputs)
|
||||
# L
|
||||
outputs = image_segmenter(dataset[2]["file"])
|
||||
outputs = image_segmenter(dataset[2]["file"], threshold=0.0, mask_threshold=0, overlap_mask_area_threshold=0)
|
||||
m = len(outputs)
|
||||
self.assertEqual([{"score": ANY(float, type(None)), "label": ANY(str), "mask": ANY(Image.Image)}] * m, outputs)
|
||||
|
||||
@@ -126,7 +131,9 @@ class ImageSegmentationPipelineTests(unittest.TestCase, metaclass=PipelineTestCa
|
||||
"./tests/fixtures/tests_samples/COCO/000000039769.png",
|
||||
"./tests/fixtures/tests_samples/COCO/000000039769.png",
|
||||
]
|
||||
outputs = image_segmenter(batch, threshold=0.0, batch_size=batch_size)
|
||||
outputs = image_segmenter(
|
||||
batch, threshold=0.0, mask_threshold=0, overlap_mask_area_threshold=0, batch_size=batch_size
|
||||
)
|
||||
self.assertEqual(len(batch), len(outputs))
|
||||
self.assertEqual(len(outputs[0]), n)
|
||||
self.assertEqual(
|
||||
@@ -152,55 +159,29 @@ class ImageSegmentationPipelineTests(unittest.TestCase, metaclass=PipelineTestCa
|
||||
|
||||
model = AutoModelForImageSegmentation.from_pretrained(model_id)
|
||||
feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
|
||||
image_segmenter = ImageSegmentationPipeline(
|
||||
model=model,
|
||||
feature_extractor=feature_extractor,
|
||||
task="semantic",
|
||||
threshold=0.0,
|
||||
overlap_mask_area_threshold=0.0,
|
||||
)
|
||||
image_segmenter = ImageSegmentationPipeline(model=model, feature_extractor=feature_extractor)
|
||||
|
||||
outputs = image_segmenter(
|
||||
"http://images.cocodataset.org/val2017/000000039769.jpg",
|
||||
subtask="panoptic",
|
||||
threshold=0.0,
|
||||
mask_threshold=0.0,
|
||||
overlap_mask_area_threshold=0.0,
|
||||
)
|
||||
|
||||
# Shortening by hashing
|
||||
for o in outputs:
|
||||
o["mask"] = mask_to_test_readable(o["mask"])
|
||||
|
||||
# This is extremely brittle, and those values are made specific for the CI.
|
||||
self.assertEqual(
|
||||
nested_simplify(outputs, decimals=4),
|
||||
[
|
||||
{
|
||||
"label": "LABEL_88",
|
||||
"mask": {"hash": "7f0bf661a4", "shape": (480, 640), "white_pixels": 3},
|
||||
"score": None,
|
||||
},
|
||||
{
|
||||
"label": "LABEL_101",
|
||||
"mask": {"hash": "10ab738dc9", "shape": (480, 640), "white_pixels": 8948},
|
||||
"score": None,
|
||||
},
|
||||
{
|
||||
"score": 0.004,
|
||||
"label": "LABEL_215",
|
||||
"mask": {"hash": "b431e0946c", "shape": (480, 640), "white_pixels": 298249},
|
||||
"score": None,
|
||||
"mask": {"hash": "a01498ca7c", "shape": (480, 640), "white_pixels": 307200},
|
||||
},
|
||||
]
|
||||
# Temporary: Keeping around the old values as they might provide useful later
|
||||
# [
|
||||
# {
|
||||
# "score": 0.004,
|
||||
# "label": "LABEL_215",
|
||||
# "mask": {"hash": "34eecd16bb", "shape": (480, 640), "white_pixels": 0},
|
||||
# },
|
||||
# {
|
||||
# "score": 0.004,
|
||||
# "label": "LABEL_215",
|
||||
# "mask": {"hash": "34eecd16bb", "shape": (480, 640), "white_pixels": 0},
|
||||
# },
|
||||
# ],
|
||||
],
|
||||
)
|
||||
|
||||
outputs = image_segmenter(
|
||||
@@ -209,6 +190,8 @@ class ImageSegmentationPipelineTests(unittest.TestCase, metaclass=PipelineTestCa
|
||||
"http://images.cocodataset.org/val2017/000000039769.jpg",
|
||||
],
|
||||
threshold=0.0,
|
||||
mask_threshold=0.0,
|
||||
overlap_mask_area_threshold=0.0,
|
||||
)
|
||||
for output in outputs:
|
||||
for o in output:
|
||||
@@ -219,62 +202,18 @@ class ImageSegmentationPipelineTests(unittest.TestCase, metaclass=PipelineTestCa
|
||||
[
|
||||
[
|
||||
{
|
||||
"label": "LABEL_88",
|
||||
"mask": {"hash": "7f0bf661a4", "shape": (480, 640), "white_pixels": 3},
|
||||
"score": None,
|
||||
},
|
||||
{
|
||||
"label": "LABEL_101",
|
||||
"mask": {"hash": "10ab738dc9", "shape": (480, 640), "white_pixels": 8948},
|
||||
"score": None,
|
||||
},
|
||||
{
|
||||
"score": 0.004,
|
||||
"label": "LABEL_215",
|
||||
"mask": {"hash": "b431e0946c", "shape": (480, 640), "white_pixels": 298249},
|
||||
"score": None,
|
||||
"mask": {"hash": "a01498ca7c", "shape": (480, 640), "white_pixels": 307200},
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
"label": "LABEL_88",
|
||||
"mask": {"hash": "7f0bf661a4", "shape": (480, 640), "white_pixels": 3},
|
||||
"score": None,
|
||||
},
|
||||
{
|
||||
"label": "LABEL_101",
|
||||
"mask": {"hash": "10ab738dc9", "shape": (480, 640), "white_pixels": 8948},
|
||||
"score": None,
|
||||
},
|
||||
{
|
||||
"score": 0.004,
|
||||
"label": "LABEL_215",
|
||||
"mask": {"hash": "b431e0946c", "shape": (480, 640), "white_pixels": 298249},
|
||||
"score": None,
|
||||
"mask": {"hash": "a01498ca7c", "shape": (480, 640), "white_pixels": 307200},
|
||||
},
|
||||
]
|
||||
# [
|
||||
# {
|
||||
# "score": 0.004,
|
||||
# "label": "LABEL_215",
|
||||
# "mask": {"hash": "34eecd16bb", "shape": (480, 640), "white_pixels": 0},
|
||||
# },
|
||||
# {
|
||||
# "score": 0.004,
|
||||
# "label": "LABEL_215",
|
||||
# "mask": {"hash": "34eecd16bb", "shape": (480, 640), "white_pixels": 0},
|
||||
# },
|
||||
# ],
|
||||
# [
|
||||
# {
|
||||
# "score": 0.004,
|
||||
# "label": "LABEL_215",
|
||||
# "mask": {"hash": "34eecd16bb", "shape": (480, 640), "white_pixels": 0},
|
||||
# },
|
||||
# {
|
||||
# "score": 0.004,
|
||||
# "label": "LABEL_215",
|
||||
# "mask": {"hash": "34eecd16bb", "shape": (480, 640), "white_pixels": 0},
|
||||
# },
|
||||
# ],
|
||||
],
|
||||
],
|
||||
)
|
||||
|
||||
@@ -311,7 +250,7 @@ class ImageSegmentationPipelineTests(unittest.TestCase, metaclass=PipelineTestCa
|
||||
|
||||
outputs = image_segmenter(
|
||||
"http://images.cocodataset.org/val2017/000000039769.jpg",
|
||||
task="panoptic",
|
||||
subtask="panoptic",
|
||||
threshold=0,
|
||||
overlap_mask_area_threshold=0.0,
|
||||
)
|
||||
@@ -361,7 +300,7 @@ class ImageSegmentationPipelineTests(unittest.TestCase, metaclass=PipelineTestCa
|
||||
"http://images.cocodataset.org/val2017/000000039769.jpg",
|
||||
"http://images.cocodataset.org/val2017/000000039769.jpg",
|
||||
],
|
||||
task="panoptic",
|
||||
subtask="panoptic",
|
||||
threshold=0.0,
|
||||
overlap_mask_area_threshold=0.0,
|
||||
)
|
||||
@@ -448,7 +387,7 @@ class ImageSegmentationPipelineTests(unittest.TestCase, metaclass=PipelineTestCa
|
||||
image_segmenter = pipeline("image-segmentation", model=model_id)
|
||||
|
||||
outputs = image_segmenter(
|
||||
"http://images.cocodataset.org/val2017/000000039769.jpg", task="panoptic", threshold=0.999
|
||||
"http://images.cocodataset.org/val2017/000000039769.jpg", subtask="panoptic", threshold=0.999
|
||||
)
|
||||
# Shortening by hashing
|
||||
for o in outputs:
|
||||
@@ -471,7 +410,7 @@ class ImageSegmentationPipelineTests(unittest.TestCase, metaclass=PipelineTestCa
|
||||
)
|
||||
|
||||
outputs = image_segmenter(
|
||||
"http://images.cocodataset.org/val2017/000000039769.jpg", task="panoptic", threshold=0.5
|
||||
"http://images.cocodataset.org/val2017/000000039769.jpg", subtask="panoptic", threshold=0.5
|
||||
)
|
||||
|
||||
for o in outputs:
|
||||
@@ -521,7 +460,7 @@ class ImageSegmentationPipelineTests(unittest.TestCase, metaclass=PipelineTestCa
|
||||
|
||||
image = load_dataset("hf-internal-testing/fixtures_ade20k", split="test")
|
||||
file = image[0]["file"]
|
||||
outputs = image_segmenter(file, task="panoptic", threshold=threshold)
|
||||
outputs = image_segmenter(file, subtask="panoptic", threshold=threshold)
|
||||
|
||||
# Shortening by hashing
|
||||
for o in outputs:
|
||||
|
||||
Reference in New Issue
Block a user