'''simple docstring'''

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 SegformerImageProcessor
class 							lowercase_    (     unittest.TestCase						):







    def __init__(	self       ,     UpperCamelCase__       ,     UpperCamelCase__=7       ,     UpperCamelCase__=3       ,     UpperCamelCase__=3_0       ,     UpperCamelCase__=4_0_0       ,     UpperCamelCase__=True       ,     UpperCamelCase__=None       ,     UpperCamelCase__=True       ,     UpperCamelCase__=[0.5, 0.5, 0.5]       ,     UpperCamelCase__=[0.5, 0.5, 0.5]       ,     UpperCamelCase__=False       ,     )				->       int:





     """simple docstring"""

     UpperCAmelCase_				       =					size if size is not None else {"height": 3_0, "width": 3_0}
     UpperCAmelCase_				       =					parent
     UpperCAmelCase_				       =					batch_size
     UpperCAmelCase_				       =					num_channels
     UpperCAmelCase_				       =					min_resolution
     UpperCAmelCase_				       =					max_resolution
     UpperCAmelCase_				       =					do_resize
     UpperCAmelCase_				       =					size
     UpperCAmelCase_				       =					do_normalize
     UpperCAmelCase_				       =					image_mean
     UpperCAmelCase_				       =					image_std
     UpperCAmelCase_				       =					do_reduce_labels







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





     """simple docstring"""

     return {
         "do_resize": self.do_resize,
         "size": self.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__						(       ):
 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__						(       ):
 UpperCAmelCase_				       =					load_dataset("hf-internal-testing/fixtures_ade20k"       ,			split="test"       )

 UpperCAmelCase_				       =					Image.open(dataset[0]["file"]       )
 UpperCAmelCase_				       =					Image.open(dataset[1]["file"]       )
 UpperCAmelCase_				       =					Image.open(dataset[2]["file"]       )
 UpperCAmelCase_				       =					Image.open(dataset[3]["file"]       )

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


@require_torch
@require_vision
class 							lowercase_    (     _A ,  unittest.TestCase						):
    a_             =   SegformerImageProcessor if is_vision_available() else None







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





     """simple docstring"""

     UpperCAmelCase_				       =					SegformerImageProcessingTester(self				)







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





     """simple docstring"""

     return self.image_processor_tester.prepare_image_processor_dict()







    def   lowerCamelCase_    (	self				)				->       Tuple:





     """simple docstring"""

     UpperCAmelCase_				       =					self.image_processing_class(**self.image_processor_dict				)
     self.assertTrue(hasattr(UpperCamelCase__       ,     "do_resize"				)				)
     self.assertTrue(hasattr(UpperCamelCase__       ,     "size"				)				)
     self.assertTrue(hasattr(UpperCamelCase__       ,     "do_normalize"				)				)
     self.assertTrue(hasattr(UpperCamelCase__       ,     "image_mean"				)				)
     self.assertTrue(hasattr(UpperCamelCase__       ,     "image_std"				)				)
     self.assertTrue(hasattr(UpperCamelCase__       ,     "do_reduce_labels"				)				)







    def   lowerCamelCase_    (	self				)				->       Any:





     """simple docstring"""

     UpperCAmelCase_				       =					self.image_processing_class.from_dict(self.image_processor_dict				)
     self.assertEqual(image_processor.size       ,     {"height": 3_0, "width": 3_0}				)
     self.assertEqual(image_processor.do_reduce_labels       ,     UpperCamelCase__				)

     UpperCAmelCase_				       =					self.image_processing_class.from_dict(self.image_processor_dict       ,     size=4_2       ,     reduce_labels=UpperCamelCase__				)
     self.assertEqual(image_processor.size       ,     {"height": 4_2, "width": 4_2}				)
     self.assertEqual(image_processor.do_reduce_labels       ,     UpperCamelCase__				)







    def   lowerCamelCase_    (	self				)				->       int:





     """simple docstring"""

     pass







    def   lowerCamelCase_    (	self				)				->       Optional[int]:





     """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=UpperCamelCase__				)
     for image in image_inputs:
      self.assertIsInstance(UpperCamelCase__       ,     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.size["height"],
             self.image_processor_tester.size["width"],
         )       ,     )

     # Test batched
     UpperCAmelCase_				       =					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.size["height"],
             self.image_processor_tester.size["width"],
         )       ,     )







    def   lowerCamelCase_    (	self				)				->       int:





     """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=UpperCamelCase__       ,     numpify=UpperCamelCase__				)
     for image in image_inputs:
      self.assertIsInstance(UpperCamelCase__       ,     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.size["height"],
             self.image_processor_tester.size["width"],
         )       ,     )

     # Test batched
     UpperCAmelCase_				       =					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.size["height"],
             self.image_processor_tester.size["width"],
         )       ,     )







    def   lowerCamelCase_    (	self				)				->       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=UpperCamelCase__       ,     torchify=UpperCamelCase__				)
     for image in image_inputs:
      self.assertIsInstance(UpperCamelCase__       ,     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.size["height"],
             self.image_processor_tester.size["width"],
         )       ,     )

     # Test batched
     UpperCAmelCase_				       =					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.size["height"],
             self.image_processor_tester.size["width"],
         )       ,     )







    def   lowerCamelCase_    (	self				)				->       Optional[int]:





     """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=UpperCamelCase__       ,     torchify=UpperCamelCase__				)
     UpperCAmelCase_				       =					[]
     for image in image_inputs:
      self.assertIsInstance(UpperCamelCase__       ,     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.size["height"],
             self.image_processor_tester.size["width"],
         )       ,     )
     self.assertEqual(
         encoding["labels"].shape       ,     (
             1,
             self.image_processor_tester.size["height"],
             self.image_processor_tester.size["width"],
         )       ,     )
     self.assertEqual(encoding["labels"].dtype       ,     torch.long				)
     self.assertTrue(encoding["labels"].min().item() >= 0				)
     self.assertTrue(encoding["labels"].max().item() <= 2_5_5				)

     # Test batched
     UpperCAmelCase_				       =					image_processing(UpperCamelCase__       ,     UpperCamelCase__       ,     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.size["height"],
             self.image_processor_tester.size["width"],
         )       ,     )
     self.assertEqual(
         encoding["labels"].shape       ,     (
             self.image_processor_tester.batch_size,
             self.image_processor_tester.size["height"],
             self.image_processor_tester.size["width"],
         )       ,     )
     self.assertEqual(encoding["labels"].dtype       ,     torch.long				)
     self.assertTrue(encoding["labels"].min().item() >= 0				)
     self.assertTrue(encoding["labels"].max().item() <= 2_5_5				)

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

     UpperCAmelCase_				       =					image_processing(UpperCamelCase__       ,     UpperCamelCase__       ,     return_tensors="pt"				)
     self.assertEqual(
         encoding["pixel_values"].shape       ,     (
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size["height"],
             self.image_processor_tester.size["width"],
         )       ,     )
     self.assertEqual(
         encoding["labels"].shape       ,     (
             1,
             self.image_processor_tester.size["height"],
             self.image_processor_tester.size["width"],
         )       ,     )
     self.assertEqual(encoding["labels"].dtype       ,     torch.long				)
     self.assertTrue(encoding["labels"].min().item() >= 0				)
     self.assertTrue(encoding["labels"].max().item() <= 2_5_5				)

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

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







    def   lowerCamelCase_    (	self				)				->       List[str]:





     """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(UpperCamelCase__       ,     UpperCamelCase__       ,     return_tensors="pt"				)
     self.assertTrue(encoding["labels"].min().item() >= 0				)
     self.assertTrue(encoding["labels"].max().item() <= 1_5_0				)

     UpperCAmelCase_				       =					True
     UpperCAmelCase_				       =					image_processing(UpperCamelCase__       ,     UpperCamelCase__       ,     return_tensors="pt"				)
     self.assertTrue(encoding["labels"].min().item() >= 0				)
     self.assertTrue(encoding["labels"].max().item() <= 2_5_5				)

'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


__snake_case       :   Optional[int]  		=    {
    '''configuration_gptsan_japanese''': ['''GPTSAN_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTSanJapaneseConfig'''],
    '''tokenization_gptsan_japanese''': ['''GPTSanJapaneseTokenizer'''],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __snake_case       :   List[Any]  		=    [
          '''GPTSAN_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST''',
          '''GPTSanJapaneseForConditionalGeneration''',
          '''GPTSanJapaneseModel''',
          '''GPTSanJapanesePreTrainedModel''',
      ]
      __snake_case       :   Optional[int]  		=    [
          '''GPTSanJapaneseTokenizer''',
      ]


if TYPE_CHECKING:
      from .configuration_gptsan_japanese import GPTSAN_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTSanJapaneseConfig
      from .tokenization_gptsan_japanese import GPTSanJapaneseTokenizer

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_gptsan_japanese import (
                GPTSAN_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST,
                GPTSanJapaneseForConditionalGeneration,
                GPTSanJapaneseModel,
                GPTSanJapanesePreTrainedModel,
            )
            from .tokenization_gptsan_japanese import GPTSanJapaneseTokenizer


else:
      import sys

      __snake_case       :   Tuple  		=    _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
