




import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import CvtForImageClassification, CvtModel
     from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import AutoImageProcessor




class       __lowerCAmelCase				(							lowerCAmelCase):






       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         lowercase			:List[Any]							=  self.config_class(**self.inputs_dict )
         self.parent.assertTrue(hasattr(_lowerCAmelCase				,    "embed_dim" ) )
         self.parent.assertTrue(hasattr(_lowerCAmelCase				,    "num_heads" ) )







class       __lowerCAmelCase				:






       def __init__(      self:    str				,    _lowerCAmelCase:    Union[str, Any]				,    _lowerCAmelCase:    int=13				,    _lowerCAmelCase:    Union[str, Any]=64				,    _lowerCAmelCase:    int=3				,    _lowerCAmelCase:    Any=[16, 48, 96]				,    _lowerCAmelCase:    Tuple=[1, 3, 6]				,    _lowerCAmelCase:    int=[1, 2, 10]				,    _lowerCAmelCase:    Any=[7, 3, 3]				,    _lowerCAmelCase:    int=[4, 2, 2]				,    _lowerCAmelCase:    Tuple=[2, 1, 1]				,    _lowerCAmelCase:    Optional[Any]=[2, 2, 2]				,    _lowerCAmelCase:    Optional[int]=[False, False, True]				,    _lowerCAmelCase:    Tuple=[0.0, 0.0, 0.0]				,    _lowerCAmelCase:    str=0.02				,    _lowerCAmelCase:    Union[str, Any]=1e-1_2				,    _lowerCAmelCase:    Optional[Any]=True				,    _lowerCAmelCase:    Optional[int]=True				,    _lowerCAmelCase:    str=2				,    ):
         lowercase			:Union[str, Any]							=  parent
         lowercase			:Any							=  batch_size
         lowercase			:Any							=  image_size
         lowercase			:Optional[int]							=  patch_sizes
         lowercase			:Dict							=  patch_stride
         lowercase			:List[Any]							=  patch_padding
         lowercase			:Dict							=  is_training
         lowercase			:Any							=  use_labels
         lowercase			:int							=  num_labels
         lowercase			:str							=  num_channels
         lowercase			:List[Any]							=  embed_dim
         lowercase			:Tuple							=  num_heads
         lowercase			:Tuple							=  stride_kv
         lowercase			:Any							=  depth
         lowercase			:List[Any]							=  cls_token
         lowercase			:Optional[Any]							=  attention_drop_rate
         lowercase			:Dict							=  initializer_range
         lowercase			:List[Any]							=  layer_norm_eps






       def     SCREAMING_SNAKE_CASE			(      self:    List[Any] ):
         lowercase			:Union[str, Any]							=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

         lowercase			:int							=  None
         if self.use_labels:
           lowercase			:List[Any]							=  ids_tensor([self.batch_size]				,    self.num_labels )

         lowercase			:Optional[int]							=  self.get_config()
         return config, pixel_values, labels






       def     SCREAMING_SNAKE_CASE			(      self:    Any ):
         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     SCREAMING_SNAKE_CASE			(      self:    str				,    _lowerCAmelCase:    Any				,    _lowerCAmelCase:    Dict				,    _lowerCAmelCase:    List[str] ):
         lowercase			:Tuple							=  CvtModel(config=_lowerCAmelCase )
         model.to(_lowerCAmelCase )
         model.eval()
         lowercase			:Any							=  model(_lowerCAmelCase )
         lowercase			:Dict							=  (self.image_size, self.image_size)
         lowercase		,			lowercase			:List[Any]							=  image_size[0], image_size[1]
         for i in range(len(self.depth ) ):
           lowercase			:str							=  floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 )
           lowercase			: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     SCREAMING_SNAKE_CASE			(      self:    str				,    _lowerCAmelCase:    Optional[Any]				,    _lowerCAmelCase:    Tuple				,    _lowerCAmelCase:    str ):
         lowercase			:Optional[int]							=  self.num_labels
         lowercase			:Dict							=  CvtForImageClassification(_lowerCAmelCase )
         model.to(_lowerCAmelCase )
         model.eval()
         lowercase			:str							=  model(_lowerCAmelCase				,    labels=_lowerCAmelCase )
         self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels) )






       def     SCREAMING_SNAKE_CASE			(      self:    Union[str, Any] ):
         lowercase			:Dict							=  self.prepare_config_and_inputs()
         lowercase		,			lowercase		,			lowercase			:List[Any]							=  config_and_inputs
         lowercase			:Optional[Any]							=  {"pixel_values": pixel_values}
         return config, inputs_dict







@require_torch
class       __lowerCAmelCase				(							lowerCAmelCase					,       lowerCAmelCase					,       unittest.TestCase):
       _a           =  (CvtModel, CvtForImageClassification) if is_torch_available() else ()
       _a           =  (
           {'''feature-extraction''': CvtModel, '''image-classification''': CvtForImageClassification}
           if is_torch_available()
           else {}
       )

       _a           =  False
       _a           =  False
       _a           =  False
       _a           =  False
       _a           =  False






       def     SCREAMING_SNAKE_CASE			(      self:    Optional[Any] ):
         lowercase			:Dict							=  CvtModelTester(self )
         lowercase			:str							=  ConfigTester(self				,    config_class=_lowerCAmelCase				,    has_text_modality=_lowerCAmelCase				,    hidden_size=37 )






       def     SCREAMING_SNAKE_CASE			(      self:    List[str] ):
         self.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()






       def     SCREAMING_SNAKE_CASE			(      self:    Optional[Any] ):
         return






       @unittest.skip(reason="Cvt does not output attentions" )
       def     SCREAMING_SNAKE_CASE			(      self:    Union[str, Any] ):
         pass






       @unittest.skip(reason="Cvt does not use inputs_embeds" )
       def     SCREAMING_SNAKE_CASE			(      self:    Optional[int] ):
         pass






       @unittest.skip(reason="Cvt does not support input and output embeddings" )
       def     SCREAMING_SNAKE_CASE			(      self:    int ):
         pass






       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         lowercase		,			lowercase			:Optional[int]							=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           lowercase			:Optional[Any]							=  model_class(_lowerCAmelCase )
           lowercase			:Dict							=  inspect.signature(model.forward )
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           lowercase			:Tuple							=  [*signature.parameters.keys()]

           lowercase			:List[str]							=  ["pixel_values"]
           self.assertListEqual(arg_names[:1]				,    _lowerCAmelCase )






       def     SCREAMING_SNAKE_CASE			(      self:    Dict ):
         lowercase			:Tuple							=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCAmelCase )






       def     SCREAMING_SNAKE_CASE			(      self:    Dict ):
         def check_hidden_states_output(_lowerCAmelCase:    str				,    _lowerCAmelCase:    Union[str, Any]				,    _lowerCAmelCase:    int ):
           lowercase			:Any							=  model_class(_lowerCAmelCase )
           model.to(_lowerCAmelCase )
           model.eval()

           with torch.no_grad():
             lowercase			:Tuple							=  model(**self._prepare_for_class(_lowerCAmelCase				,    _lowerCAmelCase ) )

           lowercase			:Dict							=  outputs.hidden_states

           lowercase			:int							=  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,
               ]				,    )

         lowercase		,			lowercase			:Dict							=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           lowercase			:List[str]							=  True
           check_hidden_states_output(_lowerCAmelCase				,    _lowerCAmelCase				,    _lowerCAmelCase )

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

           check_hidden_states_output(_lowerCAmelCase				,    _lowerCAmelCase				,    _lowerCAmelCase )






       def     SCREAMING_SNAKE_CASE			(      self:    List[str] ):
         lowercase			:Union[str, Any]							=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase )






       @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." )
       def     SCREAMING_SNAKE_CASE			(      self:    List[str] ):
         pass






       @slow
       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase			:int							=  CvtModel.from_pretrained(_lowerCAmelCase )
           self.assertIsNotNone(_lowerCAmelCase )







def 					UpperCAmelCase__     (   ):
  lowercase			:Dict							=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
  return image







@require_torch
@require_vision
class       __lowerCAmelCase				(							unittest.TestCase):






       @cached_property
       def     SCREAMING_SNAKE_CASE			(      self:    Union[str, Any] ):
         return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )






       @slow
       def     SCREAMING_SNAKE_CASE			(      self:    int ):
         lowercase			:int							=  CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(_lowerCAmelCase )

         lowercase			:Optional[Any]							=  self.default_image_processor
         lowercase			:List[str]							=  prepare_img()
         lowercase			:Optional[int]							=  image_processor(images=_lowerCAmelCase				,    return_tensors="pt" ).to(_lowerCAmelCase )

         # forward pass
         with torch.no_grad():
           lowercase			:Any							=  model(**_lowerCAmelCase )

         # verify the logits
         lowercase			:List[Any]							=  torch.Size((1, 10_00) )
         self.assertEqual(outputs.logits.shape				,    _lowerCAmelCase )

         lowercase			:Tuple							=  torch.tensor([0.92_85, 0.90_15, -0.31_50] ).to(_lowerCAmelCase )

         self.assertTrue(torch.allclose(outputs.logits[0, :3]				,    _lowerCAmelCase				,    atol=1e-4 ) )









import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import GLPNImageProcessor




class       __lowerCAmelCase				(							unittest.TestCase):






       def __init__(      self:    List[Any]				,    _lowerCAmelCase:    str				,    _lowerCAmelCase:    Dict=7				,    _lowerCAmelCase:    Optional[Any]=3				,    _lowerCAmelCase:    List[Any]=18				,    _lowerCAmelCase:    int=30				,    _lowerCAmelCase:    Union[str, Any]=4_00				,    _lowerCAmelCase:    Any=True				,    _lowerCAmelCase:    Union[str, Any]=32				,    _lowerCAmelCase:    int=True				,    ):
         lowercase			:Any							=  parent
         lowercase			:List[str]							=  batch_size
         lowercase			:Any							=  num_channels
         lowercase			:Optional[Any]							=  image_size
         lowercase			:Optional[Any]							=  min_resolution
         lowercase			:str							=  max_resolution
         lowercase			:str							=  do_resize
         lowercase			:Optional[int]							=  size_divisor
         lowercase			:Tuple							=  do_rescale






       def     SCREAMING_SNAKE_CASE			(      self:    Any ):
         return {
             "do_resize": self.do_resize,
             "size_divisor": self.size_divisor,
             "do_rescale": self.do_rescale,
         }







@require_torch
@require_vision
class       __lowerCAmelCase				(							lowerCAmelCase					,       unittest.TestCase):
       _a           =  GLPNImageProcessor if is_vision_available() else None






       def     SCREAMING_SNAKE_CASE			(      self:    List[str] ):
         lowercase			:int							=  GLPNImageProcessingTester(self )






       @property
       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         return self.image_processor_tester.prepare_image_processor_dict()






       def     SCREAMING_SNAKE_CASE			(      self:    Optional[int] ):
         lowercase			:Optional[Any]							=  self.image_processing_class(**self.image_processor_dict )
         self.assertTrue(hasattr(_lowerCAmelCase				,    "do_resize" ) )
         self.assertTrue(hasattr(_lowerCAmelCase				,    "size_divisor" ) )
         self.assertTrue(hasattr(_lowerCAmelCase				,    "resample" ) )
         self.assertTrue(hasattr(_lowerCAmelCase				,    "do_rescale" ) )






       def     SCREAMING_SNAKE_CASE			(      self:    List[Any] ):
         pass






       def     SCREAMING_SNAKE_CASE			(      self:    Any ):
         # Initialize image_processing
         lowercase			:Union[str, Any]							=  self.image_processing_class(**self.image_processor_dict )
         # create random PIL images
         lowercase			:List[str]							=  prepare_image_inputs(self.image_processor_tester				,    equal_resolution=_lowerCAmelCase )
         for image in image_inputs:
           self.assertIsInstance(_lowerCAmelCase				,    Image.Image )

         # Test not batched input (GLPNImageProcessor doesn't support batching)
         lowercase			:str							=  image_processing(image_inputs[0]				,    return_tensors="pt" ).pixel_values
         self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 )
         self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )






       def     SCREAMING_SNAKE_CASE			(      self:    Dict ):
         # Initialize image_processing
         lowercase			:Union[str, Any]							=  self.image_processing_class(**self.image_processor_dict )
         # create random numpy tensors
         lowercase			:Union[str, Any]							=  prepare_image_inputs(self.image_processor_tester				,    equal_resolution=_lowerCAmelCase				,    numpify=_lowerCAmelCase )
         for image in image_inputs:
           self.assertIsInstance(_lowerCAmelCase				,    np.ndarray )

         # Test not batched input (GLPNImageProcessor doesn't support batching)
         lowercase			:Optional[int]							=  image_processing(image_inputs[0]				,    return_tensors="pt" ).pixel_values
         self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 )
         self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )






       def     SCREAMING_SNAKE_CASE			(      self:    Tuple ):
         # Initialize image_processing
         lowercase			:Optional[Any]							=  self.image_processing_class(**self.image_processor_dict )
         # create random PyTorch tensors
         lowercase			:List[str]							=  prepare_image_inputs(self.image_processor_tester				,    equal_resolution=_lowerCAmelCase				,    torchify=_lowerCAmelCase )
         for image in image_inputs:
           self.assertIsInstance(_lowerCAmelCase				,    torch.Tensor )

         # Test not batched input (GLPNImageProcessor doesn't support batching)
         lowercase			:Dict							=  image_processing(image_inputs[0]				,    return_tensors="pt" ).pixel_values
         self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0 )
         self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0 )



