


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 CLIPImageProcessor



class  UpperCAmelCase_						(				unittest.TestCase):
       '''simple docstring'''






       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			,       ):




              """simple docstring"""




              UpperCamelCase     :						Optional[int] 						=		size if size is not None else {"""shortest_edge""": 20}
              UpperCamelCase     :						Any 						=		crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
              UpperCamelCase     :						str 						=		parent
              UpperCamelCase     :						Optional[int] 						=		batch_size
              UpperCamelCase     :						int 						=		num_channels
              UpperCamelCase     :						str 						=		image_size
              UpperCamelCase     :						Tuple 						=		min_resolution
              UpperCamelCase     :						int 						=		max_resolution
              UpperCamelCase     :						Dict 						=		do_resize
              UpperCamelCase     :						List[str] 						=		size
              UpperCamelCase     :						Tuple 						=		do_center_crop
              UpperCamelCase     :						int 						=		crop_size
              UpperCamelCase     :						List[Any] 						=		do_normalize
              UpperCamelCase     :						Any 						=		image_mean
              UpperCamelCase     :						Optional[Any] 						=		image_std
              UpperCamelCase     :						List[str] 						=		do_convert_rgb






       def 			_lowercase (  self    ):




              """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 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE=False			,       __SCREAMING_SNAKE_CASE=False			,       __SCREAMING_SNAKE_CASE=False    ):




              """simple docstring"""




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

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

              if torchify:
                     UpperCamelCase     :						List[Any] 						=		[torch.from_numpy(UpperCAmelCase__    ) for x in image_inputs]

              return image_inputs




@require_torch
@require_vision
class  UpperCAmelCase_						(				__UpperCamelCase,  unittest.TestCase):
       '''simple docstring'''
       __UpperCamelCase   :  List[str]				=       CLIPImageProcessor if is_vision_available() else None






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						List[str] 						=		CLIPImageProcessingTester(self    )






       @property
       def 			_lowercase (  self    ):




              """simple docstring"""




              return self.image_processor_tester.prepare_image_processor_dict()






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						Tuple 						=		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 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						Optional[int] 						=		self.image_processing_class.from_dict(self.image_processor_dict    )
              self.assertEqual(image_processor.size			,       {'''shortest_edge''': 20}    )
              self.assertEqual(image_processor.crop_size			,       {'''height''': 18, '''width''': 18}    )

              UpperCamelCase     :						Tuple 						=		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 			_lowercase (  self    ):




              """simple docstring"""




              pass






       def 			_lowercase (  self    ):




              """simple docstring"""




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

              # Test not batched input
              UpperCamelCase     :						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
              UpperCamelCase     :						List[str] 						=		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 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						Dict 						=		self.image_processing_class(**self.image_processor_dict    )
              # create random numpy tensors
              UpperCamelCase     :						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
              UpperCamelCase     :						str 						=		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
              UpperCamelCase     :						Tuple 						=		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 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						List[Any] 						=		self.image_processing_class(**self.image_processor_dict    )
              # create random PyTorch tensors
              UpperCamelCase     :						List[Any] 						=		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
              UpperCamelCase     :						Union[str, 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
              UpperCamelCase     :						Dict 						=		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  UpperCAmelCase_						(				__UpperCamelCase,  unittest.TestCase):
       '''simple docstring'''
       __UpperCamelCase   :  Union[str, Any]				=       CLIPImageProcessor if is_vision_available() else None






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						List[Any] 						=		CLIPImageProcessingTester(self			,       num_channels=4    )
              UpperCamelCase     :						Union[str, Any] 						=		3






       @property
       def 			_lowercase (  self    ):




              """simple docstring"""




              return self.image_processor_tester.prepare_image_processor_dict()






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						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 			_lowercase (  self    ):




              """simple docstring"""




              pass






       def 			_lowercase (  self    ):




              """simple docstring"""




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

              # Test not batched input
              UpperCamelCase     :						Tuple 						=		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
              UpperCamelCase     :						Any 						=		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_torch_available,
    is_vision_available,
)


a 			=							{
    'configuration_mobilevitv2': [
        'MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'MobileViTV2Config',
        'MobileViTV2OnnxConfig',
    ],
}


try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 a 			=							[
     'MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST',
     'MobileViTV2ForImageClassification',
     'MobileViTV2ForSemanticSegmentation',
     'MobileViTV2Model',
     'MobileViTV2PreTrainedModel',
 ]

if TYPE_CHECKING:
 from .configuration_mobilevitva import (
     MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
     MobileViTVaConfig,
     MobileViTVaOnnxConfig,
 )

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_mobilevitva import (
      MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
      MobileViTVaForImageClassification,
      MobileViTVaForSemanticSegmentation,
      MobileViTVaModel,
      MobileViTVaPreTrainedModel,
  )

else:
 import sys

 a 			=							_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


