import argparse
import json

import gdown
import numpy as np
import torch
from huggingface_hub import hf_hub_download

from transformers import (
    VideoMAEConfig,
    VideoMAEForPreTraining,
    VideoMAEForVideoClassification,
    VideoMAEImageProcessor,
)





def   __lowerCamelCase							(UpperCAmelCase__       :							Union[str, Any]					):
					SCREAMING_SNAKE_CASE           =    VideoMAEConfig()

					set_architecture_configs(UpperCAmelCase__ ,	UpperCAmelCase__					)

					if "finetuned" not in model_name:
										SCREAMING_SNAKE_CASE           =    False

					if "finetuned" in model_name:
										SCREAMING_SNAKE_CASE           =    "huggingface/label-files"
										if "kinetics" in model_name:
															SCREAMING_SNAKE_CASE           =    4_0_0
															SCREAMING_SNAKE_CASE           =    "kinetics400-id2label.json"
										elif "ssv2" in model_name:
															SCREAMING_SNAKE_CASE           =    1_7_4
															SCREAMING_SNAKE_CASE           =    "something-something-v2-id2label.json"
										else:
															raise ValueError("Model name should either contain 'kinetics' or 'ssv2' in case it's fine-tuned."					)
										SCREAMING_SNAKE_CASE           =    json.load(open(hf_hub_download(UpperCAmelCase__ ,	UpperCAmelCase__ ,	repo_type="dataset"					) ,	"r"					)					)
										SCREAMING_SNAKE_CASE           =    {int(UpperCAmelCase__					): v for k, v in idalabel.items()}
										SCREAMING_SNAKE_CASE           =    idalabel
										SCREAMING_SNAKE_CASE           =    {v: k for k, v in idalabel.items()}

					return config





def   __lowerCamelCase							(UpperCAmelCase__       :							Union[str, Any] ,	UpperCAmelCase__       :							Dict					):
					if "small" in model_name:
										SCREAMING_SNAKE_CASE           =    3_8_4
										SCREAMING_SNAKE_CASE           =    1_5_3_6
										SCREAMING_SNAKE_CASE           =    1_2
										SCREAMING_SNAKE_CASE           =    1_6
										SCREAMING_SNAKE_CASE           =    1_2
										SCREAMING_SNAKE_CASE           =    3
										SCREAMING_SNAKE_CASE           =    1_9_2
										SCREAMING_SNAKE_CASE           =    7_6_8
					elif "large" in model_name:
										SCREAMING_SNAKE_CASE           =    1_0_2_4
										SCREAMING_SNAKE_CASE           =    4_0_9_6
										SCREAMING_SNAKE_CASE           =    2_4
										SCREAMING_SNAKE_CASE           =    1_6
										SCREAMING_SNAKE_CASE           =    1_2
										SCREAMING_SNAKE_CASE           =    8
										SCREAMING_SNAKE_CASE           =    5_1_2
										SCREAMING_SNAKE_CASE           =    2_0_4_8
					elif "huge" in model_name:
										SCREAMING_SNAKE_CASE           =    1_2_8_0
										SCREAMING_SNAKE_CASE           =    5_1_2_0
										SCREAMING_SNAKE_CASE           =    3_2
										SCREAMING_SNAKE_CASE           =    1_6
										SCREAMING_SNAKE_CASE           =    1_2
										SCREAMING_SNAKE_CASE           =    8
										SCREAMING_SNAKE_CASE           =    6_4_0
										SCREAMING_SNAKE_CASE           =    2_5_6_0
					elif "base" not in model_name:
										raise ValueError("Model name should include either \"small\", \"base\", \"large\", or \"huge\""					)





def   __lowerCamelCase							(UpperCAmelCase__       :							List[str]					):
					if "encoder." in name:
										SCREAMING_SNAKE_CASE           =    name.replace("encoder." ,	""					)
					if "cls_token" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("cls_token" ,	"videomae.embeddings.cls_token"					)
					if "decoder_pos_embed" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("decoder_pos_embed" ,	"decoder.decoder_pos_embed"					)
					if "pos_embed" in name and "decoder" not in name:
										SCREAMING_SNAKE_CASE           =    name.replace("pos_embed" ,	"videomae.embeddings.position_embeddings"					)
					if "patch_embed.proj" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("patch_embed.proj" ,	"videomae.embeddings.patch_embeddings.projection"					)
					if "patch_embed.norm" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("patch_embed.norm" ,	"videomae.embeddings.norm"					)
					if "decoder.blocks" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("decoder.blocks" ,	"decoder.decoder_layers"					)
					if "blocks" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("blocks" ,	"videomae.encoder.layer"					)
					if "attn.proj" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("attn.proj" ,	"attention.output.dense"					)
					if "attn" in name and "bias" not in name:
										SCREAMING_SNAKE_CASE           =    name.replace("attn" ,	"attention.self"					)
					if "attn" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("attn" ,	"attention.attention"					)
					if "norm1" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("norm1" ,	"layernorm_before"					)
					if "norm2" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("norm2" ,	"layernorm_after"					)
					if "mlp.fc1" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("mlp.fc1" ,	"intermediate.dense"					)
					if "mlp.fc2" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("mlp.fc2" ,	"output.dense"					)
					if "decoder_embed" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("decoder_embed" ,	"decoder.decoder_embed"					)
					if "decoder_norm" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("decoder_norm" ,	"decoder.decoder_norm"					)
					if "decoder_pred" in name:
										SCREAMING_SNAKE_CASE           =    name.replace("decoder_pred" ,	"decoder.decoder_pred"					)
					if "norm.weight" in name and "decoder" not in name and "fc" not in name:
										SCREAMING_SNAKE_CASE           =    name.replace("norm.weight" ,	"videomae.layernorm.weight"					)
					if "norm.bias" in name and "decoder" not in name and "fc" not in name:
										SCREAMING_SNAKE_CASE           =    name.replace("norm.bias" ,	"videomae.layernorm.bias"					)
					if "head" in name and "decoder" not in name:
										SCREAMING_SNAKE_CASE           =    name.replace("head" ,	"classifier"					)

					return name





def   __lowerCamelCase							(UpperCAmelCase__       :							Tuple ,	UpperCAmelCase__       :							int					):
					for key in orig_state_dict.copy().keys():
										SCREAMING_SNAKE_CASE           =    orig_state_dict.pop(UpperCAmelCase__					)

										if key.startswith("encoder."					):
															SCREAMING_SNAKE_CASE           =    key.replace("encoder." ,	""					)

										if "qkv" in key:
															SCREAMING_SNAKE_CASE           =    key.split("."					)
															if key.startswith("decoder.blocks"					):
																				SCREAMING_SNAKE_CASE           =    config.decoder_hidden_size
																				SCREAMING_SNAKE_CASE           =    int(key_split[2]					)
																				SCREAMING_SNAKE_CASE           =    "decoder.decoder_layers."
																				if "weight" in key:
																									SCREAMING_SNAKE_CASE           =    val[:dim, :]
																									SCREAMING_SNAKE_CASE           =    val[dim : dim * 2, :]
																									SCREAMING_SNAKE_CASE           =    val[-dim:, :]
															else:
																				SCREAMING_SNAKE_CASE           =    config.hidden_size
																				SCREAMING_SNAKE_CASE           =    int(key_split[1]					)
																				SCREAMING_SNAKE_CASE           =    "videomae.encoder.layer."
																				if "weight" in key:
																									SCREAMING_SNAKE_CASE           =    val[:dim, :]
																									SCREAMING_SNAKE_CASE           =    val[dim : dim * 2, :]
																									SCREAMING_SNAKE_CASE           =    val[-dim:, :]
										else:
															SCREAMING_SNAKE_CASE           =    val

					return orig_state_dict





def   __lowerCamelCase							():
					SCREAMING_SNAKE_CASE           =    hf_hub_download(
					    repo_id="hf-internal-testing/spaghetti-video" ,	filename="eating_spaghetti.npy" ,	repo_type="dataset"					)
					SCREAMING_SNAKE_CASE           =    np.load(UpperCAmelCase__					)
					return list(UpperCAmelCase__					)





def   __lowerCamelCase							(UpperCAmelCase__       :							List[Any] ,	UpperCAmelCase__       :							List[str] ,	UpperCAmelCase__       :							Union[str, Any] ,	UpperCAmelCase__       :							Optional[int]					):
					SCREAMING_SNAKE_CASE           =    get_videomae_config(UpperCAmelCase__					)

					if "finetuned" in model_name:
										SCREAMING_SNAKE_CASE           =    VideoMAEForVideoClassification(UpperCAmelCase__					)
					else:
										SCREAMING_SNAKE_CASE           =    VideoMAEForPreTraining(UpperCAmelCase__					)

					# download original checkpoint, hosted on Google Drive
					SCREAMING_SNAKE_CASE           =    "pytorch_model.bin"
					gdown.cached_download(UpperCAmelCase__ ,	UpperCAmelCase__ ,	quiet=UpperCAmelCase__					)
					SCREAMING_SNAKE_CASE           =    torch.load(UpperCAmelCase__ ,	map_location="cpu"					)
					if "model" in files:
										SCREAMING_SNAKE_CASE           =    files["model"]
					else:
										SCREAMING_SNAKE_CASE           =    files["module"]
					SCREAMING_SNAKE_CASE           =    convert_state_dict(UpperCAmelCase__ ,	UpperCAmelCase__					)

					model.load_state_dict(UpperCAmelCase__					)
					model.eval()

					# verify model on basic input
					SCREAMING_SNAKE_CASE           =    VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] ,	image_std=[0.5, 0.5, 0.5]					)
					SCREAMING_SNAKE_CASE           =    prepare_video()
					SCREAMING_SNAKE_CASE           =    image_processor(UpperCAmelCase__ ,	return_tensors="pt"					)

					if "finetuned" not in model_name:
										SCREAMING_SNAKE_CASE           =    hf_hub_download(repo_id="hf-internal-testing/bool-masked-pos" ,	filename="bool_masked_pos.pt"					)
										SCREAMING_SNAKE_CASE           =    torch.load(UpperCAmelCase__					)

					SCREAMING_SNAKE_CASE           =    model(**UpperCAmelCase__					)
					SCREAMING_SNAKE_CASE           =    outputs.logits

					SCREAMING_SNAKE_CASE           =    [
					    "videomae-small-finetuned-kinetics",
					    "videomae-small-finetuned-ssv2",
					    # Kinetics-400 checkpoints (short = pretrained only for 800 epochs instead of 1600)
					    "videomae-base-short",
					    "videomae-base-short-finetuned-kinetics",
					    "videomae-base",
					    "videomae-base-finetuned-kinetics",
					    "videomae-large",
					    "videomae-large-finetuned-kinetics",
					    "videomae-huge-finetuned-kinetics",
					    # Something-Something-v2 checkpoints (short = pretrained only for 800 epochs instead of 2400)
					    "videomae-base-short-ssv2",
					    "videomae-base-short-finetuned-ssv2",
					    "videomae-base-ssv2",
					    "videomae-base-finetuned-ssv2",
					]

					# NOTE: logits were tested with image_mean and image_std equal to [0.5, 0.5, 0.5] and [0.5, 0.5, 0.5]
					if model_name == "videomae-small-finetuned-kinetics":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 4_0_0]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([-0.9291, -0.4061, -0.9307]					)
					elif model_name == "videomae-small-finetuned-ssv2":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 1_7_4]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([0.2671, -0.4689, -0.8235]					)
					elif model_name == "videomae-base":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 1_4_0_8, 1_5_3_6]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([[0.7739, 0.7968, 0.7089], [0.6701, 0.7487, 0.6209], [0.4287, 0.5158, 0.4773]]					)
					elif model_name == "videomae-base-short":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 1_4_0_8, 1_5_3_6]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]]					)
										# we verified the loss both for normalized and unnormalized targets for this one
										SCREAMING_SNAKE_CASE           =    torch.tensor([0.5142]					) if config.norm_pix_loss else torch.tensor([0.6469]					)
					elif model_name == "videomae-large":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 1_4_0_8, 1_5_3_6]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([[0.7149, 0.7997, 0.6966], [0.6768, 0.7869, 0.6948], [0.5139, 0.6221, 0.5605]]					)
					elif model_name == "videomae-large-finetuned-kinetics":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 4_0_0]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([0.0771, 0.0011, -0.3625]					)
					elif model_name == "videomae-huge-finetuned-kinetics":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 4_0_0]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([0.2433, 0.1632, -0.4894]					)
					elif model_name == "videomae-base-short-finetuned-kinetics":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 4_0_0]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([0.6588, 0.0990, -0.2493]					)
					elif model_name == "videomae-base-finetuned-kinetics":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 4_0_0]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([0.3669, -0.0688, -0.2421]					)
					elif model_name == "videomae-base-short-ssv2":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 1_4_0_8, 1_5_3_6]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([[0.4712, 0.5296, 0.5786], [0.2278, 0.2729, 0.4026], [0.0352, 0.0730, 0.2506]]					)
					elif model_name == "videomae-base-short-finetuned-ssv2":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 1_7_4]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([-0.0537, -0.1539, -0.3266]					)
					elif model_name == "videomae-base-ssv2":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 1_4_0_8, 1_5_3_6]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([[0.8131, 0.8727, 0.8546], [0.7366, 0.9377, 0.8870], [0.5935, 0.8874, 0.8564]]					)
					elif model_name == "videomae-base-finetuned-ssv2":
										SCREAMING_SNAKE_CASE           =    torch.Size([1, 1_7_4]					)
										SCREAMING_SNAKE_CASE           =    torch.tensor([0.1961, -0.8337, -0.6389]					)
					else:
										raise ValueError(F"Model name not supported. Should be one of {model_names}"					)

					# verify logits
					assert logits.shape == expected_shape
					if "finetuned" in model_name:
										assert torch.allclose(logits[0, :3] ,	UpperCAmelCase__ ,	atol=1e-4					)
					else:
										print("Logits:" ,	logits[0, :3, :3]					)
										assert torch.allclose(logits[0, :3, :3] ,	UpperCAmelCase__ ,	atol=1e-4					)
					print("Logits ok!"					)

					# verify loss, if applicable
					if model_name == "videomae-base-short":
										SCREAMING_SNAKE_CASE           =    outputs.loss
										assert torch.allclose(UpperCAmelCase__ ,	UpperCAmelCase__ ,	atol=1e-4					)
										print("Loss ok!"					)

					if pytorch_dump_folder_path is not None:
										print(F"Saving model and image processor to {pytorch_dump_folder_path}"					)
										image_processor.save_pretrained(UpperCAmelCase__					)
										model.save_pretrained(UpperCAmelCase__					)

					if push_to_hub:
										print("Pushing to the hub..."					)
										model.push_to_hub(UpperCAmelCase__ ,	organization="nielsr"					)


if __name__ == "__main__":
							_lowerCamelCase   :				int     			=						argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    '''--checkpoint_url''',
							    default='''https://drive.google.com/u/1/uc?id=1tEhLyskjb755TJ65ptsrafUG2llSwQE1&amp;export=download&amp;confirm=t&amp;uuid=aa3276eb-fb7e-482a-adec-dc7171df14c4''',
							    type=str,
							    help=(
							        '''URL of the original PyTorch checkpoint (on Google Drive) you\'d like to convert. Should be a direct'''
							        ''' download link.'''
							    ),
							)
							parser.add_argument(
							    '''--pytorch_dump_folder_path''',
							    default='''/Users/nielsrogge/Documents/VideoMAE/Test''',
							    type=str,
							    help='''Path to the output PyTorch model directory.''',
							)
							parser.add_argument('''--model_name''', default='''videomae-base''', type=str, help='''Name of the model.''')
							parser.add_argument(
							    '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.'''
							)

							_lowerCamelCase   :				List[Any]     			=						parser.parse_args()
							convert_videomae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)





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   lowercase      (       a       ):




							def 		__snake_case(  self				:   Optional[int] )   ->			str:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    self.config_class(**self.inputs_dict )
												self.parent.assertTrue(hasattr(_UpperCamelCase		,							"tf_padding" ) )
												self.parent.assertTrue(hasattr(_UpperCamelCase		,							"depth_multiplier" ) )



class   lowercase      :




							def __init__(  self				:   Tuple		,							_UpperCamelCase				:   Optional[Any]		,							_UpperCamelCase				:   Any=13		,							_UpperCamelCase				:   Any=3		,							_UpperCamelCase				:   Union[str, Any]=32		,							_UpperCamelCase				:   Optional[Any]=0.2_5		,							_UpperCamelCase				:   int=8		,							_UpperCamelCase				:   str=True		,							_UpperCamelCase				:   Any=1_024		,							_UpperCamelCase				:   Tuple=32		,							_UpperCamelCase				:   List[str]="relu6"		,							_UpperCamelCase				:   Tuple=0.1		,							_UpperCamelCase				:   List[str]=0.0_2		,							_UpperCamelCase				:   int=True		,							_UpperCamelCase				:   int=True		,							_UpperCamelCase				:   Optional[Any]=10		,							_UpperCamelCase				:   List[str]=None		,							)   ->			Any:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    parent
												SCREAMING_SNAKE_CASE           =    batch_size
												SCREAMING_SNAKE_CASE           =    num_channels
												SCREAMING_SNAKE_CASE           =    image_size
												SCREAMING_SNAKE_CASE           =    depth_multiplier
												SCREAMING_SNAKE_CASE           =    min_depth
												SCREAMING_SNAKE_CASE           =    tf_padding
												SCREAMING_SNAKE_CASE           =    int(last_hidden_size * depth_multiplier )
												SCREAMING_SNAKE_CASE           =    output_stride
												SCREAMING_SNAKE_CASE           =    hidden_act
												SCREAMING_SNAKE_CASE           =    classifier_dropout_prob
												SCREAMING_SNAKE_CASE           =    use_labels
												SCREAMING_SNAKE_CASE           =    is_training
												SCREAMING_SNAKE_CASE           =    num_labels
												SCREAMING_SNAKE_CASE           =    initializer_range
												SCREAMING_SNAKE_CASE           =    scope




							def 		__snake_case(  self				:   Dict )   ->			List[str]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

												SCREAMING_SNAKE_CASE           =    None
												SCREAMING_SNAKE_CASE           =    None
												if self.use_labels:
																	SCREAMING_SNAKE_CASE           =    ids_tensor([self.batch_size]		,							self.num_labels )
																	SCREAMING_SNAKE_CASE           =    ids_tensor([self.batch_size, self.image_size, self.image_size]		,							self.num_labels )

												SCREAMING_SNAKE_CASE           =    self.get_config()

												return config, pixel_values, labels, pixel_labels




							def 		__snake_case(  self				:   Optional[Any] )   ->			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 		__snake_case(  self				:   int		,							_UpperCamelCase				:   Optional[int]		,							_UpperCamelCase				:   Any		,							_UpperCamelCase				:   Union[str, Any]		,							_UpperCamelCase				:   int )   ->			List[str]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    MobileNetVaModel(config=_UpperCamelCase )
												model.to(_UpperCamelCase )
												model.eval()
												SCREAMING_SNAKE_CASE           =    model(_UpperCamelCase )
												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 		__snake_case(  self				:   Dict		,							_UpperCamelCase				:   Tuple		,							_UpperCamelCase				:   Any		,							_UpperCamelCase				:   str		,							_UpperCamelCase				:   List[Any] )   ->			Optional[int]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    self.num_labels
												SCREAMING_SNAKE_CASE           =    MobileNetVaForImageClassification(_UpperCamelCase )
												model.to(_UpperCamelCase )
												model.eval()
												SCREAMING_SNAKE_CASE           =    model(_UpperCamelCase		,							labels=_UpperCamelCase )
												self.parent.assertEqual(result.logits.shape		,							(self.batch_size, self.num_labels) )





							def 		__snake_case(  self				:   Optional[Any] )   ->			str:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    self.prepare_config_and_inputs()
												SCREAMING_SNAKE_CASE     ,				SCREAMING_SNAKE_CASE     ,				SCREAMING_SNAKE_CASE     ,				SCREAMING_SNAKE_CASE           =    config_and_inputs
												SCREAMING_SNAKE_CASE           =    {"pixel_values": pixel_values}
												return config, inputs_dict



@require_torch
class   lowercase      (       a			,    a			,    unittest.TestCase       ):
							lowercase__  :    Dict									=							(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
							lowercase__  :    Tuple									=							(
							    {"""feature-extraction""": MobileNetVaModel, """image-classification""": MobileNetVaForImageClassification}
							    if is_torch_available()
							    else {}
							)

							lowercase__  :    Union[str, Any]									=							False
							lowercase__  :    Union[str, Any]									=							False
							lowercase__  :    Tuple									=							False
							lowercase__  :    List[str]									=							False




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



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    MobileNetVaModelTester(self )
												SCREAMING_SNAKE_CASE           =    MobileNetVaConfigTester(self		,							config_class=_UpperCamelCase		,							has_text_modality=_UpperCamelCase )




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



												'''simple docstring'''

												self.config_tester.run_common_tests()




							@unittest.skip(reason="MobileNetV1 does not use inputs_embeds" )
							def 		__snake_case(  self				:   Tuple )   ->			Tuple:



												'''simple docstring'''

												pass




							@unittest.skip(reason="MobileNetV1 does not support input and output embeddings" )
							def 		__snake_case(  self				:   Optional[int] )   ->			Dict:



												'''simple docstring'''

												pass




							@unittest.skip(reason="MobileNetV1 does not output attentions" )
							def 		__snake_case(  self				:   Any )   ->			List[str]:



												'''simple docstring'''

												pass




							def 		__snake_case(  self				:   List[Any] )   ->			str:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE     ,				SCREAMING_SNAKE_CASE           =    self.model_tester.prepare_config_and_inputs_for_common()

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

																	SCREAMING_SNAKE_CASE           =    ["pixel_values"]
																	self.assertListEqual(arg_names[:1]		,							_UpperCamelCase )




							def 		__snake_case(  self				:   List[Any] )   ->			Dict:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_UpperCamelCase )




							def 		__snake_case(  self				:   List[str] )   ->			Optional[int]:



												'''simple docstring'''

												def check_hidden_states_output(_UpperCamelCase				:   Any		,							_UpperCamelCase				:   Dict		,							_UpperCamelCase				:   Tuple ):
																	SCREAMING_SNAKE_CASE           =    model_class(_UpperCamelCase )
																	model.to(_UpperCamelCase )
																	model.eval()

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

																	SCREAMING_SNAKE_CASE           =    outputs.hidden_states

																	SCREAMING_SNAKE_CASE           =    26
																	self.assertEqual(len(_UpperCamelCase )		,							_UpperCamelCase )

												SCREAMING_SNAKE_CASE     ,				SCREAMING_SNAKE_CASE           =    self.model_tester.prepare_config_and_inputs_for_common()

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

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

																	check_hidden_states_output(_UpperCamelCase		,							_UpperCamelCase		,							_UpperCamelCase )




							def 		__snake_case(  self				:   Any )   ->			Optional[int]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase )





							@slow
							def 		__snake_case(  self				:   int )   ->			str:



												'''simple docstring'''

												for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	SCREAMING_SNAKE_CASE           =    MobileNetVaModel.from_pretrained(_UpperCamelCase )
																	self.assertIsNotNone(_UpperCamelCase )





def 		__lowerCamelCase							():
					SCREAMING_SNAKE_CASE           =    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
					return image


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




							@cached_property
							def 		__snake_case(  self				:   List[Any] )   ->			Optional[int]:



												'''simple docstring'''

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





							@slow
							def 		__snake_case(  self				:   str )   ->			Union[str, Any]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v1_1.0_224" ).to(_UpperCamelCase )

												SCREAMING_SNAKE_CASE           =    self.default_image_processor
												SCREAMING_SNAKE_CASE           =    prepare_img()
												SCREAMING_SNAKE_CASE           =    image_processor(images=_UpperCamelCase		,							return_tensors="pt" ).to(_UpperCamelCase )

												# forward pass
												with torch.no_grad():
																	SCREAMING_SNAKE_CASE           =    model(**_UpperCamelCase )

												# verify the logits
												SCREAMING_SNAKE_CASE           =    torch.Size((1, 1_001) )
												self.assertEqual(outputs.logits.shape		,							_UpperCamelCase )

												SCREAMING_SNAKE_CASE           =    torch.tensor([-4.1_7_3_9, -1.1_2_3_3, 3.1_2_0_5] ).to(_UpperCamelCase )

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




