




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


 '''simple docstring'''







 def __init__(							self						:	List[str]       ,       _lowerCAmelCase						:	int       ,       _lowerCAmelCase						:	List[Any]=7       ,       _lowerCAmelCase						:	Optional[int]=3       ,       _lowerCAmelCase						:	List[str]=30       ,       _lowerCAmelCase						:	Optional[Any]=400       ,       _lowerCAmelCase						:	Dict=True       ,       _lowerCAmelCase						:	Tuple=None       ,       _lowerCAmelCase						:	Tuple=True       ,       _lowerCAmelCase						:	Tuple=[0.5, 0.5, 0.5]       ,       _lowerCAmelCase						:	Optional[int]=[0.5, 0.5, 0.5]       ,       _lowerCAmelCase						:	List[Any]=False       ,       ):
       SCREAMING_SNAKE_CASE_					=    size if size is not None else {'height': 30, 'width': 30}
       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    lowerCAmelCase_						(							self						:	Optional[int]		):
       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       UpperCAmelCase_							(   )      ->				Dict:
      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       UpperCAmelCase_							(   )      ->				Any:
      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  lowerCamelCase_							(   _UpperCAmelCase ,     unittest.TestCase      ):


 '''simple docstring'''







 lowercase_       					=					SegformerImageProcessor if is_vision_available() else None



 def    lowerCAmelCase_						(							self						:	Optional[int]		):
       SCREAMING_SNAKE_CASE_					=    SegformerImageProcessingTester(self		)



 @property
 def    lowerCAmelCase_						(							self						:	Any		):
       return self.image_processor_tester.prepare_image_processor_dict()



 def    lowerCAmelCase_						(							self						:	int		):
       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    lowerCAmelCase_						(							self						:	int		):
       SCREAMING_SNAKE_CASE_					=    self.image_processing_class.from_dict(self.image_processor_dict		)
       self.assertEqual(image_processor.size       ,       {'height': 30, 'width': 30}		)
       self.assertEqual(image_processor.do_reduce_labels       ,       _lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    self.image_processing_class.from_dict(self.image_processor_dict       ,       size=42       ,       reduce_labels=_lowerCAmelCase		)
       self.assertEqual(image_processor.size       ,       {'height': 42, 'width': 42}		)
       self.assertEqual(image_processor.do_reduce_labels       ,       _lowerCAmelCase		)



 def    lowerCAmelCase_						(							self						:	List[Any]		):
       pass



 def    lowerCAmelCase_						(							self						:	Union[str, Any]		):
       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    lowerCAmelCase_						(							self						:	List[str]		):
       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    lowerCAmelCase_						(							self						:	str		):
       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    lowerCAmelCase_						(							self						:	int		):
       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() <= 255		)

       # 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() <= 255		)

       # 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() <= 255		)

       # 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() <= 255		)




 def    lowerCAmelCase_						(							self						:	List[Any]		):
       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() <= 150		)

       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() <= 255		)





def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round((float(__UpperCAmelCase							) * 9 / 5) + 32    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round(float(__UpperCAmelCase							) + 2_7_3.1_5    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round((float(__UpperCAmelCase							) * 9 / 5) + 4_9_1.6_7    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round((float(__UpperCAmelCase							) - 32) * 5 / 9    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round(((float(__UpperCAmelCase							) - 32) * 5 / 9) + 2_7_3.1_5    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round(float(__UpperCAmelCase							) + 4_5_9.6_7    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round(float(__UpperCAmelCase							) - 2_7_3.1_5    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round(((float(__UpperCAmelCase							) - 2_7_3.1_5) * 9 / 5) + 32    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round((float(__UpperCAmelCase							) * 9 / 5)    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round((float(__UpperCAmelCase							) - 4_9_1.6_7) * 5 / 9    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round(float(__UpperCAmelCase							) - 4_5_9.6_7    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round((float(__UpperCAmelCase							) * 5 / 9)    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round((float(__UpperCAmelCase							) * 1.2_5 + 2_7_3.1_5)    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round((float(__UpperCAmelCase							) * 2.2_5 + 32)    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round((float(__UpperCAmelCase							) * 1.2_5)    ,    __UpperCAmelCase							)

def       UpperCAmelCase_							(   __UpperCAmelCase		:			float    ,    __UpperCAmelCase		:			int = 2							)      ->				float:
      return round((float(__UpperCAmelCase							) * 2.2_5 + 32 + 4_5_9.6_7)    ,    __UpperCAmelCase							)


if __name__ == "__main__":
   import doctest

   doctest.testmod()