


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 BlipImageProcessor






class        UpperCAmelCase__			(   unittest.TestCase						):



    """simple docstring"""






    def __init__(     self   ,  A_   ,  A_=7   ,  A_=3   ,  A_=18   ,  A_=30   ,  A_=400   ,  A_=True   ,  A_=None   ,  A_=True   ,  A_=False   ,  A_=[0.4814_5466, 0.457_8275, 0.4082_1073]   ,  A_=[0.2686_2954, 0.2613_0258, 0.2757_7711]   ,  A_=True   ,  )    ->			Optional[int]:
         __UpperCamelCase       				=size if size is not None else {'height': 20, 'width': 20}
         __UpperCamelCase       				=parent
         __UpperCamelCase       				=batch_size
         __UpperCamelCase       				=num_channels
         __UpperCamelCase       				=image_size
         __UpperCamelCase       				=min_resolution
         __UpperCamelCase       				=max_resolution
         __UpperCamelCase       				=do_resize
         __UpperCamelCase       				=size
         __UpperCamelCase       				=do_normalize
         __UpperCamelCase       				=image_mean
         __UpperCamelCase       				=image_std
         __UpperCamelCase       				=do_pad
         __UpperCamelCase       				=do_convert_rgb






    def _a   (     self       )    ->			Dict:
         return {
             "do_resize": self.do_resize,
             "size": self.size,
             "do_normalize": self.do_normalize,
             "image_mean": self.image_mean,
             "image_std": self.image_std,
             "do_convert_rgb": self.do_convert_rgb,
             "do_pad": self.do_pad,
         }






    def _a   (     self   ,  A_=False   ,  A_=False   ,  A_=False       )    ->			Optional[int]:
         assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

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

         if torchify:
              __UpperCamelCase       				=[torch.from_numpy(A_       ) for x in image_inputs]

         return image_inputs






@require_torch
@require_vision
class        UpperCAmelCase__			(   A_  ,    unittest.TestCase						):



    """simple docstring"""






    UpperCAmelCase__		:						Union[str, Any]    =    BlipImageProcessor if is_vision_available() else None






    def _a   (     self       )    ->			int:
         __UpperCamelCase       				=BlipImageProcessingTester(self       )






    @property
    def _a   (     self       )    ->			Optional[Any]:
         return self.image_processor_tester.prepare_image_processor_dict()






    def _a   (     self       )    ->			Union[str, Any]:
         __UpperCamelCase       				=self.image_processing_class(**self.image_processor_dict       )
         self.assertTrue(hasattr(A_   ,  'do_resize'       )       )
         self.assertTrue(hasattr(A_   ,  'size'       )       )
         self.assertTrue(hasattr(A_   ,  'do_normalize'       )       )
         self.assertTrue(hasattr(A_   ,  'image_mean'       )       )
         self.assertTrue(hasattr(A_   ,  'image_std'       )       )
         self.assertTrue(hasattr(A_   ,  'do_convert_rgb'       )       )






    def _a   (     self       )    ->			Union[str, Any]:
         pass






    def _a   (     self       )    ->			Tuple:
         # Initialize image_processor
         __UpperCamelCase       				=self.image_processing_class(**self.image_processor_dict       )
         # create random PIL images
         __UpperCamelCase       				=self.image_processor_tester.prepare_inputs(equal_resolution=A_       )
         for image in image_inputs:
              self.assertIsInstance(A_   ,  Image.Image       )

         # Test not batched input
         __UpperCamelCase       				=image_processor(image_inputs[0]   ,  return_tensors='pt'       ).pixel_values
         self.assertEqual(
             encoded_images.shape   ,  (
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )

         # Test batched
         __UpperCamelCase       				=image_processor(A_   ,  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.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )






    def _a   (     self       )    ->			Any:
         # Initialize image_processor
         __UpperCamelCase       				=self.image_processing_class(**self.image_processor_dict       )
         # create random numpy tensors
         __UpperCamelCase       				=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       				=image_processor(image_inputs[0]   ,  return_tensors='pt'       ).pixel_values
         self.assertEqual(
             encoded_images.shape   ,  (
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )

         # Test batched
         __UpperCamelCase       				=image_processor(A_   ,  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.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )






    def _a   (     self       )    ->			Union[str, Any]:
         # Initialize image_processor
         __UpperCamelCase       				=self.image_processing_class(**self.image_processor_dict       )
         # create random PyTorch tensors
         __UpperCamelCase       				=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       				=image_processor(image_inputs[0]   ,  return_tensors='pt'       ).pixel_values
         self.assertEqual(
             encoded_images.shape   ,  (
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )

         # Test batched
         __UpperCamelCase       				=image_processor(A_   ,  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.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )






@require_torch
@require_vision
class        UpperCAmelCase__			(   A_  ,    unittest.TestCase						):



    """simple docstring"""






    UpperCAmelCase__		:						Any    =    BlipImageProcessor if is_vision_available() else None






    def _a   (     self       )    ->			Optional[int]:
         __UpperCamelCase       				=BlipImageProcessingTester(self   ,  num_channels=4       )
         __UpperCamelCase       				=3






    @property
    def _a   (     self       )    ->			Dict:
         return self.image_processor_tester.prepare_image_processor_dict()






    def _a   (     self       )    ->			List[Any]:
         __UpperCamelCase       				=self.image_processing_class(**self.image_processor_dict       )
         self.assertTrue(hasattr(A_   ,  'do_resize'       )       )
         self.assertTrue(hasattr(A_   ,  'size'       )       )
         self.assertTrue(hasattr(A_   ,  'do_normalize'       )       )
         self.assertTrue(hasattr(A_   ,  'image_mean'       )       )
         self.assertTrue(hasattr(A_   ,  'image_std'       )       )
         self.assertTrue(hasattr(A_   ,  'do_convert_rgb'       )       )






    def _a   (     self       )    ->			List[str]:
         pass






    def _a   (     self       )    ->			str:
         # Initialize image_processor
         __UpperCamelCase       				=self.image_processing_class(**self.image_processor_dict       )
         # create random PIL images
         __UpperCamelCase       				=self.image_processor_tester.prepare_inputs(equal_resolution=A_       )
         for image in image_inputs:
              self.assertIsInstance(A_   ,  Image.Image       )

         # Test not batched input
         __UpperCamelCase       				=image_processor(image_inputs[0]   ,  return_tensors='pt'       ).pixel_values
         self.assertEqual(
             encoded_images.shape   ,  (
                 1,
                 self.expected_encoded_image_num_channels,
                 self.image_processor_tester.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )

         # Test batched
         __UpperCamelCase       				=image_processor(A_   ,  return_tensors='pt'       ).pixel_values
         self.assertEqual(
             encoded_images.shape   ,  (
                 self.image_processor_tester.batch_size,
                 self.expected_encoded_image_num_channels,
                 self.image_processor_tester.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )









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 EfficientNetImageProcessor






class        UpperCAmelCase__			(   unittest.TestCase						):



    """simple docstring"""






    def __init__(     self   ,  A_   ,  A_=13   ,  A_=3   ,  A_=18   ,  A_=30   ,  A_=400   ,  A_=True   ,  A_=None   ,  A_=True   ,  A_=[0.5, 0.5, 0.5]   ,  A_=[0.5, 0.5, 0.5]   ,  )    ->			int:
         __UpperCamelCase       				=size if size is not None else {'height': 18, 'width': 18}
         __UpperCamelCase       				=parent
         __UpperCamelCase       				=batch_size
         __UpperCamelCase       				=num_channels
         __UpperCamelCase       				=image_size
         __UpperCamelCase       				=min_resolution
         __UpperCamelCase       				=max_resolution
         __UpperCamelCase       				=do_resize
         __UpperCamelCase       				=size
         __UpperCamelCase       				=do_normalize
         __UpperCamelCase       				=image_mean
         __UpperCamelCase       				=image_std






    def _a   (     self       )    ->			Optional[Any]:
         return {
             "image_mean": self.image_mean,
             "image_std": self.image_std,
             "do_normalize": self.do_normalize,
             "do_resize": self.do_resize,
             "size": self.size,
         }






@require_torch
@require_vision
class        UpperCAmelCase__			(   A_  ,    unittest.TestCase						):



    """simple docstring"""






    UpperCAmelCase__		:						List[Any]    =    EfficientNetImageProcessor if is_vision_available() else None






    def _a   (     self       )    ->			List[str]:
         __UpperCamelCase       				=EfficientNetImageProcessorTester(self       )






    @property
    def _a   (     self       )    ->			Optional[Any]:
         return self.image_processor_tester.prepare_image_processor_dict()






    def _a   (     self       )    ->			str:
         __UpperCamelCase       				=self.image_processing_class(**self.image_processor_dict       )
         self.assertTrue(hasattr(A_   ,  'image_mean'       )       )
         self.assertTrue(hasattr(A_   ,  'image_std'       )       )
         self.assertTrue(hasattr(A_   ,  'do_normalize'       )       )
         self.assertTrue(hasattr(A_   ,  'do_resize'       )       )
         self.assertTrue(hasattr(A_   ,  'size'       )       )






    def _a   (     self       )    ->			Dict:
         __UpperCamelCase       				=self.image_processing_class.from_dict(self.image_processor_dict       )
         self.assertEqual(image_processor.size   ,  {'height': 18, 'width': 18}       )

         __UpperCamelCase       				=self.image_processing_class.from_dict(self.image_processor_dict   ,  size=42       )
         self.assertEqual(image_processor.size   ,  {'height': 42, 'width': 42}       )






    def _a   (     self       )    ->			List[str]:
         # Initialize image_processing
         __UpperCamelCase       				=self.image_processing_class(**self.image_processor_dict       )
         # create random PIL images
         __UpperCamelCase       				=prepare_image_inputs(self.image_processor_tester   ,  equal_resolution=A_       )
         for image in image_inputs:
              self.assertIsInstance(A_   ,  Image.Image       )

         # Test not batched input
         __UpperCamelCase       				=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.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )

         # Test batched
         __UpperCamelCase       				=image_processing(A_   ,  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.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )






    def _a   (     self       )    ->			List[str]:
         # Initialize image_processing
         __UpperCamelCase       				=self.image_processing_class(**self.image_processor_dict       )
         # create random numpy tensors
         __UpperCamelCase       				=prepare_image_inputs(self.image_processor_tester   ,  equal_resolution=A_   ,  numpify=A_       )
         for image in image_inputs:
              self.assertIsInstance(A_   ,  np.ndarray       )

         # Test not batched input
         __UpperCamelCase       				=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.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )

         # Test batched
         __UpperCamelCase       				=image_processing(A_   ,  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.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )






    def _a   (     self       )    ->			Dict:
         # Initialize image_processing
         __UpperCamelCase       				=self.image_processing_class(**self.image_processor_dict       )
         # create random PyTorch tensors
         __UpperCamelCase       				=prepare_image_inputs(self.image_processor_tester   ,  equal_resolution=A_   ,  torchify=A_       )
         for image in image_inputs:
              self.assertIsInstance(A_   ,  torch.Tensor       )

         # Test not batched input
         __UpperCamelCase       				=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.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )

         # Test batched
         __UpperCamelCase       				=image_processing(A_   ,  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.size['height'],
                 self.image_processor_tester.size['width'],
             )   ,  )





