

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






     def __init__(							self		,	_lowercase		,	_lowercase=7		,	_lowercase=3		,	_lowercase=30		,	_lowercase=400		,	_lowercase=True		,	_lowercase=None		,	_lowercase=True		,	_lowercase=[0.5, 0.5, 0.5]		,	_lowercase=[0.5, 0.5, 0.5]		,	_lowercase=False		,	)->    Tuple:
         UpperCamelCase_      =	size if size is not None else {"height": 30, "width": 30}
         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   UpperCAmelCase_							(							self  )->    List[str]:
         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(			)->	Dict:



    """simple docstring"""






    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(			)->	str:



    """simple docstring"""






    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 	__magic_name__				(       snake_case ,			unittest.TestCase							):
     UpperCamelCase_  :str								=			SegformerImageProcessor if is_vision_available() else None






     def   UpperCAmelCase_							(							self  )->    Optional[int]:
         UpperCamelCase_      =	SegformerImageProcessingTester(self  )






     @property
     def   UpperCAmelCase_							(							self  )->    List[Any]:
         return self.image_processor_tester.prepare_image_processor_dict()






     def   UpperCAmelCase_							(							self  )->    List[str]:
         UpperCamelCase_      =	self.image_processing_class(**self.image_processor_dict  )
         self.assertTrue(hasattr(_lowercase		,	"do_resize"  )  )
         self.assertTrue(hasattr(_lowercase		,	"size"  )  )
         self.assertTrue(hasattr(_lowercase		,	"do_normalize"  )  )
         self.assertTrue(hasattr(_lowercase		,	"image_mean"  )  )
         self.assertTrue(hasattr(_lowercase		,	"image_std"  )  )
         self.assertTrue(hasattr(_lowercase		,	"do_reduce_labels"  )  )






     def   UpperCAmelCase_							(							self  )->    Optional[int]:
         UpperCamelCase_      =	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		,	_lowercase  )

         UpperCamelCase_      =	self.image_processing_class.from_dict(self.image_processor_dict		,	size=42		,	reduce_labels=_lowercase  )
         self.assertEqual(image_processor.size		,	{"height": 42, "width": 42}  )
         self.assertEqual(image_processor.do_reduce_labels		,	_lowercase  )






     def   UpperCAmelCase_							(							self  )->    Optional[int]:
         pass






     def   UpperCAmelCase_							(							self  )->    List[Any]:
         # Initialize image_processing
         UpperCamelCase_      =	self.image_processing_class(**self.image_processor_dict  )
         # create random PIL images
         UpperCamelCase_      =	prepare_image_inputs(self.image_processor_tester		,	equal_resolution=_lowercase  )
         for image in image_inputs:
             self.assertIsInstance(_lowercase		,	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(_lowercase		,	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  )->    str:
         # Initialize image_processing
         UpperCamelCase_      =	self.image_processing_class(**self.image_processor_dict  )
         # create random numpy tensors
         UpperCamelCase_      =	prepare_image_inputs(self.image_processor_tester		,	equal_resolution=_lowercase		,	numpify=_lowercase  )
         for image in image_inputs:
             self.assertIsInstance(_lowercase		,	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(_lowercase		,	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]:
         # Initialize image_processing
         UpperCamelCase_      =	self.image_processing_class(**self.image_processor_dict  )
         # create random PyTorch tensors
         UpperCamelCase_      =	prepare_image_inputs(self.image_processor_tester		,	equal_resolution=_lowercase		,	torchify=_lowercase  )
         for image in image_inputs:
             self.assertIsInstance(_lowercase		,	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(_lowercase		,	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  )->    Dict:
         # Initialize image_processing
         UpperCamelCase_      =	self.image_processing_class(**self.image_processor_dict  )
         # create random PyTorch tensors
         UpperCamelCase_      =	prepare_image_inputs(self.image_processor_tester		,	equal_resolution=_lowercase		,	torchify=_lowercase  )
         UpperCamelCase_      =	[]
         for image in image_inputs:
             self.assertIsInstance(_lowercase		,	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() <= 255  )

         # Test batched
         UpperCamelCase_      =	image_processing(_lowercase		,	_lowercase		,	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)
         UpperCamelCase_     ,					UpperCamelCase_      =	prepare_semantic_single_inputs()

         UpperCamelCase_      =	image_processing(_lowercase		,	_lowercase		,	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)
         UpperCamelCase_     ,					UpperCamelCase_      =	prepare_semantic_batch_inputs()

         UpperCamelCase_      =	image_processing(_lowercase		,	_lowercase		,	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   UpperCAmelCase_							(							self  )->    Optional[Any]:
         # Initialize image_processing
         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(_lowercase		,	_lowercase		,	return_tensors="pt"  )
         self.assertTrue(encoding["labels"].min().item() >= 0  )
         self.assertTrue(encoding["labels"].max().item() <= 150  )

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



def  lowerCAmelCase(			SCREAMING_SNAKE_CASE_ ,	SCREAMING_SNAKE_CASE_ ,	SCREAMING_SNAKE_CASE_ )->	str:



    """simple docstring"""






    if power < 0:
        return -1
    base %= mod
    UpperCamelCase_      =	1

    while power > 0:
        if power & 1:
            UpperCamelCase_      =	(result * base) % mod
        UpperCamelCase_      =	power >> 1
        UpperCamelCase_      =	(base * base) % mod

    return result




def  lowerCAmelCase(			)->	Tuple:



    """simple docstring"""






    print(modular_exponential(3 ,	2_0_0 ,	1_3 ) )


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 main()
