

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 OwlViTImageProcessor







class 		__magic_name__     (				unittest.TestCase				):

   '''simple docstring'''




   def __init__(							self,       lowercase_,       lowercase_=7,       lowercase_=3,       lowercase_=18,       lowercase_=30,       lowercase_=400,       lowercase_=True,       lowercase_=None,       lowercase_=True,       lowercase_=None,       lowercase_=True,       lowercase_=[0.48145466, 0.4578275, 0.40821073],       lowercase_=[0.26862954, 0.26130258, 0.27577711],       lowercase_=True,       )				->       str:






     """simple docstring"""






     a__    =parent
     a__    =batch_size
     a__    =num_channels
     a__    =image_size
     a__    =min_resolution
     a__    =max_resolution
     a__    =do_resize
     a__    =size if size is not None else {'''height''': 18, '''width''': 18}
     a__    =do_center_crop
     a__    =crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
     a__    =do_normalize
     a__    =image_mean
     a__    =image_std
     a__    =do_convert_rgb



   def 					_UpperCAmelCase					(							self			)				->       Optional[int]:






     """simple docstring"""






     return {
         "do_resize": self.do_resize,
         "size": self.size,
         "do_center_crop": self.do_center_crop,
         "crop_size": self.crop_size,
         "do_normalize": self.do_normalize,
         "image_mean": self.image_mean,
         "image_std": self.image_std,
         "do_convert_rgb": self.do_convert_rgb,
     }







@require_torch
@require_vision
class 		__magic_name__     (				lowerCamelCase__						,			unittest.TestCase				):

   '''simple docstring'''




   lowerCamelCase__					:      int        =  OwlViTImageProcessor if is_vision_available() else None



   def 					_UpperCAmelCase					(							self			)				->       Tuple:






     """simple docstring"""






     a__    =OwlViTImageProcessingTester(self			)



   @property
   def 					_UpperCAmelCase					(							self			)				->       Optional[Any]:






     """simple docstring"""






     return self.image_processor_tester.prepare_image_processor_dict()



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






     """simple docstring"""






     a__    =self.image_processing_class(**self.image_processor_dict			)
     self.assertTrue(hasattr(lowercase_,       '''do_resize'''			)			)
     self.assertTrue(hasattr(lowercase_,       '''size'''			)			)
     self.assertTrue(hasattr(lowercase_,       '''do_center_crop'''			)			)
     self.assertTrue(hasattr(lowercase_,       '''center_crop'''			)			)
     self.assertTrue(hasattr(lowercase_,       '''do_normalize'''			)			)
     self.assertTrue(hasattr(lowercase_,       '''image_mean'''			)			)
     self.assertTrue(hasattr(lowercase_,       '''image_std'''			)			)
     self.assertTrue(hasattr(lowercase_,       '''do_convert_rgb'''			)			)



   def 					_UpperCAmelCase					(							self			)				->       str:






     """simple docstring"""






     a__    =self.image_processing_class.from_dict(self.image_processor_dict			)
     self.assertEqual(image_processor.size,       {'''height''': 18, '''width''': 18}			)
     self.assertEqual(image_processor.crop_size,       {'''height''': 18, '''width''': 18}			)

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



   def 					_UpperCAmelCase					(							self			)				->       Tuple:






     """simple docstring"""






     a__    =self.image_processing_class(**self.image_processor_dict			)
     # create random PIL images
     a__    =prepare_image_inputs(self.image_processor_tester,       equal_resolution=lowercase_			)

     for image in image_inputs:
       self.assertIsInstance(lowercase_,       Image.Image			)

     # Test not batched input
     a__    =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
     a__    =image_processing(lowercase_,       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 					_UpperCAmelCase					(							self			)				->       Optional[int]:






     """simple docstring"""






     a__    =self.image_processing_class(**self.image_processor_dict			)
     # create random numpy tensors
     a__    =prepare_image_inputs(self.image_processor_tester,       equal_resolution=lowercase_,       numpify=lowercase_			)
     for image in image_inputs:
       self.assertIsInstance(lowercase_,       np.ndarray			)

     # Test not batched input
     a__    =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
     a__    =image_processing(lowercase_,       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 					_UpperCAmelCase					(							self			)				->       Optional[Any]:






     """simple docstring"""






     a__    =self.image_processing_class(**self.image_processor_dict			)
     # create random PyTorch tensors
     a__    =prepare_image_inputs(self.image_processor_tester,       equal_resolution=lowercase_,       torchify=lowercase_			)
     for image in image_inputs:
       self.assertIsInstance(lowercase_,       torch.Tensor			)

     # Test not batched input
     a__    =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
     a__    =image_processing(lowercase_,       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

import numpy as np
import PIL
import torch

from diffusers.image_processor import VaeImageProcessor







class 		__magic_name__     (				unittest.TestCase				):

   '''simple docstring'''




   @property
   def 					_UpperCAmelCase					(							self			)				->       Tuple:






     """simple docstring"""






     a__    =1
     a__    =3
     a__    =8
     a__    =8

     a__    =torch.rand((batch_size, num_channels, height, width)			)

     return sample



   def 					_UpperCAmelCase					(							self,       lowercase_			)				->       int:






     """simple docstring"""






     if isinstance(image[0],       PIL.Image.Image			):
       return np.stack([np.array(lowercase_			) for i in image],       axis=0			)
     elif isinstance(lowercase_,       torch.Tensor			):
       return image.cpu().numpy().transpose(0,       2,       3,       1			)
     return image



   def 					_UpperCAmelCase					(							self			)				->       int:






     """simple docstring"""






     a__    =VaeImageProcessor(do_resize=lowercase_,       do_normalize=lowercase_			)

     a__    =self.dummy_sample
     a__    =self.to_np(lowercase_			)

     for output_type in ["pt", "np", "pil"]:
       a__    =image_processor.postprocess(
           image_processor.preprocess(lowercase_			),       output_type=lowercase_,       )
       a__    =self.to_np(lowercase_			)
       a__    =(input_np * 255).round() if output_type == '''pil''' else input_np
       assert (
           np.abs(in_np - out_np			).max() < 1E-6
       ), F"""decoded output does not match input for output_type {output_type}"""



   def 					_UpperCAmelCase					(							self			)				->       List[Any]:






     """simple docstring"""






     a__    =VaeImageProcessor(do_resize=lowercase_,       do_normalize=lowercase_			)
     a__    =self.dummy_sample.cpu().numpy().transpose(0,       2,       3,       1			)

     for output_type in ["pt", "np", "pil"]:
       a__    =image_processor.postprocess(image_processor.preprocess(lowercase_			),       output_type=lowercase_			)

       a__    =self.to_np(lowercase_			)
       a__    =(input_np * 255).round() if output_type == '''pil''' else input_np
       assert (
           np.abs(in_np - out_np			).max() < 1E-6
       ), F"""decoded output does not match input for output_type {output_type}"""



   def 					_UpperCAmelCase					(							self			)				->       Optional[Any]:






     """simple docstring"""






     a__    =VaeImageProcessor(do_resize=lowercase_,       do_normalize=lowercase_			)

     a__    =self.dummy_sample.cpu().numpy().transpose(0,       2,       3,       1			)
     a__    =image_processor.numpy_to_pil(lowercase_			)

     for output_type in ["pt", "np", "pil"]:
       a__    =image_processor.postprocess(image_processor.preprocess(lowercase_			),       output_type=lowercase_			)
       for i, o in zip(lowercase_,       lowercase_			):
         a__    =np.array(lowercase_			)
         a__    =self.to_np(lowercase_			) if output_type == '''pil''' else (self.to_np(lowercase_			) * 255).round()
         assert (
             np.abs(in_np - out_np			).max() < 1E-6
         ), F"""decoded output does not match input for output_type {output_type}"""



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






     """simple docstring"""






     a__    =VaeImageProcessor(do_resize=lowercase_,       do_normalize=lowercase_			)

     a__    =self.dummy_sample
     a__    =input_pt_ad.squeeze(0			)

     a__    =image_processor.postprocess(
         image_processor.preprocess(lowercase_			),       output_type='''np''',       )
     a__    =image_processor.postprocess(
         image_processor.preprocess(lowercase_			),       output_type='''np''',       )

     a__    =self.to_np(self.dummy_sample			)
     a__    =input_np_ad.squeeze(0			)

     a__    =image_processor.postprocess(
         image_processor.preprocess(lowercase_			),       output_type='''np''',       )
     a__    =image_processor.postprocess(
         image_processor.preprocess(lowercase_			),       output_type='''np''',       )

     assert np.abs(out_pt_ad - out_pt_ad			).max() < 1E-6
     assert np.abs(out_np_ad - out_np_ad			).max() < 1E-6



   def 					_UpperCAmelCase					(							self			)				->       List[Any]:






     """simple docstring"""






     a__    =VaeImageProcessor(do_resize=lowercase_,       do_normalize=lowercase_			)

     a__    =self.dummy_sample
     a__    =list(lowercase_			)

     a__    =image_processor.postprocess(
         image_processor.preprocess(lowercase_			),       output_type='''np''',       )

     a__    =image_processor.postprocess(
         image_processor.preprocess(lowercase_			),       output_type='''np''',       )

     a__    =self.to_np(self.dummy_sample			)
     list(lowercase_			)

     a__    =image_processor.postprocess(
         image_processor.preprocess(lowercase_			),       output_type='''np''',       )

     a__    =image_processor.postprocess(
         image_processor.preprocess(lowercase_			),       output_type='''np''',       )

     assert np.abs(out_pt_ad - out_pt_list			).max() < 1E-6
     assert np.abs(out_np_ad - out_np_list			).max() < 1E-6
