




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				:     Any ,			UpperCAmelCase__				:     int ,			UpperCAmelCase__				:     List[Any]=7 ,			UpperCAmelCase__				:     Optional[int]=3 ,			UpperCAmelCase__				:     Any=18 ,			UpperCAmelCase__				:     str=30 ,			UpperCAmelCase__				:     int=400 ,			UpperCAmelCase__				:     Tuple=True ,			UpperCAmelCase__				:     Union[str, Any]=None ,			UpperCAmelCase__				:     int=True ,			UpperCAmelCase__				:     Union[str, Any]=None ,			UpperCAmelCase__				:     Optional[int]=True ,			UpperCAmelCase__				:     Dict=[0.48145466, 0.4578275, 0.40821073] ,			UpperCAmelCase__				:     Optional[int]=[0.26862954, 0.26130258, 0.27577711] ,			UpperCAmelCase__				:     str=True ,			)						->Optional[Any]:





           '''simple docstring'''

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


    def 	SCREAMING_SNAKE_CASE							(      self				:     List[str])						->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,
           }





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





           '''simple docstring'''

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

           if equal_resolution:
                  A__							=   []
                  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:
                  A__							=   []
                  for i in range(self.batch_size):
                         A__							=   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
                  A__							=   [Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE_ ,			0 ,			-1)) for x in image_inputs]

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

           return image_inputs


@require_torch
@require_vision
class     UpperCamelCase_   (			_UpperCAmelCase		, unittest.TestCase					):

    '''simple docstring'''






    UpperCAmelCase__							  =				ChineseCLIPImageProcessor if is_vision_available() else None


    def 	SCREAMING_SNAKE_CASE							(      self				:     int)						->str:





           '''simple docstring'''

           A__							=   ChineseCLIPImageProcessingTester(self ,			do_center_crop=SCREAMING_SNAKE_CASE_)


    @property
    def 	SCREAMING_SNAKE_CASE							(      self				:     Dict)						->Tuple:





           '''simple docstring'''

           return self.image_processor_tester.prepare_image_processor_dict()


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





           '''simple docstring'''

           A__							=   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)						->Optional[Any]:





           '''simple docstring'''

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

           A__							=   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 	SCREAMING_SNAKE_CASE							(      self				:     List[str])						->List[Any]:





           '''simple docstring'''

           pass


    def 	SCREAMING_SNAKE_CASE							(      self				:     str)						->List[Any]:





           '''simple docstring'''

           A__							=   self.image_processing_class(**self.image_processor_dict)
           # create random PIL images
           A__							=   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
           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(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])						->int:





           '''simple docstring'''

           A__							=   self.image_processing_class(**self.image_processor_dict)
           # create random numpy tensors
           A__							=   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
           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(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				:     Tuple)						->Any:





           '''simple docstring'''

           A__							=   self.image_processing_class(**self.image_processor_dict)
           # create random PyTorch tensors
           A__							=   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
           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(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_   (			_UpperCAmelCase		, unittest.TestCase					):

    '''simple docstring'''






    UpperCAmelCase__							  =				ChineseCLIPImageProcessor if is_vision_available() else None


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





           '''simple docstring'''

           A__							=   ChineseCLIPImageProcessingTester(self ,			num_channels=4 ,			do_center_crop=SCREAMING_SNAKE_CASE_)
           A__							=   3


    @property
    def 	SCREAMING_SNAKE_CASE							(      self				:     Tuple)						->List[str]:





           '''simple docstring'''

           return self.image_processor_tester.prepare_image_processor_dict()


    def 	SCREAMING_SNAKE_CASE							(      self				:     Dict)						->Any:





           '''simple docstring'''

           A__							=   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				:     List[str])						->List[str]:





           '''simple docstring'''

           pass





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





           '''simple docstring'''

           A__							=   self.image_processing_class(**self.image_processor_dict)
           # create random PIL images
           A__							=   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
           A__							=   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
           A__							=   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'''],
               ) ,			)











from typing import TYPE_CHECKING

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


_lowerCamelCase     :		Any								=       {
    """configuration_nezha""": ["""NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """NezhaConfig"""],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _lowerCamelCase     :		str								=       [
      """NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """NezhaForNextSentencePrediction""",
      """NezhaForMaskedLM""",
      """NezhaForPreTraining""",
      """NezhaForMultipleChoice""",
      """NezhaForQuestionAnswering""",
      """NezhaForSequenceClassification""",
      """NezhaForTokenClassification""",
      """NezhaModel""",
      """NezhaPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_nezha import NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP, NezhaConfig

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_nezha import (
        NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST,
        NezhaForMaskedLM,
        NezhaForMultipleChoice,
        NezhaForNextSentencePrediction,
        NezhaForPreTraining,
        NezhaForQuestionAnswering,
        NezhaForSequenceClassification,
        NezhaForTokenClassification,
        NezhaModel,
        NezhaPreTrainedModel,
    )


else:
  import sys

  _lowerCamelCase     :		List[str]								=       _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


