






'''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 PoolFormerImageProcessor
class UpperCAmelCase_					(    unittest.TestCase				):




      """simple docstring"""




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


       '''simple docstring'''

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






      def 				A__      ( self			:		Optional[Any]		)  ->  List[str]:


       '''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 UpperCAmelCase_					(    __A				,       unittest.TestCase				):




      """simple docstring"""




      UpperCamelCase_								=			PoolFormerImageProcessor if is_vision_available() else None






      def 				A__      ( self			:		List[str]		)  ->  List[Any]:


       '''simple docstring'''

       lowercase					:   Tuple				  =PoolFormerImageProcessingTester(self		)






      @property
      def 				A__      ( self			:		Union[str, Any]		)  ->  int:


       '''simple docstring'''

       return self.image_processor_tester.prepare_image_processor_dict()






      def 				A__      ( self			:		List[str]		)  ->  Any:


       '''simple docstring'''

       lowercase					:   Optional[int]				  =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 				A__      ( self			:		Tuple		)  ->  Optional[Any]:


       '''simple docstring'''

       lowercase					:   List[str]				  =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}		)

       lowercase					:   Union[str, 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 				A__      ( self			:		Union[str, Any]		)  ->  Tuple:


       '''simple docstring'''

       pass






      def 				A__      ( self			:		Optional[Any]		)  ->  str:


       '''simple docstring'''

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

       # Test not batched input
       lowercase					:   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.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )     ,      )

       # Test batched
       lowercase					:   str				  =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 				A__      ( self			:		Optional[int]		)  ->  List[str]:


       '''simple docstring'''

       lowercase					:   List[str]				  =self.image_processing_class(**self.image_processor_dict		)
       # create random numpy tensors
       lowercase					:   Union[str, Any]				  =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
       lowercase					:   Optional[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.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )     ,      )

       # Test batched
       lowercase					:   Optional[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 				A__      ( self			:		Dict		)  ->  int:


       '''simple docstring'''

       lowercase					:   Tuple				  =self.image_processing_class(**self.image_processor_dict		)
       # create random PyTorch tensors
       lowercase					:   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
       lowercase					:   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.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )     ,      )

       # Test batched
       lowercase					:   Optional[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'''],
           )     ,      )











'''simple docstring'''



import importlib.metadata
from typing import Union

from packaging.version import Version, parse

from .constants import STR_OPERATION_TO_FUNC


SCREAMING_SNAKE_CASE						=	parse(importlib.metadata.version('torch'))


def 							lowercase_				(    __A :       Union[str, Version]					,      __A :       str					,      __A :       str			)       ->      Union[str, Any]:



 """simple docstring"""




 if operation not in STR_OPERATION_TO_FUNC.keys():
  raise ValueError(F'`operation` must be one of {list(STR_OPERATION_TO_FUNC.keys()			)}, received {operation}'			)
 lowercase					:   Any				  =STR_OPERATION_TO_FUNC[operation]
 if isinstance(__A					,      __A			):
  lowercase					:   List[Any]				  =parse(importlib.metadata.version(__A			)			)
 return operation(__A					,      parse(__A			)			)





def 							lowercase_				(    __A :       str					,      __A :       str			)       ->      Tuple:



 """simple docstring"""




 return compare_versions(__A					,      __A					,      __A			)



