



'''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 (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


lowercase							       =			logging.get_logger(__name__)


if is_vision_available():
  import PIL







class 	UpperCAmelCase	(		__a):


 '''simple docstring'''




 __magic_name__					: Tuple  =      ["pixel_values"]




 def __init__(   self							,  lowerCAmelCase_ = True							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = PILImageResampling.BICUBIC							,  lowerCAmelCase_ = True							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = True							,  lowerCAmelCase_ = 1 / 2_5_5							,  lowerCAmelCase_ = True							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = True							,  **lowerCAmelCase_							,  ) ->      None:



      """simple docstring"""







      super().__init__(**lowerCAmelCase_)
      a_       		=size if size is not None else {"shortest_edge": 2_2_4}
      a_       		=get_size_dict(lowerCAmelCase_							,  default_to_square=lowerCAmelCase_)
      a_       		=crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4}
      a_       		=get_size_dict(lowerCAmelCase_							,  default_to_square=lowerCAmelCase_							,  param_name="crop_size")

      a_       		=do_resize
      a_       		=size
      a_       		=resample
      a_       		=do_center_crop
      a_       		=crop_size
      a_       		=do_rescale
      a_       		=rescale_factor
      a_       		=do_normalize
      a_       		=image_mean if image_mean is not None else OPENAI_CLIP_MEAN
      a_       		=image_std if image_std is not None else OPENAI_CLIP_STD
      a_       		=do_convert_rgb




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



      """simple docstring"""







      a_       		=get_size_dict(lowerCAmelCase_							,  default_to_square=lowerCAmelCase_)
      if "shortest_edge" not in size:
           raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""")
      a_       		=get_resize_output_image_size(lowerCAmelCase_							,  size=size["shortest_edge"]							,  default_to_square=lowerCAmelCase_)
      return resize(lowerCAmelCase_							,  size=lowerCAmelCase_							,  resample=lowerCAmelCase_							,  data_format=lowerCAmelCase_							,  **lowerCAmelCase_)




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_ = None							,  **lowerCAmelCase_							,  ) ->      np.ndarray:



      """simple docstring"""







      a_       		=get_size_dict(lowerCAmelCase_)
      if "height" not in size or "width" not in size:
           raise ValueError(f"""The `size` parameter must contain the keys (height, width). Got {size.keys()}""")
      return center_crop(lowerCAmelCase_							,  size=(size["height"], size["width"])							,  data_format=lowerCAmelCase_							,  **lowerCAmelCase_)




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_ = None							,  **lowerCAmelCase_							,  ) ->      List[Any]:



      """simple docstring"""







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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_ = None							,  **lowerCAmelCase_							,  ) ->      np.ndarray:



      """simple docstring"""







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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = None							,  lowerCAmelCase_ = ChannelDimension.FIRST							,  **lowerCAmelCase_							,  ) ->      PIL.Image.Image:



      """simple docstring"""







      a_       		=do_resize if do_resize is not None else self.do_resize
      a_       		=size if size is not None else self.size
      a_       		=get_size_dict(lowerCAmelCase_							,  param_name="size"							,  default_to_square=lowerCAmelCase_)
      a_       		=resample if resample is not None else self.resample
      a_       		=do_center_crop if do_center_crop is not None else self.do_center_crop
      a_       		=crop_size if crop_size is not None else self.crop_size
      a_       		=get_size_dict(lowerCAmelCase_							,  param_name="crop_size"							,  default_to_square=lowerCAmelCase_)
      a_       		=do_rescale if do_rescale is not None else self.do_rescale
      a_       		=rescale_factor if rescale_factor is not None else self.rescale_factor
      a_       		=do_normalize if do_normalize is not None else self.do_normalize
      a_       		=image_mean if image_mean is not None else self.image_mean
      a_       		=image_std if image_std is not None else self.image_std
      a_       		=do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

      a_       		=make_list_of_images(lowerCAmelCase_)

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

      # PIL RGBA images are converted to RGB
      if do_convert_rgb:
           a_       		=[convert_to_rgb(lowerCAmelCase_) for image in images]

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

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

      if do_center_crop:
           a_       		=[self.center_crop(image=lowerCAmelCase_							,  size=lowerCAmelCase_) for image in images]

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

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

      a_       		=[to_channel_dimension_format(lowerCAmelCase_							,  lowerCAmelCase_) for image in images]

      a_       		={"pixel_values": images}
      return BatchFeature(data=lowerCAmelCase_							,  tensor_type=lowerCAmelCase_)







'''simple docstring'''


import json
import logging
import os
import sys
from time import time
from unittest.mock import patch

from transformers.testing_utils import TestCasePlus, require_torch_tpu


logging.basicConfig(level=logging.DEBUG)

lowercase							       =			logging.getLogger()






def  UpperCAmelCase_       (		lowercase__       ):


     '''simple docstring'''







     a_       		={}
     a_       		=os.path.join(lowercase__       ,	"all_results.json"       )
     if os.path.exists(lowercase__       ):
          with open(lowercase__       ,	"r"       ) as f:
               a_       		=json.load(lowercase__       )
     else:
          raise ValueError(F"""can't find {path}"""       )
     return results


lowercase							       =			logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)







@require_torch_tpu
class 	UpperCAmelCase	(		__a):


 '''simple docstring'''




 def       lowercase_  (   self) ->      List[Any]:



      """simple docstring"""







      import xla_spawn

      a_       		=self.get_auto_remove_tmp_dir()
      a_       		=f"""
            ./examples/pytorch/text-classification/run_glue.py
            --num_cores=8
            ./examples/pytorch/text-classification/run_glue.py
            --model_name_or_path distilbert-base-uncased
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv
            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv
            --do_train
            --do_eval
            --debug tpu_metrics_debug
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --learning_rate=1e-4
            --max_steps=10
            --warmup_steps=2
            --seed=42
            --max_seq_length=128
            """.split()

      with patch.object(lowerCAmelCase_							,  "argv"							,  lowerCAmelCase_):
           a_       		=time()
           xla_spawn.main()
           a_       		=time()

           a_       		=get_results(lowerCAmelCase_)
           self.assertGreaterEqual(result["eval_accuracy"]							,  0.7_5)

           # Assert that the script takes less than 500 seconds to make sure it doesn't hang.
           self.assertLess(end - start							,  5_0_0)




 def       lowercase_  (   self) ->      Tuple:



      """simple docstring"""







      import xla_spawn

      a_       		="\n            ./tests/test_trainer_tpu.py\n            --num_cores=8\n            ./tests/test_trainer_tpu.py\n            ".split()
      with patch.object(lowerCAmelCase_							,  "argv"							,  lowerCAmelCase_):
           xla_spawn.main()


