





"""simple docstring"""



from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
       import tensorflow as tf

       from transformers import TFCvtForImageClassification, TFCvtModel
       from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor






class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						int									=					self.config_class(**self.inputs_dict  )
     self.parent.assertTrue(hasattr(__lowerCamelCase ,						'''embed_dim'''  )  )
     self.parent.assertTrue(hasattr(__lowerCamelCase ,						'''num_heads'''  )  )






class 		__snake_case :





 """simple docstring"""


 def __init__(   self ,						__lowerCamelCase ,						__lowerCamelCase=13 ,						__lowerCamelCase=64 ,						__lowerCamelCase=3 ,						__lowerCamelCase=[16, 48, 96] ,						__lowerCamelCase=[1, 3, 6] ,						__lowerCamelCase=[1, 2, 10] ,						__lowerCamelCase=[7, 3, 3] ,						__lowerCamelCase=[4, 2, 2] ,						__lowerCamelCase=[2, 1, 1] ,						__lowerCamelCase=[2, 2, 2] ,						__lowerCamelCase=[False, False, True] ,						__lowerCamelCase=[0.0, 0.0, 0.0] ,						__lowerCamelCase=0.0_2 ,						__lowerCamelCase=1e-1_2 ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						__lowerCamelCase=2 ,						):



     '''simple docstring'''




     __A   :						Optional[int]									=					parent
     __A   :						Tuple									=					batch_size
     __A   :						int									=					image_size
     __A   :						Optional[int]									=					patch_sizes
     __A   :						List[Any]									=					patch_stride
     __A   :						Any									=					patch_padding
     __A   :						Dict									=					is_training
     __A   :						Tuple									=					use_labels
     __A   :						Optional[Any]									=					num_labels
     __A   :						List[Any]									=					num_channels
     __A   :						Union[str, Any]									=					embed_dim
     __A   :						List[Any]									=					num_heads
     __A   :						Tuple									=					stride_kv
     __A   :						List[Any]									=					depth
     __A   :						List[Any]									=					cls_token
     __A   :						List[Any]									=					attention_drop_rate
     __A   :						Optional[int]									=					initializer_range
     __A   :						List[Any]									=					layer_norm_eps

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						str									=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

     __A   :						Optional[Any]									=					None
     if self.use_labels:
         # create a random int32 tensor of given shape
         __A   :						List[str]									=					ids_tensor([self.batch_size] ,						self.num_labels  )

     __A   :						Optional[Any]									=					self.get_config()
     return config, pixel_values, labels

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     return CvtConfig(
         image_size=self.image_size ,						num_labels=self.num_labels ,						num_channels=self.num_channels ,						embed_dim=self.embed_dim ,						num_heads=self.num_heads ,						patch_sizes=self.patch_sizes ,						patch_padding=self.patch_padding ,						patch_stride=self.patch_stride ,						stride_kv=self.stride_kv ,						depth=self.depth ,						cls_token=self.cls_token ,						attention_drop_rate=self.attention_drop_rate ,						initializer_range=self.initializer_range ,						)

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Optional[Any]									=					TFCvtModel(config=__lowerCamelCase  )
     __A   :						List[str]									=					model(__lowerCamelCase ,						training=__lowerCamelCase  )
     __A   :						Optional[Any]									=					(self.image_size, self.image_size)
     __A			,	__A   :						int									=					image_size[0], image_size[1]
     for i in range(len(self.depth  )  ):
         __A   :						Optional[Any]									=					floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1  )
         __A   :						List[str]									=					floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1  )
     self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, self.embed_dim[-1], height, width)  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						List[str]									=					self.num_labels
     __A   :						Dict									=					TFCvtForImageClassification(__lowerCamelCase  )
     __A   :						str									=					model(__lowerCamelCase ,						labels=__lowerCamelCase ,						training=__lowerCamelCase  )
     self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.num_labels)  )







 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[str]									=					self.prepare_config_and_inputs()
     __A			,	__A			,	__A   :						Optional[int]									=					config_and_inputs
     __A   :						List[Any]									=					{'''pixel_values''': pixel_values}
     return config, inputs_dict






@require_tf
class 		__snake_case (		SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__							,							unittest.TestCase					):





 """simple docstring"""


 _lowerCamelCase				=					(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
 _lowerCamelCase				=					(
     {"""feature-extraction""": TFCvtModel, """image-classification""": TFCvtForImageClassification}
     if is_tf_available()
     else {}
 )
 _lowerCamelCase				=					False
 _lowerCamelCase				=					False
 _lowerCamelCase				=					False
 _lowerCamelCase				=					False
 _lowerCamelCase				=					False

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						str									=					TFCvtModelTester(self  )
     __A   :						Union[str, Any]									=					TFCvtConfigTester(self ,						config_class=__lowerCamelCase ,						has_text_modality=__lowerCamelCase ,						hidden_size=37  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     self.config_tester.create_and_test_config_common_properties()
     self.config_tester.create_and_test_config_to_json_string()
     self.config_tester.create_and_test_config_to_json_file()
     self.config_tester.create_and_test_config_from_and_save_pretrained()
     self.config_tester.create_and_test_config_with_num_labels()
     self.config_tester.check_config_can_be_init_without_params()
     self.config_tester.check_config_arguments_init()

 @unittest.skip(reason='''Cvt does not output attentions'''  )
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     pass

 @unittest.skip(reason='''Cvt does not use inputs_embeds'''  )
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     pass

 @unittest.skip(reason='''Cvt does not support input and output embeddings'''  )
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     pass

 @unittest.skipIf(
     not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''  )  ) == 0 ,						reason='''TF does not support backprop for grouped convolutions on CPU.''' ,						)
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     super().test_dataset_conversion()

 @unittest.skipIf(
     not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''  )  ) == 0 ,						reason='''TF does not support backprop for grouped convolutions on CPU.''' ,						)
 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     super().test_keras_fit()

 @unittest.skip(reason='''Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8'''  )
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Dict									=					tf.keras.mixed_precision.Policy('''mixed_float16'''  )
     tf.keras.mixed_precision.set_global_policy(__lowerCamelCase  )
     super().test_keras_fit()
     tf.keras.mixed_precision.set_global_policy('''float32'''  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A			,	__A   :						int									=					self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         __A   :						Tuple									=					model_class(__lowerCamelCase  )
         __A   :						int									=					inspect.signature(model.call  )
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         __A   :						Any									=					[*signature.parameters.keys()]

         __A   :						int									=					['''pixel_values''']
         self.assertListEqual(arg_names[:1] ,						__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     def check_hidden_states_output(__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  ):
         __A   :						Dict									=					model_class(__lowerCamelCase  )

         __A   :						Any									=					model(**self._prepare_for_class(__lowerCamelCase ,						__lowerCamelCase  )  )
         __A   :						str									=					outputs.hidden_states

         __A   :						List[str]									=					len(self.model_tester.depth  )
         self.assertEqual(len(__lowerCamelCase  ) ,						__lowerCamelCase  )

         # verify the first hidden states (first block)
         self.assertListEqual(
             list(hidden_states[0].shape[-3:]  ) ,						[
                 self.model_tester.embed_dim[0],
                 self.model_tester.image_size // 4,
                 self.model_tester.image_size // 4,
             ] ,						)

     __A			,	__A   :						int									=					self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         __A   :						Optional[Any]									=					True
         check_hidden_states_output(__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  )

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         __A   :						Dict									=					True

         check_hidden_states_output(__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[str]									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Union[str, Any]									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase  )







 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __A   :						Optional[Any]									=					TFCvtModel.from_pretrained(__lowerCamelCase  )
         self.assertIsNotNone(__lowerCamelCase  )







def 		__lowercase					(       )       ->Optional[int]:



    '''simple docstring'''







    __A   :						Dict									=					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
    return image






@require_tf
@require_vision
class 		__snake_case (		unittest.TestCase					):





 """simple docstring"""


 @cached_property
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]  )







 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[str]									=					TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]  )

     __A   :						List[str]									=					self.default_image_processor
     __A   :						Union[str, Any]									=					prepare_img()
     __A   :						List[Any]									=					image_processor(images=__lowerCamelCase ,						return_tensors='''tf'''  )

     # forward pass
     __A   :						Any									=					model(**__lowerCamelCase  )

     # verify the logits
     __A   :						Dict									=					tf.TensorShape((1, 1000)  )
     self.assertEqual(outputs.logits.shape ,						__lowerCamelCase  )

     __A   :						Dict									=					tf.constant([0.9_2_8_5, 0.9_0_1_5, -0.3_1_5_0]  )
     self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() ,						__lowerCamelCase ,						atol=1e-4  )  )











"""simple docstring"""



from collections import UserDict
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
       from PIL import Image

       from ..image_utils import load_image

if is_torch_available():
       from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
       from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
       from ..tf_utils import stable_softmax

a_			=      logging.get_logger(__name__)






@add_end_docstrings(SCREAMING_SNAKE_CASE__					)
class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 def __init__(   self ,						**__lowerCamelCase  ):



     '''simple docstring'''




     super().__init__(**__lowerCamelCase  )

     requires_backends(self ,						'''vision'''  )
     self.check_model_type(
         TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
         if self.framework == '''tf'''
         else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING  )

 def __call__(   self ,						__lowerCamelCase ,						**__lowerCamelCase  ):



     '''simple docstring'''




     return super().__call__(__lowerCamelCase ,						**__lowerCamelCase  )

 def  UpperCamelCase__(   self ,						**__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Union[str, Any]									=					{}
     if "candidate_labels" in kwargs:
         __A   :						Tuple									=					kwargs['''candidate_labels''']
     if "hypothesis_template" in kwargs:
         __A   :						List[str]									=					kwargs['''hypothesis_template''']

     return preprocess_params, {}, {}

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase=None ,						__lowerCamelCase="This is a photo of {}."  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					load_image(__lowerCamelCase  )
     __A   :						Optional[int]									=					self.image_processor(images=[image] ,						return_tensors=self.framework  )
     __A   :						int									=					candidate_labels
     __A   :						int									=					[hypothesis_template.format(__lowerCamelCase  ) for x in candidate_labels]
     __A   :						Dict									=					self.tokenizer(__lowerCamelCase ,						return_tensors=self.framework ,						padding=__lowerCamelCase  )
     __A   :						int									=					[text_inputs]
     return inputs

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					model_inputs.pop('''candidate_labels'''  )
     __A   :						str									=					model_inputs.pop('''text_inputs'''  )
     if isinstance(text_inputs[0] ,						__lowerCamelCase  ):
         __A   :						Union[str, Any]									=					text_inputs[0]
     else:
         # Batching case.
         __A   :						str									=					text_inputs[0][0]

     __A   :						List[str]									=					self.model(**__lowerCamelCase ,						**__lowerCamelCase  )

     __A   :						Dict									=					{
         '''candidate_labels''': candidate_labels,
         '''logits''': outputs.logits_per_image,
     }
     return model_outputs







 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					model_outputs.pop('''candidate_labels'''  )
     __A   :						int									=					model_outputs['''logits'''][0]
     if self.framework == "pt":
         __A   :						Union[str, Any]									=					logits.softmax(dim=-1  ).squeeze(-1  )
         __A   :						Dict									=					probs.tolist()
         if not isinstance(__lowerCamelCase ,						__lowerCamelCase  ):
             __A   :						List[Any]									=					[scores]
     elif self.framework == "tf":
         __A   :						List[Any]									=					stable_softmax(__lowerCamelCase ,						axis=-1  )
         __A   :						str									=					probs.numpy().tolist()
     else:
         raise ValueError(F"""Unsupported framework: {self.framework}"""  )

     __A   :						str									=					[
         {'''score''': score, '''label''': candidate_label}
         for score, candidate_label in sorted(zip(__lowerCamelCase ,						__lowerCamelCase  ) ,						key=lambda __lowerCamelCase  : -x[0]  )
     ]
     return result




