



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

import numpy as np


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

UpperCAmelCase      :							str							       =None



def       _lowerCAmelCase			():
					UpperCamelCase_						     =      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=__UpperCAmelCase      ,		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=__UpperCAmelCase      ,		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       _lowerCAmelCase			(_lowerCAmelCase):
					UpperCamelCase_						     =      {}
					for article in dataset:
										for p in article["paragraphs"]:
															for qa in p["qas"]:
																				UpperCamelCase_						     =      bool(qa["answers"]["text"])
					return qid_to_has_ans



def       _lowerCAmelCase			(_lowerCAmelCase):

					def remove_articles(_lowerCAmelCase):
										return ARTICLES_REGEX.sub(" "      ,		__UpperCAmelCase)

					def white_space_fix(_lowerCAmelCase):
										return " ".join(text.split())

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

					def lower(_lowerCAmelCase):
										return text.lower()

					return white_space_fix(remove_articles(remove_punc(lower(__UpperCAmelCase))))



def       _lowerCAmelCase			(_lowerCAmelCase):
					if not s:
										return []
					return normalize_answer(__UpperCAmelCase).split()



def       _lowerCAmelCase			(_lowerCAmelCase      ,		_lowerCAmelCase):
					return int(normalize_answer(__UpperCAmelCase) == normalize_answer(__UpperCAmelCase))



def       _lowerCAmelCase			(_lowerCAmelCase      ,		_lowerCAmelCase):
					UpperCamelCase_						     =      get_tokens(__UpperCAmelCase)
					UpperCamelCase_						     =      get_tokens(__UpperCAmelCase)
					UpperCamelCase_						     =      collections.Counter(__UpperCAmelCase) & collections.Counter(__UpperCAmelCase)
					UpperCamelCase_						     =      sum(common.values())
					if len(__UpperCAmelCase) == 0 or len(__UpperCAmelCase) == 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
					UpperCamelCase_						     =      1.0 * num_same / len(__UpperCAmelCase)
					UpperCamelCase_						     =      1.0 * num_same / len(__UpperCAmelCase)
					UpperCamelCase_						     =      (2 * precision * recall) / (precision + recall)
					return fa



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



def       _lowerCAmelCase			(_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase):
					UpperCamelCase_						     =      {}
					for qid, s in scores.items():
										UpperCamelCase_						     =      na_probs[qid] > na_prob_thresh
										if pred_na:
															UpperCamelCase_						     =      float(not qid_to_has_ans[qid])
										else:
															UpperCamelCase_						     =      s
					return new_scores



def       _lowerCAmelCase			(_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase=None):
					if not qid_list:
										UpperCamelCase_						     =      len(__UpperCAmelCase)
										return collections.OrderedDict(
										    [
										        ("exact", 1_00.0 * sum(exact_scores.values()) / total),
										        ("f1", 1_00.0 * sum(fa_scores.values()) / total),
										        ("total", total),
										    ])
					else:
										UpperCamelCase_						     =      len(__UpperCAmelCase)
										return collections.OrderedDict(
										    [
										        ("exact", 1_00.0 * sum(exact_scores[k] for k in qid_list) / total),
										        ("f1", 1_00.0 * sum(fa_scores[k] for k in qid_list) / total),
										        ("total", total),
										    ])



def       _lowerCAmelCase			(_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase):
					for k in new_eval:
										UpperCamelCase_						     =      new_eval[k]



def       _lowerCAmelCase			(_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase):
					plt.step(__UpperCAmelCase      ,		__UpperCAmelCase      ,		color="b"      ,		alpha=0.2      ,		where="post")
					plt.fill_between(__UpperCAmelCase      ,		__UpperCAmelCase      ,		step="post"      ,		alpha=0.2      ,		color="b")
					plt.xlabel("Recall")
					plt.ylabel("Precision")
					plt.xlim([0.0, 1.05])
					plt.ylim([0.0, 1.05])
					plt.title(__UpperCAmelCase)
					plt.savefig(__UpperCAmelCase)
					plt.clf()



def       _lowerCAmelCase			(_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase=None      ,		_lowerCAmelCase=None):
					UpperCamelCase_						     =      sorted(__UpperCAmelCase      ,		key=lambda _lowerCAmelCase: na_probs[k])
					UpperCamelCase_						     =      0.0
					UpperCamelCase_						     =      1.0
					UpperCamelCase_						     =      0.0
					UpperCamelCase_						     =      [1.0]
					UpperCamelCase_						     =      [0.0]
					UpperCamelCase_						     =      0.0
					for i, qid in enumerate(__UpperCAmelCase):
										if qid_to_has_ans[qid]:
															true_pos += scores[qid]
										UpperCamelCase_						     =      true_pos / float(i + 1)
										UpperCamelCase_						     =      true_pos / float(__UpperCAmelCase)
										if i == len(__UpperCAmelCase) - 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(__UpperCAmelCase)
															recalls.append(__UpperCAmelCase)
					if out_image:
										plot_pr_curve(__UpperCAmelCase      ,		__UpperCAmelCase      ,		__UpperCAmelCase      ,		__UpperCAmelCase)
					return {"ap": 1_00.0 * avg_prec}



def       _lowerCAmelCase			(_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase):
					if out_image_dir and not os.path.exists(__UpperCAmelCase):
										os.makedirs(__UpperCAmelCase)
					UpperCamelCase_						     =      sum(1 for v in qid_to_has_ans.values() if v)
					if num_true_pos == 0:
										return
					UpperCamelCase_						     =      make_precision_recall_eval(
					    __UpperCAmelCase      ,		__UpperCAmelCase      ,		__UpperCAmelCase      ,		__UpperCAmelCase      ,		out_image=os.path.join(__UpperCAmelCase      ,		"pr_exact.png")      ,		title="Precision-Recall curve for Exact Match score"      ,		)
					UpperCamelCase_						     =      make_precision_recall_eval(
					    __UpperCAmelCase      ,		__UpperCAmelCase      ,		__UpperCAmelCase      ,		__UpperCAmelCase      ,		out_image=os.path.join(__UpperCAmelCase      ,		"pr_f1.png")      ,		title="Precision-Recall curve for F1 score"      ,		)
					UpperCamelCase_						     =      {k: float(__UpperCAmelCase) for k, v in qid_to_has_ans.items()}
					UpperCamelCase_						     =      make_precision_recall_eval(
					    __UpperCAmelCase      ,		__UpperCAmelCase      ,		__UpperCAmelCase      ,		__UpperCAmelCase      ,		out_image=os.path.join(__UpperCAmelCase      ,		"pr_oracle.png")      ,		title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)"      ,		)
					merge_eval(__UpperCAmelCase      ,		__UpperCAmelCase      ,		"pr_exact")
					merge_eval(__UpperCAmelCase      ,		__UpperCAmelCase      ,		"pr_f1")
					merge_eval(__UpperCAmelCase      ,		__UpperCAmelCase      ,		"pr_oracle")



def       _lowerCAmelCase			(_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase):
					if not qid_list:
										return
					UpperCamelCase_						     =      [na_probs[k] for k in qid_list]
					UpperCamelCase_						     =      np.ones_like(__UpperCAmelCase) / float(len(__UpperCAmelCase))
					plt.hist(__UpperCAmelCase      ,		weights=__UpperCAmelCase      ,		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(__UpperCAmelCase      ,		f"""na_prob_hist_{name}.png"""))
					plt.clf()



def       _lowerCAmelCase			(_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase):
					UpperCamelCase_						     =      sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k])
					UpperCamelCase_						     =      num_no_ans
					UpperCamelCase_						     =      cur_score
					UpperCamelCase_						     =      0.0
					UpperCamelCase_						     =      sorted(__UpperCAmelCase      ,		key=lambda _lowerCAmelCase: na_probs[k])
					for i, qid in enumerate(__UpperCAmelCase):
										if qid not in scores:
															continue
										if qid_to_has_ans[qid]:
															UpperCamelCase_						     =      scores[qid]
										else:
															if preds[qid]:
																				UpperCamelCase_						     =      -1
															else:
																				UpperCamelCase_						     =      0
										cur_score += diff
										if cur_score > best_score:
															UpperCamelCase_						     =      cur_score
															UpperCamelCase_						     =      na_probs[qid]
					return 1_00.0 * best_score / len(__UpperCAmelCase), best_thresh



def       _lowerCAmelCase			(_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase      ,		_lowerCAmelCase):
					UpperCamelCase_						     =      find_best_thresh(__UpperCAmelCase      ,		__UpperCAmelCase      ,		__UpperCAmelCase      ,		__UpperCAmelCase)
					UpperCamelCase_						     =      find_best_thresh(__UpperCAmelCase      ,		__UpperCAmelCase      ,		__UpperCAmelCase      ,		__UpperCAmelCase)
					UpperCamelCase_						     =      best_exact
					UpperCamelCase_						     =      exact_thresh
					UpperCamelCase_						     =      best_fa
					UpperCamelCase_						     =      fa_thresh



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


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

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

"""simple docstring"""






from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import ResNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
       import tensorflow as tf

       from transformers import TFResNetForImageClassification, TFResNetModel
       from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor


class     UpperCAmelCase						:



    """simple docstring"""

    def __init__(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase=3    ,			_UpperCAmelCase=32    ,			_UpperCAmelCase=3    ,			_UpperCAmelCase=10    ,			_UpperCAmelCase=[10, 20, 30, 40]    ,			_UpperCAmelCase=[1, 1, 2, 1]    ,			_UpperCAmelCase=True    ,			_UpperCAmelCase=True    ,			_UpperCAmelCase="relu"    ,			_UpperCAmelCase=3    ,			_UpperCAmelCase=None    ,			):
       lowercase__:   Optional[Any]					=      parent
       lowercase__:   Union[str, Any]					=      batch_size
       lowercase__:   int					=      image_size
       lowercase__:   Optional[Any]					=      num_channels
       lowercase__:   Optional[int]					=      embeddings_size
       lowercase__:   Dict					=      hidden_sizes
       lowercase__:   Union[str, Any]					=      depths
       lowercase__:   str					=      is_training
       lowercase__:   Optional[int]					=      use_labels
       lowercase__:   List[str]					=      hidden_act
       lowercase__:   Dict					=      num_labels
       lowercase__:   Any					=      scope
       lowercase__:   Optional[Any]					=      len(_UpperCAmelCase							)






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

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

       lowercase__:   Optional[int]					=      self.get_config()

       return config, pixel_values, labels






    def _snake_case			(   self							):
       return ResNetConfig(
           num_channels=self.num_channels    ,			embeddings_size=self.embeddings_size    ,			hidden_sizes=self.hidden_sizes    ,			depths=self.depths    ,			hidden_act=self.hidden_act    ,			num_labels=self.num_labels    ,			image_size=self.image_size    ,			)






    def _snake_case			(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase							):
       lowercase__:   Optional[Any]					=      TFResNetModel(config=_UpperCAmelCase							)
       lowercase__:   Dict					=      model(_UpperCAmelCase							)
       # expected last hidden states: B, C, H // 32, W // 32
       self.parent.assertEqual(
           result.last_hidden_state.shape    ,			(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)    ,			)






    def _snake_case			(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase							):
       lowercase__:   str					=      self.num_labels
       lowercase__:   int					=      TFResNetForImageClassification(_UpperCAmelCase							)
       lowercase__:   Optional[Any]					=      model(_UpperCAmelCase    ,			labels=_UpperCAmelCase							)
       self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_labels)							)






    def _snake_case			(   self							):
       lowercase__:   int					=      self.prepare_config_and_inputs()
       lowercase__, lowercase__, lowercase__:   Optional[Any]					=      config_and_inputs
       lowercase__:   Union[str, Any]					=      {'''pixel_values''': pixel_values}
       return config, inputs_dict



@require_tf
class     UpperCAmelCase						(_UpperCAmelCase    ,_UpperCAmelCase    ,unittest.TestCase  ):



    """simple docstring"""

    _UpperCAmelCase	:Any						    = (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
    _UpperCAmelCase	:List[str]						    = (
        {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification}
        if is_tf_available()
        else {}
    )

    _UpperCAmelCase	:Any						    = False
    _UpperCAmelCase	:List[str]						    = False
    _UpperCAmelCase	:Optional[Any]						    = False
    _UpperCAmelCase	:Tuple						    = False
    _UpperCAmelCase	:List[Any]						    = False






    def _snake_case			(   self							):
       lowercase__:   Union[str, Any]					=      TFResNetModelTester(self							)
       lowercase__:   Optional[int]					=      ConfigTester(self    ,			config_class=_UpperCAmelCase    ,			has_text_modality=_UpperCAmelCase							)






    def _snake_case			(   self							):
       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							):
       return






    @unittest.skip(reason='''ResNet does not use inputs_embeds'''							)
    def _snake_case			(   self							):
       pass






    @unittest.skip(reason='''ResNet does not support input and output embeddings'''							)
    def _snake_case			(   self							):
       pass






    def _snake_case			(   self							):
       lowercase__, lowercase__:   int					=      self.model_tester.prepare_config_and_inputs_for_common()

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

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






    def _snake_case			(   self							):
       lowercase__:   Any					=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_UpperCAmelCase							)






    def _snake_case			(   self							):
       def check_hidden_states_output(_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase							):
          lowercase__:   Union[str, Any]					=      model_class(_UpperCAmelCase							)
          lowercase__:   List[Any]					=      model(**self._prepare_for_class(_UpperCAmelCase    ,			_UpperCAmelCase							)							)

          lowercase__:   Union[str, Any]					=      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

          lowercase__:   Tuple					=      self.model_tester.num_stages
          self.assertEqual(len(_UpperCAmelCase							)    ,			expected_num_stages + 1							)

          # ResNet'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 // 4, self.model_tester.image_size // 4]    ,			)

       lowercase__, lowercase__:   Optional[Any]					=      self.model_tester.prepare_config_and_inputs_for_common()
       lowercase__:   Union[str, Any]					=      ['''basic''', '''bottleneck''']
       for model_class in self.all_model_classes:
          for layer_type in layers_type:
             lowercase__:   Tuple					=      layer_type
             lowercase__:   Any					=      True
             check_hidden_states_output(_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase							)

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

             check_hidden_states_output(_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase							)






    def _snake_case			(   self							):
       lowercase__:   Optional[int]					=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase							)






    @slow
    def _snake_case			(   self							):
       for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowercase__:   Dict					=      TFResNetModel.from_pretrained(_UpperCAmelCase							)
          self.assertIsNotNone(_UpperCAmelCase							)



def SCREAMING_SNAKE_CASE__			(   )  ->  List[Any]:
   lowercase__:   List[Any]					=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
   return image




@require_tf
@require_vision
class     UpperCAmelCase						(unittest.TestCase  ):



    """simple docstring"""

    @cached_property
    def _snake_case			(   self							):
       return (
           AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)
           if is_vision_available()
           else None
       )






    @slow
    def _snake_case			(   self							):
       lowercase__:   Tuple					=      TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)

       lowercase__:   Any					=      self.default_image_processor
       lowercase__:   List[Any]					=      prepare_img()
       lowercase__:   List[Any]					=      image_processor(images=_UpperCAmelCase    ,			return_tensors='''tf'''							)

       # forward pass
       lowercase__:   Dict					=      model(**_UpperCAmelCase							)

       # verify the logits
       lowercase__:   int					=      tf.TensorShape((1, 1000)							)
       self.assertEqual(outputs.logits.shape    ,			_UpperCAmelCase							)

       lowercase__:   Dict					=      tf.constant([-11.1_069, -9.7_877, -8.3_777]							)

       self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()    ,			_UpperCAmelCase    ,			atol=1e-4							)							)
