import unittest

import numpy as np
from datasets import load_dataset

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 BeitImageProcessor


class 			UpperCAmelCase__    (		unittest.TestCase					):



  """simple docstring"""






  def __init__(				self:			Optional[int]				,				__lowerCAmelCase:			Dict				,				__lowerCAmelCase:			Tuple=7				,				__lowerCAmelCase:			int=3				,				__lowerCAmelCase:			List[Any]=18				,				__lowerCAmelCase:			Optional[Any]=30				,				__lowerCAmelCase:			List[Any]=400				,				__lowerCAmelCase:			Tuple=True				,				__lowerCAmelCase:			Dict=None				,				__lowerCAmelCase:			List[Any]=True				,				__lowerCAmelCase:			Optional[Any]=None				,				__lowerCAmelCase:			Tuple=True				,				__lowerCAmelCase:			List[str]=[0.5, 0.5, 0.5]				,				__lowerCAmelCase:			Optional[int]=[0.5, 0.5, 0.5]				,				__lowerCAmelCase:			int=False				,				) -> List[str]:




    '''simple docstring'''




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






  def 	_UpperCAmelCase				(				self:			Optional[Any]   ) -> Optional[Any]:




    '''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_reduce_labels": self.do_reduce_labels,
    }






def       __lowerCAmelCase      (						)    ->  List[Any]:
  __UpperCAmelCase						    =       load_dataset("hf-internal-testing/fixtures_ade20k"						,	split="test"					)

  __UpperCAmelCase						    =       Image.open(dataset[0]["file"]					)
  __UpperCAmelCase						    =       Image.open(dataset[1]["file"]					)

  return image, map




def       __lowerCAmelCase      (						)    ->  Optional[int]:
  __UpperCAmelCase						    =       load_dataset("hf-internal-testing/fixtures_ade20k"						,	split="test"					)

  __UpperCAmelCase						    =       Image.open(ds[0]["file"]					)
  __UpperCAmelCase						    =       Image.open(ds[1]["file"]					)
  __UpperCAmelCase						    =       Image.open(ds[2]["file"]					)
  __UpperCAmelCase						    =       Image.open(ds[3]["file"]					)

  return [imagea, imagea], [mapa, mapa]





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



  """simple docstring"""






  lowerCAmelCase__		:	int    		= BeitImageProcessor if is_vision_available() else None






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




    '''simple docstring'''




    __UpperCAmelCase						    =       BeitImageProcessingTester(self   )






  @property
  def 	_UpperCAmelCase				(				self:			Optional[Any]   ) -> Tuple:




    '''simple docstring'''




    return self.image_processor_tester.prepare_image_processor_dict()






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




    '''simple docstring'''




    __UpperCAmelCase						    =       self.image_processing_class(**self.image_processor_dict   )
    self.assertTrue(hasattr(__lowerCAmelCase				,				"do_resize"   )   )
    self.assertTrue(hasattr(__lowerCAmelCase				,				"size"   )   )
    self.assertTrue(hasattr(__lowerCAmelCase				,				"do_center_crop"   )   )
    self.assertTrue(hasattr(__lowerCAmelCase				,				"center_crop"   )   )
    self.assertTrue(hasattr(__lowerCAmelCase				,				"do_normalize"   )   )
    self.assertTrue(hasattr(__lowerCAmelCase				,				"image_mean"   )   )
    self.assertTrue(hasattr(__lowerCAmelCase				,				"image_std"   )   )






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




    '''simple docstring'''




    __UpperCAmelCase						    =       self.image_processing_class.from_dict(self.image_processor_dict   )
    self.assertEqual(image_processor.size				,				{"height": 20, "width": 20}   )
    self.assertEqual(image_processor.crop_size				,				{"height": 18, "width": 18}   )
    self.assertEqual(image_processor.do_reduce_labels				,				__lowerCAmelCase   )

    __UpperCAmelCase						    =       self.image_processing_class.from_dict(
        self.image_processor_dict				,				size=42				,				crop_size=84				,				reduce_labels=__lowerCAmelCase   )
    self.assertEqual(image_processor.size				,				{"height": 42, "width": 42}   )
    self.assertEqual(image_processor.crop_size				,				{"height": 84, "width": 84}   )
    self.assertEqual(image_processor.do_reduce_labels				,				__lowerCAmelCase   )






  def 	_UpperCAmelCase				(				self:			Any   ) -> Dict:




    '''simple docstring'''




    pass






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




    '''simple docstring'''




    __UpperCAmelCase						    =       self.image_processing_class(**self.image_processor_dict   )
    # create random PIL images
    __UpperCAmelCase						    =       prepare_image_inputs(self.image_processor_tester				,				equal_resolution=__lowerCAmelCase   )
    for image in image_inputs:
      self.assertIsInstance(__lowerCAmelCase				,				Image.Image   )

    # Test not batched input
    __UpperCAmelCase						    =       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						    =       image_processing(__lowerCAmelCase				,				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:			Tuple   ) -> Union[str, Any]:




    '''simple docstring'''




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

    # Test not batched input
    __UpperCAmelCase						    =       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						    =       image_processing(__lowerCAmelCase				,				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:			Optional[Any]   ) -> List[Any]:




    '''simple docstring'''




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

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




    '''simple docstring'''




    __UpperCAmelCase						    =       self.image_processing_class(**self.image_processor_dict   )
    # create random PyTorch tensors
    __UpperCAmelCase						    =       prepare_image_inputs(self.image_processor_tester				,				equal_resolution=__lowerCAmelCase				,				torchify=__lowerCAmelCase   )
    __UpperCAmelCase						    =       []
    for image in image_inputs:
      self.assertIsInstance(__lowerCAmelCase				,				torch.Tensor   )
      maps.append(torch.zeros(image.shape[-2:]   ).long()   )

    # Test not batched input
    __UpperCAmelCase						    =       image_processing(image_inputs[0]				,				maps[0]				,				return_tensors="pt"   )
    self.assertEqual(
        encoding["pixel_values"].shape				,				(
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size["height"],
            self.image_processor_tester.crop_size["width"],
        )				,				)
    self.assertEqual(
        encoding["labels"].shape				,				(
            1,
            self.image_processor_tester.crop_size["height"],
            self.image_processor_tester.crop_size["width"],
        )				,				)
    self.assertEqual(encoding["labels"].dtype				,				torch.long   )
    self.assertTrue(encoding["labels"].min().item() >= 0   )
    self.assertTrue(encoding["labels"].max().item() <= 255   )

    # Test batched
    __UpperCAmelCase						    =       image_processing(__lowerCAmelCase				,				__lowerCAmelCase				,				return_tensors="pt"   )
    self.assertEqual(
        encoding["pixel_values"].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"],
        )				,				)
    self.assertEqual(
        encoding["labels"].shape				,				(
            self.image_processor_tester.batch_size,
            self.image_processor_tester.crop_size["height"],
            self.image_processor_tester.crop_size["width"],
        )				,				)
    self.assertEqual(encoding["labels"].dtype				,				torch.long   )
    self.assertTrue(encoding["labels"].min().item() >= 0   )
    self.assertTrue(encoding["labels"].max().item() <= 255   )

    # Test not batched input (PIL images)
    __UpperCAmelCase    ,		__UpperCAmelCase						    =       prepare_semantic_single_inputs()

    __UpperCAmelCase						    =       image_processing(__lowerCAmelCase				,				__lowerCAmelCase				,				return_tensors="pt"   )
    self.assertEqual(
        encoding["pixel_values"].shape				,				(
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size["height"],
            self.image_processor_tester.crop_size["width"],
        )				,				)
    self.assertEqual(
        encoding["labels"].shape				,				(
            1,
            self.image_processor_tester.crop_size["height"],
            self.image_processor_tester.crop_size["width"],
        )				,				)
    self.assertEqual(encoding["labels"].dtype				,				torch.long   )
    self.assertTrue(encoding["labels"].min().item() >= 0   )
    self.assertTrue(encoding["labels"].max().item() <= 255   )

    # Test batched input (PIL images)
    __UpperCAmelCase    ,		__UpperCAmelCase						    =       prepare_semantic_batch_inputs()

    __UpperCAmelCase						    =       image_processing(__lowerCAmelCase				,				__lowerCAmelCase				,				return_tensors="pt"   )
    self.assertEqual(
        encoding["pixel_values"].shape				,				(
            2,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size["height"],
            self.image_processor_tester.crop_size["width"],
        )				,				)
    self.assertEqual(
        encoding["labels"].shape				,				(
            2,
            self.image_processor_tester.crop_size["height"],
            self.image_processor_tester.crop_size["width"],
        )				,				)
    self.assertEqual(encoding["labels"].dtype				,				torch.long   )
    self.assertTrue(encoding["labels"].min().item() >= 0   )
    self.assertTrue(encoding["labels"].max().item() <= 255   )






  def 	_UpperCAmelCase				(				self:			Tuple   ) -> List[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.image_processing_class(**self.image_processor_dict   )

    # ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
    __UpperCAmelCase    ,		__UpperCAmelCase						    =       prepare_semantic_single_inputs()
    __UpperCAmelCase						    =       image_processing(__lowerCAmelCase				,				__lowerCAmelCase				,				return_tensors="pt"   )
    self.assertTrue(encoding["labels"].min().item() >= 0   )
    self.assertTrue(encoding["labels"].max().item() <= 150   )

    __UpperCAmelCase						    =       True
    __UpperCAmelCase						    =       image_processing(__lowerCAmelCase				,				__lowerCAmelCase				,				return_tensors="pt"   )
    self.assertTrue(encoding["labels"].min().item() >= 0   )
    self.assertTrue(encoding["labels"].max().item() <= 255   )




from typing import TYPE_CHECKING

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


a_							=      {
    """configuration_altclip""": [
        """ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """AltCLIPConfig""",
        """AltCLIPTextConfig""",
        """AltCLIPVisionConfig""",
    ],
    """processing_altclip""": ["""AltCLIPProcessor"""],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       a_							=      [
           """ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST""",
           """AltCLIPPreTrainedModel""",
           """AltCLIPModel""",
           """AltCLIPTextModel""",
           """AltCLIPVisionModel""",
       ]


if TYPE_CHECKING:
       from .configuration_altclip import (
           ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
           AltCLIPConfig,
           AltCLIPTextConfig,
           AltCLIPVisionConfig,
       )
       from .processing_altclip import AltCLIPProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_altclip import (
                  ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
                  AltCLIPModel,
                  AltCLIPPreTrainedModel,
                  AltCLIPTextModel,
                  AltCLIPVisionModel,
              )


else:
       import sys

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



