




import logging
import os
from dataclasses import dataclass, field
from typing import Dict, Optional

import numpy as np
from utils_multiple_choice import MultipleChoiceDataset, Split, processors

import transformers
from transformers import (
    AutoConfig,
    AutoModelForMultipleChoice,
    AutoTokenizer,
    DataCollatorWithPadding,
    EvalPrediction,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    set_seed,
)
from transformers.trainer_utils import is_main_process


__lowerCAmelCase	:       List[str]     =	logging.getLogger(__name__)




def     __magic_name__       (				A	:      int,						A	:      str     ):






			'''simple docstring'''







			return (preds == labels).mean()




@dataclass
class 					snake_case__							:
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_ :							int											=      field(
				    metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}     )
				SCREAMING_SNAKE_CASE_ :							int											=      field(
				    default=snake_case__					, metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}     )
				SCREAMING_SNAKE_CASE_ :							Any											=      field(
				    default=snake_case__					, metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}     )
				SCREAMING_SNAKE_CASE_ :							Any											=      field(
				    default=snake_case__					, metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}					, )




@dataclass
class 					snake_case__							:
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_ :							int											=      field(metadata={"""help""": """The name of the task to train on: """ + """, """.join(processors.keys()     )}     )
				SCREAMING_SNAKE_CASE_ :							Tuple											=      field(metadata={"""help""": """Should contain the data files for the task."""}     )
				SCREAMING_SNAKE_CASE_ :							Any											=      field(
				    default=1_28					, metadata={
				        """help""": (
				            """The maximum total input sequence length after tokenization. Sequences longer """
				            """than this will be truncated, sequences shorter will be padded."""
				        )
				    }					, )
				SCREAMING_SNAKE_CASE_ :							int											=      field(
				    default=snake_case__					, metadata={"""help""": """Overwrite the cached training and evaluation sets"""}     )




def     __magic_name__       (				):






			'''simple docstring'''







			a					=    HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)     )
			a					=    parser.parse_args_into_dataclasses()

			if (
			    os.path.exists(training_args.output_dir     )
			    and os.listdir(training_args.output_dir     )
			    and training_args.do_train
			    and not training_args.overwrite_output_dir
			):
						raise ValueError(
						    F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use"""
						    " --overwrite_output_dir to overcome."     )

			# Setup logging
			logging.basicConfig(
			    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",						datefmt="%m/%d/%Y %H:%M:%S",						level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN,						)
			logger.warning(
			    "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s",						training_args.local_rank,						training_args.device,						training_args.n_gpu,						bool(training_args.local_rank != -1     ),						training_args.fpaa,						)
			# Set the verbosity to info of the Transformers logger (on main process only):
			if is_main_process(training_args.local_rank     ):
						transformers.utils.logging.set_verbosity_info()
						transformers.utils.logging.enable_default_handler()
						transformers.utils.logging.enable_explicit_format()
			logger.info("Training/evaluation parameters %s",						__SCREAMING_SNAKE_CASE     )

			# Set seed
			set_seed(training_args.seed     )

			try:
						a					=    processors[data_args.task_name]()
						a					=    processor.get_labels()
						a					=    len(__SCREAMING_SNAKE_CASE     )
			except KeyError:
						raise ValueError("Task not found: %s" % (data_args.task_name)     )

			# Load pretrained model and tokenizer
			#
			# Distributed training:
			# The .from_pretrained methods guarantee that only one local process can concurrently
			# download model & vocab.

			a					=    AutoConfig.from_pretrained(
			    model_args.config_name if model_args.config_name else model_args.model_name_or_path,						num_labels=__SCREAMING_SNAKE_CASE,						finetuning_task=data_args.task_name,						cache_dir=model_args.cache_dir,						)
			a					=    AutoTokenizer.from_pretrained(
			    model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path,						cache_dir=model_args.cache_dir,						)
			a					=    AutoModelForMultipleChoice.from_pretrained(
			    model_args.model_name_or_path,						from_tf=bool(".ckpt" in model_args.model_name_or_path     ),						config=__SCREAMING_SNAKE_CASE,						cache_dir=model_args.cache_dir,						)

			# Get datasets
			a					=    (
			    MultipleChoiceDataset(
			        data_dir=data_args.data_dir,						tokenizer=__SCREAMING_SNAKE_CASE,						task=data_args.task_name,						max_seq_length=data_args.max_seq_length,						overwrite_cache=data_args.overwrite_cache,						mode=Split.train,						)
			    if training_args.do_train
			    else None
			)
			a					=    (
			    MultipleChoiceDataset(
			        data_dir=data_args.data_dir,						tokenizer=__SCREAMING_SNAKE_CASE,						task=data_args.task_name,						max_seq_length=data_args.max_seq_length,						overwrite_cache=data_args.overwrite_cache,						mode=Split.dev,						)
			    if training_args.do_eval
			    else None
			)

			def compute_metrics(A	:      Dict     ) -> Dict:
						a					=    np.argmax(p.predictions,						axis=1     )
						return {"acc": simple_accuracy(__SCREAMING_SNAKE_CASE,						p.label_ids     )}

			# Data collator
			a					=    DataCollatorWithPadding(__SCREAMING_SNAKE_CASE,						pad_to_multiple_of=8     ) if training_args.fpaa else None

			# Initialize our Trainer
			a					=    Trainer(
			    model=__SCREAMING_SNAKE_CASE,						args=__SCREAMING_SNAKE_CASE,						train_dataset=__SCREAMING_SNAKE_CASE,						eval_dataset=__SCREAMING_SNAKE_CASE,						compute_metrics=__SCREAMING_SNAKE_CASE,						data_collator=__SCREAMING_SNAKE_CASE,						)

			# Training
			if training_args.do_train:
						trainer.train(
						    model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path     ) else None     )
						trainer.save_model()
						# For convenience, we also re-save the tokenizer to the same directory,
						# so that you can share your model easily on huggingface.co/models =)
						if trainer.is_world_master():
									tokenizer.save_pretrained(training_args.output_dir     )

    # Evaluation
			a					=    {}
			if training_args.do_eval:
						logger.info("*** Evaluate ***"     )

						a					=    trainer.evaluate()

						a					=    os.path.join(training_args.output_dir,						"eval_results.txt"     )
						if trainer.is_world_master():
									with open(__SCREAMING_SNAKE_CASE,						"w"     ) as writer:
												logger.info("***** Eval results *****"     )
												for key, value in result.items():
															logger.info("  %s = %s",						__SCREAMING_SNAKE_CASE,						__SCREAMING_SNAKE_CASE     )
															writer.write("%s = %s\n" % (key, value)     )

												results.update(__SCREAMING_SNAKE_CASE     )

			return results




def     __magic_name__       (				A	:      Optional[Any]     ):






			'''simple docstring'''







			main()


if __name__ == "__main__":
		main()












import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
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 DetaImageProcessor




class 					snake_case__							(unittest.TestCase     ):
				"""simple docstring"""



				def __init__(   self       :  Dict      ,   __lowerCamelCase       :  Any      ,   __lowerCamelCase       :  Any=7      ,   __lowerCamelCase       :  List[Any]=3      ,   __lowerCamelCase       :  int=30      ,   __lowerCamelCase       :  int=4_00      ,   __lowerCamelCase       :  Dict=True      ,   __lowerCamelCase       :  Tuple=None      ,   __lowerCamelCase       :  Optional[int]=True      ,   __lowerCamelCase       :  Optional[Any]=[0.5, 0.5, 0.5]      ,   __lowerCamelCase       :  Optional[Any]=[0.5, 0.5, 0.5]      ,   __lowerCamelCase       :  Dict=True      ,   __lowerCamelCase       :  List[str]=1 / 2_55      ,   __lowerCamelCase       :  Optional[int]=True      ,   )      ->  str:
							# by setting size["longest_edge"] > max_resolution we're effectively not testing this :p
							a					=    size if size is not None else {"shortest_edge": 18, "longest_edge": 13_33}
							a					=    parent
							a					=    batch_size
							a					=    num_channels
							a					=    min_resolution
							a					=    max_resolution
							a					=    do_resize
							a					=    size
							a					=    do_normalize
							a					=    image_mean
							a					=    image_std
							a					=    do_rescale
							a					=    rescale_factor
							a					=    do_pad





				def 							__UpperCAmelCase							(   self       :  List[Any]							)      ->  Any:
							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_rescale": self.do_rescale,
							    "rescale_factor": self.rescale_factor,
							    "do_pad": self.do_pad,
							}





				def 							__UpperCAmelCase							(   self       :  Union[str, Any]      ,   __lowerCamelCase       :  Any      ,   __lowerCamelCase       :  str=False							)      ->  List[str]:
							if not batched:
										a					=    image_inputs[0]
										if isinstance(__lowerCamelCase      ,   Image.Image							):
													a					,	a					=    image.size
										else:
													a					,	a					=    image.shape[1], image.shape[2]
										if w < h:
													a					=    int(self.size["shortest_edge"] * h / w							)
													a					=    self.size["shortest_edge"]
										elif w > h:
													a					=    self.size["shortest_edge"]
													a					=    int(self.size["shortest_edge"] * w / h							)
										else:
													a					=    self.size["shortest_edge"]
													a					=    self.size["shortest_edge"]

							else:
										a					=    []
										for image in image_inputs:
													a					,	a					=    self.get_expected_values([image]							)
													expected_values.append((expected_height, expected_width)							)
										a					=    max(__lowerCamelCase      ,   key=lambda __lowerCamelCase							: item[0]							)[0]
										a					=    max(__lowerCamelCase      ,   key=lambda __lowerCamelCase							: item[1]							)[1]

							return expected_height, expected_width




@require_torch
@require_vision
class 					snake_case__							(_UpperCamelCase					, unittest.TestCase     ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_ :							Tuple											=      DetaImageProcessor if is_vision_available() else None





				def 							__UpperCAmelCase							(   self       :  Union[str, Any]							)      ->  List[Any]:
							a					=    DetaImageProcessingTester(self							)





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





				def 							__UpperCAmelCase							(   self       :  Optional[int]							)      ->  Tuple:
							a					=    self.image_processing_class(**self.image_processor_dict							)
							self.assertTrue(hasattr(__lowerCamelCase      ,   "image_mean"							)							)
							self.assertTrue(hasattr(__lowerCamelCase      ,   "image_std"							)							)
							self.assertTrue(hasattr(__lowerCamelCase      ,   "do_normalize"							)							)
							self.assertTrue(hasattr(__lowerCamelCase      ,   "do_resize"							)							)
							self.assertTrue(hasattr(__lowerCamelCase      ,   "do_rescale"							)							)
							self.assertTrue(hasattr(__lowerCamelCase      ,   "do_pad"							)							)
							self.assertTrue(hasattr(__lowerCamelCase      ,   "size"							)							)





				def 							__UpperCAmelCase							(   self       :  List[str]							)      ->  Union[str, Any]:
							a					=    self.image_processing_class.from_dict(self.image_processor_dict							)
							self.assertEqual(image_processor.size      ,   {"shortest_edge": 18, "longest_edge": 13_33}							)
							self.assertEqual(image_processor.do_pad      ,   __lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Any							)      ->  int:
							pass





				def 							__UpperCAmelCase							(   self       :  Any							)      ->  Any:
							# Initialize image_processing
							a					=    self.image_processing_class(**self.image_processor_dict							)
							# create random PIL images
							a					=    prepare_image_inputs(self.image_processor_tester      ,   equal_resolution=__lowerCamelCase							)
							for image in image_inputs:
										self.assertIsInstance(__lowerCamelCase      ,   Image.Image							)

							# Test not batched input
							a					=    image_processing(image_inputs[0]      ,   return_tensors="pt"							).pixel_values

							a					,	a					=    self.image_processor_tester.get_expected_values(__lowerCamelCase							)

							self.assertEqual(
							    encoded_images.shape      ,   (1, self.image_processor_tester.num_channels, expected_height, expected_width)      ,   )

							# Test batched
							a					,	a					=    self.image_processor_tester.get_expected_values(__lowerCamelCase      ,   batched=__lowerCamelCase							)

							a					=    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,
							        expected_height,
							        expected_width,
							    )      ,   )





				def 							__UpperCAmelCase							(   self       :  Union[str, Any]							)      ->  Optional[Any]:
							# Initialize image_processing
							a					=    self.image_processing_class(**self.image_processor_dict							)
							# create random numpy tensors
							a					=    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
							a					=    image_processing(image_inputs[0]      ,   return_tensors="pt"							).pixel_values

							a					,	a					=    self.image_processor_tester.get_expected_values(__lowerCamelCase							)

							self.assertEqual(
							    encoded_images.shape      ,   (1, self.image_processor_tester.num_channels, expected_height, expected_width)      ,   )

							# Test batched
							a					=    image_processing(__lowerCamelCase      ,   return_tensors="pt"							).pixel_values

							a					,	a					=    self.image_processor_tester.get_expected_values(__lowerCamelCase      ,   batched=__lowerCamelCase							)

							self.assertEqual(
							    encoded_images.shape      ,   (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    )      ,   )





				def 							__UpperCAmelCase							(   self       :  Any							)      ->  List[str]:
							# Initialize image_processing
							a					=    self.image_processing_class(**self.image_processor_dict							)
							# create random PyTorch tensors
							a					=    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
							a					=    image_processing(image_inputs[0]      ,   return_tensors="pt"							).pixel_values

							a					,	a					=    self.image_processor_tester.get_expected_values(__lowerCamelCase							)

							self.assertEqual(
							    encoded_images.shape      ,   (1, self.image_processor_tester.num_channels, expected_height, expected_width)      ,   )

							# Test batched
							a					=    image_processing(__lowerCamelCase      ,   return_tensors="pt"							).pixel_values

							a					,	a					=    self.image_processor_tester.get_expected_values(__lowerCamelCase      ,   batched=__lowerCamelCase							)

							self.assertEqual(
							    encoded_images.shape      ,   (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    )      ,   )





				@slow
				def 							__UpperCAmelCase							(   self       :  Any							)      ->  List[Any]:
							# prepare image and target
							a					=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
							with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt"      ,   "r"							) as f:
										a					=    json.loads(f.read()							)

							a					=    {"image_id": 3_97_69, "annotations": target}

							# encode them
							a					=    DetaImageProcessor()
							a					=    image_processing(images=__lowerCamelCase      ,   annotations=__lowerCamelCase      ,   return_tensors="pt"							)

							# verify pixel values
							a					=    torch.Size([1, 3, 8_00, 10_66]							)
							self.assertEqual(encoding["pixel_values"].shape      ,   __lowerCamelCase							)

							a					=    torch.tensor([0.2_796, 0.3_138, 0.3_481]							)
							self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]      ,   __lowerCamelCase      ,   atol=1e-4							)							)

							# verify area
							a					=    torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["area"]      ,   __lowerCamelCase							)							)
							# verify boxes
							a					=    torch.Size([6, 4]							)
							self.assertEqual(encoding["labels"][0]["boxes"].shape      ,   __lowerCamelCase							)
							a					=    torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]      ,   __lowerCamelCase      ,   atol=1e-3							)							)
							# verify image_id
							a					=    torch.tensor([3_97_69]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]      ,   __lowerCamelCase							)							)
							# verify is_crowd
							a					=    torch.tensor([0, 0, 0, 0, 0, 0]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]      ,   __lowerCamelCase							)							)
							# verify class_labels
							a					=    torch.tensor([75, 75, 63, 65, 17, 17]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]      ,   __lowerCamelCase							)							)
							# verify orig_size
							a					=    torch.tensor([4_80, 6_40]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]      ,   __lowerCamelCase							)							)
							# verify size
							a					=    torch.tensor([8_00, 10_66]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["size"]      ,   __lowerCamelCase							)							)





				@slow
				def 							__UpperCAmelCase							(   self       :  Any							)      ->  Union[str, Any]:
							# prepare image, target and masks_path
							a					=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
							with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt"      ,   "r"							) as f:
										a					=    json.loads(f.read()							)

							a					=    {"file_name": "000000039769.png", "image_id": 3_97_69, "segments_info": target}

							a					=    pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic"							)

							# encode them
							a					=    DetaImageProcessor(format="coco_panoptic"							)
							a					=    image_processing(images=__lowerCamelCase      ,   annotations=__lowerCamelCase      ,   masks_path=__lowerCamelCase      ,   return_tensors="pt"							)

							# verify pixel values
							a					=    torch.Size([1, 3, 8_00, 10_66]							)
							self.assertEqual(encoding["pixel_values"].shape      ,   __lowerCamelCase							)

							a					=    torch.tensor([0.2_796, 0.3_138, 0.3_481]							)
							self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]      ,   __lowerCamelCase      ,   atol=1e-4							)							)

							# verify area
							a					=    torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["area"]      ,   __lowerCamelCase							)							)
							# verify boxes
							a					=    torch.Size([6, 4]							)
							self.assertEqual(encoding["labels"][0]["boxes"].shape      ,   __lowerCamelCase							)
							a					=    torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]      ,   __lowerCamelCase      ,   atol=1e-3							)							)
							# verify image_id
							a					=    torch.tensor([3_97_69]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]      ,   __lowerCamelCase							)							)
							# verify is_crowd
							a					=    torch.tensor([0, 0, 0, 0, 0, 0]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]      ,   __lowerCamelCase							)							)
							# verify class_labels
							a					=    torch.tensor([17, 17, 63, 75, 75, 93]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]      ,   __lowerCamelCase							)							)
							# verify masks
							a					=    82_28_73
							self.assertEqual(encoding["labels"][0]["masks"].sum().item()      ,   __lowerCamelCase							)
							# verify orig_size
							a					=    torch.tensor([4_80, 6_40]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]      ,   __lowerCamelCase							)							)
							# verify size
							a					=    torch.tensor([8_00, 10_66]							)
							self.assertTrue(torch.allclose(encoding["labels"][0]["size"]      ,   __lowerCamelCase							)							)






