def get_test_pipeline(self, model, tokenizer, feature_extractor): image_segmenter = ImageSegmentationPipeline( model=model, feature_extractor=feature_extractor) return image_segmenter, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ]
def run_pipeline_test(self, model, tokenizer, feature_extractor): image_segmenter = ImageSegmentationPipeline(model=model, feature_extractor=feature_extractor) outputs = image_segmenter("./tests/fixtures/tests_samples/COCO/000000039769.png", threshold=0.0) self.assertEqual(outputs, [{"score": ANY(float), "label": ANY(str), "mask": ANY(str)}] * 12) import datasets dataset = datasets.load_dataset("Narsil/image_dummy", "image", split="test") batch = [ Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"), "http://images.cocodataset.org/val2017/000000039769.jpg", # RGBA dataset[0]["file"], # LA dataset[1]["file"], # L dataset[2]["file"], ] outputs = image_segmenter(batch, threshold=0.0) self.assertEqual(len(batch), len(outputs)) self.assertEqual( outputs, [ [{"score": ANY(float), "label": ANY(str), "mask": ANY(str)}] * 12, [{"score": ANY(float), "label": ANY(str), "mask": ANY(str)}] * 12, [{"score": ANY(float), "label": ANY(str), "mask": ANY(str)}] * 12, [{"score": ANY(float), "label": ANY(str), "mask": ANY(str)}] * 12, [{"score": ANY(float), "label": ANY(str), "mask": ANY(str)}] * 12, ], )
def test_small_model_pt(self): model_id = "hf-internal-testing/tiny-detr-mobilenetsv3-panoptic" model = AutoModelForImageSegmentation.from_pretrained(model_id) feature_extractor = AutoFeatureExtractor.from_pretrained(model_id) image_segmenter = ImageSegmentationPipeline( model=model, feature_extractor=feature_extractor) outputs = image_segmenter( "http://images.cocodataset.org/val2017/000000039769.jpg", threshold=0.0) for o in outputs: # shortening by hashing o["mask"] = hashimage(o["mask"]) self.assertEqual( nested_simplify(outputs, decimals=4), [ { "score": 0.004, "label": "LABEL_0", "mask": "34eecd16bbfb0f476083ef947d81bf66", }, { "score": 0.004, "label": "LABEL_0", "mask": "34eecd16bbfb0f476083ef947d81bf66", }, ], ) outputs = image_segmenter( [ "http://images.cocodataset.org/val2017/000000039769.jpg", "http://images.cocodataset.org/val2017/000000039769.jpg", ], threshold=0.0, ) for output in outputs: for o in output: o["mask"] = hashimage(o["mask"]) self.assertEqual( nested_simplify(outputs, decimals=4), [ [ { "score": 0.004, "label": "LABEL_0", "mask": "34eecd16bbfb0f476083ef947d81bf66", }, { "score": 0.004, "label": "LABEL_0", "mask": "34eecd16bbfb0f476083ef947d81bf66", }, ], [ { "score": 0.004, "label": "LABEL_0", "mask": "34eecd16bbfb0f476083ef947d81bf66", }, { "score": 0.004, "label": "LABEL_0", "mask": "34eecd16bbfb0f476083ef947d81bf66", }, ], ], )
def test_small_model_pt(self): model_id = "mishig/tiny-detr-mobilenetsv3-panoptic" model = AutoModelForImageSegmentation.from_pretrained(model_id) feature_extractor = AutoFeatureExtractor.from_pretrained(model_id) image_segmenter = ImageSegmentationPipeline( model=model, feature_extractor=feature_extractor) outputs = image_segmenter( "http://images.cocodataset.org/val2017/000000039769.jpg", threshold=0.0) for o in outputs: # shortening by hashing o["mask"] = hashlib.sha1(o["mask"].encode("UTF-8")).hexdigest() self.assertEqual( nested_simplify(outputs, decimals=4), [ { "score": 0.004, "label": "LABEL_0", "mask": "4276f7db4ca2983b2666f7e0c102d8186aed20be", }, { "score": 0.004, "label": "LABEL_0", "mask": "4276f7db4ca2983b2666f7e0c102d8186aed20be", }, ], ) outputs = image_segmenter( [ "http://images.cocodataset.org/val2017/000000039769.jpg", "http://images.cocodataset.org/val2017/000000039769.jpg", ], threshold=0.0, ) for output in outputs: for o in output: o["mask"] = hashlib.sha1(o["mask"].encode("UTF-8")).hexdigest() self.assertEqual( nested_simplify(outputs, decimals=4), [ [ { "score": 0.004, "label": "LABEL_0", "mask": "4276f7db4ca2983b2666f7e0c102d8186aed20be", }, { "score": 0.004, "label": "LABEL_0", "mask": "4276f7db4ca2983b2666f7e0c102d8186aed20be", }, ], [ { "score": 0.004, "label": "LABEL_0", "mask": "4276f7db4ca2983b2666f7e0c102d8186aed20be", }, { "score": 0.004, "label": "LABEL_0", "mask": "4276f7db4ca2983b2666f7e0c102d8186aed20be", }, ], ], )