





import argparse
import collections
import json
import os
import re
import string
import sys

import numpy as np


UpperCAmelCase     :     Union[str, Any]							=	re.compile(r"""\b(a|an|the)\b""", re.UNICODE)

UpperCAmelCase     :     int							=	None
def 					_A	(		):





  """simple docstring"""



  a__     : int  =argparse.ArgumentParser("Official evaluation script for SQuAD version 2.0."						)
  parser.add_argument("data_file"	,	metavar="data.json"	,	help="Input data JSON file."						)
  parser.add_argument("pred_file"	,	metavar="pred.json"	,	help="Model predictions."						)
  parser.add_argument(
      "--out-file"	,	"-o"	,	metavar="eval.json"	,	help="Write accuracy metrics to file (default is stdout)."						)
  parser.add_argument(
      "--na-prob-file"	,	"-n"	,	metavar="na_prob.json"	,	help="Model estimates of probability of no answer."						)
  parser.add_argument(
      "--na-prob-thresh"	,	"-t"	,	type=SCREAMING_SNAKE_CASE	,	default=1.0	,	help="Predict \"\" if no-answer probability exceeds this (default = 1.0)."	,	)
  parser.add_argument(
      "--out-image-dir"	,	"-p"	,	metavar="out_images"	,	default=SCREAMING_SNAKE_CASE	,	help="Save precision-recall curves to directory."						)
  parser.add_argument("--verbose"	,	"-v"	,	action="store_true"						)
  if len(sys.argv						) == 1:
    parser.print_help()
    sys.exit(1						)
  return parser.parse_args()
def 					_A	(		SCREAMING_SNAKE_CASE					:       List[str]						):





  """simple docstring"""



  a__     : Any  ={}
  for article in dataset:
    for p in article["paragraphs"]:
      for qa in p["qas"]:
        a__     : int  =bool(qa["answers"]["text"]						)
  return qid_to_has_ans
def 					_A	(		SCREAMING_SNAKE_CASE					:       Optional[int]						):





  """simple docstring"""



  def remove_articles(SCREAMING_SNAKE_CASE					:       Optional[int]						):
    return ARTICLES_REGEX.sub(" "	,	SCREAMING_SNAKE_CASE						)

  def white_space_fix(SCREAMING_SNAKE_CASE					:       List[Any]						):
    return " ".join(text.split()						)

  def remove_punc(SCREAMING_SNAKE_CASE					:       Tuple						):
    a__     : Tuple  =set(string.punctuation						)
    return "".join(ch for ch in text if ch not in exclude						)

  def lower(SCREAMING_SNAKE_CASE					:       Union[str, Any]						):
    return text.lower()

  return white_space_fix(remove_articles(remove_punc(lower(SCREAMING_SNAKE_CASE						)						)						)						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Dict						):





  """simple docstring"""



  if not s:
    return []
  return normalize_answer(SCREAMING_SNAKE_CASE						).split()
def 					_A	(		SCREAMING_SNAKE_CASE					:       str	,	SCREAMING_SNAKE_CASE					:       str						):





  """simple docstring"""



  return int(normalize_answer(SCREAMING_SNAKE_CASE						) == normalize_answer(SCREAMING_SNAKE_CASE						)						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Dict	,	SCREAMING_SNAKE_CASE					:       Union[str, Any]						):





  """simple docstring"""



  a__     : Tuple  =get_tokens(SCREAMING_SNAKE_CASE						)
  a__     : int  =get_tokens(SCREAMING_SNAKE_CASE						)
  a__     : Dict  =collections.Counter(SCREAMING_SNAKE_CASE						) & collections.Counter(SCREAMING_SNAKE_CASE						)
  a__     : Union[str, Any]  =sum(common.values()						)
  if len(SCREAMING_SNAKE_CASE						) == 0 or len(SCREAMING_SNAKE_CASE						) == 0:
    # If either is no-answer, then F1 is 1 if they agree, 0 otherwise
    return int(gold_toks == pred_toks						)
  if num_same == 0:
    return 0
  a__     : Dict  =1.0 * num_same / len(SCREAMING_SNAKE_CASE						)
  a__     : Tuple  =1.0 * num_same / len(SCREAMING_SNAKE_CASE						)
  a__     : Dict  =(2 * precision * recall) / (precision + recall)
  return fa
def 					_A	(		SCREAMING_SNAKE_CASE					:       int	,	SCREAMING_SNAKE_CASE					:       str						):





  """simple docstring"""



  a__     : Dict  ={}
  a__     : Optional[Any]  ={}
  for article in dataset:
    for p in article["paragraphs"]:
      for qa in p["qas"]:
        a__     : Tuple  =qa["id"]
        a__     : Tuple  =[t for t in qa["answers"]["text"] if normalize_answer(SCREAMING_SNAKE_CASE						)]
        if not gold_answers:
          # For unanswerable questions, only correct answer is empty string
          a__     : Optional[int]  =[""]
        if qid not in preds:
          print(f'''Missing prediction for {qid}'''						)
          continue
        a__     : int  =preds[qid]
        # Take max over all gold answers
        a__     : Union[str, Any]  =max(compute_exact(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						) for a in gold_answers						)
        a__     : Optional[Any]  =max(compute_fa(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						) for a in gold_answers						)
  return exact_scores, fa_scores
def 					_A	(		SCREAMING_SNAKE_CASE					:       Optional[Any]	,	SCREAMING_SNAKE_CASE					:       Any	,	SCREAMING_SNAKE_CASE					:       Any	,	SCREAMING_SNAKE_CASE					:       Optional[Any]						):





  """simple docstring"""



  a__     : str  ={}
  for qid, s in scores.items():
    a__     : List[Any]  =na_probs[qid] > na_prob_thresh
    if pred_na:
      a__     : Tuple  =float(not qid_to_has_ans[qid]						)
    else:
      a__     : Any  =s
  return new_scores
def 					_A	(		SCREAMING_SNAKE_CASE					:       Optional[int]	,	SCREAMING_SNAKE_CASE					:       Tuple	,	SCREAMING_SNAKE_CASE					:       Union[str, Any]=None						):





  """simple docstring"""



  if not qid_list:
    a__     : Dict  =len(SCREAMING_SNAKE_CASE						)
    return collections.OrderedDict(
        [
            ("exact", 1_0_0.0 * sum(exact_scores.values()						) / total),
            ("f1", 1_0_0.0 * sum(fa_scores.values()						) / total),
            ("total", total),
        ]						)
  else:
    a__     : Optional[int]  =len(SCREAMING_SNAKE_CASE						)
    return collections.OrderedDict(
        [
            ("exact", 1_0_0.0 * sum(exact_scores[k] for k in qid_list						) / total),
            ("f1", 1_0_0.0 * sum(fa_scores[k] for k in qid_list						) / total),
            ("total", total),
        ]						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Dict	,	SCREAMING_SNAKE_CASE					:       int	,	SCREAMING_SNAKE_CASE					:       str						):





  """simple docstring"""



  for k in new_eval:
    a__     : List[Any]  =new_eval[k]
def 					_A	(		SCREAMING_SNAKE_CASE					:       Optional[int]	,	SCREAMING_SNAKE_CASE					:       Union[str, Any]	,	SCREAMING_SNAKE_CASE					:       Dict	,	SCREAMING_SNAKE_CASE					:       List[Any]						):





  """simple docstring"""



  plt.step(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	color="b"	,	alpha=0.2	,	where="post"						)
  plt.fill_between(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	step="post"	,	alpha=0.2	,	color="b"						)
  plt.xlabel("Recall"						)
  plt.ylabel("Precision"						)
  plt.xlim([0.0, 1.0_5]						)
  plt.ylim([0.0, 1.0_5]						)
  plt.title(SCREAMING_SNAKE_CASE						)
  plt.savefig(SCREAMING_SNAKE_CASE						)
  plt.clf()
def 					_A	(		SCREAMING_SNAKE_CASE					:       Any	,	SCREAMING_SNAKE_CASE					:       Dict	,	SCREAMING_SNAKE_CASE					:       List[str]	,	SCREAMING_SNAKE_CASE					:       Tuple	,	SCREAMING_SNAKE_CASE					:       Dict=None	,	SCREAMING_SNAKE_CASE					:       Optional[int]=None						):





  """simple docstring"""



  a__     : Any  =sorted(SCREAMING_SNAKE_CASE	,	key=lambda SCREAMING_SNAKE_CASE						: na_probs[k]						)
  a__     : str  =0.0
  a__     : Dict  =1.0
  a__     : Tuple  =0.0
  a__     : List[Any]  =[1.0]
  a__     : Dict  =[0.0]
  a__     : Any  =0.0
  for i, qid in enumerate(SCREAMING_SNAKE_CASE						):
    if qid_to_has_ans[qid]:
      true_pos += scores[qid]
    a__     : List[str]  =true_pos / float(i + 1						)
    a__     : List[Any]  =true_pos / float(SCREAMING_SNAKE_CASE						)
    if i == len(SCREAMING_SNAKE_CASE						) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]:
      # i.e., if we can put a threshold after this point
      avg_prec += cur_p * (cur_r - recalls[-1])
      precisions.append(SCREAMING_SNAKE_CASE						)
      recalls.append(SCREAMING_SNAKE_CASE						)
  if out_image:
    plot_pr_curve(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
  return {"ap": 1_0_0.0 * avg_prec}
def 					_A	(		SCREAMING_SNAKE_CASE					:       Tuple	,	SCREAMING_SNAKE_CASE					:       int	,	SCREAMING_SNAKE_CASE					:       Optional[int]	,	SCREAMING_SNAKE_CASE					:       Dict	,	SCREAMING_SNAKE_CASE					:       Optional[int]	,	SCREAMING_SNAKE_CASE					:       Optional[Any]						):





  """simple docstring"""



  if out_image_dir and not os.path.exists(SCREAMING_SNAKE_CASE						):
    os.makedirs(SCREAMING_SNAKE_CASE						)
  a__     : str  =sum(1 for v in qid_to_has_ans.values() if v						)
  if num_true_pos == 0:
    return
  a__     : Tuple  =make_precision_recall_eval(
      SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	out_image=os.path.join(SCREAMING_SNAKE_CASE	,	"pr_exact.png"						)	,	title="Precision-Recall curve for Exact Match score"	,	)
  a__     : int  =make_precision_recall_eval(
      SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	out_image=os.path.join(SCREAMING_SNAKE_CASE	,	"pr_f1.png"						)	,	title="Precision-Recall curve for F1 score"	,	)
  a__     : str  ={k: float(SCREAMING_SNAKE_CASE						) for k, v in qid_to_has_ans.items()}
  a__     : Dict  =make_precision_recall_eval(
      SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	out_image=os.path.join(SCREAMING_SNAKE_CASE	,	"pr_oracle.png"						)	,	title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)"	,	)
  merge_eval(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	"pr_exact"						)
  merge_eval(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	"pr_f1"						)
  merge_eval(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	"pr_oracle"						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Optional[int]	,	SCREAMING_SNAKE_CASE					:       Union[str, Any]	,	SCREAMING_SNAKE_CASE					:       str	,	SCREAMING_SNAKE_CASE					:       str						):





  """simple docstring"""



  if not qid_list:
    return
  a__     : Any  =[na_probs[k] for k in qid_list]
  a__     : Optional[Any]  =np.ones_like(SCREAMING_SNAKE_CASE						) / float(len(SCREAMING_SNAKE_CASE						)						)
  plt.hist(SCREAMING_SNAKE_CASE	,	weights=SCREAMING_SNAKE_CASE	,	bins=20	,	range=(0.0, 1.0)						)
  plt.xlabel("Model probability of no-answer"						)
  plt.ylabel("Proportion of dataset"						)
  plt.title(f'''Histogram of no-answer probability: {name}'''						)
  plt.savefig(os.path.join(SCREAMING_SNAKE_CASE	,	f'''na_prob_hist_{name}.png'''						)						)
  plt.clf()
def 					_A	(		SCREAMING_SNAKE_CASE					:       Optional[Any]	,	SCREAMING_SNAKE_CASE					:       int	,	SCREAMING_SNAKE_CASE					:       List[Any]	,	SCREAMING_SNAKE_CASE					:       str						):





  """simple docstring"""



  a__     : Optional[int]  =sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k]						)
  a__     : Dict  =num_no_ans
  a__     : Dict  =cur_score
  a__     : str  =0.0
  a__     : Union[str, Any]  =sorted(SCREAMING_SNAKE_CASE	,	key=lambda SCREAMING_SNAKE_CASE						: na_probs[k]						)
  for i, qid in enumerate(SCREAMING_SNAKE_CASE						):
    if qid not in scores:
      continue
    if qid_to_has_ans[qid]:
      a__     : Union[str, Any]  =scores[qid]
    else:
      if preds[qid]:
        a__     : Optional[Any]  =-1
      else:
        a__     : Union[str, Any]  =0
    cur_score += diff
    if cur_score > best_score:
      a__     : Optional[int]  =cur_score
      a__     : Optional[int]  =na_probs[qid]
  return 1_0_0.0 * best_score / len(SCREAMING_SNAKE_CASE						), best_thresh
def 					_A	(		SCREAMING_SNAKE_CASE					:       List[str]	,	SCREAMING_SNAKE_CASE					:       Union[str, Any]	,	SCREAMING_SNAKE_CASE					:       Union[str, Any]	,	SCREAMING_SNAKE_CASE					:       Any	,	SCREAMING_SNAKE_CASE					:       Tuple	,	SCREAMING_SNAKE_CASE					:       Tuple						):





  """simple docstring"""



  a__ ,							a__     : Optional[int]  =find_best_thresh(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
  a__ ,							a__     : int  =find_best_thresh(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
  a__     : Optional[Any]  =best_exact
  a__     : Tuple  =exact_thresh
  a__     : Any  =best_fa
  a__     : List[Any]  =fa_thresh
def 					_A	(		):





  """simple docstring"""



  with open(OPTS.data_file						) as f:
    a__     : str  =json.load(SCREAMING_SNAKE_CASE						)
    a__     : Any  =dataset_json["data"]
  with open(OPTS.pred_file						) as f:
    a__     : List[str]  =json.load(SCREAMING_SNAKE_CASE						)
  if OPTS.na_prob_file:
    with open(OPTS.na_prob_file						) as f:
      a__     : int  =json.load(SCREAMING_SNAKE_CASE						)
  else:
    a__     : Tuple  ={k: 0.0 for k in preds}
  a__     : Any  =make_qid_to_has_ans(SCREAMING_SNAKE_CASE						)  # maps qid to True/False
  a__     : Optional[int]  =[k for k, v in qid_to_has_ans.items() if v]
  a__     : List[str]  =[k for k, v in qid_to_has_ans.items() if not v]
  a__ ,							a__     : Optional[int]  =get_raw_scores(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
  a__     : Dict  =apply_no_ans_threshold(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	OPTS.na_prob_thresh						)
  a__     : int  =apply_no_ans_threshold(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	OPTS.na_prob_thresh						)
  a__     : str  =make_eval_dict(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
  if has_ans_qids:
    a__     : Union[str, Any]  =make_eval_dict(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	qid_list=SCREAMING_SNAKE_CASE						)
    merge_eval(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	"HasAns"						)
  if no_ans_qids:
    a__     : List[Any]  =make_eval_dict(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	qid_list=SCREAMING_SNAKE_CASE						)
    merge_eval(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	"NoAns"						)
  if OPTS.na_prob_file:
    find_all_best_thresh(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
  if OPTS.na_prob_file and OPTS.out_image_dir:
    run_precision_recall_analysis(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	OPTS.out_image_dir						)
    histogram_na_prob(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	OPTS.out_image_dir	,	"hasAns"						)
    histogram_na_prob(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	OPTS.out_image_dir	,	"noAns"						)
  if OPTS.out_file:
    with open(OPTS.out_file	,	"w"						) as f:
      json.dump(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
  else:
    print(json.dumps(SCREAMING_SNAKE_CASE	,	indent=2						)						)


if __name__ == "__main__":
     UpperCAmelCase     :     Any							=	parse_args()
     if OPTS.out_image_dir:
          import matplotlib

          matplotlib.use("""Agg""")
          import matplotlib.pyplot as plt
     main()













import inspect
import unittest

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


if is_torch_available():
     import torch

     from transformers import MobileNetVaForImageClassification, MobileNetVaModel
     from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import MobileNetVaImageProcessor







class     __lowerCAmelCase	(   UpperCamelCase__):







      def        _lowercase  (   self							)       ->						Any:



        '''simple docstring'''




        a__     : Optional[int]  =self.config_class(**self.inputs_dict							)
        self.parent.assertTrue(hasattr(lowerCAmelCase__		,				"tf_padding"							)							)
        self.parent.assertTrue(hasattr(lowerCAmelCase__		,				"depth_multiplier"							)							)







class     __lowerCAmelCase	:







      def __init__(   self		,				lowerCAmelCase__		,				lowerCAmelCase__=1_3		,				lowerCAmelCase__=3		,				lowerCAmelCase__=3_2		,				lowerCAmelCase__=0.25		,				lowerCAmelCase__=8		,				lowerCAmelCase__=True		,				lowerCAmelCase__=1_0_2_4		,				lowerCAmelCase__=3_2		,				lowerCAmelCase__="relu6"		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=0.02		,				lowerCAmelCase__=True		,				lowerCAmelCase__=True		,				lowerCAmelCase__=1_0		,				lowerCAmelCase__=None		,				)       ->						Dict:



        '''simple docstring'''




        a__     : int  =parent
        a__     : Optional[Any]  =batch_size
        a__     : Tuple  =num_channels
        a__     : Dict  =image_size
        a__     : Union[str, Any]  =depth_multiplier
        a__     : List[str]  =min_depth
        a__     : Dict  =tf_padding
        a__     : Any  =int(last_hidden_size * depth_multiplier							)
        a__     : Tuple  =output_stride
        a__     : Optional[Any]  =hidden_act
        a__     : str  =classifier_dropout_prob
        a__     : int  =use_labels
        a__     : List[Any]  =is_training
        a__     : List[str]  =num_labels
        a__     : Dict  =initializer_range
        a__     : Tuple  =scope







      def        _lowercase  (   self							)       ->						List[str]:



        '''simple docstring'''




        a__     : Union[str, Any]  =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

        a__     : Any  =None
        a__     : List[Any]  =None
        if self.use_labels:
          a__     : Dict  =ids_tensor([self.batch_size]		,				self.num_labels							)
          a__     : Tuple  =ids_tensor([self.batch_size, self.image_size, self.image_size]		,				self.num_labels							)

        a__     : Optional[Any]  =self.get_config()

        return config, pixel_values, labels, pixel_labels







      def        _lowercase  (   self							)       ->						str:



        '''simple docstring'''




        return MobileNetVaConfig(
            num_channels=self.num_channels		,				image_size=self.image_size		,				depth_multiplier=self.depth_multiplier		,				min_depth=self.min_depth		,				tf_padding=self.tf_padding		,				hidden_act=self.hidden_act		,				classifier_dropout_prob=self.classifier_dropout_prob		,				initializer_range=self.initializer_range		,				)







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__							)       ->						Union[str, Any]:



        '''simple docstring'''




        a__     : Union[str, Any]  =MobileNetVaModel(config=lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.eval()
        a__     : str  =model(lowerCAmelCase__							)
        self.parent.assertEqual(
            result.last_hidden_state.shape		,				(
                self.batch_size,
                self.last_hidden_size,
                self.image_size // self.output_stride,
                self.image_size // self.output_stride,
            )		,				)







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__							)       ->						str:



        '''simple docstring'''




        a__     : Any  =self.num_labels
        a__     : Dict  =MobileNetVaForImageClassification(lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.eval()
        a__     : Optional[int]  =model(lowerCAmelCase__		,				labels=lowerCAmelCase__							)
        self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.num_labels)							)







      def        _lowercase  (   self							)       ->						str:



        '''simple docstring'''




        a__     : str  =self.prepare_config_and_inputs()
        a__ ,							a__ ,							a__ ,							a__     : List[str]  =config_and_inputs
        a__     : Tuple  ={"pixel_values": pixel_values}
        return config, inputs_dict







@require_torch
class     __lowerCAmelCase	(   UpperCamelCase__						,    UpperCamelCase__						,    unittest.TestCase):
      _lowercase    : List[str]            =      (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
      _lowercase    : Optional[int]            =      (
          {"""feature-extraction""": MobileNetVaModel, """image-classification""": MobileNetVaForImageClassification}
          if is_torch_available()
          else {}
      )

      _lowercase    : Union[str, Any]            =      False
      _lowercase    : Optional[int]            =      False
      _lowercase    : List[str]            =      False
      _lowercase    : str            =      False







      def        _lowercase  (   self							)       ->						Optional[int]:



        '''simple docstring'''




        a__     : Optional[Any]  =MobileNetVaModelTester(self							)
        a__     : Optional[Any]  =MobileNetVaConfigTester(self		,				config_class=lowerCAmelCase__		,				has_text_modality=lowerCAmelCase__							)







      def        _lowercase  (   self							)       ->						Dict:



        '''simple docstring'''




        self.config_tester.run_common_tests()







      @unittest.skip(reason="MobileNetV1 does not use inputs_embeds"							)
      def        _lowercase  (   self							)       ->						Union[str, Any]:



        '''simple docstring'''




        pass







      @unittest.skip(reason="MobileNetV1 does not support input and output embeddings"							)
      def        _lowercase  (   self							)       ->						List[str]:



        '''simple docstring'''




        pass







      @unittest.skip(reason="MobileNetV1 does not output attentions"							)
      def        _lowercase  (   self							)       ->						Any:



        '''simple docstring'''




        pass







      def        _lowercase  (   self							)       ->						str:



        '''simple docstring'''




        a__ ,							a__     : Optional[int]  =self.model_tester.prepare_config_and_inputs_for_common()

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

          a__     : Union[str, Any]  =["pixel_values"]
          self.assertListEqual(arg_names[:1]		,				lowerCAmelCase__							)







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



        '''simple docstring'''




        a__     : Optional[Any]  =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase__							)







      def        _lowercase  (   self							)       ->						List[str]:



        '''simple docstring'''




        def check_hidden_states_output(lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__							):
          a__     : int  =model_class(lowerCAmelCase__							)
          model.to(lowerCAmelCase__							)
          model.eval()

          with torch.no_grad():
            a__     : List[Any]  =model(**self._prepare_for_class(lowerCAmelCase__		,				lowerCAmelCase__							)							)

          a__     : Optional[int]  =outputs.hidden_states

          a__     : Optional[int]  =2_6
          self.assertEqual(len(lowerCAmelCase__							)		,				lowerCAmelCase__							)

        a__ ,							a__     : Optional[Any]  =self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          a__     : Optional[int]  =True
          check_hidden_states_output(lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__							)

          # check that output_hidden_states also work using config
          del inputs_dict["output_hidden_states"]
          a__     : Union[str, Any]  =True

          check_hidden_states_output(lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__							)







      def        _lowercase  (   self							)       ->						Any:



        '''simple docstring'''




        a__     : Dict  =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__							)







      @slow
      def        _lowercase  (   self							)       ->						Any:



        '''simple docstring'''




        for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          a__     : List[str]  =MobileNetVaModel.from_pretrained(lowerCAmelCase__							)
          self.assertIsNotNone(lowerCAmelCase__							)
def 					_A	(		):





  """simple docstring"""



  a__     : Optional[Any]  =Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
  return image







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







      @cached_property
      def        _lowercase  (   self							)       ->						Optional[Any]:



        '''simple docstring'''




        return (
            MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v1_1.0_224"							) if is_vision_available() else None
        )







      @slow
      def        _lowercase  (   self							)       ->						Optional[int]:



        '''simple docstring'''




        a__     : str  =MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v1_1.0_224"							).to(lowerCAmelCase__							)

        a__     : Optional[Any]  =self.default_image_processor
        a__     : Optional[int]  =prepare_img()
        a__     : Optional[int]  =image_processor(images=lowerCAmelCase__		,				return_tensors="pt"							).to(lowerCAmelCase__							)

        # forward pass
        with torch.no_grad():
          a__     : int  =model(**lowerCAmelCase__							)

        # verify the logits
        a__     : str  =torch.Size((1, 1_0_0_1)							)
        self.assertEqual(outputs.logits.shape		,				lowerCAmelCase__							)

        a__     : int  =torch.tensor([-4.17_39, -1.12_33, 3.12_05]							).to(lowerCAmelCase__							)

        self.assertTrue(torch.allclose(outputs.logits[0, :3]		,				lowerCAmelCase__		,				atol=1E-4							)							)






