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 PoolFormerImageProcessor




class 	__snake_case	( unittest.TestCase		):





      '''simple docstring'''






      def __init__(					self     :				int    ,  _UpperCamelCase     :				str    ,  _UpperCamelCase     :				Tuple=7    ,  _UpperCamelCase     :				Tuple=3    ,  _UpperCamelCase     :				str=30    ,  _UpperCamelCase     :				Any=400    ,  _UpperCamelCase     :				List[Any]=True    ,  _UpperCamelCase     :				Optional[int]=None    ,  _UpperCamelCase     :				Dict=0.9    ,  _UpperCamelCase     :				Any=None    ,  _UpperCamelCase     :				Optional[int]=True    ,  _UpperCamelCase     :				Dict=[0.5, 0.5, 0.5]    ,  _UpperCamelCase     :				List[Any]=[0.5, 0.5, 0.5]    ,  )				->str:


          """simple docstring"""


          _lowerCamelCase     :		Optional[Any] 						=						size if size is not None else {'''shortest_edge''': 30}
          _lowerCamelCase     :		Union[str, Any] 						=						crop_size if crop_size is not None else {'''height''': 30, '''width''': 30}
          _lowerCamelCase     :		List[str] 						=						parent
          _lowerCamelCase     :		Optional[Any] 						=						batch_size
          _lowerCamelCase     :		str 						=						num_channels
          _lowerCamelCase     :		Any 						=						min_resolution
          _lowerCamelCase     :		Optional[int] 						=						max_resolution
          _lowerCamelCase     :		Dict 						=						do_resize_and_center_crop
          _lowerCamelCase     :		Optional[Any] 						=						size
          _lowerCamelCase     :		List[Any] 						=						crop_pct
          _lowerCamelCase     :		List[str] 						=						crop_size
          _lowerCamelCase     :		List[str] 						=						do_normalize
          _lowerCamelCase     :		Dict 						=						image_mean
          _lowerCamelCase     :		Tuple 						=						image_std




      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any)				->Any:


          """simple docstring"""


          return {
              "size": self.size,
              "do_resize_and_center_crop": self.do_resize_and_center_crop,
              "crop_pct": self.crop_pct,
              "crop_size": self.crop_size,
              "do_normalize": self.do_normalize,
              "image_mean": self.image_mean,
              "image_std": self.image_std,
          }





@require_torch
@require_vision
class 	__snake_case	( UpperCamelCase_ ,  unittest.TestCase		):





      '''simple docstring'''






      _snake_case    		=       PoolFormerImageProcessor if is_vision_available() else None



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any)				->Dict:


          """simple docstring"""


          _lowerCamelCase     :		Any 						=						PoolFormerImageProcessingTester(self)



      @property
      def 	_SCREAMING_SNAKE_CASE    (					self     :				Tuple)				->Optional[int]:


          """simple docstring"""


          return self.image_processor_tester.prepare_image_processor_dict()



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any])				->List[Any]:


          """simple docstring"""


          _lowerCamelCase     :		Tuple 						=						self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(_UpperCamelCase    ,  """do_resize_and_center_crop"""))
          self.assertTrue(hasattr(_UpperCamelCase    ,  """size"""))
          self.assertTrue(hasattr(_UpperCamelCase    ,  """crop_pct"""))
          self.assertTrue(hasattr(_UpperCamelCase    ,  """do_normalize"""))
          self.assertTrue(hasattr(_UpperCamelCase    ,  """image_mean"""))
          self.assertTrue(hasattr(_UpperCamelCase    ,  """image_std"""))



      def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->List[str]:


          """simple docstring"""


          _lowerCamelCase     :		int 						=						self.image_processing_class.from_dict(self.image_processor_dict)
          self.assertEqual(image_processor.size    ,  {"""shortest_edge""": 30})
          self.assertEqual(image_processor.crop_size    ,  {"""height""": 30, """width""": 30})

          _lowerCamelCase     :		Any 						=						self.image_processing_class.from_dict(self.image_processor_dict    ,  size=42    ,  crop_size=84)
          self.assertEqual(image_processor.size    ,  {"""shortest_edge""": 42})
          self.assertEqual(image_processor.crop_size    ,  {"""height""": 84, """width""": 84})



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Dict)				->Optional[int]:


          """simple docstring"""


          pass



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any)				->str:


          """simple docstring"""


          _lowerCamelCase     :		Optional[Any] 						=						self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          _lowerCamelCase     :		Dict 						=						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     :		Optional[int] 						=						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.crop_size["""height"""],
                  self.image_processor_tester.crop_size["""width"""],
              )    ,  )

          # Test batched
          _lowerCamelCase     :		List[Any] 						=						image_processing(_UpperCamelCase    ,  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.crop_size["""height"""],
                  self.image_processor_tester.crop_size["""width"""],
              )    ,  )



      def 	_SCREAMING_SNAKE_CASE    (					self     :				str)				->List[str]:


          """simple docstring"""


          _lowerCamelCase     :		Tuple 						=						self.image_processing_class(**self.image_processor_dict)
          # create random numpy tensors
          _lowerCamelCase     :		str 						=						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     :		List[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.crop_size["""height"""],
                  self.image_processor_tester.crop_size["""width"""],
              )    ,  )

          # Test batched
          _lowerCamelCase     :		int 						=						image_processing(_UpperCamelCase    ,  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.crop_size["""height"""],
                  self.image_processor_tester.crop_size["""width"""],
              )    ,  )




      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any])				->Tuple:


          """simple docstring"""


          _lowerCamelCase     :		Optional[int] 						=						self.image_processing_class(**self.image_processor_dict)
          # create random PyTorch tensors
          _lowerCamelCase     :		Optional[Any] 						=						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     :		int 						=						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.crop_size["""height"""],
                  self.image_processor_tester.crop_size["""width"""],
              )    ,  )

          # Test batched
          _lowerCamelCase     :		Any 						=						image_processing(_UpperCamelCase    ,  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.crop_size["""height"""],
                  self.image_processor_tester.crop_size["""width"""],
              )    ,  )





import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)




class 	__snake_case	( unittest.TestCase		):





      '''simple docstring'''






      def 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any])				->Optional[Any]:


          """simple docstring"""


          _lowerCamelCase     :		Tuple 						=						["""a""", """b""", """c"""]

          # Defaults to last layer if both are None
          _lowerCamelCase						,							_lowerCamelCase     :		int 						=						get_aligned_output_features_output_indices(_UpperCamelCase    ,  _UpperCamelCase    ,  _UpperCamelCase)
          self.assertEqual(_UpperCamelCase    ,  ["""c"""])
          self.assertEqual(_UpperCamelCase    ,  [2])

          # Out indices set to match out features
          _lowerCamelCase						,							_lowerCamelCase     :		int 						=						get_aligned_output_features_output_indices(["""a""", """c"""]    ,  _UpperCamelCase    ,  _UpperCamelCase)
          self.assertEqual(_UpperCamelCase    ,  ["""a""", """c"""])
          self.assertEqual(_UpperCamelCase    ,  [0, 2])

          # Out features set to match out indices
          _lowerCamelCase						,							_lowerCamelCase     :		Tuple 						=						get_aligned_output_features_output_indices(_UpperCamelCase    ,  [0, 2]    ,  _UpperCamelCase)
          self.assertEqual(_UpperCamelCase    ,  ["""a""", """c"""])
          self.assertEqual(_UpperCamelCase    ,  [0, 2])

          # Out features selected from negative indices
          _lowerCamelCase						,							_lowerCamelCase     :		str 						=						get_aligned_output_features_output_indices(_UpperCamelCase    ,  [-3, -1]    ,  _UpperCamelCase)
          self.assertEqual(_UpperCamelCase    ,  ["""a""", """c"""])
          self.assertEqual(_UpperCamelCase    ,  [-3, -1])



      def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->int:


          """simple docstring"""


          with self.assertRaises(_UpperCamelCase):
              verify_out_features_out_indices(["""a""", """b"""]    ,  (0, 1)    ,  _UpperCamelCase)

          # Out features must be a list
          with self.assertRaises(_UpperCamelCase):
              verify_out_features_out_indices(("""a""", """b""")    ,  (0, 1)    ,  ["""a""", """b"""])

          # Out features must be a subset of stage names
          with self.assertRaises(_UpperCamelCase):
              verify_out_features_out_indices(["""a""", """b"""]    ,  (0, 1)    ,  ["""a"""])

          # Out indices must be a list or tuple
          with self.assertRaises(_UpperCamelCase):
              verify_out_features_out_indices(_UpperCamelCase    ,  0    ,  ["""a""", """b"""])

          # Out indices must be a subset of stage names
          with self.assertRaises(_UpperCamelCase):
              verify_out_features_out_indices(_UpperCamelCase    ,  (0, 1)    ,  ["""a"""])

          # Out features and out indices must be the same length
          with self.assertRaises(_UpperCamelCase):
              verify_out_features_out_indices(["""a""", """b"""]    ,  (0,)    ,  ["""a""", """b""", """c"""])

          # Out features should match out indices
          with self.assertRaises(_UpperCamelCase):
              verify_out_features_out_indices(["""a""", """b"""]    ,  (0, 2)    ,  ["""a""", """b""", """c"""])

          # Out features and out indices should be in order
          with self.assertRaises(_UpperCamelCase):
              verify_out_features_out_indices(["""b""", """a"""]    ,  (0, 1)    ,  ["""a""", """b"""])

          # Check passes with valid inputs
          verify_out_features_out_indices(["""a""", """b""", """d"""]    ,  (0, 1, -1)    ,  ["""a""", """b""", """c""", """d"""])




      def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->List[Any]:


          """simple docstring"""


          _lowerCamelCase     :		int 						=						BackboneMixin()

          _lowerCamelCase     :		Union[str, Any] 						=						["""a""", """b""", """c"""]
          _lowerCamelCase     :		Tuple 						=						["""a""", """c"""]
          _lowerCamelCase     :		List[Any] 						=						[0, 2]

          # Check that the output features and indices are set correctly
          self.assertEqual(backbone.out_features    ,  ["""a""", """c"""])
          self.assertEqual(backbone.out_indices    ,  [0, 2])

          # Check out features and indices are updated correctly
          _lowerCamelCase     :		str 						=						["""a""", """b"""]
          self.assertEqual(backbone.out_features    ,  ["""a""", """b"""])
          self.assertEqual(backbone.out_indices    ,  [0, 1])

          _lowerCamelCase     :		Optional[int] 						=						[-3, -1]
          self.assertEqual(backbone.out_features    ,  ["""a""", """c"""])
          self.assertEqual(backbone.out_indices    ,  [-3, -1])




