





'''simple docstring'''

from typing import Dict, Iterable, List, Optional, Union

import numpy as np

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


UpperCAmelCase					:		List[Any]		=							logging.get_logger(__name__)





class lowerCamelCase						(a__					):
  _lowercase  :	int       					=				["""pixel_values"""]







  def __init__(		self     ,     lowercase__ = True     ,     lowercase__ = None     ,     lowercase__ = PILImageResampling.BICUBIC     ,     lowercase__ = True     ,     lowercase__ = None     ,     lowercase__ = True     ,     lowercase__ = 1 / 255     ,     lowercase__ = True     ,     lowercase__ = IMAGENET_DEFAULT_MEAN     ,     lowercase__ = IMAGENET_DEFAULT_STD     ,     **lowercase__     ,     )		->						None:






        """simple docstring"""





        super().__init__(**lowercase__		)
        _snake_case		:	Any					     =     size if size is not None else {'''shortest_edge''': 224}
        _snake_case		:	Union[str, Any]					     =     get_size_dict(lowercase__     ,     default_to_square=lowercase__		)
        _snake_case		:	Optional[int]					     =     crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
        _snake_case		:	str					     =     get_size_dict(lowercase__     ,     param_name='''crop_size'''		)

        _snake_case		:	Optional[Any]					     =     do_resize
        _snake_case		:	Tuple					     =     size
        _snake_case		:	Dict					     =     resample
        _snake_case		:	Optional[int]					     =     do_center_crop
        _snake_case		:	List[str]					     =     crop_size
        _snake_case		:	int					     =     do_rescale
        _snake_case		:	Dict					     =     rescale_factor
        _snake_case		:	List[str]					     =     do_normalize
        _snake_case		:	Tuple					     =     image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
        _snake_case		:	List[Any]					     =     image_std if image_std is not None else IMAGENET_DEFAULT_STD







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__ = PILImageResampling.BICUBIC     ,     lowercase__ = None     ,     **lowercase__     ,     )		->						np.ndarray:






        """simple docstring"""





        _snake_case		:	Optional[Any]					     =     get_size_dict(lowercase__     ,     default_to_square=lowercase__		)
        # size_dict is a dict with either keys "height" and "width" or "shortest_edge"
        if "shortest_edge" in size:
              _snake_case		:	Union[str, Any]					     =     int((256 / 224) * size['''shortest_edge''']		)
              _snake_case		:	Tuple					     =     get_resize_output_image_size(lowercase__     ,     size=lowercase__     ,     default_to_square=lowercase__		)
              _snake_case		:	str					     =     {'''height''': output_size[0], '''width''': output_size[1]}
        if "height" not in size_dict or "width" not in size_dict:
              raise ValueError(
                  F'''Size dict must have keys \'height\' and \'width\' or \'shortest_edge\'. Got {size_dict.keys()}'''		)
        return resize(
            lowercase__     ,     size=(size_dict['''height'''], size_dict['''width'''])     ,     resample=lowercase__     ,     data_format=lowercase__     ,     **lowercase__		)







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__ = None     ,     **lowercase__     ,     )		->						np.ndarray:






        """simple docstring"""





        _snake_case		:	Union[str, Any]					     =     get_size_dict(lowercase__		)
        if "height" not in size or "width" not in size:
              raise ValueError(F'''Size dict must have keys \'height\' and \'width\'. Got {size.keys()}'''		)
        return center_crop(lowercase__     ,     size=(size['''height'''], size['''width'''])     ,     data_format=lowercase__     ,     **lowercase__		)







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__ = None     ,     **lowercase__     ,     )		->						np.ndarray:






        """simple docstring"""





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







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__ = None     ,     **lowercase__     ,     )		->						np.ndarray:






        """simple docstring"""





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







  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = ChannelDimension.FIRST     ,     **lowercase__     ,     )		->						BatchFeature:






        """simple docstring"""





        _snake_case		:	Optional[int]					     =     do_resize if do_resize is not None else self.do_resize
        _snake_case		:	Dict					     =     resample if resample is not None else self.resample
        _snake_case		:	Any					     =     do_center_crop if do_center_crop is not None else self.do_center_crop
        _snake_case		:	Optional[Any]					     =     do_rescale if do_rescale is not None else self.do_rescale
        _snake_case		:	Optional[int]					     =     rescale_factor if rescale_factor is not None else self.rescale_factor
        _snake_case		:	List[str]					     =     do_normalize if do_normalize is not None else self.do_normalize
        _snake_case		:	List[Any]					     =     image_mean if image_mean is not None else self.image_mean
        _snake_case		:	Union[str, Any]					     =     image_std if image_std is not None else self.image_std

        _snake_case		:	Tuple					     =     size if size is not None else self.size
        _snake_case		:	Optional[Any]					     =     get_size_dict(lowercase__     ,     default_to_square=lowercase__		)
        _snake_case		:	Union[str, Any]					     =     crop_size if crop_size is not None else self.crop_size
        _snake_case		:	str					     =     get_size_dict(lowercase__     ,     param_name='''crop_size'''		)

        _snake_case		:	str					     =     make_list_of_images(lowercase__		)

        if not valid_images(lowercase__		):
              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_center_crop and crop_size is None:
              raise ValueError('''Crop size must be specified if do_center_crop is True.'''		)

        if do_rescale and rescale_factor is None:
              raise ValueError('''Rescale factor must be specified if do_rescale 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.
        _snake_case		:	List[Any]					     =     [to_numpy_array(lowercase__		) for image in images]

        if do_resize:
              _snake_case		:	Optional[int]					     =     [self.resize(lowercase__     ,     lowercase__     ,     lowercase__		) for image in images]

        if do_center_crop:
              _snake_case		:	Tuple					     =     [self.center_crop(lowercase__     ,     lowercase__		) for image in images]

        if do_rescale:
              _snake_case		:	Optional[Any]					     =     [self.rescale(lowercase__     ,     lowercase__		) for image in images]

        if do_normalize:
              _snake_case		:	str					     =     [self.normalize(lowercase__     ,     lowercase__     ,     lowercase__		) for image in images]

        _snake_case		:	int					     =     [to_channel_dimension_format(lowercase__     ,     lowercase__		) for image in images]

        _snake_case		:	Dict					     =     {'''pixel_values''': images}
        return BatchFeature(data=lowercase__     ,     tensor_type=lowercase__		)









'''simple docstring'''

from __future__ import annotations

import unittest

from transformers import is_tf_available, is_torch_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow


if is_tf_available():
   from transformers import (
       AutoConfig,
       BertConfig,
       GPTaConfig,
       TaConfig,
       TFAutoModel,
       TFAutoModelForCausalLM,
       TFAutoModelForMaskedLM,
       TFAutoModelForPreTraining,
       TFAutoModelForQuestionAnswering,
       TFAutoModelForSeqaSeqLM,
       TFAutoModelForSequenceClassification,
       TFAutoModelWithLMHead,
       TFBertForMaskedLM,
       TFBertForPreTraining,
       TFBertForQuestionAnswering,
       TFBertForSequenceClassification,
       TFBertModel,
       TFGPTaLMHeadModel,
       TFRobertaForMaskedLM,
       TFTaForConditionalGeneration,
   )
   from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
   from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
   from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST

if is_torch_available():
   from transformers import (
       AutoModel,
       AutoModelForCausalLM,
       AutoModelForMaskedLM,
       AutoModelForPreTraining,
       AutoModelForQuestionAnswering,
       AutoModelForSeqaSeqLM,
       AutoModelForSequenceClassification,
       AutoModelWithLMHead,
       BertForMaskedLM,
       BertForPreTraining,
       BertForQuestionAnswering,
       BertForSequenceClassification,
       BertModel,
       GPTaLMHeadModel,
       RobertaForMaskedLM,
       TaForConditionalGeneration,
   )





@is_pt_tf_cross_test
class lowerCamelCase						(unittest.TestCase					):







  @slow
  def 				UpperCAmelCase_				(		self		)		->						List[Any]:






        """simple docstring"""





        for model_name in ["bert-base-uncased"]:
              _snake_case		:	Union[str, Any]					     =     AutoConfig.from_pretrained(lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	Any					     =     TFAutoModel.from_pretrained(lowercase__     ,     from_pt=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	str					     =     AutoModel.from_pretrained(lowercase__     ,     from_tf=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)







  @slow
  def 				UpperCAmelCase_				(		self		)		->						List[str]:






        """simple docstring"""





        for model_name in ["bert-base-uncased"]:
              _snake_case		:	Optional[Any]					     =     AutoConfig.from_pretrained(lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	Dict					     =     TFAutoModelForPreTraining.from_pretrained(lowercase__     ,     from_pt=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	Tuple					     =     AutoModelForPreTraining.from_pretrained(lowercase__     ,     from_tf=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)







  @slow
  def 				UpperCAmelCase_				(		self		)		->						Union[str, Any]:






        """simple docstring"""





        for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _snake_case		:	Optional[int]					     =     AutoConfig.from_pretrained(lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	List[Any]					     =     TFAutoModelForCausalLM.from_pretrained(lowercase__     ,     from_pt=lowercase__		)
              _snake_case     ,							_snake_case		:	Tuple					     =     TFAutoModelForCausalLM.from_pretrained(
                  lowercase__     ,     output_loading_info=lowercase__     ,     from_pt=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	Optional[int]					     =     AutoModelForCausalLM.from_pretrained(lowercase__     ,     from_tf=lowercase__		)
              _snake_case     ,							_snake_case		:	Optional[Any]					     =     AutoModelForCausalLM.from_pretrained(
                  lowercase__     ,     output_loading_info=lowercase__     ,     from_tf=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)







  @slow
  def 				UpperCAmelCase_				(		self		)		->						Optional[Any]:






        """simple docstring"""





        for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _snake_case		:	List[Any]					     =     AutoConfig.from_pretrained(lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	Tuple					     =     TFAutoModelWithLMHead.from_pretrained(lowercase__     ,     from_pt=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	int					     =     AutoModelWithLMHead.from_pretrained(lowercase__     ,     from_tf=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)







  @slow
  def 				UpperCAmelCase_				(		self		)		->						Any:






        """simple docstring"""





        for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _snake_case		:	List[str]					     =     AutoConfig.from_pretrained(lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	Union[str, Any]					     =     TFAutoModelForMaskedLM.from_pretrained(lowercase__     ,     from_pt=lowercase__		)
              _snake_case     ,							_snake_case		:	List[str]					     =     TFAutoModelForMaskedLM.from_pretrained(
                  lowercase__     ,     output_loading_info=lowercase__     ,     from_pt=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	int					     =     AutoModelForMaskedLM.from_pretrained(lowercase__     ,     from_tf=lowercase__		)
              _snake_case     ,							_snake_case		:	Optional[int]					     =     AutoModelForMaskedLM.from_pretrained(
                  lowercase__     ,     output_loading_info=lowercase__     ,     from_tf=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)







  @slow
  def 				UpperCAmelCase_				(		self		)		->						List[str]:






        """simple docstring"""





        for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _snake_case		:	List[str]					     =     AutoConfig.from_pretrained(lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	Tuple					     =     TFAutoModelForSeqaSeqLM.from_pretrained(lowercase__     ,     from_pt=lowercase__		)
              _snake_case     ,							_snake_case		:	List[str]					     =     TFAutoModelForSeqaSeqLM.from_pretrained(
                  lowercase__     ,     output_loading_info=lowercase__     ,     from_pt=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	List[str]					     =     AutoModelForSeqaSeqLM.from_pretrained(lowercase__     ,     from_tf=lowercase__		)
              _snake_case     ,							_snake_case		:	Dict					     =     AutoModelForSeqaSeqLM.from_pretrained(
                  lowercase__     ,     output_loading_info=lowercase__     ,     from_tf=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)







  @slow
  def 				UpperCAmelCase_				(		self		)		->						Dict:






        """simple docstring"""





        for model_name in ["bert-base-uncased"]:
              _snake_case		:	Any					     =     AutoConfig.from_pretrained(lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	Any					     =     TFAutoModelForSequenceClassification.from_pretrained(lowercase__     ,     from_pt=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	Dict					     =     AutoModelForSequenceClassification.from_pretrained(lowercase__     ,     from_tf=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)







  @slow
  def 				UpperCAmelCase_				(		self		)		->						Optional[int]:






        """simple docstring"""





        for model_name in ["bert-base-uncased"]:
              _snake_case		:	str					     =     AutoConfig.from_pretrained(lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	str					     =     TFAutoModelForQuestionAnswering.from_pretrained(lowercase__     ,     from_pt=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)

              _snake_case		:	Union[str, Any]					     =     AutoModelForQuestionAnswering.from_pretrained(lowercase__     ,     from_tf=lowercase__		)
              self.assertIsNotNone(lowercase__		)
              self.assertIsInstance(lowercase__     ,     lowercase__		)







  def 				UpperCAmelCase_				(		self		)		->						str:






        """simple docstring"""





        _snake_case		:	Union[str, Any]					     =     TFAutoModelWithLMHead.from_pretrained(lowercase__     ,     from_pt=lowercase__		)
        self.assertIsInstance(lowercase__     ,     lowercase__		)
        self.assertEqual(model.num_parameters()     ,     14_410		)
        self.assertEqual(model.num_parameters(only_trainable=lowercase__		)     ,     14_410		)

        _snake_case		:	Tuple					     =     AutoModelWithLMHead.from_pretrained(lowercase__     ,     from_tf=lowercase__		)
        self.assertIsInstance(lowercase__     ,     lowercase__		)
        self.assertEqual(model.num_parameters()     ,     14_410		)
        self.assertEqual(model.num_parameters(only_trainable=lowercase__		)     ,     14_410		)







  def 				UpperCAmelCase_				(		self		)		->						str:






        """simple docstring"""





        _snake_case		:	List[str]					     =     TFAutoModelWithLMHead.from_pretrained(lowercase__     ,     from_pt=lowercase__		)
        self.assertIsInstance(lowercase__     ,     lowercase__		)
        self.assertEqual(model.num_parameters()     ,     14_410		)
        self.assertEqual(model.num_parameters(only_trainable=lowercase__		)     ,     14_410		)

        _snake_case		:	int					     =     AutoModelWithLMHead.from_pretrained(lowercase__     ,     from_tf=lowercase__		)
        self.assertIsInstance(lowercase__     ,     lowercase__		)
        self.assertEqual(model.num_parameters()     ,     14_410		)
        self.assertEqual(model.num_parameters(only_trainable=lowercase__		)     ,     14_410		)


