


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 		_snake_case  :




    @staticmethod
    def 	SCREAMING_SNAKE_CASE__    (		*_lowerCamelCase ,  **_lowerCamelCase   ):
          pass







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




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

       # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
       # python and torch versions.
       self.assertIn(
           nested_simplify(lowerCamelCase_   ) ,  [
               [{'''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'''}],
           ] ,  )

       a						:Any										=	image_classifier([image] * 5 ,  candidate_labels=['''A''', '''B''', '''C'''] ,  batch_size=2   )
       self.assertEqual(
           nested_simplify(lowerCamelCase_   ) ,  [
               [
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
               ],
           ] ,  )




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

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

       a						:List[Any]										=	image_classifier([image] * 5 ,  candidate_labels=['''A''', '''B''', '''C'''] ,  batch_size=2   )
       self.assertEqual(
           nested_simplify(lowerCamelCase_   ) ,  [
               [
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
                   {'''score''': 0.333, '''label''': ANY(lowerCamelCase_   )},
               ],
           ] ,  )




 @slow
 @require_torch
 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Any										=	pipeline(
           task='''zero-shot-image-classification''' ,  model='''openai/clip-vit-base-patch32''' ,  )
       # This is an image of 2 cats with remotes and no planes
       a						:Optional[int]										=	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
       a						:str										=	image_classifier(lowerCamelCase_ ,  candidate_labels=['''cat''', '''plane''', '''remote''']   )

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

       a						:Union[str, Any]										=	image_classifier([image] * 5 ,  candidate_labels=['''cat''', '''plane''', '''remote'''] ,  batch_size=2   )
       self.assertEqual(
           nested_simplify(lowerCamelCase_   ) ,  [
               [
                   {'''score''': 0.511, '''label''': '''remote'''},
                   {'''score''': 0.485, '''label''': '''cat'''},
                   {'''score''': 0.004, '''label''': '''plane'''},
               ],
           ]
           * 5 ,  )




 @slow
 @require_tf
 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       a						:Dict										=	pipeline(
           task='''zero-shot-image-classification''' ,  model='''openai/clip-vit-base-patch32''' ,  framework='''tf'''   )
       # This is an image of 2 cats with remotes and no planes
       a						:Optional[Any]										=	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
       a						:str										=	image_classifier(lowerCamelCase_ ,  candidate_labels=['''cat''', '''plane''', '''remote''']   )
       self.assertEqual(
           nested_simplify(lowerCamelCase_   ) ,  [
               {'''score''': 0.511, '''label''': '''remote'''},
               {'''score''': 0.485, '''label''': '''cat'''},
               {'''score''': 0.004, '''label''': '''plane'''},
           ] ,  )

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









from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


snake_case    :   Optional[Any]	    =					logging.get_logger(__name__)

snake_case    :   Dict	    =					{
    '''facebook/data2vec-vision-base-ft''': (
        '''https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json'''
    ),
}







class 		_snake_case  (				_snake_case							):
 SCREAMING_SNAKE_CASE__				=	'data2vec-vision'




 def __init__(		self ,  _lowerCamelCase=768 ,  _lowerCamelCase=12 ,  _lowerCamelCase=12 ,  _lowerCamelCase=3072 ,  _lowerCamelCase="gelu" ,  _lowerCamelCase=0.0 ,  _lowerCamelCase=0.0 ,  _lowerCamelCase=0.02 ,  _lowerCamelCase=1e-12 ,  _lowerCamelCase=224 ,  _lowerCamelCase=16 ,  _lowerCamelCase=3 ,  _lowerCamelCase=False ,  _lowerCamelCase=False ,  _lowerCamelCase=False ,  _lowerCamelCase=False ,  _lowerCamelCase=0.1 ,  _lowerCamelCase=0.1 ,  _lowerCamelCase=True ,  _lowerCamelCase=[3, 5, 7, 11] ,  _lowerCamelCase=[1, 2, 3, 6] ,  _lowerCamelCase=True ,  _lowerCamelCase=0.4 ,  _lowerCamelCase=256 ,  _lowerCamelCase=1 ,  _lowerCamelCase=False ,  _lowerCamelCase=255 ,  **_lowerCamelCase ,  ):
       super().__init__(**_lowerCamelCase   )

       a						:Tuple										=	hidden_size
       a						:Any										=	num_hidden_layers
       a						:Optional[int]										=	num_attention_heads
       a						:Dict										=	intermediate_size
       a						:List[Any]										=	hidden_act
       a						:List[str]										=	hidden_dropout_prob
       a						:Union[str, Any]										=	attention_probs_dropout_prob
       a						:Any										=	initializer_range
       a						:Any										=	layer_norm_eps

       a						:Union[str, Any]										=	image_size
       a						:int										=	patch_size
       a						:Optional[int]										=	num_channels
       a						:Union[str, Any]										=	use_mask_token
       a						:Optional[Any]										=	use_absolute_position_embeddings
       a						:Tuple										=	use_relative_position_bias
       a						:List[Any]										=	use_shared_relative_position_bias
       a						:Dict										=	layer_scale_init_value
       a						:Optional[int]										=	drop_path_rate
       a						:List[str]										=	use_mean_pooling
       # decode head attributes (semantic segmentation)
       a						:str										=	out_indices
       a						:Tuple										=	pool_scales
       # auxiliary head attributes (semantic segmentation)
       a						:List[Any]										=	use_auxiliary_head
       a						:List[Any]										=	auxiliary_loss_weight
       a						:Optional[int]										=	auxiliary_channels
       a						:List[str]										=	auxiliary_num_convs
       a						:str										=	auxiliary_concat_input
       a						:Union[str, Any]										=	semantic_loss_ignore_index







class 		_snake_case  (				_snake_case							):
 SCREAMING_SNAKE_CASE__				=	version.parse('1.11'							)




 @property
 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       return OrderedDict(
           [
               ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
           ]   )




 @property
 def 	SCREAMING_SNAKE_CASE__    (		self   ):
       return 1e-4





