
"""simple docstring"""
import inspect
import unittest

from transformers import DataaVecVisionConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
   import torch
   from torch import nn

   from transformers import (
       MODEL_MAPPING,
       DataaVecVisionForImageClassification,
       DataaVecVisionForSemanticSegmentation,
       DataaVecVisionModel,
   )
   from transformers.models.dataavec.modeling_dataavec_vision import DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import BeitImageProcessor




class    _snake_case  :






       def __init__(					self						:			str						, snake_case__						:			Optional[int]						, snake_case__						:			Union[str, Any]=1_0_0						, snake_case__						:			Tuple=1_3						, snake_case__						:			Optional[int]=3_0						, snake_case__						:			Optional[int]=2						, snake_case__						:			Optional[Any]=3						, snake_case__						:			List[Any]=True						, snake_case__						:			Union[str, Any]=True						, snake_case__						:			Any=3_2						, snake_case__						:			Tuple=4						, snake_case__						:			Any=4						, snake_case__						:			Union[str, Any]=3_7						, snake_case__						:			List[str]="gelu"						, snake_case__						:			Dict=0.1						, snake_case__						:			List[str]=0.1						, snake_case__						:			Tuple=1_0						, snake_case__						:			List[str]=0.02						, snake_case__						:			Dict=3						, snake_case__						:			Tuple=None						, snake_case__						:			Any=[0, 1, 2, 3]						, ):






            """simple docstring"""



            __a:						Tuple		    =					parent
            __a:						Dict		    =					1_0_0
            __a:						Tuple		    =					batch_size
            __a:						Optional[Any]		    =					image_size
            __a:						Tuple		    =					patch_size
            __a:						str		    =					num_channels
            __a:						Dict		    =					is_training
            __a:						Any		    =					use_labels
            __a:						Any		    =					hidden_size
            __a:						Union[str, Any]		    =					num_hidden_layers
            __a:						Optional[int]		    =					num_attention_heads
            __a:						List[Any]		    =					intermediate_size
            __a:						List[str]		    =					hidden_act
            __a:						List[Any]		    =					hidden_dropout_prob
            __a:						Optional[Any]		    =					attention_probs_dropout_prob
            __a:						List[str]		    =					type_sequence_label_size
            __a:						str		    =					initializer_range
            __a:						Any		    =					scope
            __a:						int		    =					out_indices
            __a:						List[Any]		    =					num_labels

            # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
            __a:						int		    =					(image_size // patch_size) ** 2
            __a:						Tuple		    =					num_patches + 1






       def 							__lowercase  (					self						:			str    ):






            """simple docstring"""



            __a:						Any		    =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

            __a:						Any		    =					None
            __a:						Optional[int]		    =					None
            if self.use_labels:
                 __a:						Optional[Any]		    =					ids_tensor([self.batch_size]						, self.type_sequence_label_size    )
                 __a:						Optional[Any]		    =					ids_tensor([self.batch_size, self.image_size, self.image_size]						, self.num_labels    )

            __a:						int		    =					self.get_config()

            return config, pixel_values, labels, pixel_labels






       def 							__lowercase  (					self						:			Tuple    ):






            """simple docstring"""



            return DataaVecVisionConfig(
                vocab_size=self.vocab_size						, image_size=self.image_size						, patch_size=self.patch_size						, num_channels=self.num_channels						, hidden_size=self.hidden_size						, num_hidden_layers=self.num_hidden_layers						, num_attention_heads=self.num_attention_heads						, intermediate_size=self.intermediate_size						, hidden_act=self.hidden_act						, hidden_dropout_prob=self.hidden_dropout_prob						, attention_probs_dropout_prob=self.attention_probs_dropout_prob						, is_decoder=snake_case__						, initializer_range=self.initializer_range						, out_indices=self.out_indices						, )






       def 							__lowercase  (					self						:			Optional[Any]						, snake_case__						:			Dict						, snake_case__						:			int						, snake_case__						:			int						, snake_case__						:			Optional[int]    ):






            """simple docstring"""



            __a:						Tuple		    =					DataaVecVisionModel(config=snake_case__    )
            model.to(snake_case__    )
            model.eval()
            __a:						Optional[Any]		    =					model(snake_case__    )
            # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
            __a:						Tuple		    =					(self.image_size // self.patch_size) ** 2
            self.parent.assertEqual(result.last_hidden_state.shape						, (self.batch_size, num_patches + 1, self.hidden_size)    )






       def 							__lowercase  (					self						:			List[str]						, snake_case__						:			Any						, snake_case__						:			int						, snake_case__						:			Optional[int]						, snake_case__						:			List[Any]    ):






            """simple docstring"""



            __a:						Union[str, Any]		    =					self.type_sequence_label_size
            __a:						Tuple		    =					DataaVecVisionForImageClassification(snake_case__    )
            model.to(snake_case__    )
            model.eval()
            __a:						Tuple		    =					model(snake_case__						, labels=snake_case__    )
            self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.type_sequence_label_size)    )






       def 							__lowercase  (					self						:			List[Any]						, snake_case__						:			Tuple						, snake_case__						:			int						, snake_case__						:			List[str]						, snake_case__						:			Any    ):






            """simple docstring"""



            __a:						int		    =					self.num_labels
            __a:						List[Any]		    =					DataaVecVisionForSemanticSegmentation(snake_case__    )
            model.to(snake_case__    )
            model.eval()
            __a:						Optional[Any]		    =					model(snake_case__    )
            self.parent.assertEqual(
                result.logits.shape						, (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)    )
            __a:						Dict		    =					model(snake_case__						, labels=snake_case__    )
            self.parent.assertEqual(
                result.logits.shape						, (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)    )






       def 							__lowercase  (					self						:			Any    ):






            """simple docstring"""



            __a:						Dict		    =					self.prepare_config_and_inputs()
            __a:						List[Any]		    =					config_and_inputs
            __a:						Optional[Any]		    =					{"""pixel_values""": pixel_values}
            return config, inputs_dict






@require_torch
class    _snake_case  (						_lowercase ,				_lowercase ,				unittest.TestCase       ):
       SCREAMING_SNAKE_CASE		     =						(
           (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
           if is_torch_available()
           else ()
       )
       SCREAMING_SNAKE_CASE		     =						(
           {
               '''feature-extraction''': DataaVecVisionModel,
               '''image-classification''': DataaVecVisionForImageClassification,
               '''image-segmentation''': DataaVecVisionForSemanticSegmentation,
           }
           if is_torch_available()
           else {}
       )

       SCREAMING_SNAKE_CASE		     =						False
       SCREAMING_SNAKE_CASE		     =						False
       SCREAMING_SNAKE_CASE		     =						False






       def 							__lowercase  (					self						:			Dict    ):






            """simple docstring"""



            __a:						List[Any]		    =					DataaVecVisionModelTester(self    )
            __a:						Optional[int]		    =					ConfigTester(
                self						, config_class=snake_case__						, has_text_modality=snake_case__						, hidden_size=3_7    )






       def 							__lowercase  (					self						:			int    ):






            """simple docstring"""



            self.config_tester.run_common_tests()






       def 							__lowercase  (					self						:			Optional[int]    ):






            """simple docstring"""



            pass






       @require_torch_multi_gpu
       @unittest.skip(
           reason="""Data2VecVision has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""    )
       def 							__lowercase  (					self						:			Any    ):






            """simple docstring"""



            pass






       def 							__lowercase  (					self						:			int    ):






            """simple docstring"""



            __a:						Optional[Any]		    =					self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 __a:						Union[str, Any]		    =					model_class(snake_case__    )
                 self.assertIsInstance(model.get_input_embeddings()						, (nn.Module)    )
                 __a:						int		    =					model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(snake_case__						, nn.Linear    )    )






       def 							__lowercase  (					self						:			List[str]    ):






            """simple docstring"""



            __a:						Any		    =					self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 __a:						Union[str, Any]		    =					model_class(snake_case__    )
                 __a:						Optional[Any]		    =					inspect.signature(model.forward    )
                 # signature.parameters is an OrderedDict => so arg_names order is deterministic
                 __a:						Optional[Any]		    =					[*signature.parameters.keys()]

                 __a:						Union[str, Any]		    =					["""pixel_values"""]
                 self.assertListEqual(arg_names[:1]						, snake_case__    )






       def 							__lowercase  (					self						:			Union[str, Any]    ):






            """simple docstring"""



            __a:						Union[str, Any]		    =					self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*snake_case__    )






       def 							__lowercase  (					self						:			Dict    ):






            """simple docstring"""



            __a:						int		    =					self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_segmentation(*snake_case__    )






       def 							__lowercase  (					self						:			Tuple    ):






            """simple docstring"""



            if not self.model_tester.is_training:
                 return

            __a:						Optional[int]		    =					self.model_tester.prepare_config_and_inputs_for_common()
            __a:						Dict		    =					True

            for model_class in self.all_model_classes:
                 if model_class in [*get_values(snake_case__    )]:
                      continue

                 __a:						Any		    =					model_class(snake_case__    )
                 model.to(snake_case__    )
                 model.train()
                 __a:						str		    =					self._prepare_for_class(snake_case__						, snake_case__						, return_labels=snake_case__    )
                 __a:						Tuple		    =					model(**snake_case__    ).loss
                 loss.backward()






       def 							__lowercase  (					self						:			Union[str, Any]    ):






            """simple docstring"""



            __a:						int		    =					self.model_tester.prepare_config_and_inputs_for_common()
            if not self.model_tester.is_training:
                 return

            __a:						Tuple		    =					False
            __a:						Dict		    =					True

            for model_class in self.all_model_classes:
                 if model_class in [*get_values(snake_case__    )] or not model_class.supports_gradient_checkpointing:
                      continue
                 # TODO: remove the following 3 lines once we have a MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING
                 # this can then be incorporated into _prepare_for_class in test_modeling_common.py
                 elif model_class.__name__ == "Data2VecVisionForSemanticSegmentation":
                      __a:						Tuple		    =					inputs_dict["""pixel_values"""].shape
                      __a:						List[Any]		    =					torch.zeros(
                          [self.model_tester.batch_size, height, width]						, device=snake_case__    ).long()
                 __a:						List[str]		    =					model_class(snake_case__    )
                 model.gradient_checkpointing_enable()
                 model.to(snake_case__    )
                 model.train()
                 __a:						Any		    =					self._prepare_for_class(snake_case__						, snake_case__						, return_labels=snake_case__    )
                 __a:						Union[str, Any]		    =					model(**snake_case__    ).loss
                 loss.backward()






       def 							__lowercase  (					self						:			Optional[Any]    ):






            """simple docstring"""



            __a:						Optional[int]		    =					self.model_tester.prepare_config_and_inputs_for_common()

            __a:						List[Any]		    =					_config_zero_init(snake_case__    )
            for model_class in self.all_model_classes:
                 __a:						List[Any]		    =					model_class(config=snake_case__    )
                 for name, param in model.named_parameters():
                      # we skip lambda parameters as these require special initial values
                      # determined by config.layer_scale_init_value
                      if "lambda" in name:
                           continue
                      if param.requires_grad:
                           self.assertIn(
                               ((param.data.mean() * 1e9).round() / 1e9).item()						, [0.0, 1.0]						, msg=f"""Parameter {name} of model {model_class} seems not properly initialized"""						, )






       def 							__lowercase  (					self						:			Dict						, snake_case__						:			Any						, snake_case__						:			Optional[int]						, snake_case__						:			Any						, snake_case__						:			Optional[int]=2e-4						, snake_case__						:			List[str]="outputs"						, snake_case__						:			Dict=None    ):






            """simple docstring"""



            super().check_pt_tf_outputs(snake_case__						, snake_case__						, snake_case__						, snake_case__						, snake_case__						, snake_case__    )






       def 							__lowercase  (					self						:			Union[str, Any]    ):






            """simple docstring"""



            __a:						Dict		    =					self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*snake_case__    )






       @slow
       def 							__lowercase  (					self						:			List[Any]    ):






            """simple docstring"""



            for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 __a:						str		    =					DataaVecVisionModel.from_pretrained(snake_case__    )
                 self.assertIsNotNone(snake_case__    )






def     lowerCamelCase_    (	)     ->			Dict:


     '''simple docstring'''
     __a:						List[str]		    =					Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
     return image




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






       @cached_property
       def 							__lowercase  (					self						:			Dict    ):






            """simple docstring"""



            return (
                BeitImageProcessor.from_pretrained("""facebook/data2vec-vision-base-ft1k"""    ) if is_vision_available() else None
            )






       @slow
       def 							__lowercase  (					self						:			Any    ):






            """simple docstring"""



            __a:						Optional[int]		    =					DataaVecVisionForImageClassification.from_pretrained("""facebook/data2vec-vision-base-ft1k"""    ).to(
                snake_case__    )

            __a:						Tuple		    =					self.default_image_processor
            __a:						int		    =					prepare_img()
            __a:						Optional[Any]		    =					image_processor(images=snake_case__						, return_tensors="""pt"""    ).to(snake_case__    )

            # forward pass
            with torch.no_grad():
                 __a:						str		    =					model(**snake_case__    )
            __a:						Dict		    =					outputs.logits

            # verify the logits
            __a:						Tuple		    =					torch.Size((1, 1_0_0_0)    )
            self.assertEqual(logits.shape						, snake_case__    )

            __a:						List[str]		    =					torch.tensor([0.3_277, -0.1_395, 0.0_911]    ).to(snake_case__    )

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

            __a:						int		    =					[model.config.labelaid[i] for i in ["""remote control, remote""", """tabby, tabby cat"""]]
            self.assertEqual(logits[0].topk(2    ).indices.cpu().tolist()						, snake_case__    )


"""simple docstring"""
from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


lowercase		:						Tuple =					logging.get_logger(__name__)




class    _snake_case  (						_lowercase       ):
							SCREAMING_SNAKE_CASE		     =						['''pixel_values''']






							def __init__(					self						:			Union[str, Any]						, snake_case__						:			bool = True						, snake_case__						:			Optional[Dict[str, int]] = None						, snake_case__						:			PILImageResampling = PILImageResampling.BILINEAR						, snake_case__						:			bool = True						, snake_case__						:			Union[int, float] = 1 / 2_5_5						, snake_case__						:			bool = True						, snake_case__						:			Optional[Union[float, List[float]]] = None						, snake_case__						:			Optional[Union[float, List[float]]] = None						, **snake_case__						:			Optional[int]						, ):






												"""simple docstring"""



												super().__init__(**snake_case__    )
												__a:						Dict		    =					size if size is not None else {"""height""": 2_2_4, """width""": 2_2_4}
												__a:						Optional[Any]		    =					get_size_dict(snake_case__    )
												__a:						Union[str, Any]		    =					do_resize
												__a:						str		    =					do_rescale
												__a:						Tuple		    =					do_normalize
												__a:						Dict		    =					size
												__a:						str		    =					resample
												__a:						Any		    =					rescale_factor
												__a:						Optional[int]		    =					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
												__a:						int		    =					image_std if image_std is not None else IMAGENET_STANDARD_STD






							def 							__lowercase  (					self						:			Optional[Any]						, snake_case__						:			np.ndarray						, snake_case__						:			Dict[str, int]						, snake_case__						:			PILImageResampling = PILImageResampling.BILINEAR						, snake_case__						:			Optional[Union[str, ChannelDimension]] = None						, **snake_case__						:			List[str]						, ):






												"""simple docstring"""



												__a:						int		    =					get_size_dict(snake_case__    )
												if "height" not in size or "width" not in size:
																	raise ValueError(f"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}"""    )
												return resize(
												    snake_case__						, size=(size["""height"""], size["""width"""])						, resample=snake_case__						, data_format=snake_case__						, **snake_case__    )






							def 							__lowercase  (					self						:			List[str]						, snake_case__						:			np.ndarray						, snake_case__						:			float						, snake_case__						:			Optional[Union[str, ChannelDimension]] = None						, **snake_case__						:			Union[str, Any]    ):






												"""simple docstring"""



												return rescale(snake_case__						, scale=snake_case__						, data_format=snake_case__						, **snake_case__    )






							def 							__lowercase  (					self						:			Dict						, snake_case__						:			np.ndarray						, snake_case__						:			Union[float, List[float]]						, snake_case__						:			Union[float, List[float]]						, snake_case__						:			Optional[Union[str, ChannelDimension]] = None						, **snake_case__						:			Union[str, Any]						, ):






												"""simple docstring"""



												return normalize(snake_case__						, mean=snake_case__						, std=snake_case__						, data_format=snake_case__						, **snake_case__    )






							def 							__lowercase  (					self						:			Optional[int]						, snake_case__						:			ImageInput						, snake_case__						:			Optional[bool] = None						, snake_case__						:			Dict[str, int] = None						, snake_case__						:			PILImageResampling = None						, snake_case__						:			Optional[bool] = None						, snake_case__						:			Optional[float] = None						, snake_case__						:			Optional[bool] = None						, snake_case__						:			Optional[Union[float, List[float]]] = None						, snake_case__						:			Optional[Union[float, List[float]]] = None						, snake_case__						:			Optional[Union[str, TensorType]] = None						, snake_case__						:			Union[str, ChannelDimension] = ChannelDimension.FIRST						, **snake_case__						:			List[str]						, ):






												"""simple docstring"""



												__a:						Any		    =					do_resize if do_resize is not None else self.do_resize
												__a:						Any		    =					do_rescale if do_rescale is not None else self.do_rescale
												__a:						int		    =					do_normalize if do_normalize is not None else self.do_normalize
												__a:						Any		    =					resample if resample is not None else self.resample
												__a:						Any		    =					rescale_factor if rescale_factor is not None else self.rescale_factor
												__a:						Dict		    =					image_mean if image_mean is not None else self.image_mean
												__a:						List[str]		    =					image_std if image_std is not None else self.image_std

												__a:						int		    =					size if size is not None else self.size
												__a:						Any		    =					get_size_dict(snake_case__    )

												__a:						List[str]		    =					make_list_of_images(snake_case__    )

												if not valid_images(snake_case__    ):
																	raise ValueError(
																	    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
																	    """torch.Tensor, tf.Tensor or jax.ndarray."""    )

												if do_resize and size is None:
																	raise ValueError("""Size must be specified if do_resize is True."""    )

												if do_rescale and rescale_factor is None:
																	raise ValueError("""Rescale factor must be specified if do_rescale is True."""    )

												# All transformations expect numpy arrays.
												__a:						str		    =					[to_numpy_array(snake_case__    ) for image in images]

												if do_resize:
																	__a:						Dict		    =					[self.resize(image=snake_case__						, size=snake_case__						, resample=snake_case__    ) for image in images]

												if do_rescale:
																	__a:						str		    =					[self.rescale(image=snake_case__						, scale=snake_case__    ) for image in images]

												if do_normalize:
																	__a:						Dict		    =					[self.normalize(image=snake_case__						, mean=snake_case__						, std=snake_case__    ) for image in images]

												__a:						int		    =					[to_channel_dimension_format(snake_case__						, snake_case__    ) for image in images]

												__a:						str		    =					{"""pixel_values""": images}
												return BatchFeature(data=snake_case__						, tensor_type=snake_case__    )
