




'''simple docstring'''





import unittest

import numpy as np

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 ViltImageProcessor




class      UpperCamelCase       (	unittest.TestCase       ):



  def __init__(			self		,     __lowerCAmelCase		,     __lowerCAmelCase=7		,     __lowerCAmelCase=3		,     __lowerCAmelCase=18		,     __lowerCAmelCase=30		,     __lowerCAmelCase=400		,     __lowerCAmelCase=True		,     __lowerCAmelCase=None		,     __lowerCAmelCase=2		,     __lowerCAmelCase=True		,     __lowerCAmelCase=[0.5, 0.5, 0.5]		,     __lowerCAmelCase=[0.5, 0.5, 0.5]		,     ):







      '''simple docstring'''


      _a				:							Tuple							       =      size if size is not None else {"""shortest_edge""": 30}
      _a				:							Optional[int]							       =      parent
      _a				:							Union[str, Any]							       =      batch_size
      _a				:							int							       =      num_channels
      _a				:							List[str]							       =      image_size
      _a				:							str							       =      min_resolution
      _a				:							Union[str, Any]							       =      max_resolution
      _a				:							Tuple							       =      do_resize
      _a				:							str							       =      size
      _a				:							Any							       =      size_divisor
      _a				:							List[str]							       =      do_normalize
      _a				:							Tuple							       =      image_mean
      _a				:							str							       =      image_std



  def     a_							(			self ):







      '''simple docstring'''


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



  def     a_							(			self		,     __lowerCAmelCase		,     __lowerCAmelCase=False ):







      '''simple docstring'''


      if not batched:
          _a				:							List[Any]							       =      self.size["""shortest_edge"""]
          _a				:							Optional[int]							       =      image_inputs[0]
          if isinstance(__lowerCAmelCase		,     Image.Image ):
              _a       , _a				:							Any							       =      image.size
          else:
              _a       , _a				:							Any							       =      image.shape[1], image.shape[2]
          _a				:							int							       =      size / min(__lowerCAmelCase		,     __lowerCAmelCase )
          if h < w:
              _a       , _a				:							str							       =      size, scale * w
          else:
              _a       , _a				:							Any							       =      scale * h, size

          _a				:							List[Any]							       =      int((1333 / 800) * size )
          if max(__lowerCAmelCase		,     __lowerCAmelCase ) > max_size:
              _a				:							Optional[Any]							       =      max_size / max(__lowerCAmelCase		,     __lowerCAmelCase )
              _a				:							Optional[int]							       =      newh * scale
              _a				:							List[str]							       =      neww * scale

          _a       , _a				:							int							       =      int(newh + 0.5 ), int(neww + 0.5 )
          _a       , _a				:							Dict							       =      (
              newh // self.size_divisor * self.size_divisor,
              neww // self.size_divisor * self.size_divisor,
          )

      else:
          _a				:							int							       =      []
          for image in image_inputs:
              _a       , _a				:							Any							       =      self.get_expected_values([image] )
              expected_values.append((expected_height, expected_width) )
          _a				:							Tuple							       =      max(__lowerCAmelCase		,     key=lambda __lowerCAmelCase : item[0] )[0]
          _a				:							List[str]							       =      max(__lowerCAmelCase		,     key=lambda __lowerCAmelCase : item[1] )[1]

      return expected_height, expected_width






@require_torch
@require_vision
class      UpperCamelCase       (	snake_case__      ,		unittest.TestCase       ):
  a     :Any          =					ViltImageProcessor if is_vision_available() else None



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Dict							       =      ViltImageProcessingTester(self )



  @property
  def     a_							(			self ):







      '''simple docstring'''


      return self.image_processor_tester.prepare_image_processor_dict()



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Tuple							       =      self.image_processing_class(**self.image_processor_dict )
      self.assertTrue(hasattr(__lowerCAmelCase		,     """image_mean""" ) )
      self.assertTrue(hasattr(__lowerCAmelCase		,     """image_std""" ) )
      self.assertTrue(hasattr(__lowerCAmelCase		,     """do_normalize""" ) )
      self.assertTrue(hasattr(__lowerCAmelCase		,     """do_resize""" ) )
      self.assertTrue(hasattr(__lowerCAmelCase		,     """size""" ) )
      self.assertTrue(hasattr(__lowerCAmelCase		,     """size_divisor""" ) )



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Any							       =      self.image_processing_class.from_dict(self.image_processor_dict )
      self.assertEqual(image_processor.size		,     {"""shortest_edge""": 30} )

      _a				:							List[Any]							       =      self.image_processing_class.from_dict(self.image_processor_dict		,     size=42 )
      self.assertEqual(image_processor.size		,     {"""shortest_edge""": 42} )



  def     a_							(			self ):







      '''simple docstring'''


      pass



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Optional[int]							       =      self.image_processing_class(**self.image_processor_dict )
      # create random PIL images
      _a				:							int							       =      prepare_image_inputs(self.image_processor_tester		,     equal_resolution=__lowerCAmelCase )
      for image in image_inputs:
          self.assertIsInstance(__lowerCAmelCase		,     Image.Image )

      # Test not batched input
      _a				:							str							       =      image_processing(image_inputs[0]		,     return_tensors="""pt""" ).pixel_values

      _a       , _a				:							int							       =      self.image_processor_tester.get_expected_values(__lowerCAmelCase )
      self.assertEqual(
          encoded_images.shape		,     (1, self.image_processor_tester.num_channels, expected_height, expected_width)		,     )

      # Test batched
      _a				:							Tuple							       =      image_processing(__lowerCAmelCase		,     return_tensors="""pt""" ).pixel_values

      _a       , _a				:							int							       =      self.image_processor_tester.get_expected_values(__lowerCAmelCase		,     batched=__lowerCAmelCase )
      self.assertEqual(
          encoded_images.shape		,     (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              expected_height,
              expected_width,
          )		,     )



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Union[str, Any]							       =      self.image_processing_class(**self.image_processor_dict )
      # create random numpy tensors
      _a				:							Union[str, Any]							       =      prepare_image_inputs(self.image_processor_tester		,     equal_resolution=__lowerCAmelCase		,     numpify=__lowerCAmelCase )
      for image in image_inputs:
          self.assertIsInstance(__lowerCAmelCase		,     np.ndarray )

      # Test not batched input
      _a				:							int							       =      image_processing(image_inputs[0]		,     return_tensors="""pt""" ).pixel_values

      _a       , _a				:							List[Any]							       =      self.image_processor_tester.get_expected_values(__lowerCAmelCase )
      self.assertEqual(
          encoded_images.shape		,     (1, self.image_processor_tester.num_channels, expected_height, expected_width)		,     )

      # Test batched
      _a				:							List[Any]							       =      image_processing(__lowerCAmelCase		,     return_tensors="""pt""" ).pixel_values

      _a       , _a				:							List[str]							       =      self.image_processor_tester.get_expected_values(__lowerCAmelCase		,     batched=__lowerCAmelCase )
      self.assertEqual(
          encoded_images.shape		,     (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              expected_height,
              expected_width,
          )		,     )



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							str							       =      self.image_processing_class(**self.image_processor_dict )
      # create random PyTorch tensors
      _a				:							List[Any]							       =      prepare_image_inputs(self.image_processor_tester		,     equal_resolution=__lowerCAmelCase		,     torchify=__lowerCAmelCase )
      for image in image_inputs:
          self.assertIsInstance(__lowerCAmelCase		,     torch.Tensor )

      # Test not batched input
      _a				:							Optional[int]							       =      image_processing(image_inputs[0]		,     return_tensors="""pt""" ).pixel_values

      _a       , _a				:							Any							       =      self.image_processor_tester.get_expected_values(__lowerCAmelCase )
      self.assertEqual(
          encoded_images.shape		,     (1, self.image_processor_tester.num_channels, expected_height, expected_width)		,     )

      # Test batched
      _a				:							Optional[int]							       =      image_processing(__lowerCAmelCase		,     return_tensors="""pt""" ).pixel_values

      _a       , _a				:							List[Any]							       =      self.image_processor_tester.get_expected_values(__lowerCAmelCase		,     batched=__lowerCAmelCase )
      self.assertEqual(
          encoded_images.shape		,     (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              expected_height,
              expected_width,
          )		,     )






import unittest

import numpy as np

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
       import tensorflow as tf

       from transformers.activations_tf import get_tf_activation





@require_tf
class 			lowerCAmelCase__(     unittest.TestCase    ):






    '''simple docstring'''





    def        _lowerCamelCase			(						self					: Optional[int]						):





       '''simple docstring'''
       UpperCAmelCase_				:   str							= tf.constant([-100, -1.0, -0.1, 0, 0.1, 1.0, 100.0]						)
       UpperCAmelCase_				:   List[Any]							= get_tf_activation('''gelu'''						)
       UpperCAmelCase_				:   Any							= get_tf_activation('''gelu_10'''						)

       UpperCAmelCase_				:   int							= gelu(__snake_case						)
       UpperCAmelCase_				:   Union[str, Any]							= geluaa(__snake_case						)

       UpperCAmelCase_				:   Union[str, Any]							= tf.where(y_gelu_aa < 10.0							, 1.0							, 0.0						)

       self.assertEqual(tf.math.reduce_max(__snake_case						).numpy().item()							, 10.0						)
       self.assertTrue(np.allclose(y_gelu * clipped_mask							, y_gelu_aa * clipped_mask						)						)





    def        _lowerCamelCase			(						self					: Dict						):





       '''simple docstring'''
       get_tf_activation('''gelu'''						)
       get_tf_activation('''gelu_10'''						)
       get_tf_activation('''gelu_fast'''						)
       get_tf_activation('''gelu_new'''						)
       get_tf_activation('''glu'''						)
       get_tf_activation('''mish'''						)
       get_tf_activation('''quick_gelu'''						)
       get_tf_activation('''relu'''						)
       get_tf_activation('''sigmoid'''						)
       get_tf_activation('''silu'''						)
       get_tf_activation('''swish'''						)
       get_tf_activation('''tanh'''						)
       with self.assertRaises(__snake_case						):
          get_tf_activation('''bogus'''						)
       with self.assertRaises(__snake_case						):
          get_tf_activation(__snake_case						)