


"""simple docstring"""




import inspect
import unittest

from transformers import YolosConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch
	from torch import nn

	from transformers import YolosForObjectDetection, YolosModel
	from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor
class 					lowerCAmelCase	:




		'''simple docstring'''







		def __init__(	self						,       lowerCAmelCase__						,       lowerCAmelCase__=13						,       lowerCAmelCase__=[30, 30]						,       lowerCAmelCase__=2						,       lowerCAmelCase__=3						,       lowerCAmelCase__=True						,       lowerCAmelCase__=True						,       lowerCAmelCase__=32						,       lowerCAmelCase__=5						,       lowerCAmelCase__=4						,       lowerCAmelCase__=37						,       lowerCAmelCase__="gelu"						,       lowerCAmelCase__=0.1						,       lowerCAmelCase__=0.1						,       lowerCAmelCase__=10						,       lowerCAmelCase__=0.02						,       lowerCAmelCase__=3						,       lowerCAmelCase__=None						,       lowerCAmelCase__=8						,       lowerCAmelCase__=10						,       )     ->							int:
								SCREAMING_SNAKE_CASE													=					parent
								SCREAMING_SNAKE_CASE													=					batch_size
								SCREAMING_SNAKE_CASE													=					image_size
								SCREAMING_SNAKE_CASE													=					patch_size
								SCREAMING_SNAKE_CASE													=					num_channels
								SCREAMING_SNAKE_CASE													=					is_training
								SCREAMING_SNAKE_CASE													=					use_labels
								SCREAMING_SNAKE_CASE													=					hidden_size
								SCREAMING_SNAKE_CASE													=					num_hidden_layers
								SCREAMING_SNAKE_CASE													=					num_attention_heads
								SCREAMING_SNAKE_CASE													=					intermediate_size
								SCREAMING_SNAKE_CASE													=					hidden_act
								SCREAMING_SNAKE_CASE													=					hidden_dropout_prob
								SCREAMING_SNAKE_CASE													=					attention_probs_dropout_prob
								SCREAMING_SNAKE_CASE													=					type_sequence_label_size
								SCREAMING_SNAKE_CASE													=					initializer_range
								SCREAMING_SNAKE_CASE													=					num_labels
								SCREAMING_SNAKE_CASE													=					scope
								SCREAMING_SNAKE_CASE													=					n_targets
								SCREAMING_SNAKE_CASE													=					num_detection_tokens
								# we set the expected sequence length (which is used in several tests)
								# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
								SCREAMING_SNAKE_CASE													=					(image_size[1] // patch_size) * (image_size[0] // patch_size)
								SCREAMING_SNAKE_CASE													=					num_patches + 1 + self.num_detection_tokens







		def 							__A						(	self				)     ->							List[str]:
								SCREAMING_SNAKE_CASE													=					floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]				)

								SCREAMING_SNAKE_CASE													=					None
								if self.use_labels:
														# labels is a list of Dict (each Dict being the labels for a given example in the batch)
														SCREAMING_SNAKE_CASE													=					[]
														for i in range(self.batch_size				):
																				SCREAMING_SNAKE_CASE													=					{}
																				SCREAMING_SNAKE_CASE													=					torch.randint(
																				    high=self.num_labels						,       size=(self.n_targets,)						,       device=lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE													=					torch.rand(self.n_targets						,       4						,       device=lowerCAmelCase__				)
																				labels.append(lowerCAmelCase__				)

								SCREAMING_SNAKE_CASE													=					self.get_config()

								return config, pixel_values, labels







		def 							__A						(	self				)     ->							Tuple:
								return YolosConfig(
								    image_size=self.image_size						,       patch_size=self.patch_size						,       num_channels=self.num_channels						,       hidden_size=self.hidden_size						,       num_hidden_layers=self.num_hidden_layers						,       num_attention_heads=self.num_attention_heads						,       intermediate_size=self.intermediate_size						,       hidden_act=self.hidden_act						,       hidden_dropout_prob=self.hidden_dropout_prob						,       attention_probs_dropout_prob=self.attention_probs_dropout_prob						,       is_decoder=lowerCAmelCase__						,       initializer_range=self.initializer_range						,       num_detection_tokens=self.num_detection_tokens						,       num_labels=self.num_labels						,       )







		def 							__A						(	self						,       lowerCAmelCase__						,       lowerCAmelCase__						,       lowerCAmelCase__				)     ->							List[str]:
								SCREAMING_SNAKE_CASE													=					YolosModel(config=lowerCAmelCase__				)
								model.to(lowerCAmelCase__				)
								model.eval()
								SCREAMING_SNAKE_CASE													=					model(lowerCAmelCase__				)
								self.parent.assertEqual(
								    result.last_hidden_state.shape						,       (self.batch_size, self.expected_seq_len, self.hidden_size)				)







		def 							__A						(	self						,       lowerCAmelCase__						,       lowerCAmelCase__						,       lowerCAmelCase__				)     ->							int:
								SCREAMING_SNAKE_CASE													=					YolosForObjectDetection(lowerCAmelCase__				)
								model.to(lowerCAmelCase__				)
								model.eval()

								SCREAMING_SNAKE_CASE													=					model(pixel_values=lowerCAmelCase__				)
								SCREAMING_SNAKE_CASE													=					model(lowerCAmelCase__				)

								self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.num_detection_tokens, self.num_labels + 1)				)
								self.parent.assertEqual(result.pred_boxes.shape						,       (self.batch_size, self.num_detection_tokens, 4)				)

								SCREAMING_SNAKE_CASE													=					model(pixel_values=lowerCAmelCase__						,       labels=lowerCAmelCase__				)

								self.parent.assertEqual(result.loss.shape						,       ()				)
								self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.num_detection_tokens, self.num_labels + 1)				)
								self.parent.assertEqual(result.pred_boxes.shape						,       (self.batch_size, self.num_detection_tokens, 4)				)







		def 							__A						(	self				)     ->							List[Any]:
								SCREAMING_SNAKE_CASE													=					self.prepare_config_and_inputs()
								SCREAMING_SNAKE_CASE							,				SCREAMING_SNAKE_CASE							,				SCREAMING_SNAKE_CASE													=					config_and_inputs
								SCREAMING_SNAKE_CASE													=					{'pixel_values': pixel_values}
								return config, inputs_dict





@require_torch
class 					lowerCAmelCase	(					lowerCamelCase_	,    lowerCamelCase_	,    unittest.TestCase    ):




		'''simple docstring'''







		SCREAMING_SNAKE_CASE_						:     Optional[int]								=     (YolosModel, YolosForObjectDetection) if is_torch_available() else ()
		SCREAMING_SNAKE_CASE_						:     str								=     (
		    {"""feature-extraction""": YolosModel, """object-detection""": YolosForObjectDetection} if is_torch_available() else {}
		)

		SCREAMING_SNAKE_CASE_						:     List[str]								=     False
		SCREAMING_SNAKE_CASE_						:     int								=     False
		SCREAMING_SNAKE_CASE_						:     Tuple								=     False
		SCREAMING_SNAKE_CASE_						:     Optional[int]								=     False







		def 							__A						(	self						,       lowerCAmelCase__						,       lowerCAmelCase__						,       lowerCAmelCase__=False				)     ->							Dict:
								SCREAMING_SNAKE_CASE													=					super()._prepare_for_class(lowerCAmelCase__						,       lowerCAmelCase__						,       return_labels=lowerCAmelCase__				)

								if return_labels:
														if model_class.__name__ == "YolosForObjectDetection":
																				SCREAMING_SNAKE_CASE													=					[]
																				for i in range(self.model_tester.batch_size				):
																										SCREAMING_SNAKE_CASE													=					{}
																										SCREAMING_SNAKE_CASE													=					torch.ones(
																										    size=(self.model_tester.n_targets,)						,       device=lowerCAmelCase__						,       dtype=torch.long				)
																										SCREAMING_SNAKE_CASE													=					torch.ones(
																										    self.model_tester.n_targets						,       4						,       device=lowerCAmelCase__						,       dtype=torch.float				)
																										labels.append(lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE													=					labels

								return inputs_dict







		def 							__A						(	self				)     ->							int:
								SCREAMING_SNAKE_CASE													=					YolosModelTester(self				)
								SCREAMING_SNAKE_CASE													=					ConfigTester(self						,       config_class=lowerCAmelCase__						,       has_text_modality=lowerCAmelCase__						,       hidden_size=37				)







		def 							__A						(	self				)     ->							Dict:
								self.config_tester.run_common_tests()







		def 							__A						(	self				)     ->							List[str]:
								# YOLOS does not use inputs_embeds
								pass







		def 							__A						(	self				)     ->							int:
								SCREAMING_SNAKE_CASE							,				SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
														SCREAMING_SNAKE_CASE													=					model_class(lowerCAmelCase__				)
														self.assertIsInstance(model.get_input_embeddings()						,       (nn.Module)				)
														SCREAMING_SNAKE_CASE													=					model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(lowerCAmelCase__						,       nn.Linear				)				)







		def 							__A						(	self				)     ->							Union[str, Any]:
								SCREAMING_SNAKE_CASE							,				SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
														SCREAMING_SNAKE_CASE													=					model_class(lowerCAmelCase__				)
														SCREAMING_SNAKE_CASE													=					inspect.signature(model.forward				)
														# signature.parameters is an OrderedDict => so arg_names order is deterministic
														SCREAMING_SNAKE_CASE													=					[*signature.parameters.keys()]

														SCREAMING_SNAKE_CASE													=					['pixel_values']
														self.assertListEqual(arg_names[:1]						,       lowerCAmelCase__				)







		def 							__A						(	self				)     ->							str:
								SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCAmelCase__				)







		def 							__A						(	self				)     ->							Optional[int]:
								SCREAMING_SNAKE_CASE							,				SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs_for_common()
								SCREAMING_SNAKE_CASE													=					True

								# in YOLOS, the seq_len is different
								SCREAMING_SNAKE_CASE													=					self.model_tester.expected_seq_len
								for model_class in self.all_model_classes:
														SCREAMING_SNAKE_CASE													=					True
														SCREAMING_SNAKE_CASE													=					False
														SCREAMING_SNAKE_CASE													=					True
														SCREAMING_SNAKE_CASE													=					model_class(lowerCAmelCase__				)
														model.to(lowerCAmelCase__				)
														model.eval()
														with torch.no_grad():
																				SCREAMING_SNAKE_CASE													=					model(**self._prepare_for_class(lowerCAmelCase__						,       lowerCAmelCase__				)				)
														SCREAMING_SNAKE_CASE													=					outputs.attentions
														self.assertEqual(len(lowerCAmelCase__				)						,       self.model_tester.num_hidden_layers				)

														# check that output_attentions also work using config
														del inputs_dict["output_attentions"]
														SCREAMING_SNAKE_CASE													=					True
														SCREAMING_SNAKE_CASE													=					model_class(lowerCAmelCase__				)
														model.to(lowerCAmelCase__				)
														model.eval()
														with torch.no_grad():
																				SCREAMING_SNAKE_CASE													=					model(**self._prepare_for_class(lowerCAmelCase__						,       lowerCAmelCase__				)				)
														SCREAMING_SNAKE_CASE													=					outputs.attentions
														self.assertEqual(len(lowerCAmelCase__				)						,       self.model_tester.num_hidden_layers				)

														self.assertListEqual(
														    list(attentions[0].shape[-3:]				)						,       [self.model_tester.num_attention_heads, seq_len, seq_len]						,       )
														SCREAMING_SNAKE_CASE													=					len(lowerCAmelCase__				)

														# Check attention is always last and order is fine
														SCREAMING_SNAKE_CASE													=					True
														SCREAMING_SNAKE_CASE													=					True
														SCREAMING_SNAKE_CASE													=					model_class(lowerCAmelCase__				)
														model.to(lowerCAmelCase__				)
														model.eval()
														with torch.no_grad():
																				SCREAMING_SNAKE_CASE													=					model(**self._prepare_for_class(lowerCAmelCase__						,       lowerCAmelCase__				)				)

														SCREAMING_SNAKE_CASE													=					1
														self.assertEqual(out_len + added_hidden_states						,       len(lowerCAmelCase__				)				)

														SCREAMING_SNAKE_CASE													=					outputs.attentions

														self.assertEqual(len(lowerCAmelCase__				)						,       self.model_tester.num_hidden_layers				)
														self.assertListEqual(
														    list(self_attentions[0].shape[-3:]				)						,       [self.model_tester.num_attention_heads, seq_len, seq_len]						,       )







		def 							__A						(	self				)     ->							Optional[Any]:
								def check_hidden_states_output(lowerCAmelCase__						,       lowerCAmelCase__						,       lowerCAmelCase__				):
														SCREAMING_SNAKE_CASE													=					model_class(lowerCAmelCase__				)
														model.to(lowerCAmelCase__				)
														model.eval()

														with torch.no_grad():
																				SCREAMING_SNAKE_CASE													=					model(**self._prepare_for_class(lowerCAmelCase__						,       lowerCAmelCase__				)				)

														SCREAMING_SNAKE_CASE													=					outputs.hidden_states

														SCREAMING_SNAKE_CASE													=					getattr(
														    self.model_tester						,       'expected_num_hidden_layers'						,       self.model_tester.num_hidden_layers + 1				)
														self.assertEqual(len(lowerCAmelCase__				)						,       lowerCAmelCase__				)

														# YOLOS has a different seq_length
														SCREAMING_SNAKE_CASE													=					self.model_tester.expected_seq_len

														self.assertListEqual(
														    list(hidden_states[0].shape[-2:]				)						,       [seq_length, self.model_tester.hidden_size]						,       )

								SCREAMING_SNAKE_CASE							,				SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
														SCREAMING_SNAKE_CASE													=					True
														check_hidden_states_output(lowerCAmelCase__						,       lowerCAmelCase__						,       lowerCAmelCase__				)

														# check that output_hidden_states also work using config
														del inputs_dict["output_hidden_states"]
														SCREAMING_SNAKE_CASE													=					True

														check_hidden_states_output(lowerCAmelCase__						,       lowerCAmelCase__						,       lowerCAmelCase__				)







		def 							__A						(	self				)     ->							int:
								SCREAMING_SNAKE_CASE													=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_object_detection(*lowerCAmelCase__				)







		@slow
		def 							__A						(	self				)     ->							Any:
								for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														SCREAMING_SNAKE_CASE													=					YolosModel.from_pretrained(lowerCAmelCase__				)
														self.assertIsNotNone(lowerCAmelCase__				)





def     lowercase			()   ->						Optional[Any]:
						SCREAMING_SNAKE_CASE													=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
						return image


@require_torch
@require_vision
class 					lowerCAmelCase	(					unittest.TestCase    ):




		'''simple docstring'''







		@cached_property
		def 							__A						(	self				)     ->							Tuple:
								return AutoImageProcessor.from_pretrained('hustvl/yolos-small'				) if is_vision_available() else None







		@slow
		def 							__A						(	self				)     ->							str:
								SCREAMING_SNAKE_CASE													=					YolosForObjectDetection.from_pretrained('hustvl/yolos-small'				).to(lowerCAmelCase__				)

								SCREAMING_SNAKE_CASE													=					self.default_image_processor
								SCREAMING_SNAKE_CASE													=					prepare_img()
								SCREAMING_SNAKE_CASE													=					image_processor(images=lowerCAmelCase__						,       return_tensors='pt'				).to(lowerCAmelCase__				)

								# forward pass
								with torch.no_grad():
														SCREAMING_SNAKE_CASE													=					model(inputs.pixel_values				)

								# verify outputs
								SCREAMING_SNAKE_CASE													=					torch.Size((1, 100, 92)				)
								self.assertEqual(outputs.logits.shape						,       lowerCAmelCase__				)

								SCREAMING_SNAKE_CASE													=					torch.tensor(
								    [[-24.02_48, -10.30_24, -14.82_90], [-42.03_92, -16.82_00, -27.43_34], [-27.27_43, -11.81_54, -18.71_48]]						,       device=lowerCAmelCase__						,       )
								SCREAMING_SNAKE_CASE													=					torch.tensor(
								    [[0.25_59, 0.54_55, 0.47_06], [0.29_89, 0.72_79, 0.18_75], [0.77_32, 0.40_17, 0.44_62]]						,       device=lowerCAmelCase__				)
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]						,       lowerCAmelCase__						,       atol=1e-4				)				)
								self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]						,       lowerCAmelCase__						,       atol=1e-4				)				)

								# verify postprocessing
								SCREAMING_SNAKE_CASE													=					image_processor.post_process_object_detection(
								    lowerCAmelCase__						,       threshold=0.3						,       target_sizes=[image.size[::-1]]				)[0]
								SCREAMING_SNAKE_CASE													=					torch.tensor([0.99_94, 0.97_90, 0.99_64, 0.99_72, 0.98_61]				).to(lowerCAmelCase__				)
								SCREAMING_SNAKE_CASE													=					[75, 75, 17, 63, 17]
								SCREAMING_SNAKE_CASE													=					torch.tensor([3_35.06_09, 79.38_48, 3_75.42_16, 1_87.24_95]				).to(lowerCAmelCase__				)

								self.assertEqual(len(results['scores']				)						,       5				)
								self.assertTrue(torch.allclose(results['scores']						,       lowerCAmelCase__						,       atol=1e-4				)				)
								self.assertSequenceEqual(results['labels'].tolist()						,       lowerCAmelCase__				)
								self.assertTrue(torch.allclose(results['boxes'][0, :]						,       lowerCAmelCase__				)				)






"""simple docstring"""




def     lowercase			(SCREAMING_SNAKE_CASE_		:				int = 10_00  )   ->						int:
						SCREAMING_SNAKE_CASE													=					2**power
						SCREAMING_SNAKE_CASE													=					0
						while n:
												SCREAMING_SNAKE_CASE							,				SCREAMING_SNAKE_CASE													=					r + n % 10, n // 10
						return r


if __name__ == "__main__":
	print(solution(int(str(input()).strip())))


