



"""simple docstring"""




import inspect
import unittest

from transformers import EfficientNetConfig
from transformers.testing_utils import is_pipeline_test, 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, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import EfficientNetForImageClassification, EfficientNetModel
 from transformers.models.efficientnet.modeling_efficientnet import EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor






class 			UpperCAmelCase_						:



      def __init__(  self  :			Optional[Any]  ,			A  :			List[Any]  ,			A  :			Any=1_3  ,			A  :			Dict=3_2  ,			A  :			str=3  ,			A  :			Union[str, Any]=[3, 3, 5]  ,			A  :			List[Any]=[3_2, 1_6, 2_4]  ,			A  :			int=[1_6, 2_4, 4_0]  ,			A  :			Union[str, Any]=[1, 1, 2]  ,			A  :			str=[1, 1, 2]  ,			A  :			Optional[Any]=[1, 6, 6]  ,			A  :			Dict=True  ,			A  :			Tuple=True  ,			A  :			int=3_7  ,			A  :			List[str]="gelu"  ,			A  :			List[str]=1_0  ,			):
           _UpperCAmelCase    :			int              =					parent
           _UpperCAmelCase    :			int              =					batch_size
           _UpperCAmelCase    :			Optional[int]              =					image_size
           _UpperCAmelCase    :			Dict              =					num_channels
           _UpperCAmelCase    :			Dict              =					kernel_sizes
           _UpperCAmelCase    :			Union[str, Any]              =					in_channels
           _UpperCAmelCase    :			str              =					out_channels
           _UpperCAmelCase    :			Union[str, Any]              =					strides
           _UpperCAmelCase    :			Any              =					num_block_repeats
           _UpperCAmelCase    :			Tuple              =					expand_ratios
           _UpperCAmelCase    :			Any              =					is_training
           _UpperCAmelCase    :			Any              =					hidden_act
           _UpperCAmelCase    :			Tuple              =					num_labels
           _UpperCAmelCase    :			Optional[Any]              =					use_labels



      def 		snake_case_      (  self  :			Optional[int]		):
           _UpperCAmelCase    :			List[Any]              =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

           _UpperCAmelCase    :			int              =					None
           if self.use_labels:
                _UpperCAmelCase    :			List[Any]              =					ids_tensor([self.batch_size]  ,			self.num_labels		)

           _UpperCAmelCase    :			Union[str, Any]              =					self.get_config()
           return config, pixel_values, labels



      def 		snake_case_      (  self  :			int		):
           return EfficientNetConfig(
               num_channels=self.num_channels  ,			kernel_sizes=self.kernel_sizes  ,			in_channels=self.in_channels  ,			out_channels=self.out_channels  ,			strides=self.strides  ,			num_block_repeats=self.num_block_repeats  ,			expand_ratios=self.expand_ratios  ,			hidden_act=self.hidden_act  ,			num_labels=self.num_labels  ,			)



      def 		snake_case_      (  self  :			List[str]  ,			A  :			Union[str, Any]  ,			A  :			List[Any]  ,			A  :			str		):
           _UpperCAmelCase    :			Optional[Any]              =					EfficientNetModel(config=A		)
           model.to(A		)
           model.eval()
           _UpperCAmelCase    :			List[str]              =					model(A		)
           # expected last hidden states: B, C, H // 4, W // 4
           self.parent.assertEqual(
               result.last_hidden_state.shape  ,			(self.batch_size, config.hidden_dim, self.image_size // 4, self.image_size // 4)  ,			)



      def 		snake_case_      (  self  :			Any  ,			A  :			Tuple  ,			A  :			List[Any]  ,			A  :			Optional[Any]		):
           _UpperCAmelCase    :			Optional[Any]              =					EfficientNetForImageClassification(A		)
           model.to(A		)
           model.eval()
           _UpperCAmelCase    :			Union[str, Any]              =					model(A  ,			labels=A		)
           self.parent.assertEqual(result.logits.shape  ,			(self.batch_size, self.num_labels)		)



      def 		snake_case_      (  self  :			List[Any]		):
           _UpperCAmelCase    :			Union[str, Any]              =					self.prepare_config_and_inputs()
           _UpperCAmelCase       , _UpperCAmelCase       , _UpperCAmelCase    :			Any              =					config_and_inputs
           _UpperCAmelCase    :			Tuple              =					{"pixel_values": pixel_values}
           return config, inputs_dict







@require_torch
class 			UpperCAmelCase_						(       _UpperCamelCase ,    _UpperCamelCase ,    unittest.TestCase		):
      __SCREAMING_SNAKE_CASE		:     Union[str, Any]				  =				(EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
      __SCREAMING_SNAKE_CASE		:     int				  =				(
          {'feature-extraction': EfficientNetModel, 'image-classification': EfficientNetForImageClassification}
          if is_torch_available()
          else {}
      )

      __SCREAMING_SNAKE_CASE		:     str				  =				False
      __SCREAMING_SNAKE_CASE		:     List[str]				  =				False
      __SCREAMING_SNAKE_CASE		:     Any				  =				False
      __SCREAMING_SNAKE_CASE		:     Any				  =				False
      __SCREAMING_SNAKE_CASE		:     Tuple				  =				False



      def 		snake_case_      (  self  :			Optional[Any]		):
           _UpperCAmelCase    :			Union[str, Any]              =					EfficientNetModelTester(self		)
           _UpperCAmelCase    :			List[Any]              =					ConfigTester(
               self  ,			config_class=A  ,			has_text_modality=A  ,			hidden_size=3_7		)



      def 		snake_case_      (  self  :			Dict		):
           self.create_and_test_config_common_properties()
           self.config_tester.create_and_test_config_to_json_string()
           self.config_tester.create_and_test_config_to_json_file()
           self.config_tester.create_and_test_config_from_and_save_pretrained()
           self.config_tester.create_and_test_config_with_num_labels()
           self.config_tester.check_config_can_be_init_without_params()
           self.config_tester.check_config_arguments_init()



      def 		snake_case_      (  self  :			List[str]		):
           return



      @unittest.skip(reason="EfficientNet does not use inputs_embeds"		)
      def 		snake_case_      (  self  :			int		):
           pass



      @unittest.skip(reason="EfficientNet does not support input and output embeddings"		)
      def 		snake_case_      (  self  :			List[str]		):
           pass



      @unittest.skip(reason="EfficientNet does not use feedforward chunking"		)
      def 		snake_case_      (  self  :			Optional[int]		):
           pass



      def 		snake_case_      (  self  :			Optional[Any]		):
           _UpperCAmelCase       , _UpperCAmelCase    :			Optional[Any]              =					self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                _UpperCAmelCase    :			List[Any]              =					model_class(A		)
                _UpperCAmelCase    :			Tuple              =					inspect.signature(model.forward		)
                # signature.parameters is an OrderedDict => so arg_names order is deterministic
                _UpperCAmelCase    :			str              =					[*signature.parameters.keys()]

                _UpperCAmelCase    :			Optional[Any]              =					["pixel_values"]
                self.assertListEqual(arg_names[:1]  ,			A		)



      def 		snake_case_      (  self  :			Tuple		):
           _UpperCAmelCase    :			Tuple              =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*A		)



      def 		snake_case_      (  self  :			List[Any]		):
           def check_hidden_states_output(A  :			Tuple  ,			A  :			Dict  ,			A  :			Optional[int]		):
                _UpperCAmelCase    :			Dict              =					model_class(A		)
                model.to(A		)
                model.eval()

                with torch.no_grad():
                     _UpperCAmelCase    :			str              =					model(**self._prepare_for_class(A  ,			A		)		)

                _UpperCAmelCase    :			Union[str, Any]              =					outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
                _UpperCAmelCase    :			Optional[int]              =					sum(config.num_block_repeats		) * 4
                self.assertEqual(len(A		)  ,			A		)

                # EfficientNet's feature maps are of shape (batch_size, num_channels, height, width)
                self.assertListEqual(
                    list(hidden_states[0].shape[-2:]		)  ,			[self.model_tester.image_size // 2, self.model_tester.image_size // 2]  ,			)

           _UpperCAmelCase       , _UpperCAmelCase    :			Any              =					self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                _UpperCAmelCase    :			Union[str, Any]              =					True
                check_hidden_states_output(A  ,			A  ,			A		)

                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                _UpperCAmelCase    :			List[Any]              =					True

                check_hidden_states_output(A  ,			A  ,			A		)



      def 		snake_case_      (  self  :			Dict		):
           _UpperCAmelCase    :			List[str]              =					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*A		)



      @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."		)
      def 		snake_case_      (  self  :			Optional[Any]		):
           pass



      @slow
      def 		snake_case_      (  self  :			str		):
           for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _UpperCAmelCase    :			Dict              =					EfficientNetModel.from_pretrained(A		)
                self.assertIsNotNone(A		)



      @is_pipeline_test
      @require_vision
      @slow
      def 		snake_case_      (  self  :			str		):
           super().test_pipeline_image_classification()







def 		__snake_case		(   )						->     List[str]:





     '''simple docstring'''

     _UpperCAmelCase    :			List[str]              =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"       )
     return image






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



      @cached_property
      def 		snake_case_      (  self  :			Dict		):
           return AutoImageProcessor.from_pretrained("google/efficientnet-b7"		) if is_vision_available() else None



      @slow
      def 		snake_case_      (  self  :			Union[str, Any]		):
           _UpperCAmelCase    :			int              =					EfficientNetForImageClassification.from_pretrained("google/efficientnet-b7"		).to(A		)

           _UpperCAmelCase    :			List[Any]              =					self.default_image_processor
           _UpperCAmelCase    :			Any              =					prepare_img()
           _UpperCAmelCase    :			Tuple              =					image_processor(images=A  ,			return_tensors="pt"		).to(A		)

           # forward pass
           with torch.no_grad():
                _UpperCAmelCase    :			Tuple              =					model(**A		)

           # verify the logits
           _UpperCAmelCase    :			Tuple              =					torch.Size((1, 1_0_0_0)		)
           self.assertEqual(outputs.logits.shape  ,			A		)

           _UpperCAmelCase    :			Dict              =					torch.tensor([0.0_001, 0.0_002, 0.0_002]		).to(A		)
           self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,			A  ,			atol=1e-4		)		)





"""simple docstring"""




import argparse
import json
import re
import string
import sys
from collections import Counter

def 		__snake_case		(   SCREAMING_SNAKE_CASE__			:		Optional[int]       )						->     Union[str, Any]:





     '''simple docstring'''

     def remove_articles(SCREAMING_SNAKE_CASE__			:		Tuple       ):
          return re.sub(R"\b(a|an|the)\b"		,						" "		,						SCREAMING_SNAKE_CASE__       )

     def white_space_fix(SCREAMING_SNAKE_CASE__			:		Any       ):
          return " ".join(text.split()       )

     def remove_punc(SCREAMING_SNAKE_CASE__			:		Optional[int]       ):
          _UpperCAmelCase    :			List[str]              =					set(string.punctuation       )
          return "".join(ch for ch in text if ch not in exclude       )

     def lower(SCREAMING_SNAKE_CASE__			:		List[str]       ):
          return text.lower()

     return white_space_fix(remove_articles(remove_punc(lower(SCREAMING_SNAKE_CASE__       )       )       )       )

def 		__snake_case		(   SCREAMING_SNAKE_CASE__			:		Union[str, Any]		,						SCREAMING_SNAKE_CASE__			:		str       )						->     Dict:





     '''simple docstring'''

     _UpperCAmelCase    :			List[Any]              =					normalize_answer(SCREAMING_SNAKE_CASE__       ).split()
     _UpperCAmelCase    :			Tuple              =					normalize_answer(SCREAMING_SNAKE_CASE__       ).split()
     _UpperCAmelCase    :			List[Any]              =					Counter(SCREAMING_SNAKE_CASE__       ) & Counter(SCREAMING_SNAKE_CASE__       )
     _UpperCAmelCase    :			List[str]              =					sum(common.values()       )
     if num_same == 0:
          return 0
     _UpperCAmelCase    :			Union[str, Any]              =					1.0 * num_same / len(SCREAMING_SNAKE_CASE__       )
     _UpperCAmelCase    :			Union[str, Any]              =					1.0 * num_same / len(SCREAMING_SNAKE_CASE__       )
     _UpperCAmelCase    :			Union[str, Any]              =					(2 * precision * recall) / (precision + recall)
     return fa

def 		__snake_case		(   SCREAMING_SNAKE_CASE__			:		Union[str, Any]		,						SCREAMING_SNAKE_CASE__			:		Dict       )						->     str:





     '''simple docstring'''

     return normalize_answer(SCREAMING_SNAKE_CASE__       ) == normalize_answer(SCREAMING_SNAKE_CASE__       )

def 		__snake_case		(   SCREAMING_SNAKE_CASE__			:		List[Any]		,						SCREAMING_SNAKE_CASE__			:		Dict		,						SCREAMING_SNAKE_CASE__			:		Optional[Any]       )						->     str:





     '''simple docstring'''

     _UpperCAmelCase    :			int              =					[]
     for ground_truth in ground_truths:
          _UpperCAmelCase    :			Dict              =					metric_fn(SCREAMING_SNAKE_CASE__		,						SCREAMING_SNAKE_CASE__       )
          scores_for_ground_truths.append(SCREAMING_SNAKE_CASE__       )
     return max(SCREAMING_SNAKE_CASE__       )

def 		__snake_case		(   SCREAMING_SNAKE_CASE__			:		Optional[int]		,						SCREAMING_SNAKE_CASE__			:		Any       )						->     Tuple:





     '''simple docstring'''

     _UpperCAmelCase    :			str              =					0
     _UpperCAmelCase    :			int              =					[]
     for passage in dataset:
          for qa in passage["qas"]:
               total += 1
               if qa["id"] not in predictions:
                    _UpperCAmelCase    :			List[Any]              =					f'Unanswered question {qa["id"]} will receive score 0.'
                    print(SCREAMING_SNAKE_CASE__		,						file=sys.stderr       )
                    continue

               _UpperCAmelCase    :			Union[str, Any]              =					[x["text"] for x in qa["answers"]]
               _UpperCAmelCase    :			Optional[int]              =					predictions[qa["id"]]

               _UpperCAmelCase    :			int              =					metric_max_over_ground_truths(SCREAMING_SNAKE_CASE__		,						SCREAMING_SNAKE_CASE__		,						SCREAMING_SNAKE_CASE__       )
               if int(_exact_match       ) == 1:
                    correct_ids.append(qa["id"]       )
               exact_match += _exact_match

               fa += metric_max_over_ground_truths(SCREAMING_SNAKE_CASE__		,						SCREAMING_SNAKE_CASE__		,						SCREAMING_SNAKE_CASE__       )

     _UpperCAmelCase    :			List[str]              =					exact_match / total
     _UpperCAmelCase    :			str              =					fa / total

     return {"exact_match": exact_match, "f1": fa}, correct_ids


if __name__ == "__main__":
 _lowerCAmelCase       :      Any							=							"1.0"
 _lowerCAmelCase       :      Any							=							argparse.ArgumentParser("Official evaluation script for ReCoRD v1.0.")
 parser.add_argument("data_file", help="The dataset file in JSON format.")
 parser.add_argument("pred_file", help="The model prediction file in JSON format.")
 parser.add_argument("--output_correct_ids", action="store_true", help="Output the correctly answered query IDs.")
 _lowerCAmelCase       :      Tuple							=							parser.parse_args()

 with open(args.data_file) as data_file:
  _lowerCAmelCase       :      int							=							json.load(data_file)
  if dataset_json["version"] != expected_version:
   print(
       F"Evaluation expects v-{expected_version}, but got dataset with v-{dataset_json['version']}",
       file=sys.stderr,
   )
  _lowerCAmelCase       :      List[str]							=							dataset_json["data"]

 with open(args.pred_file) as pred_file:
  _lowerCAmelCase       :      str							=							json.load(pred_file)

 _lowerCAmelCase,		_lowerCAmelCase       :      List[Any]							=							evaluate(dataset, predictions)

 if args.output_correct_ids:
  print(F"Output {len(correct_ids)} correctly answered question IDs.")
  with open("correct_ids.json", "w") as f:
   json.dump(correct_ids, f)
