






"""simple docstring"""


import unittest

import numpy as np
from datasets import load_dataset

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 SegformerImageProcessor






class 		snake_case    (    unittest.TestCase			):





    def __init__(				self      :   List[Any]							,						A      :   Optional[Any]							,						A      :   str=7							,						A      :   List[str]=3							,						A      :   Union[str, Any]=3_0							,						A      :   Union[str, Any]=4_0_0							,						A      :   List[Any]=True							,						A      :   Any=None							,						A      :   Union[str, Any]=True							,						A      :   List[str]=[0.5, 0.5, 0.5]							,						A      :   Any=[0.5, 0.5, 0.5]							,						A      :   Optional[Any]=False							,						):


      '''simple docstring'''




      a	:     Optional[int]							=  size if size is not None else {'height': 3_0, 'width': 3_0}
      a	:     List[Any]							=  parent
      a	:     Optional[int]							=  batch_size
      a	:     Optional[Any]							=  num_channels
      a	:     str							=  min_resolution
      a	:     Tuple							=  max_resolution
      a	:     Dict							=  do_resize
      a	:     Optional[int]							=  size
      a	:     Tuple							=  do_normalize
      a	:     Tuple							=  image_mean
      a	:     str							=  image_std
      a	:     Optional[int]							=  do_reduce_labels





    def 							lowerCamelCase__ (				self      :   Optional[int]     ):


      '''simple docstring'''




      return {
          "do_resize": self.do_resize,
          "size": self.size,
          "do_normalize": self.do_normalize,
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_reduce_labels": self.do_reduce_labels,
      }




def      snake_case    ():







  '''simple docstring'''

  a	:     Any							=  load_dataset('hf-internal-testing/fixtures_ade20k'      ,     split='test' )

  a	:     List[str]							=  Image.open(dataset[0]['file'] )
  a	:     Tuple							=  Image.open(dataset[1]['file'] )

  return image, map




def      snake_case    ():







  '''simple docstring'''

  a	:     Union[str, Any]							=  load_dataset('hf-internal-testing/fixtures_ade20k'      ,     split='test' )

  a	:     List[Any]							=  Image.open(dataset[0]['file'] )
  a	:     int							=  Image.open(dataset[1]['file'] )
  a	:     List[Any]							=  Image.open(dataset[2]['file'] )
  a	:     Tuple							=  Image.open(dataset[3]['file'] )

  return [imagea, imagea], [mapa, mapa]






@require_torch
@require_vision
class 		snake_case    (    UpperCAmelCase  ,					unittest.TestCase			):
    __magic_name__       	=	SegformerImageProcessor if is_vision_available() else None





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


      '''simple docstring'''




      a	:     Dict							=  SegformerImageProcessingTester(self     )





    @property
    def 							lowerCamelCase__ (				self      :   Any     ):


      '''simple docstring'''




      return self.image_processor_tester.prepare_image_processor_dict()





    def 							lowerCamelCase__ (				self      :   int     ):


      '''simple docstring'''




      a	:     int							=  self.image_processing_class(**self.image_processor_dict     )
      self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_							,						'do_resize'     )     )
      self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_							,						'size'     )     )
      self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_							,						'do_normalize'     )     )
      self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_							,						'image_mean'     )     )
      self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_							,						'image_std'     )     )
      self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_							,						'do_reduce_labels'     )     )





    def 							lowerCamelCase__ (				self      :   List[Any]     ):


      '''simple docstring'''




      a	:     Union[str, Any]							=  self.image_processing_class.from_dict(self.image_processor_dict     )
      self.assertEqual(image_processor.size							,						{'height': 3_0, 'width': 3_0}     )
      self.assertEqual(image_processor.do_reduce_labels							,						SCREAMING_SNAKE_CASE_     )

      a	:     Dict							=  self.image_processing_class.from_dict(self.image_processor_dict							,						size=4_2							,						reduce_labels=SCREAMING_SNAKE_CASE_     )
      self.assertEqual(image_processor.size							,						{'height': 4_2, 'width': 4_2}     )
      self.assertEqual(image_processor.do_reduce_labels							,						SCREAMING_SNAKE_CASE_     )





    def 							lowerCamelCase__ (				self      :   List[str]     ):


      '''simple docstring'''




      pass





    def 							lowerCamelCase__ (				self      :   Optional[Any]     ):


      '''simple docstring'''




      a	:     str							=  self.image_processing_class(**self.image_processor_dict     )
      # create random PIL images
      a	:     Optional[Any]							=  prepare_image_inputs(self.image_processor_tester							,						equal_resolution=SCREAMING_SNAKE_CASE_     )
      for image in image_inputs:
        self.assertIsInstance(SCREAMING_SNAKE_CASE_							,						Image.Image     )

      # Test not batched input
      a	:     str							=  image_processing(image_inputs[0]							,						return_tensors='pt'     ).pixel_values
      self.assertEqual(
          encoded_images.shape							,						(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)

      # Test batched
      a	:     str							=  image_processing(SCREAMING_SNAKE_CASE_							,						return_tensors='pt'     ).pixel_values
      self.assertEqual(
          encoded_images.shape							,						(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)





    def 							lowerCamelCase__ (				self      :   Optional[int]     ):


      '''simple docstring'''




      a	:     int							=  self.image_processing_class(**self.image_processor_dict     )
      # create random numpy tensors
      a	:     int							=  prepare_image_inputs(self.image_processor_tester							,						equal_resolution=SCREAMING_SNAKE_CASE_							,						numpify=SCREAMING_SNAKE_CASE_     )
      for image in image_inputs:
        self.assertIsInstance(SCREAMING_SNAKE_CASE_							,						np.ndarray     )

      # Test not batched input
      a	:     List[Any]							=  image_processing(image_inputs[0]							,						return_tensors='pt'     ).pixel_values
      self.assertEqual(
          encoded_images.shape							,						(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)

      # Test batched
      a	:     Union[str, Any]							=  image_processing(SCREAMING_SNAKE_CASE_							,						return_tensors='pt'     ).pixel_values
      self.assertEqual(
          encoded_images.shape							,						(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)





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


      '''simple docstring'''




      a	:     Optional[Any]							=  self.image_processing_class(**self.image_processor_dict     )
      # create random PyTorch tensors
      a	:     Dict							=  prepare_image_inputs(self.image_processor_tester							,						equal_resolution=SCREAMING_SNAKE_CASE_							,						torchify=SCREAMING_SNAKE_CASE_     )
      for image in image_inputs:
        self.assertIsInstance(SCREAMING_SNAKE_CASE_							,						torch.Tensor     )

      # Test not batched input
      a	:     Any							=  image_processing(image_inputs[0]							,						return_tensors='pt'     ).pixel_values
      self.assertEqual(
          encoded_images.shape							,						(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)

      # Test batched
      a	:     Any							=  image_processing(SCREAMING_SNAKE_CASE_							,						return_tensors='pt'     ).pixel_values
      self.assertEqual(
          encoded_images.shape							,						(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)





    def 							lowerCamelCase__ (				self      :   Any     ):


      '''simple docstring'''




      a	:     Union[str, Any]							=  self.image_processing_class(**self.image_processor_dict     )
      # create random PyTorch tensors
      a	:     Union[str, Any]							=  prepare_image_inputs(self.image_processor_tester							,						equal_resolution=SCREAMING_SNAKE_CASE_							,						torchify=SCREAMING_SNAKE_CASE_     )
      a	:     List[Any]							=  []
      for image in image_inputs:
        self.assertIsInstance(SCREAMING_SNAKE_CASE_							,						torch.Tensor     )
        maps.append(torch.zeros(image.shape[-2:]     ).long()     )

      # Test not batched input
      a	:     str							=  image_processing(image_inputs[0]							,						maps[0]							,						return_tensors='pt'     )
      self.assertEqual(
          encoding['pixel_values'].shape							,						(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)
      self.assertEqual(
          encoding['labels'].shape							,						(
              1,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)
      self.assertEqual(encoding['labels'].dtype							,						torch.long     )
      self.assertTrue(encoding['labels'].min().item() >= 0     )
      self.assertTrue(encoding['labels'].max().item() <= 2_5_5     )

      # Test batched
      a	:     Union[str, Any]							=  image_processing(SCREAMING_SNAKE_CASE_							,						SCREAMING_SNAKE_CASE_							,						return_tensors='pt'     )
      self.assertEqual(
          encoding['pixel_values'].shape							,						(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)
      self.assertEqual(
          encoding['labels'].shape							,						(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)
      self.assertEqual(encoding['labels'].dtype							,						torch.long     )
      self.assertTrue(encoding['labels'].min().item() >= 0     )
      self.assertTrue(encoding['labels'].max().item() <= 2_5_5     )

      # Test not batched input (PIL images)
      a,	a	:     List[Any]							=  prepare_semantic_single_inputs()

      a	:     Optional[int]							=  image_processing(SCREAMING_SNAKE_CASE_							,						SCREAMING_SNAKE_CASE_							,						return_tensors='pt'     )
      self.assertEqual(
          encoding['pixel_values'].shape							,						(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)
      self.assertEqual(
          encoding['labels'].shape							,						(
              1,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)
      self.assertEqual(encoding['labels'].dtype							,						torch.long     )
      self.assertTrue(encoding['labels'].min().item() >= 0     )
      self.assertTrue(encoding['labels'].max().item() <= 2_5_5     )

      # Test batched input (PIL images)
      a,	a	:     Dict							=  prepare_semantic_batch_inputs()

      a	:     Optional[int]							=  image_processing(SCREAMING_SNAKE_CASE_							,						SCREAMING_SNAKE_CASE_							,						return_tensors='pt'     )
      self.assertEqual(
          encoding['pixel_values'].shape							,						(
              2,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)
      self.assertEqual(
          encoding['labels'].shape							,						(
              2,
              self.image_processor_tester.size['height'],
              self.image_processor_tester.size['width'],
          )							,						)
      self.assertEqual(encoding['labels'].dtype							,						torch.long     )
      self.assertTrue(encoding['labels'].min().item() >= 0     )
      self.assertTrue(encoding['labels'].max().item() <= 2_5_5     )





    def 							lowerCamelCase__ (				self      :   Optional[int]     ):


      '''simple docstring'''




      a	:     Tuple							=  self.image_processing_class(**self.image_processor_dict     )

      # ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
      a,	a	:     Any							=  prepare_semantic_single_inputs()
      a	:     Tuple							=  image_processing(SCREAMING_SNAKE_CASE_							,						SCREAMING_SNAKE_CASE_							,						return_tensors='pt'     )
      self.assertTrue(encoding['labels'].min().item() >= 0     )
      self.assertTrue(encoding['labels'].max().item() <= 1_5_0     )

      a	:     List[str]							=  True
      a	:     Union[str, Any]							=  image_processing(SCREAMING_SNAKE_CASE_							,						SCREAMING_SNAKE_CASE_							,						return_tensors='pt'     )
      self.assertTrue(encoding['labels'].min().item() >= 0     )
      self.assertTrue(encoding['labels'].max().item() <= 2_5_5     )






import warnings
from dataclasses import dataclass, field
from typing import Optional, Tuple

from .training_args import TrainingArguments
from .utils import cached_property, is_tf_available, logging, requires_backends


lowerCamelCase_				:							Optional[Any]       =    logging.get_logger(__name__)

if is_tf_available():
 import tensorflow as tf







@dataclass
class  _lowerCamelCase			(lowerCamelCase					):
     lowercase__				  =			"""tf"""
     lowercase__				  =			field(
         default=lowerCamelCase							,					metadata={"""help""": """Name of TPU"""}							,					)

     lowercase__				  =			field(
         default=lowerCamelCase							,					metadata={"""help""": """Zone of TPU"""}							,					)

     lowercase__				  =			field(
         default=lowerCamelCase							,					metadata={"""help""": """Name of Cloud TPU-enabled project"""}							,					)

     lowercase__				  =			field(
         default=1.0							,					metadata={"""help""": """Power for the Polynomial decay LR scheduler."""}							,					)

     lowercase__				  =			field(default=lowerCamelCase							,					metadata={"""help""": """Whether to activate the XLA compilation or not"""}					)

     @cached_property
     def 						__lowerCamelCase				(  self    ):
            requires_backends(self							,       ['tf']    )
            logger.info('Tensorflow: setting up strategy'    )

            __snake_case						       =   tf.config.list_physical_devices('GPU'    )

            # Set to float16 at first
            if self.fpaa:
                   tf.keras.mixed_precision.set_global_policy('mixed_float16'    )

            if self.no_cuda:
                   __snake_case						       =   tf.distribute.OneDeviceStrategy(device='/cpu:0'    )
            else:
                   try:
                          if self.tpu_name:
                                 __snake_case						       =   tf.distribute.cluster_resolver.TPUClusterResolver(
                                     self.tpu_name							,       zone=self.tpu_zone							,       project=self.gcp_project    )
                          else:
                                 __snake_case						       =   tf.distribute.cluster_resolver.TPUClusterResolver()
                   except ValueError:
                          if self.tpu_name:
                                 raise RuntimeError(f'''Couldn\'t connect to TPU {self.tpu_name}!'''    )
                          else:
                                 __snake_case						       =   None

                   if tpu:
                          # Set to bfloat16 in case of TPU
                          if self.fpaa:
                                 tf.keras.mixed_precision.set_global_policy('mixed_bfloat16'    )

                          tf.config.experimental_connect_to_cluster(SCREAMING_SNAKE_CASE_    )
                          tf.tpu.experimental.initialize_tpu_system(SCREAMING_SNAKE_CASE_    )

                          __snake_case						       =   tf.distribute.TPUStrategy(SCREAMING_SNAKE_CASE_    )

                   elif len(SCREAMING_SNAKE_CASE_    ) == 0:
                          __snake_case						       =   tf.distribute.OneDeviceStrategy(device='/cpu:0'    )
                   elif len(SCREAMING_SNAKE_CASE_    ) == 1:
                          __snake_case						       =   tf.distribute.OneDeviceStrategy(device='/gpu:0'    )
                   elif len(SCREAMING_SNAKE_CASE_    ) > 1:
                          # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
                          __snake_case						       =   tf.distribute.MirroredStrategy()
                   else:
                          raise ValueError('Cannot find the proper strategy, please check your environment properties.'    )

            return strategy

     @property
     def 						__lowerCamelCase				(  self    ):
            requires_backends(self							,       ['tf']    )
            return self._setup_strategy

     @property
     def 						__lowerCamelCase				(  self    ):
            requires_backends(self							,       ['tf']    )
            return self._setup_strategy.num_replicas_in_sync

     @property
     def 						__lowerCamelCase				(  self    ):
            return False  # TF Logging is handled by Keras not the Trainer

     @property
     def 						__lowerCamelCase				(  self    ):
            if self.per_gpu_train_batch_size:
                   logger.warning(
                       'Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future '
                       'version. Using `--per_device_train_batch_size` is preferred.'    )
            __snake_case						       =   self.per_gpu_train_batch_size or self.per_device_train_batch_size
            return per_device_batch_size * self.n_replicas

     @property
     def 						__lowerCamelCase				(  self    ):
            if self.per_gpu_eval_batch_size:
                   logger.warning(
                       'Using deprecated `--per_gpu_eval_batch_size` argument which will be removed in a future '
                       'version. Using `--per_device_eval_batch_size` is preferred.'    )
            __snake_case						       =   self.per_gpu_eval_batch_size or self.per_device_eval_batch_size
            return per_device_batch_size * self.n_replicas

     @property
     def 						__lowerCamelCase				(  self    ):
            requires_backends(self							,       ['tf']    )
            warnings.warn(
                'The n_gpu argument is deprecated and will be removed in a future version, use n_replicas instead.'							,       SCREAMING_SNAKE_CASE_							,       )
            return self._setup_strategy.num_replicas_in_sync
