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


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





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							size if size is not None else {'''height''': 20, '''width''': 20}
												SCREAMING_SNAKE_CASE							:    List[Any]							=							crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							parent
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							batch_size
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							num_channels
												SCREAMING_SNAKE_CASE							:    str							=							image_size
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							min_resolution
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							max_resolution
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							do_resize
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							size
												SCREAMING_SNAKE_CASE							:    int							=							do_center_crop
												SCREAMING_SNAKE_CASE							:    List[str]							=							crop_size
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							do_normalize
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							image_mean
												SCREAMING_SNAKE_CASE							:    Dict							=							image_std
												SCREAMING_SNAKE_CASE							:    int							=							do_reduce_labels



						def        __A       (       self	:				List[str]						):





												'''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	(    ):
						SCREAMING_SNAKE_CASE							:    Dict							=							load_dataset('''hf-internal-testing/fixtures_ade20k'''		,  split='''test'''						)

						SCREAMING_SNAKE_CASE							:    Optional[Any]							=							Image.open(dataset[0]['''file''']						)
						SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							Image.open(dataset[1]['''file''']						)

						return image, map
def       A	(    ):
						SCREAMING_SNAKE_CASE							:    List[str]							=							load_dataset('''hf-internal-testing/fixtures_ade20k'''		,  split='''test'''						)

						SCREAMING_SNAKE_CASE							:    Optional[int]							=							Image.open(ds[0]['''file''']						)
						SCREAMING_SNAKE_CASE							:    str							=							Image.open(ds[1]['''file''']						)
						SCREAMING_SNAKE_CASE							:    int							=							Image.open(ds[2]['''file''']						)
						SCREAMING_SNAKE_CASE							:    Any							=							Image.open(ds[3]['''file''']						)

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





@require_torch
@require_vision
class 					lowercase__						(   UpperCamelCase_	,							unittest.TestCase):
						UpperCamelCase_     =		BeitImageProcessor if is_vision_available() else None


						def        __A       (       self	:				Tuple						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    str							=							BeitImageProcessingTester(self						)


						@property
						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												return self.image_processor_tester.prepare_image_processor_dict()


						def        __A       (       self	:				str						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    int							=							self.image_processing_class(**self.image_processor_dict						)
												self.assertTrue(hasattr(UpperCamelCase__    ,		'''do_resize'''						)						)
												self.assertTrue(hasattr(UpperCamelCase__    ,		'''size'''						)						)
												self.assertTrue(hasattr(UpperCamelCase__    ,		'''do_center_crop'''						)						)
												self.assertTrue(hasattr(UpperCamelCase__    ,		'''center_crop'''						)						)
												self.assertTrue(hasattr(UpperCamelCase__    ,		'''do_normalize'''						)						)
												self.assertTrue(hasattr(UpperCamelCase__    ,		'''image_mean'''						)						)
												self.assertTrue(hasattr(UpperCamelCase__    ,		'''image_std'''						)						)


						def        __A       (       self	:				Tuple						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							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    ,		UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    str							=							self.image_processing_class.from_dict(
												    self.image_processor_dict    ,		size=42    ,		crop_size=84    ,		reduce_labels=UpperCamelCase__						)
												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    ,		UpperCamelCase__						)


						def        __A       (       self	:				Union[str, Any]						):





												'''simple docstring'''





												pass


						def        __A       (       self	:				str						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							self.image_processing_class(**self.image_processor_dict						)
												# create random PIL images
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							prepare_image_inputs(self.image_processor_tester    ,		equal_resolution=UpperCamelCase__						)
												for image in image_inputs:
																		self.assertIsInstance(UpperCamelCase__    ,		Image.Image						)

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


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							self.image_processing_class(**self.image_processor_dict						)
												# create random numpy tensors
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							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
												SCREAMING_SNAKE_CASE							:    List[str]							=							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
												SCREAMING_SNAKE_CASE							:    str							=							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.crop_size['''height'''],
												        self.image_processor_tester.crop_size['''width'''],
												    )    ,		)


						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    int							=							self.image_processing_class(**self.image_processor_dict						)
												# create random PyTorch tensors
												SCREAMING_SNAKE_CASE							:    List[str]							=							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
												SCREAMING_SNAKE_CASE							:    Any							=							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
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							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.crop_size['''height'''],
												        self.image_processor_tester.crop_size['''width'''],
												    )    ,		)


						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





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

												# Test not batched input
												SCREAMING_SNAKE_CASE							:    List[Any]							=							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
												SCREAMING_SNAKE_CASE							:    str							=							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.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)
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Dict							=							prepare_semantic_single_inputs()

												SCREAMING_SNAKE_CASE							:    List[str]							=							image_processing(UpperCamelCase__    ,		UpperCamelCase__    ,		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)
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    str							=							prepare_semantic_batch_inputs()

												SCREAMING_SNAKE_CASE							:    Tuple							=							image_processing(UpperCamelCase__    ,		UpperCamelCase__    ,		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        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							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							:    Tuple							=							prepare_semantic_single_inputs()
												SCREAMING_SNAKE_CASE							:    List[str]							=							image_processing(UpperCamelCase__    ,		UpperCamelCase__    ,		return_tensors='''pt'''						)
												self.assertTrue(encoding['''labels'''].min().item() >= 0						)
												self.assertTrue(encoding['''labels'''].max().item() <= 150						)

												SCREAMING_SNAKE_CASE							:    Any							=							True
												SCREAMING_SNAKE_CASE							:    int							=							image_processing(UpperCamelCase__    ,		UpperCamelCase__    ,		return_tensors='''pt'''						)
												self.assertTrue(encoding['''labels'''].min().item() >= 0						)
												self.assertTrue(encoding['''labels'''].max().item() <= 255						)






from math import ceil
def       A	(    _lowercase = 1_001						):
						SCREAMING_SNAKE_CASE							:    Any							=							1

						for i in range(1		,  int(ceil(n / 2.0						)						)						):
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							2 * i + 1
												SCREAMING_SNAKE_CASE							:    int							=							2 * i
												SCREAMING_SNAKE_CASE							:    List[str]							=							total + 4 * odd**2 - 6 * even

						return total


if __name__ == "__main__":
					import sys

					if len(sys.argv) == 1:
										print(solution())
					else:
										try:
															__UpperCamelCase		:       Dict									=     int(sys.argv[1])
															print(solution(n))
										except ValueError:
															print('Invalid entry - please enter a number')





