




'''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 OwlViTImageProcessor





class       lowerCAmelCase				(	unittest.TestCase):


       def __init__(				self      ,					__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE=7      ,					__SCREAMING_SNAKE_CASE=3      ,					__SCREAMING_SNAKE_CASE=18      ,					__SCREAMING_SNAKE_CASE=30      ,					__SCREAMING_SNAKE_CASE=400      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=None      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=None      ,					__SCREAMING_SNAKE_CASE=True      ,					__SCREAMING_SNAKE_CASE=[0.48_145_466, 0.4_578_275, 0.40_821_073]      ,					__SCREAMING_SNAKE_CASE=[0.26_862_954, 0.26_130_258, 0.27_577_711]      ,					__SCREAMING_SNAKE_CASE=True      ,					) ->					List[Any]:



            '''simple docstring'''




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




       def  lowerCAmelCase (				self					) ->					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       lowerCAmelCase				(	__lowerCAmelCase			,				unittest.TestCase):
       __lowercase					: Tuple	    =     OwlViTImageProcessor if is_vision_available() else None


       def  lowerCAmelCase (				self					) ->					Any:



            '''simple docstring'''




            __snake_case         =					OwlViTImageProcessingTester(self					)


       @property
       def  lowerCAmelCase (				self					) ->					Tuple:



            '''simple docstring'''




            return self.image_processor_tester.prepare_image_processor_dict()


       def  lowerCAmelCase (				self					) ->					Dict:



            '''simple docstring'''




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


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



            '''simple docstring'''




            __snake_case         =					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}					)

            __snake_case         =					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  lowerCAmelCase (				self					) ->					str:



            '''simple docstring'''




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

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

            # Test not batched input
            __snake_case         =					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
            __snake_case         =					image_processing(__SCREAMING_SNAKE_CASE      ,					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  lowerCAmelCase (				self					) ->					Any:



            '''simple docstring'''




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

            # Test not batched input
            __snake_case         =					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
            __snake_case         =					image_processing(__SCREAMING_SNAKE_CASE      ,					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  lowerCAmelCase (				self					) ->					Union[str, Any]:



            '''simple docstring'''




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

            # Test not batched input
            __snake_case         =					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
            __snake_case         =					image_processing(__SCREAMING_SNAKE_CASE      ,					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 inspect

from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
     import jax





@require_flax
class       lowerCAmelCase				:


       def  lowerCAmelCase (				self					) ->					int:



            '''simple docstring'''




            __snake_case   ,      __snake_case         =					self.prepare_init_args_and_inputs_for_common()

            __snake_case         =					self.model_class(**__SCREAMING_SNAKE_CASE					)
            __snake_case         =					model.init(inputs_dict['''prng_key''']      ,					inputs_dict['''sample''']					)
            jax.lax.stop_gradient(__SCREAMING_SNAKE_CASE					)

            __snake_case         =					model.apply(__SCREAMING_SNAKE_CASE      ,					inputs_dict['''sample''']					)

            if isinstance(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					):
                 __snake_case         =					output.sample

            self.assertIsNotNone(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					inputs_dict['''sample'''].shape
            self.assertEqual(output.shape      ,					__SCREAMING_SNAKE_CASE      ,					'''Input and output shapes do not match'''					)


       def  lowerCAmelCase (				self					) ->					Tuple:



            '''simple docstring'''




            __snake_case   ,      __snake_case         =					self.prepare_init_args_and_inputs_for_common()

            __snake_case         =					16
            __snake_case         =					(16, 32)

            __snake_case         =					self.model_class(**__SCREAMING_SNAKE_CASE					)
            __snake_case         =					model.init(inputs_dict['''prng_key''']      ,					inputs_dict['''sample''']					)
            jax.lax.stop_gradient(__SCREAMING_SNAKE_CASE					)

            __snake_case         =					model.apply(__SCREAMING_SNAKE_CASE      ,					inputs_dict['''sample''']					)

            if isinstance(__SCREAMING_SNAKE_CASE      ,					__SCREAMING_SNAKE_CASE					):
                 __snake_case         =					output.sample

            self.assertIsNotNone(__SCREAMING_SNAKE_CASE					)
            __snake_case         =					inputs_dict['''sample'''].shape
            self.assertEqual(output.shape      ,					__SCREAMING_SNAKE_CASE      ,					'''Input and output shapes do not match'''					)




       def  lowerCAmelCase (				self					) ->					str:



            '''simple docstring'''




            __snake_case         =					'''kwargs''' in inspect.signature(self.model_class.__init__					).parameters
            __snake_case         =					len(self.model_class._deprecated_kwargs					) > 0

            if has_kwarg_in_model_class and not has_deprecated_kwarg:
                 raise ValueError(
                     F'''{self.model_class} has `**kwargs` in its __init__ method but has not defined any deprecated kwargs'''
                     ''' under the `_deprecated_kwargs` class attribute. Make sure to either remove `**kwargs` if there are'''
                     ''' no deprecated arguments or add the deprecated argument with `_deprecated_kwargs ='''
                     ''' [<deprecated_argument>]`'''					)

            if not has_kwarg_in_model_class and has_deprecated_kwarg:
                 raise ValueError(
                     F'''{self.model_class} doesn\'t have `**kwargs` in its __init__ method but has defined deprecated kwargs'''
                     ''' under the `_deprecated_kwargs` class attribute. Make sure to either add the `**kwargs` argument to'''
                     F''' {self.model_class}.__init__ if there are deprecated arguments or remove the deprecated argument'''
                     ''' from `_deprecated_kwargs = [<deprecated_argument>]`'''					)






