




'''simple docstring'''



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





             '''simple docstring'''






             @staticmethod
             def        UpperCamelCase_							(      *A,							**A  ):







                   '''simple docstring'''







                   pass






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





       '''simple docstring'''






       @require_torch
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Any    					=						pipeline(
                 model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification',							)
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						image_classifier(A,							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(A  ),							[
                     [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'b'}, {'score': 0.3_33, 'label': 'c'}],
                     [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'c'}, {'score': 0.3_33, 'label': 'b'}],
                 ],							)

             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						image_classifier([image] * 5,							candidate_labels=['A', 'B', 'C'],							batch_size=2  )
             self.assertEqual(
                 nested_simplify(A  ),							[
                     [
                         {'score': 0.3_33, 'label': ANY(A  )},
                         {'score': 0.3_33, 'label': ANY(A  )},
                         {'score': 0.3_33, 'label': ANY(A  )},
                     ],
                     [
                         {'score': 0.3_33, 'label': ANY(A  )},
                         {'score': 0.3_33, 'label': ANY(A  )},
                         {'score': 0.3_33, 'label': ANY(A  )},
                     ],
                     [
                         {'score': 0.3_33, 'label': ANY(A  )},
                         {'score': 0.3_33, 'label': ANY(A  )},
                         {'score': 0.3_33, 'label': ANY(A  )},
                     ],
                     [
                         {'score': 0.3_33, 'label': ANY(A  )},
                         {'score': 0.3_33, 'label': ANY(A  )},
                         {'score': 0.3_33, 'label': ANY(A  )},
                     ],
                     [
                         {'score': 0.3_33, 'label': ANY(A  )},
                         {'score': 0.3_33, 'label': ANY(A  )},
                         {'score': 0.3_33, 'label': ANY(A  )},
                     ],
                 ],							)







       @require_tf
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      str    					=						pipeline(
                 model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification',							framework='tf'  )
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						image_classifier(A,							candidate_labels=['a', 'b', 'c']  )

             self.assertEqual(
                 nested_simplify(A  ),							[{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'b'}, {'score': 0.3_33, 'label': 'c'}],							)

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







       @slow
       @require_torch
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Tuple    					=						pipeline(
                 task='zero-shot-image-classification',							model='openai/clip-vit-base-patch32',							)
             # This is an image of 2 cats with remotes and no planes
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
             SCREAMING_SNAKE_CASE   :      List[str]    					=						image_classifier(A,							candidate_labels=['cat', 'plane', 'remote']  )

             self.assertEqual(
                 nested_simplify(A  ),							[
                     {'score': 0.5_11, 'label': 'remote'},
                     {'score': 0.4_85, 'label': 'cat'},
                     {'score': 0.0_04, 'label': 'plane'},
                 ],							)

             SCREAMING_SNAKE_CASE   :      List[Any]    					=						image_classifier([image] * 5,							candidate_labels=['cat', 'plane', 'remote'],							batch_size=2  )
             self.assertEqual(
                 nested_simplify(A  ),							[
                     [
                         {'score': 0.5_11, 'label': 'remote'},
                         {'score': 0.4_85, 'label': 'cat'},
                         {'score': 0.0_04, 'label': 'plane'},
                     ],
                 ]
                 * 5,							)







       @slow
       @require_tf
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Tuple    					=						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
             SCREAMING_SNAKE_CASE   :      Tuple    					=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						image_classifier(A,							candidate_labels=['cat', 'plane', 'remote']  )
             self.assertEqual(
                 nested_simplify(A  ),							[
                     {'score': 0.5_11, 'label': 'remote'},
                     {'score': 0.4_85, 'label': 'cat'},
                     {'score': 0.0_04, 'label': 'plane'},
                 ],							)

             SCREAMING_SNAKE_CASE   :      Tuple    					=						image_classifier([image] * 5,							candidate_labels=['cat', 'plane', 'remote'],							batch_size=2  )
             self.assertEqual(
                 nested_simplify(A  ),							[
                     [
                         {'score': 0.5_11, 'label': 'remote'},
                         {'score': 0.4_85, 'label': 'cat'},
                         {'score': 0.0_04, 'label': 'plane'},
                     ],
                 ]
                 * 5,							)





"""simple docstring"""





from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__UpperCamelCase    : Tuple				 =						logging.get_logger(__name__)

__UpperCamelCase    : Optional[int]				 =						{
    '''google/bit-50''': '''https://huggingface.co/google/bit-50/resolve/main/config.json''',
}







class 							SCREAMING_SNAKE_CASE   (						a_       ,				a_			):






					"""simple docstring"""



					lowercase__												=			"bit"
					lowercase__												=			["preactivation", "bottleneck"]
					lowercase__												=			["SAME", "VALID"]







					def __init__(			self     :							int  ,lowercase_     :							Optional[int]=3  ,lowercase_     :							int=6_4  ,lowercase_     :							Optional[int]=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8]  ,lowercase_     :							Union[str, Any]=[3, 4, 6, 3]  ,lowercase_     :							List[Any]="preactivation"  ,lowercase_     :							Dict="relu"  ,lowercase_     :							Dict=None  ,lowercase_     :							Optional[Any]=3_2  ,lowercase_     :							Any=0.0  ,lowercase_     :							Optional[Any]=False  ,lowercase_     :							List[Any]=3_2  ,lowercase_     :							Optional[Any]=1  ,lowercase_     :							Optional[int]=None  ,lowercase_     :							int=None  ,**lowercase_     :							List[str]  ,):
						super().__init__(**lowercase_						)
						if layer_type not in self.layer_types:
							raise ValueError(F'layer_type={layer_type} is not one of {",".join(self.layer_types						)}'						)
						if global_padding is not None:
							if global_padding.upper() in self.supported_padding:
								lowerCAmelCase__      :		Optional[Any]    							=      global_padding.upper()
							else:
								raise ValueError(F'Padding strategy {global_padding} not supported'						)
						lowerCAmelCase__      :		int    							=      num_channels
						lowerCAmelCase__      :		int    							=      embedding_size
						lowerCAmelCase__      :		Any    							=      hidden_sizes
						lowerCAmelCase__      :		int    							=      depths
						lowerCAmelCase__      :		Union[str, Any]    							=      layer_type
						lowerCAmelCase__      :		Optional[int]    							=      hidden_act
						lowerCAmelCase__      :		int    							=      global_padding
						lowerCAmelCase__      :		List[Any]    							=      num_groups
						lowerCAmelCase__      :		List[Any]    							=      drop_path_rate
						lowerCAmelCase__      :		Union[str, Any]    							=      embedding_dynamic_padding
						lowerCAmelCase__      :		int    							=      output_stride
						lowerCAmelCase__      :		Optional[Any]    							=      width_factor

						lowerCAmelCase__      :		List[str]    							=      ['''stem'''] + [F'stage{idx}' for idx in range(1  ,len(lowercase_						) + 1						)]
						lowerCAmelCase__							,lowerCAmelCase__      :		Any    							=      get_aligned_output_features_output_indices(
						    out_features=lowercase_  ,out_indices=lowercase_  ,stage_names=self.stage_names						)




