


'''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 					snake_case   (   unittest.TestCase      ):





  """simple docstring"""




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







   """simple docstring"""
   lowerCamelCase_								=  size if size is not None else {"shortest_edge": 30}
   lowerCamelCase_								=  parent
   lowerCamelCase_								=  batch_size
   lowerCamelCase_								=  num_channels
   lowerCamelCase_								=  image_size
   lowerCamelCase_								=  min_resolution
   lowerCamelCase_								=  max_resolution
   lowerCamelCase_								=  do_resize
   lowerCamelCase_								=  size
   lowerCamelCase_								=  size_divisor
   lowerCamelCase_								=  do_normalize
   lowerCamelCase_								=  image_mean
   lowerCamelCase_								=  image_std






  def   snake_case		(   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   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase=False					):







   """simple docstring"""
   if not batched:
    lowerCamelCase_								=  self.size["shortest_edge"]
    lowerCamelCase_								=  image_inputs[0]
    if isinstance(UpperCamelCase      ,  Image.Image					):
     lowerCamelCase_			,lowerCamelCase_								=  image.size
    else:
     lowerCamelCase_			,lowerCamelCase_								=  image.shape[1], image.shape[2]
    lowerCamelCase_								=  size / min(UpperCamelCase      ,  UpperCamelCase					)
    if h < w:
     lowerCamelCase_			,lowerCamelCase_								=  size, scale * w
    else:
     lowerCamelCase_			,lowerCamelCase_								=  scale * h, size

    lowerCamelCase_								=  int((1333 / 800) * size					)
    if max(UpperCamelCase      ,  UpperCamelCase					) > max_size:
     lowerCamelCase_								=  max_size / max(UpperCamelCase      ,  UpperCamelCase					)
     lowerCamelCase_								=  newh * scale
     lowerCamelCase_								=  neww * scale

    lowerCamelCase_			,lowerCamelCase_								=  int(newh + 0.5					), int(neww + 0.5					)
    lowerCamelCase_			,lowerCamelCase_								=  (
        newh // self.size_divisor * self.size_divisor,
        neww // self.size_divisor * self.size_divisor,
    )

   else:
    lowerCamelCase_								=  []
    for image in image_inputs:
     lowerCamelCase_			,lowerCamelCase_								=  self.get_expected_values([image]					)
     expected_values.append((expected_height, expected_width)					)
    lowerCamelCase_								=  max(UpperCamelCase      ,  key=lambda UpperCamelCase					: item[0]					)[0]
    lowerCamelCase_								=  max(UpperCamelCase      ,  key=lambda UpperCamelCase					: item[1]					)[1]

   return expected_height, expected_width





@require_torch
@require_vision
class 					snake_case   (   lowercase      ,						unittest.TestCase      ):





  """simple docstring"""




  _lowerCamelCase		   =  ViltImageProcessor if is_vision_available() else None






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  ViltImageProcessingTester(self					)






  @property
  def   snake_case		(   self					):







   """simple docstring"""
   return self.image_processor_tester.prepare_image_processor_dict()






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.image_processing_class(**self.image_processor_dict					)
   self.assertTrue(hasattr(UpperCamelCase      ,  "image_mean"					)					)
   self.assertTrue(hasattr(UpperCamelCase      ,  "image_std"					)					)
   self.assertTrue(hasattr(UpperCamelCase      ,  "do_normalize"					)					)
   self.assertTrue(hasattr(UpperCamelCase      ,  "do_resize"					)					)
   self.assertTrue(hasattr(UpperCamelCase      ,  "size"					)					)
   self.assertTrue(hasattr(UpperCamelCase      ,  "size_divisor"					)					)






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.image_processing_class.from_dict(self.image_processor_dict					)
   self.assertEqual(image_processor.size      ,  {"shortest_edge": 30}					)

   lowerCamelCase_								=  self.image_processing_class.from_dict(self.image_processor_dict      ,  size=42					)
   self.assertEqual(image_processor.size      ,  {"shortest_edge": 42}					)






  def   snake_case		(   self					):







   """simple docstring"""
   pass






  def   snake_case		(   self					):







   """simple docstring"""
   # Initialize image_processing
   lowerCamelCase_								=  self.image_processing_class(**self.image_processor_dict					)
   # create random PIL images
   lowerCamelCase_								=  prepare_image_inputs(self.image_processor_tester      ,  equal_resolution=UpperCamelCase					)
   for image in image_inputs:
    self.assertIsInstance(UpperCamelCase      ,  Image.Image					)

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

   lowerCamelCase_			,lowerCamelCase_								=  self.image_processor_tester.get_expected_values(UpperCamelCase					)
   self.assertEqual(
       encoded_images.shape      ,  (1, self.image_processor_tester.num_channels, expected_height, expected_width)      ,  )

   # Test batched
   lowerCamelCase_								=  image_processing(UpperCamelCase      ,  return_tensors="pt"					).pixel_values

   lowerCamelCase_			,lowerCamelCase_								=  self.image_processor_tester.get_expected_values(UpperCamelCase      ,  batched=UpperCamelCase					)
   self.assertEqual(
       encoded_images.shape      ,  (
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_channels,
           expected_height,
           expected_width,
       )      ,  )






  def   snake_case		(   self					):







   """simple docstring"""
   # Initialize image_processing
   lowerCamelCase_								=  self.image_processing_class(**self.image_processor_dict					)
   # create random numpy tensors
   lowerCamelCase_								=  prepare_image_inputs(self.image_processor_tester      ,  equal_resolution=UpperCamelCase      ,  numpify=UpperCamelCase					)
   for image in image_inputs:
    self.assertIsInstance(UpperCamelCase      ,  np.ndarray					)

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

   lowerCamelCase_			,lowerCamelCase_								=  self.image_processor_tester.get_expected_values(UpperCamelCase					)
   self.assertEqual(
       encoded_images.shape      ,  (1, self.image_processor_tester.num_channels, expected_height, expected_width)      ,  )

   # Test batched
   lowerCamelCase_								=  image_processing(UpperCamelCase      ,  return_tensors="pt"					).pixel_values

   lowerCamelCase_			,lowerCamelCase_								=  self.image_processor_tester.get_expected_values(UpperCamelCase      ,  batched=UpperCamelCase					)
   self.assertEqual(
       encoded_images.shape      ,  (
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_channels,
           expected_height,
           expected_width,
       )      ,  )







  def   snake_case		(   self					):







   """simple docstring"""
   # Initialize image_processing
   lowerCamelCase_								=  self.image_processing_class(**self.image_processor_dict					)
   # create random PyTorch tensors
   lowerCamelCase_								=  prepare_image_inputs(self.image_processor_tester      ,  equal_resolution=UpperCamelCase      ,  torchify=UpperCamelCase					)
   for image in image_inputs:
    self.assertIsInstance(UpperCamelCase      ,  torch.Tensor					)

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

   lowerCamelCase_			,lowerCamelCase_								=  self.image_processor_tester.get_expected_values(UpperCamelCase					)
   self.assertEqual(
       encoded_images.shape      ,  (1, self.image_processor_tester.num_channels, expected_height, expected_width)      ,  )

   # Test batched
   lowerCamelCase_								=  image_processing(UpperCamelCase      ,  return_tensors="pt"					).pixel_values

   lowerCamelCase_			,lowerCamelCase_								=  self.image_processor_tester.get_expected_values(UpperCamelCase      ,  batched=UpperCamelCase					)
   self.assertEqual(
       encoded_images.shape      ,  (
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_channels,
           expected_height,
           expected_width,
       )      ,  )







'''simple docstring'''

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 					snake_case   (   unittest.TestCase      ):





  """simple docstring"""




  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  tf.constant([-100, -1.0, -0.1, 0, 0.1, 1.0, 100.0]					)
   lowerCamelCase_								=  get_tf_activation("gelu"					)
   lowerCamelCase_								=  get_tf_activation("gelu_10"					)

   lowerCamelCase_								=  gelu(UpperCamelCase					)
   lowerCamelCase_								=  geluaa(UpperCamelCase					)

   lowerCamelCase_								=  tf.where(y_gelu_aa < 10.0      ,  1.0      ,  0.0					)

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







  def   snake_case		(   self					):







   """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(UpperCamelCase					):
    get_tf_activation("bogus"					)
   with self.assertRaises(UpperCamelCase					):
    get_tf_activation(UpperCamelCase					)



