


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 (
    get_resize_output_image_size,
    normalize,
    pad,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging
from ...utils.import_utils import is_vision_available


SCREAMING_SNAKE_CASE__       			=				logging.get_logger(__name__)


if is_vision_available():
							import PIL







class     __lowerCamelCase     (  lowerCAmelCase_     ):


							"""simple docstring"""

							lowerCAmelCase__          =				["pixel_values"]




							def __init__(  self						,       UpperCAmelCase = True						,       UpperCAmelCase = None						,       UpperCAmelCase = PILImageResampling.BILINEAR						,       UpperCAmelCase = True						,       UpperCAmelCase = False						,       UpperCAmelCase = True						,       UpperCAmelCase = True						,       UpperCAmelCase = 1 / 255						,       UpperCAmelCase = True						,       UpperCAmelCase = None						,       UpperCAmelCase = None						,       **UpperCAmelCase						,       )     ->       List[Any]:






								'''simple docstring'''






								super().__init__(**__UpperCamelCase  )

								lowercase_			 = size if size is not None else {"height": 2560, "width": 1920}
								if isinstance(__UpperCamelCase						,       (tuple, list)  ):
									# The previous feature extractor size parameter was in (width, height) format
									lowercase_			 = size[::-1]
								lowercase_			 = get_size_dict(__UpperCamelCase  )

								lowercase_			 = do_resize
								lowercase_			 = size
								lowercase_			 = resample
								lowercase_			 = do_thumbnail
								lowercase_			 = do_align_long_axis
								lowercase_			 = do_pad
								lowercase_			 = do_rescale
								lowercase_			 = rescale_factor
								lowercase_			 = do_normalize
								lowercase_			 = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
								lowercase_			 = image_std if image_std is not None else IMAGENET_STANDARD_STD




							def 				A__      (  self						,       UpperCAmelCase						,       UpperCAmelCase						,       UpperCAmelCase = None  )     ->       Optional[Any]:






								'''simple docstring'''






								lowercase_ ,	lowercase_			 = get_image_size(__UpperCamelCase  )
								lowercase_ ,	lowercase_			 = size["height"], size["width"]

								if (output_width < output_height and input_width > input_height) or (
								    output_width > output_height and input_width < input_height
								):
									lowercase_			 = np.rotaa(__UpperCamelCase						,       3  )

								if data_format is not None:
									lowercase_			 = to_channel_dimension_format(__UpperCamelCase						,       __UpperCamelCase  )

								return image




							def 				A__      (  self						,       UpperCAmelCase						,       UpperCAmelCase						,       UpperCAmelCase = False						,       UpperCAmelCase = None						,       )     ->       int:






								'''simple docstring'''






								lowercase_ ,	lowercase_			 = size["height"], size["width"]
								lowercase_ ,	lowercase_			 = get_image_size(__UpperCamelCase  )

								lowercase_			 = output_width - input_width
								lowercase_			 = output_height - input_height

								if random_padding:
									lowercase_			 = np.random.randint(low=0						,       high=delta_height + 1  )
									lowercase_			 = np.random.randint(low=0						,       high=delta_width + 1  )
								else:
									lowercase_			 = delta_height // 2
									lowercase_			 = delta_width // 2

								lowercase_			 = delta_height - pad_top
								lowercase_			 = delta_width - pad_left

								lowercase_			 = ((pad_top, pad_bottom), (pad_left, pad_right))
								return pad(__UpperCamelCase						,       __UpperCamelCase						,       data_format=__UpperCamelCase  )




							def 				A__      (  self						,       *UpperCAmelCase						,       **UpperCAmelCase  )     ->       Optional[Any]:






								'''simple docstring'''






								logger.info("pad is deprecated and will be removed in version 4.27. Please use pad_image instead."  )
								return self.pad_image(*__UpperCamelCase						,       **__UpperCamelCase  )




							def 				A__      (  self						,       UpperCAmelCase						,       UpperCAmelCase						,       UpperCAmelCase = PILImageResampling.BICUBIC						,       UpperCAmelCase = None						,       **UpperCAmelCase						,       )     ->       str:






								'''simple docstring'''






								lowercase_ ,	lowercase_			 = get_image_size(__UpperCamelCase  )
								lowercase_ ,	lowercase_			 = size["height"], size["width"]

								# We always resize to the smallest of either the input or output size.
								lowercase_			 = min(__UpperCamelCase						,       __UpperCamelCase  )
								lowercase_			 = min(__UpperCamelCase						,       __UpperCamelCase  )

								if height == input_height and width == input_width:
									return image

								if input_height > input_width:
									lowercase_			 = int(input_width * height / input_height  )
								elif input_width > input_height:
									lowercase_			 = int(input_height * width / input_width  )

								return resize(
								    __UpperCamelCase						,       size=(height, width)						,       resample=__UpperCamelCase						,       reducing_gap=2.0						,       data_format=__UpperCamelCase						,       **__UpperCamelCase  )




							def 				A__      (  self						,       UpperCAmelCase						,       UpperCAmelCase						,       UpperCAmelCase = PILImageResampling.BICUBIC						,       UpperCAmelCase = None						,       **UpperCAmelCase						,       )     ->       Tuple:






								'''simple docstring'''






								lowercase_			 = get_size_dict(__UpperCamelCase  )
								lowercase_			 = min(size["height"]						,       size["width"]  )
								lowercase_			 = get_resize_output_image_size(__UpperCamelCase						,       size=__UpperCamelCase						,       default_to_square=__UpperCamelCase  )
								lowercase_			 = resize(__UpperCamelCase						,       size=__UpperCamelCase						,       resample=__UpperCamelCase						,       data_format=__UpperCamelCase						,       **__UpperCamelCase  )
								return resized_image




							def 				A__      (  self						,       UpperCAmelCase						,       UpperCAmelCase						,       UpperCAmelCase = None						,       **UpperCAmelCase						,       )     ->       Optional[int]:






								'''simple docstring'''






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




							def 				A__      (  self						,       UpperCAmelCase						,       UpperCAmelCase						,       UpperCAmelCase						,       UpperCAmelCase = None						,       **UpperCAmelCase						,       )     ->       str:






								'''simple docstring'''






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




							def 				A__      (  self						,       UpperCAmelCase						,       UpperCAmelCase = None						,       UpperCAmelCase = None						,       UpperCAmelCase = None						,       UpperCAmelCase = None						,       UpperCAmelCase = None						,       UpperCAmelCase = None						,       UpperCAmelCase = False						,       UpperCAmelCase = None						,       UpperCAmelCase = None						,       UpperCAmelCase = None						,       UpperCAmelCase = None						,       UpperCAmelCase = None						,       UpperCAmelCase = None						,       UpperCAmelCase = ChannelDimension.FIRST						,       **UpperCAmelCase						,       )     ->       Any:






								'''simple docstring'''






								lowercase_			 = do_resize if do_resize is not None else self.do_resize
								lowercase_			 = size if size is not None else self.size
								if isinstance(__UpperCamelCase						,       (tuple, list)  ):
									# Previous feature extractor had size in (width, height) format
									lowercase_			 = size[::-1]
								lowercase_			 = get_size_dict(__UpperCamelCase  )
								lowercase_			 = resample if resample is not None else self.resample
								lowercase_			 = do_thumbnail if do_thumbnail is not None else self.do_thumbnail
								lowercase_			 = do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
								lowercase_			 = do_pad if do_pad is not None else self.do_pad
								lowercase_			 = do_rescale if do_rescale is not None else self.do_rescale
								lowercase_			 = rescale_factor if rescale_factor is not None else self.rescale_factor
								lowercase_			 = do_normalize if do_normalize is not None else self.do_normalize
								lowercase_			 = image_mean if image_mean is not None else self.image_mean
								lowercase_			 = image_std if image_std is not None else self.image_std

								lowercase_			 = make_list_of_images(__UpperCamelCase  )

								if not valid_images(__UpperCamelCase  ):
									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."  )

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

								if do_normalize and (image_mean is None or image_std is None):
									raise ValueError("Image mean and std must be specified if do_normalize is True."  )

								# All transformations expect numpy arrays.
								lowercase_			 = [to_numpy_array(__UpperCamelCase  ) for image in images]

								if do_align_long_axis:
									lowercase_			 = [self.align_long_axis(__UpperCamelCase						,       size=__UpperCamelCase  ) for image in images]

								if do_resize:
									lowercase_			 = [self.resize(image=__UpperCamelCase						,       size=__UpperCamelCase						,       resample=__UpperCamelCase  ) for image in images]

								if do_thumbnail:
									lowercase_			 = [self.thumbnail(image=__UpperCamelCase						,       size=__UpperCamelCase  ) for image in images]

								if do_pad:
									lowercase_			 = [self.pad_image(image=__UpperCamelCase						,       size=__UpperCamelCase						,       random_padding=__UpperCamelCase  ) for image in images]

								if do_rescale:
									lowercase_			 = [self.rescale(image=__UpperCamelCase						,       scale=__UpperCamelCase  ) for image in images]

								if do_normalize:
									lowercase_			 = [self.normalize(image=__UpperCamelCase						,       mean=__UpperCamelCase						,       std=__UpperCamelCase  ) for image in images]

								lowercase_			 = [to_channel_dimension_format(__UpperCamelCase						,       __UpperCamelCase  ) for image in images]

								lowercase_			 = {"pixel_values": images}
								return BatchFeature(data=__UpperCamelCase						,       tensor_type=__UpperCamelCase  )






'''simple docstring'''







import unittest

import numpy as np

from transformers import ElectraConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
      from transformers.models.electra.modeling_flax_electra import (
          FlaxElectraForCausalLM,
          FlaxElectraForMaskedLM,
          FlaxElectraForMultipleChoice,
          FlaxElectraForPreTraining,
          FlaxElectraForQuestionAnswering,
          FlaxElectraForSequenceClassification,
          FlaxElectraForTokenClassification,
          FlaxElectraModel,
      )

class    lowerCamelCase_				(						unittest.TestCase	):






   '''simple docstring'''

   def __init__(	self  ,    __UpperCamelCase  ,    __UpperCamelCase=13  ,    __UpperCamelCase=7  ,    __UpperCamelCase=True  ,    __UpperCamelCase=True  ,    __UpperCamelCase=True  ,    __UpperCamelCase=True  ,    __UpperCamelCase=99  ,    __UpperCamelCase=24  ,    __UpperCamelCase=32  ,    __UpperCamelCase=5  ,    __UpperCamelCase=4  ,    __UpperCamelCase=37  ,    __UpperCamelCase="gelu"  ,    __UpperCamelCase=0.1  ,    __UpperCamelCase=0.1  ,    __UpperCamelCase=5_12  ,    __UpperCamelCase=16  ,    __UpperCamelCase=2  ,    __UpperCamelCase=0.02  ,    __UpperCamelCase=4  ,    ):
       UpperCamelCase__	     =      parent
       UpperCamelCase__	     =      batch_size
       UpperCamelCase__	     =      seq_length
       UpperCamelCase__	     =      is_training
       UpperCamelCase__	     =      use_attention_mask
       UpperCamelCase__	     =      use_token_type_ids
       UpperCamelCase__	     =      use_labels
       UpperCamelCase__	     =      vocab_size
       UpperCamelCase__	     =      hidden_size
       UpperCamelCase__	     =      embedding_size
       UpperCamelCase__	     =      num_hidden_layers
       UpperCamelCase__	     =      num_attention_heads
       UpperCamelCase__	     =      intermediate_size
       UpperCamelCase__	     =      hidden_act
       UpperCamelCase__	     =      hidden_dropout_prob
       UpperCamelCase__	     =      attention_probs_dropout_prob
       UpperCamelCase__	     =      max_position_embeddings
       UpperCamelCase__	     =      type_vocab_size
       UpperCamelCase__	     =      type_sequence_label_size
       UpperCamelCase__	     =      initializer_range
       UpperCamelCase__	     =      num_choices


   def 						_UpperCamelCase							(	self			):
       UpperCamelCase__	     =      ids_tensor([self.batch_size, self.seq_length]  ,    self.vocab_size			)

       UpperCamelCase__	     =      None
       if self.use_attention_mask:
           UpperCamelCase__	     =      random_attention_mask([self.batch_size, self.seq_length]			)

       UpperCamelCase__	     =      None
       if self.use_token_type_ids:
           UpperCamelCase__	     =      ids_tensor([self.batch_size, self.seq_length]  ,    self.type_vocab_size			)

       UpperCamelCase__	     =      ElectraConfig(
           vocab_size=self.vocab_size  ,    hidden_size=self.hidden_size  ,    embedding_size=self.embedding_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  ,    max_position_embeddings=self.max_position_embeddings  ,    type_vocab_size=self.type_vocab_size  ,    initializer_range=self.initializer_range  ,    )

       return config, input_ids, token_type_ids, attention_mask







   def 						_UpperCamelCase							(	self			):
       UpperCamelCase__	     =      self.prepare_config_and_inputs()
       UpperCamelCase__     ,	UpperCamelCase__     ,	UpperCamelCase__     ,	UpperCamelCase__	     =      config_and_inputs
       UpperCamelCase__	     =      {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
       return config, inputs_dict

@require_flax
class    lowerCamelCase_				(						lowerCAmelCase_       ,     unittest.TestCase	):






   '''simple docstring'''

   A			:  Dict					=						True

   A			:  Optional[Any]					=						(
       (
           FlaxElectraModel,
           FlaxElectraForCausalLM,
           FlaxElectraForMaskedLM,
           FlaxElectraForPreTraining,
           FlaxElectraForTokenClassification,
           FlaxElectraForQuestionAnswering,
           FlaxElectraForMultipleChoice,
           FlaxElectraForSequenceClassification,
       )
       if is_flax_available()
       else ()
   )


   def 						_UpperCamelCase							(	self			):
       UpperCamelCase__	     =      FlaxElectraModelTester(self			)







   @slow
   def 						_UpperCamelCase							(	self			):
       for model_class_name in self.all_model_classes:
           if model_class_name == FlaxElectraForMaskedLM:
               UpperCamelCase__	     =      model_class_name.from_pretrained("google/electra-small-generator"			)
           else:
               UpperCamelCase__	     =      model_class_name.from_pretrained("google/electra-small-discriminator"			)
           UpperCamelCase__	     =      model(np.ones((1, 1)			)			)
           self.assertIsNotNone(__UpperCamelCase			)




