



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







class 				UpperCamelCase__       (     unittest.TestCase  ):




      """simple docstring"""




      def __init__(				self    :				Optional[int]   ,      SCREAMING_SNAKE_CASE_    :				str   ,      SCREAMING_SNAKE_CASE_    :				Dict=7   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=3   ,      SCREAMING_SNAKE_CASE_    :				Any=1_8   ,      SCREAMING_SNAKE_CASE_    :				int=3_0   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=4_0_0   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=True   ,      SCREAMING_SNAKE_CASE_    :				Any=None   ,      SCREAMING_SNAKE_CASE_    :				List[str]=True   ,      SCREAMING_SNAKE_CASE_    :				List[Any]=None   ,      SCREAMING_SNAKE_CASE_    :				Any=True   ,      SCREAMING_SNAKE_CASE_    :				int=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]   ,      SCREAMING_SNAKE_CASE_    :				Dict=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]   ,      SCREAMING_SNAKE_CASE_    :				Any=True   ,      ):
        lowerCAmelCase_       :    List[Any]								=			size if size is not None else {'height': 2_2_4, 'width': 2_2_4}
        lowerCAmelCase_       :    Any								=			crop_size if crop_size is not None else {'height': 1_8, 'width': 1_8}
        lowerCAmelCase_       :    str								=			parent
        lowerCAmelCase_       :    Dict								=			batch_size
        lowerCAmelCase_       :    List[str]								=			num_channels
        lowerCAmelCase_       :    Tuple								=			image_size
        lowerCAmelCase_       :    Union[str, Any]								=			min_resolution
        lowerCAmelCase_       :    Dict								=			max_resolution
        lowerCAmelCase_       :    Tuple								=			do_resize
        lowerCAmelCase_       :    Dict								=			size
        lowerCAmelCase_       :    Optional[int]								=			do_center_crop
        lowerCAmelCase_       :    int								=			crop_size
        lowerCAmelCase_       :    Dict								=			do_normalize
        lowerCAmelCase_       :    Any								=			image_mean
        lowerCAmelCase_       :    Any								=			image_std
        lowerCAmelCase_       :    int								=			do_convert_rgb



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Any	):
        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,
        }





      def 					SCREAMING_SNAKE_CASE__			(				self    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				List[str]=False   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=False   ,      SCREAMING_SNAKE_CASE_    :				int=False	):
        assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

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

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

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

        return image_inputs







@require_torch
@require_vision
class 				UpperCamelCase__       (     lowercase_, unittest.TestCase  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					ChineseCLIPImageProcessor if is_vision_available() else None



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]	):
        lowerCAmelCase_       :    Tuple								=			ChineseCLIPImageProcessingTester(self   ,      do_center_crop=SCREAMING_SNAKE_CASE_	)



      @property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				int	):
        return self.image_processor_tester.prepare_image_processor_dict()



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]	):
        lowerCAmelCase_       :    List[str]								=			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 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]	):
        lowerCAmelCase_       :    Tuple								=			self.image_processing_class.from_dict(self.image_processor_dict	)
        self.assertEqual(image_processor.size   ,      {'height': 2_2_4, 'width': 2_2_4}	)
        self.assertEqual(image_processor.crop_size   ,      {'height': 1_8, 'width': 1_8}	)

        lowerCAmelCase_       :    Optional[int]								=			self.image_processing_class.from_dict(self.image_processor_dict   ,      size=4_2   ,      crop_size=8_4	)
        self.assertEqual(image_processor.size   ,      {'shortest_edge': 4_2}	)
        self.assertEqual(image_processor.crop_size   ,      {'height': 8_4, 'width': 8_4}	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				int	):
        pass



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]	):
        # Initialize image_processing
        lowerCAmelCase_       :    List[Any]								=			self.image_processing_class(**self.image_processor_dict	)
        # create random PIL images
        lowerCAmelCase_       :    Any								=			self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE_	)
        for image in image_inputs:
          self.assertIsInstance(SCREAMING_SNAKE_CASE_   ,      Image.Image	)

        # Test not batched input
        lowerCAmelCase_       :    int								=			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
        lowerCAmelCase_       :    Tuple								=			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 					SCREAMING_SNAKE_CASE__			(				self    :				Dict	):
        # Initialize image_processing
        lowerCAmelCase_       :    Any								=			self.image_processing_class(**self.image_processor_dict	)
        # create random numpy tensors
        lowerCAmelCase_       :    Optional[int]								=			self.image_processor_tester.prepare_inputs(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
        lowerCAmelCase_       :    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
        lowerCAmelCase_       :    Optional[int]								=			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 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]	):
        # Initialize image_processing
        lowerCAmelCase_       :    Union[str, Any]								=			self.image_processing_class(**self.image_processor_dict	)
        # create random PyTorch tensors
        lowerCAmelCase_       :    Optional[int]								=			self.image_processor_tester.prepare_inputs(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
        lowerCAmelCase_       :    Dict								=			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
        lowerCAmelCase_       :    Tuple								=			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'],
            )   ,      )







@require_torch
@require_vision
class 				UpperCamelCase__       (     lowercase_, unittest.TestCase  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					ChineseCLIPImageProcessor if is_vision_available() else None



      def 					SCREAMING_SNAKE_CASE__			(				self    :				int	):
        lowerCAmelCase_       :    Optional[Any]								=			ChineseCLIPImageProcessingTester(self   ,      num_channels=4   ,      do_center_crop=SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Optional[int]								=			3



      @property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]	):
        return self.image_processor_tester.prepare_image_processor_dict()



      def 					SCREAMING_SNAKE_CASE__			(				self    :				int	):
        lowerCAmelCase_       :    List[str]								=			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 					SCREAMING_SNAKE_CASE__			(				self    :				Dict	):
        pass





      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]	):
        # Initialize image_processing
        lowerCAmelCase_       :    Optional[int]								=			self.image_processing_class(**self.image_processor_dict	)
        # create random PIL images
        lowerCAmelCase_       :    List[Any]								=			self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE_	)
        for image in image_inputs:
          self.assertIsInstance(SCREAMING_SNAKE_CASE_   ,      Image.Image	)

        # Test not batched input
        lowerCAmelCase_       :    List[Any]								=			image_processing(image_inputs[0]   ,      return_tensors='pt'	).pixel_values
        self.assertEqual(
            encoded_images.shape   ,      (
                1,
                self.expected_encoded_image_num_channels,
                self.image_processor_tester.crop_size['height'],
                self.image_processor_tester.crop_size['width'],
            )   ,      )

        # Test batched
        lowerCAmelCase_       :    Dict								=			image_processing(SCREAMING_SNAKE_CASE_   ,      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.crop_size['height'],
                self.image_processor_tester.crop_size['width'],
            )   ,      )










"""simple docstring"""







from diffusers.utils.testing_utils import require_onnxruntime







@require_onnxruntime
class 				UpperCamelCase__       :




      """simple docstring"""




      pass





