
"""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


if is_torch_available():
       import torch

if is_vision_available():
       from PIL import Image

       from transformers import SwinaSRImageProcessor
       from transformers.image_transforms import get_image_size



class 		UpperCAmelCase_	(unittest.TestCase						):
 """simple docstring"""




 def __init__( self       : Dict     ,   a_       : str     ,   a_       : List[str]=7     ,   a_       : List[str]=3     ,   a_       : Tuple=18     ,   a_       : Union[str, Any]=30     ,   a_       : str=4_00     ,   a_       : Optional[Any]=True     ,   a_       : Union[str, Any]=1 / 2_55     ,   a_       : Optional[Any]=True     ,   a_       : int=8     ,   )->					Union[str, Any]:



      """simple docstring"""

      UpperCAmelCase_						:    int    					=  parent
      UpperCAmelCase_						:    str    					=  batch_size
      UpperCAmelCase_						:    List[Any]    					=  num_channels
      UpperCAmelCase_						:    Tuple    					=  image_size
      UpperCAmelCase_						:    List[str]    					=  min_resolution
      UpperCAmelCase_						:    Optional[int]    					=  max_resolution
      UpperCAmelCase_						:    Any    					=  do_rescale
      UpperCAmelCase_						:    str    					=  rescale_factor
      UpperCAmelCase_						:    Dict    					=  do_pad
      UpperCAmelCase_						:    Union[str, Any]    					=  pad_size




 def   a      ( self       : int )->					Dict:



      """simple docstring"""

      return {
          "do_rescale": self.do_rescale,
          "rescale_factor": self.rescale_factor,
          "do_pad": self.do_pad,
          "pad_size": self.pad_size,
      }






 def   a      ( self       : Any     ,   a_       : List[str]=False     ,   a_       : List[str]=False     ,   a_       : Union[str, Any]=False )->					Any:



      """simple docstring"""

      assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

      if equal_resolution:
           UpperCAmelCase_						:    str    					=  []
           for i in range(self.batch_size ):
                image_inputs.append(
                    np.random.randint(
                        2_55     ,   size=(self.num_channels, self.max_resolution, self.max_resolution)     ,   dtype=np.uinta ) )
      else:
           UpperCAmelCase_						:    str    					=  []
           for i in range(self.batch_size ):
                UpperCAmelCase_					,UpperCAmelCase_						:    Optional[Any]    					=  np.random.choice(np.arange(self.min_resolution     ,   self.max_resolution )     ,   2 )
                image_inputs.append(np.random.randint(2_55     ,   size=(self.num_channels, width, height)     ,   dtype=np.uinta ) )

      if not numpify and not torchify:
           # PIL expects the channel dimension as last dimension
           UpperCAmelCase_						:    Optional[int]    					=  [Image.fromarray(np.moveaxis(a_     ,   0     ,   -1 ) ) for x in image_inputs]

      if torchify:
           UpperCAmelCase_						:    Optional[Any]    					=  [torch.from_numpy(a_ ) for x in image_inputs]

      return image_inputs



@require_torch
@require_vision
class 		UpperCAmelCase_	(lowerCamelCase_  , unittest.TestCase						):
 """simple docstring"""




 UpperCamelCase_						:     List[Any]				   =    SwinaSRImageProcessor if is_vision_available() else None




 def   a      ( self       : Dict )->					str:



      """simple docstring"""

      UpperCAmelCase_						:    Union[str, Any]    					=  SwinaSRImageProcessingTester(self )




 @property
 def   a      ( self       : Union[str, Any] )->					Tuple:



      """simple docstring"""

      return self.image_processor_tester.prepare_image_processor_dict()




 def   a      ( self       : Tuple )->					Any:



      """simple docstring"""

      UpperCAmelCase_						:    Optional[int]    					=  self.image_processing_class(**self.image_processor_dict )
      self.assertTrue(hasattr(a_     ,   """do_rescale""" ) )
      self.assertTrue(hasattr(a_     ,   """rescale_factor""" ) )
      self.assertTrue(hasattr(a_     ,   """do_pad""" ) )
      self.assertTrue(hasattr(a_     ,   """pad_size""" ) )




 def   a      ( self       : Tuple )->					Optional[Any]:



      """simple docstring"""

      pass




 def   a      ( self       : str     ,   a_       : Union[str, Any] )->					Tuple:



      """simple docstring"""

      UpperCAmelCase_					,UpperCAmelCase_						:    List[str]    					=  get_image_size(a_ )
      UpperCAmelCase_						:    Any    					=  self.image_processor_tester.pad_size

      UpperCAmelCase_						:    List[str]    					=  (old_height // size + 1) * size - old_height
      UpperCAmelCase_						:    List[Any]    					=  (old_width // size + 1) * size - old_width
      return old_height + pad_height, old_width + pad_width




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



      """simple docstring"""

      UpperCAmelCase_						:    Optional[Any]    					=  self.image_processing_class(**self.image_processor_dict )
      # create random PIL images
      UpperCAmelCase_						:    Any    					=  self.image_processor_tester.prepare_inputs(equal_resolution=a_ )
      for image in image_inputs:
           self.assertIsInstance(a_     ,   Image.Image )

      # Test not batched input
      UpperCAmelCase_						:    Optional[Any]    					=  image_processor(image_inputs[0]     ,   return_tensors="""pt""" ).pixel_values
      UpperCAmelCase_					,UpperCAmelCase_						:    int    					=  self.calculate_expected_size(np.array(image_inputs[0] ) )
      self.assertEqual(
          encoded_images.shape     ,   (
              1,
              self.image_processor_tester.num_channels,
              expected_height,
              expected_width,
          )     ,   )




 def   a      ( self       : Dict )->					Dict:



      """simple docstring"""

      UpperCAmelCase_						:    Any    					=  self.image_processing_class(**self.image_processor_dict )
      # create random numpy tensors
      UpperCAmelCase_						:    str    					=  self.image_processor_tester.prepare_inputs(equal_resolution=a_     ,   numpify=a_ )
      for image in image_inputs:
           self.assertIsInstance(a_     ,   np.ndarray )

      # Test not batched input
      UpperCAmelCase_						:    Any    					=  image_processor(image_inputs[0]     ,   return_tensors="""pt""" ).pixel_values
      UpperCAmelCase_					,UpperCAmelCase_						:    Optional[int]    					=  self.calculate_expected_size(image_inputs[0] )
      self.assertEqual(
          encoded_images.shape     ,   (
              1,
              self.image_processor_tester.num_channels,
              expected_height,
              expected_width,
          )     ,   )






 def   a      ( self       : int )->					Union[str, Any]:



      """simple docstring"""

      UpperCAmelCase_						:    List[Any]    					=  self.image_processing_class(**self.image_processor_dict )
      # create random PyTorch tensors
      UpperCAmelCase_						:    int    					=  self.image_processor_tester.prepare_inputs(equal_resolution=a_     ,   torchify=a_ )
      for image in image_inputs:
           self.assertIsInstance(a_     ,   torch.Tensor )

      # Test not batched input
      UpperCAmelCase_						:    List[Any]    					=  image_processor(image_inputs[0]     ,   return_tensors="""pt""" ).pixel_values
      UpperCAmelCase_					,UpperCAmelCase_						:    List[str]    					=  self.calculate_expected_size(image_inputs[0] )
      self.assertEqual(
          encoded_images.shape     ,   (
              1,
              self.image_processor_tester.num_channels,
              expected_height,
              expected_width,
          )     ,   )



"""simple docstring"""







import warnings

from .generation import FlaxGenerationMixin



class 		UpperCAmelCase_	(lowerCamelCase_						):
 """simple docstring"""




 warnings.warn(
     """Importing `FlaxGenerationMixin` from `src/transformers/generation_flax_utils.py` is deprecated and will """
     """be removed in Transformers v5. Import as `from transformers import FlaxGenerationMixin` instead."""  , lowerCamelCase_  , )

