



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 		snake_case						(	unittest.TestCase  ):



   '''simple docstring'''






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

    """simple docstring"""







    _snake_case    :  Any					=					size if size is not None else {'''height''': 224, '''width''': 224}
    _snake_case    :  Optional[Any]					=					crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
    _snake_case    :  Optional[Any]					=					parent
    _snake_case    :  Optional[Any]					=					batch_size
    _snake_case    :  List[str]					=					num_channels
    _snake_case    :  Tuple					=					image_size
    _snake_case    :  Union[str, Any]					=					min_resolution
    _snake_case    :  List[str]					=					max_resolution
    _snake_case    :  Union[str, Any]					=					do_resize
    _snake_case    :  int					=					size
    _snake_case    :  Tuple					=					do_center_crop
    _snake_case    :  List[Any]					=					crop_size
    _snake_case    :  List[str]					=					do_normalize
    _snake_case    :  List[Any]					=					image_mean
    _snake_case    :  Any					=					image_std
    _snake_case    :  Union[str, Any]					=					do_convert_rgb

   def      UpperCamelCase_  (					self							:			List[str])				->				Tuple:

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


   def      UpperCamelCase_  (					self							:			int				,		lowerCAmelCase							:			Any=False				,		lowerCAmelCase							:			Dict=False				,		lowerCAmelCase							:			Optional[Any]=False)				->				Dict:

    """simple docstring"""







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

    if equal_resolution:
     _snake_case    :  Union[str, Any]					=					[]
     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:
     _snake_case    :  int					=					[]
     for i in range(self.batch_size):
      _snake_case    :  Optional[int]					=					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
     _snake_case    :  Tuple					=					[Image.fromarray(np.moveaxis(UpperCamelCase__				,		0				,		-1)) for x in image_inputs]

    if torchify:
     _snake_case    :  int					=					[torch.from_numpy(UpperCamelCase__) for x in image_inputs]

    return image_inputs







@require_torch
@require_vision
class 		snake_case						(	SCREAMING_SNAKE_CASE_					,unittest.TestCase  ):



   '''simple docstring'''






   snake_case_					: Any          =		ChineseCLIPImageProcessor if is_vision_available() else None

   def      UpperCamelCase_  (					self							:			List[str])				->				int:

    """simple docstring"""







    _snake_case    :  Dict					=					ChineseCLIPImageProcessingTester(self				,		do_center_crop=UpperCamelCase__)

   @property
   def      UpperCamelCase_  (					self							:			Any)				->				List[str]:

    """simple docstring"""







    return self.image_processor_tester.prepare_image_processor_dict()

   def      UpperCamelCase_  (					self							:			Optional[int])				->				Tuple:

    """simple docstring"""







    _snake_case    :  Optional[Any]					=					self.image_processing_class(**self.image_processor_dict)
    self.assertTrue(hasattr(UpperCamelCase__				,		"""do_resize"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""size"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""do_center_crop"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""center_crop"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""do_normalize"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""image_mean"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""image_std"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""do_convert_rgb"""))

   def      UpperCamelCase_  (					self							:			Dict)				->				Any:

    """simple docstring"""







    _snake_case    :  Optional[int]					=					self.image_processing_class.from_dict(self.image_processor_dict)
    self.assertEqual(image_processor.size				,		{"""height""": 224, """width""": 224})
    self.assertEqual(image_processor.crop_size				,		{"""height""": 18, """width""": 18})

    _snake_case    :  int					=					self.image_processing_class.from_dict(self.image_processor_dict				,		size=42				,		crop_size=84)
    self.assertEqual(image_processor.size				,		{"""shortest_edge""": 42})
    self.assertEqual(image_processor.crop_size				,		{"""height""": 84, """width""": 84})

   def      UpperCamelCase_  (					self							:			List[Any])				->				Optional[int]:

    """simple docstring"""







    pass

   def      UpperCamelCase_  (					self							:			int)				->				Tuple:

    """simple docstring"""







    _snake_case    :  List[str]					=					self.image_processing_class(**self.image_processor_dict)
    # create random PIL images
    _snake_case    :  str					=					self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__)
    for image in image_inputs:
     self.assertIsInstance(UpperCamelCase__				,		Image.Image)

    # Test not batched input
    _snake_case    :  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
    _snake_case    :  Optional[Any]					=					image_processing(UpperCamelCase__				,		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							:			Dict)				->				Optional[Any]:

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					self.image_processing_class(**self.image_processor_dict)
    # create random numpy tensors
    _snake_case    :  Dict					=					self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__				,		numpify=UpperCamelCase__)
    for image in image_inputs:
     self.assertIsInstance(UpperCamelCase__				,		np.ndarray)

    # Test not batched input
    _snake_case    :  Optional[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
    _snake_case    :  int					=					image_processing(UpperCamelCase__				,		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							:			str)				->				Optional[Any]:

    """simple docstring"""







    _snake_case    :  Tuple					=					self.image_processing_class(**self.image_processor_dict)
    # create random PyTorch tensors
    _snake_case    :  Dict					=					self.image_processor_tester.prepare_inputs(equal_resolution=UpperCamelCase__				,		torchify=UpperCamelCase__)
    for image in image_inputs:
     self.assertIsInstance(UpperCamelCase__				,		torch.Tensor)

    # Test not batched input
    _snake_case    :  Optional[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
    _snake_case    :  Optional[Any]					=					image_processing(UpperCamelCase__				,		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 		snake_case						(	SCREAMING_SNAKE_CASE_					,unittest.TestCase  ):



   '''simple docstring'''






   snake_case_					: Any          =		ChineseCLIPImageProcessor if is_vision_available() else None

   def      UpperCamelCase_  (					self							:			Optional[Any])				->				List[Any]:

    """simple docstring"""







    _snake_case    :  int					=					ChineseCLIPImageProcessingTester(self				,		num_channels=4				,		do_center_crop=UpperCamelCase__)
    _snake_case    :  Optional[Any]					=					3

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

    """simple docstring"""







    return self.image_processor_tester.prepare_image_processor_dict()

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

    """simple docstring"""







    _snake_case    :  Any					=					self.image_processing_class(**self.image_processor_dict)
    self.assertTrue(hasattr(UpperCamelCase__				,		"""do_resize"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""size"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""do_center_crop"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""center_crop"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""do_normalize"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""image_mean"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""image_std"""))
    self.assertTrue(hasattr(UpperCamelCase__				,		"""do_convert_rgb"""))

   def      UpperCamelCase_  (					self							:			Dict)				->				List[Any]:

    """simple docstring"""







    pass


   def      UpperCamelCase_  (					self							:			List[str])				->				Tuple:

    """simple docstring"""







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

    # Test not batched input
    _snake_case    :  Optional[int]					=					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
    _snake_case    :  Tuple					=					image_processing(UpperCamelCase__				,		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"""],
        )				,		)





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


a__    =	{
    """configuration_canine""": ["""CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP""", """CanineConfig"""],
    """tokenization_canine""": ["""CanineTokenizer"""],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  a__    =	[
      """CANINE_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """CanineForMultipleChoice""",
      """CanineForQuestionAnswering""",
      """CanineForSequenceClassification""",
      """CanineForTokenClassification""",
      """CanineLayer""",
      """CanineModel""",
      """CaninePreTrainedModel""",
      """load_tf_weights_in_canine""",
  ]


if TYPE_CHECKING:
  from .configuration_canine import CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP, CanineConfig
  from .tokenization_canine import CanineTokenizer

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_canine import (
        CANINE_PRETRAINED_MODEL_ARCHIVE_LIST,
        CanineForMultipleChoice,
        CanineForQuestionAnswering,
        CanineForSequenceClassification,
        CanineForTokenClassification,
        CanineLayer,
        CanineModel,
        CaninePreTrainedModel,
        load_tf_weights_in_canine,
    )


else:
  import sys

  a__    =	_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
