2021 the huggingface team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license deactivating auto tests since we don t have a good modelforxx mapping and only clip would be there for now modelmapping clipconfig clipmodel def gettestpipelineself model tokenizer processor if tokenizer is none side effect of no fast tokenizer class for these model so skipping but the slow tokenizer test should still run as they re quite small self skiptestno tokenizer available return return none none imageclassifier zeroshotimageclassificationpipeline modelmodel tokenizertokenizer featureextractorprocessor test with a raw waveform image image open testsfixturestestssamplescoco000000039769 png image2 image open testsfixturestestssamplescoco000000039769 png return imageclassifier image image2 def runpipelinetestself pipe examples image image open testsfixturestestssamplescoco000000039769 png outputs pipeimage candidatelabelsa b self assertequaloutputs text anystr batching outputs pipeimage 3 batchsize2 candidatelabelsa b the floating scores are so close we enter floating error approximation and the order is not guaranteed across python and torch versions pipeline outputs are supposed to be deterministic and so we could in theory have real values a b c instead of anystr however it seems that in this particular case the floating scores are so close we enter floating error approximation and the order is not guaranteed anymore with batching pipeline outputs are supposed to be deterministic and so we could in theory have real values a b c instead of anystr however it seems that in this particular case the floating scores are so close we enter floating error approximation and the order is not guaranteed anymore with batching this is an image of 2 cats with remotes and no planes this is an image of 2 cats with remotes and no planes 2021 the huggingface team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license deactivating auto tests since we don t have a good model_for_xx mapping and only clip would be there for now model_mapping clipconfig clipmodel def get_test_pipeline self model tokenizer processor if tokenizer is none side effect of no fast tokenizer class for these model so skipping but the slow tokenizer test should still run as they re quite small self skiptest no tokenizer available return return none none image_classifier zeroshotimageclassificationpipeline model model tokenizer tokenizer feature_extractor processor test with a raw waveform image image open tests fixtures tests_samples coco 000000039769 png image2 image open tests fixtures tests_samples coco 000000039769 png return image_classifier image image2 def run_pipeline_test self pipe examples image image open tests fixtures tests_samples coco 000000039769 png outputs pipe image candidate_labels a b self assertequal outputs text any str batching outputs pipe image 3 batch_size 2 candidate_labels a b the floating scores are so close we enter floating error approximation and the order is not guaranteed across python and torch versions pipeline outputs are supposed to be deterministic and so we could in theory have real values a b c instead of any str however it seems that in this particular case the floating scores are so close we enter floating error approximation and the order is not guaranteed anymore with batching pipeline outputs are supposed to be deterministic and so we could in theory have real values a b c instead of any str however it seems that in this particular case the floating scores are so close we enter floating error approximation and the order is not guaranteed anymore with batching this is an image of 2 cats with remotes and no planes this is an image of 2 cats with remotes and no planes
import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
    from PIL import Image
else:

    class Image:
        @staticmethod
        def open(*args, **kwargs):
            pass


@is_pipeline_test
@require_vision
class ZeroShotImageClassificationPipelineTests(unittest.TestCase):
    
    
    

    
    
    
    
    
    
    

    
    
    

    
    
    
    

    
    
    
    

    
    

    @require_torch
    def test_small_model_pt(self):
        image_classifier = pipeline(
            model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification",
        )
        image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
        output = image_classifier(image, candidate_labels=["a", "b", "c"])

        
        
        self.assertIn(
            nested_simplify(output),
            [
                [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}],
                [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}],
                [{"score": 0.333, "label": "b"}, {"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}],
            ],
        )

        output = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2)
        self.assertEqual(
            nested_simplify(output),
            
            
            
            
            
            
            [
                [
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                ],
                [
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                ],
                [
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                ],
                [
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                ],
                [
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                ],
            ],
        )

    @require_tf
    def test_small_model_tf(self):
        image_classifier = pipeline(
            model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification", framework="tf"
        )
        image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
        output = image_classifier(image, candidate_labels=["a", "b", "c"])

        self.assertEqual(
            nested_simplify(output),
            [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}],
        )

        output = image_classifier([image] * 5, candidate_labels=["A", "B", "C"], batch_size=2)
        self.assertEqual(
            nested_simplify(output),
            
            
            
            
            
            
            [
                [
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                ],
                [
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                ],
                [
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                ],
                [
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                ],
                [
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                    {"score": 0.333, "label": ANY(str)},
                ],
            ],
        )

    @slow
    @require_torch
    def test_large_model_pt(self):
        image_classifier = pipeline(
            task="zero-shot-image-classification",
            model="openai/clip-vit-base-patch32",
        )
        
        image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
        output = image_classifier(image, candidate_labels=["cat", "plane", "remote"])

        self.assertEqual(
            nested_simplify(output),
            [
                {"score": 0.511, "label": "remote"},
                {"score": 0.485, "label": "cat"},
                {"score": 0.004, "label": "plane"},
            ],
        )

        output = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2)
        self.assertEqual(
            nested_simplify(output),
            [
                [
                    {"score": 0.511, "label": "remote"},
                    {"score": 0.485, "label": "cat"},
                    {"score": 0.004, "label": "plane"},
                ],
            ]
            * 5,
        )

    @slow
    @require_tf
    def test_large_model_tf(self):
        image_classifier = pipeline(
            task="zero-shot-image-classification", model="openai/clip-vit-base-patch32", framework="tf"
        )
        
        image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
        output = image_classifier(image, candidate_labels=["cat", "plane", "remote"])
        self.assertEqual(
            nested_simplify(output),
            [
                {"score": 0.511, "label": "remote"},
                {"score": 0.485, "label": "cat"},
                {"score": 0.004, "label": "plane"},
            ],
        )

        output = image_classifier([image] * 5, candidate_labels=["cat", "plane", "remote"], batch_size=2)
        self.assertEqual(
            nested_simplify(output),
            [
                [
                    {"score": 0.511, "label": "remote"},
                    {"score": 0.485, "label": "cat"},
                    {"score": 0.004, "label": "plane"},
                ],
            ]
            * 5,
        )