






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



     """simple docstring"""



     def __init__(						self			:					Tuple       ,       lowerCAmelCase__			:					Any       ,       lowerCAmelCase__			:					List[Any]=7       ,       lowerCAmelCase__			:					Optional[int]=3       ,       lowerCAmelCase__			:					Any=3_0       ,       lowerCAmelCase__			:					List[str]=4_0_0       ,       lowerCAmelCase__			:					List[str]=True       ,       lowerCAmelCase__			:					str=None       ,       lowerCAmelCase__			:					Optional[int]=True       ,       lowerCAmelCase__			:					List[str]=[0.5, 0.5, 0.5]       ,       lowerCAmelCase__			:					Dict=[0.5, 0.5, 0.5]       ,       lowerCAmelCase__			:					Any=False       ,       )    ->					Any:

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





     def   __UpperCamelCase		(						self			:					Any)    ->					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 		A_			(		)							->int:







 """simple docstring"""

 SCREAMING_SNAKE_CASE							  =						load_dataset('hf-internal-testing/fixtures_ade20k'					,       split='test'	)

 SCREAMING_SNAKE_CASE							  =						Image.open(dataset[0]['file']	)
 SCREAMING_SNAKE_CASE							  =						Image.open(dataset[1]['file']	)

 return image, map





def 		A_			(		)							->List[Any]:







 """simple docstring"""

 SCREAMING_SNAKE_CASE							  =						load_dataset('hf-internal-testing/fixtures_ade20k'					,       split='test'	)

 SCREAMING_SNAKE_CASE							  =						Image.open(dataset[0]['file']	)
 SCREAMING_SNAKE_CASE							  =						Image.open(dataset[1]['file']	)
 SCREAMING_SNAKE_CASE							  =						Image.open(dataset[2]['file']	)
 SCREAMING_SNAKE_CASE							  =						Image.open(dataset[3]['file']	)

 return [imagea, imagea], [mapa, mapa]
@require_torch
@require_vision
class 					a_(						lowercase__	,    unittest.TestCase					):



     """simple docstring"""



     __snake_case						:							Optional[Any]       			=SegformerImageProcessor if is_vision_available() else None





     def   __UpperCamelCase		(						self			:					str)    ->					int:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						SegformerImageProcessingTester(self)





     @property
     def   __UpperCamelCase		(						self			:					Optional[int])    ->					List[str]:

      """simple docstring"""
      return self.image_processor_tester.prepare_image_processor_dict()





     def   __UpperCamelCase		(						self			:					Any)    ->					Dict:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						self.image_processing_class(**self.image_processor_dict)
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'do_resize'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'size'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'do_normalize'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'image_mean'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'image_std'))
      self.assertTrue(hasattr(lowerCAmelCase__       ,       'do_reduce_labels'))





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

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						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       ,       lowerCAmelCase__)

      SCREAMING_SNAKE_CASE							  =						self.image_processing_class.from_dict(self.image_processor_dict       ,       size=4_2       ,       reduce_labels=lowerCAmelCase__)
      self.assertEqual(image_processor.size       ,       {'height': 4_2, 'width': 4_2})
      self.assertEqual(image_processor.do_reduce_labels       ,       lowerCAmelCase__)





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

      """simple docstring"""
      pass





     def   __UpperCamelCase		(						self			:					List[str])    ->					Dict:

      """simple docstring"""
      # Initialize image_processing
      SCREAMING_SNAKE_CASE							  =						self.image_processing_class(**self.image_processor_dict)
      # create random PIL images
      SCREAMING_SNAKE_CASE							  =						prepare_image_inputs(self.image_processor_tester       ,       equal_resolution=lowerCAmelCase__)
      for image in image_inputs:
       self.assertIsInstance(lowerCAmelCase__       ,       Image.Image)

      # Test not batched input
      SCREAMING_SNAKE_CASE							  =						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
      SCREAMING_SNAKE_CASE							  =						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.size['height'],
              self.image_processor_tester.size['width'],
          )       ,       )





     def   __UpperCamelCase		(						self			:					Tuple)    ->					Dict:

      """simple docstring"""
      # Initialize image_processing
      SCREAMING_SNAKE_CASE							  =						self.image_processing_class(**self.image_processor_dict)
      # create random numpy tensors
      SCREAMING_SNAKE_CASE							  =						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
      SCREAMING_SNAKE_CASE							  =						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
      SCREAMING_SNAKE_CASE							  =						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.size['height'],
              self.image_processor_tester.size['width'],
          )       ,       )





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

      """simple docstring"""
      # Initialize image_processing
      SCREAMING_SNAKE_CASE							  =						self.image_processing_class(**self.image_processor_dict)
      # create random PyTorch tensors
      SCREAMING_SNAKE_CASE							  =						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
      SCREAMING_SNAKE_CASE							  =						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
      SCREAMING_SNAKE_CASE							  =						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.size['height'],
              self.image_processor_tester.size['width'],
          )       ,       )





     def   __UpperCamelCase		(						self			:					Optional[Any])    ->					Tuple:

      """simple docstring"""
      # Initialize image_processing
      SCREAMING_SNAKE_CASE							  =						self.image_processing_class(**self.image_processor_dict)
      # create random PyTorch tensors
      SCREAMING_SNAKE_CASE							  =						prepare_image_inputs(self.image_processor_tester       ,       equal_resolution=lowerCAmelCase__       ,       torchify=lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						[]
      for image in image_inputs:
       self.assertIsInstance(lowerCAmelCase__       ,       torch.Tensor)
       maps.append(torch.zeros(image.shape[-2:]).long())

      # Test not batched input
      SCREAMING_SNAKE_CASE							  =						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
      SCREAMING_SNAKE_CASE							  =						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.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)
      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						prepare_semantic_single_inputs()

      SCREAMING_SNAKE_CASE							  =						image_processing(lowerCAmelCase__       ,       lowerCAmelCase__       ,       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)
      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						prepare_semantic_batch_inputs()

      SCREAMING_SNAKE_CASE							  =						image_processing(lowerCAmelCase__       ,       lowerCAmelCase__       ,       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   __UpperCamelCase		(						self			:					Dict)    ->					Any:

      """simple docstring"""
      # Initialize image_processing
      SCREAMING_SNAKE_CASE							  =						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
      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						prepare_semantic_single_inputs()
      SCREAMING_SNAKE_CASE							  =						image_processing(lowerCAmelCase__       ,       lowerCAmelCase__       ,       return_tensors='pt')
      self.assertTrue(encoding['labels'].min().item() >= 0)
      self.assertTrue(encoding['labels'].max().item() <= 1_5_0)

      SCREAMING_SNAKE_CASE							  =						True
      SCREAMING_SNAKE_CASE							  =						image_processing(lowerCAmelCase__       ,       lowerCAmelCase__       ,       return_tensors='pt')
      self.assertTrue(encoding['labels'].min().item() >= 0)
      self.assertTrue(encoding['labels'].max().item() <= 2_5_5)












def 		A_			(		lowercase_	)							->int:







 """simple docstring"""

 if not isinstance(lowercase_					,       lowercase_	):
  SCREAMING_SNAKE_CASE							  =						f'''Input value of [number={number}] must be an integer'''
  raise TypeError(lowercase_	)
 if number < 1:
  raise ValueError('Input must be a positive integer'	)
 return number * (2 * number - 1)


if __name__ == "__main__":
      import doctest

      doctest.testmod()




