


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

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





									"""simple docstring"""







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


							def 				lowerCamelCase_			(							self:     Optional[int]							)      ->       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       _a			(				):


		"""simple docstring"""




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

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

		return image, map
def       _a			(				):


		"""simple docstring"""




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

		lowercase__						=				Image.open(ds[0]['''file''']					)
		lowercase__						=				Image.open(ds[1]['''file''']					)
		lowercase__						=				Image.open(ds[2]['''file''']					)
		lowercase__						=				Image.open(ds[3]['''file''']					)

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





@require_torch
@require_vision
class 	_a							(      __snake_case					,     unittest.TestCase      ):
							_lowercase     :      Tuple  				=			BeitImageProcessor if is_vision_available() else None

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





									"""simple docstring"""







									lowercase__						=				BeitImageProcessingTester(self							)

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





									"""simple docstring"""







									return self.image_processor_tester.prepare_image_processor_dict()

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





									"""simple docstring"""







									lowercase__						=				self.image_processing_class(**self.image_processor_dict							)
									self.assertTrue(hasattr(a_   ,						'''do_resize'''							)							)
									self.assertTrue(hasattr(a_   ,						'''size'''							)							)
									self.assertTrue(hasattr(a_   ,						'''do_center_crop'''							)							)
									self.assertTrue(hasattr(a_   ,						'''center_crop'''							)							)
									self.assertTrue(hasattr(a_   ,						'''do_normalize'''							)							)
									self.assertTrue(hasattr(a_   ,						'''image_mean'''							)							)
									self.assertTrue(hasattr(a_   ,						'''image_std'''							)							)

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





									"""simple docstring"""







									lowercase__						=				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   ,						a_							)

									lowercase__						=				self.image_processing_class.from_dict(
									    self.image_processor_dict   ,						size=42   ,						crop_size=84   ,						reduce_labels=a_							)
									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   ,						a_							)

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





									"""simple docstring"""







									pass

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





									"""simple docstring"""







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

									# Test not batched input
									lowercase__						=				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
									lowercase__						=				image_processing(a_   ,						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 				lowerCamelCase_			(							self:     Tuple							)      ->       List[str]:





									"""simple docstring"""







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

									# Test not batched input
									lowercase__						=				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
									lowercase__						=				image_processing(a_   ,						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 				lowerCamelCase_			(							self:     int							)      ->       Tuple:





									"""simple docstring"""







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

									# Test not batched input
									lowercase__						=				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
									lowercase__						=				image_processing(a_   ,						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 				lowerCamelCase_			(							self:     Any							)      ->       Tuple:





									"""simple docstring"""







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

									# Test not batched input
									lowercase__						=				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
									lowercase__						=				image_processing(a_   ,						a_   ,						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)
									lowercase__						=				prepare_semantic_single_inputs()

									lowercase__						=				image_processing(a_   ,						a_   ,						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)
									lowercase__						=				prepare_semantic_batch_inputs()

									lowercase__						=				image_processing(a_   ,						a_   ,						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 				lowerCamelCase_			(							self:     List[str]							)      ->       Any:





									"""simple docstring"""







									lowercase__						=				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
									lowercase__						=				prepare_semantic_single_inputs()
									lowercase__						=				image_processing(a_   ,						a_   ,						return_tensors='''pt'''							)
									self.assertTrue(encoding['''labels'''].min().item() >= 0							)
									self.assertTrue(encoding['''labels'''].max().item() <= 150							)

									lowercase__						=				True
									lowercase__						=				image_processing(a_   ,						a_   ,						return_tensors='''pt'''							)
									self.assertTrue(encoding['''labels'''].min().item() >= 0							)
									self.assertTrue(encoding['''labels'''].max().item() <= 255							)






lowerCAmelCase	=							[sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(10_0000)]
def       _a			(				SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		lowercase__						=				0
		while number:
				# Increased Speed Slightly by checking every 5 digits together.
				sum_of_digits_squared += DIGITS_SQUARED[number % 10_00_00]
				number //= 10_00_00

		return sum_of_digits_squared


# There are 2 Chains made,
# One ends with 89 with the chain member 58 being the one which when declared first,
# there will be the least number of iterations for all the members to be checked.

# The other one ends with 1 and has only one element 1.

# So 58 and 1 are chosen to be declared at the starting.

# Changed dictionary to an array to quicken the solution
lowerCAmelCase	=							[None] * 1000_0000
lowerCAmelCase	=							True
lowerCAmelCase	=							False
def       _a			(				SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		if CHAINS[number - 1] is not None:
				return CHAINS[number - 1]  # type: ignore

		lowercase__						=				chain(next_number(SCREAMING_SNAKE_CASE					)					)
		lowercase__						=				number_chain

		while number < 10_00_00_00:
				lowercase__						=				number_chain
				number *= 10

		return number_chain
def       _a			(				SCREAMING_SNAKE_CASE = 10_00_00_00					):


		"""simple docstring"""




		for i in range(1						,    SCREAMING_SNAKE_CASE					):
				if CHAINS[i] is None:
						chain(i + 1					)

		return CHAINS[:number].count(SCREAMING_SNAKE_CASE					)


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			print(f"""{solution() = }""")


