


"""simple docstring"""


import argparse
import json
import os

import numpy as np
import PIL
import requests
import tensorflow.keras.applications.efficientnet as efficientnet
import torch
from huggingface_hub import hf_hub_download
from PIL import Image
from tensorflow.keras.preprocessing import image

from transformers import (
    EfficientNetConfig,
    EfficientNetForImageClassification,
    EfficientNetImageProcessor,
)
from transformers.utils import logging


logging.set_verbosity_info()
a_       							=    logging.get_logger(__name__)

a_       							=    {
    "b0": efficientnet.EfficientNetBa,
    "b1": efficientnet.EfficientNetBa,
    "b2": efficientnet.EfficientNetBa,
    "b3": efficientnet.EfficientNetBa,
    "b4": efficientnet.EfficientNetBa,
    "b5": efficientnet.EfficientNetBa,
    "b6": efficientnet.EfficientNetBa,
    "b7": efficientnet.EfficientNetBa,
}

a_       							=    {
    "b0": {
        "hidden_dim": 12_80,
        "width_coef": 1.0,
        "depth_coef": 1.0,
        "image_size": 2_24,
        "dropout_rate": 0.2,
        "dw_padding": [],
    },
    "b1": {
        "hidden_dim": 12_80,
        "width_coef": 1.0,
        "depth_coef": 1.1,
        "image_size": 2_40,
        "dropout_rate": 0.2,
        "dw_padding": [16],
    },
    "b2": {
        "hidden_dim": 14_08,
        "width_coef": 1.1,
        "depth_coef": 1.2,
        "image_size": 2_60,
        "dropout_rate": 0.3,
        "dw_padding": [5, 8, 16],
    },
    "b3": {
        "hidden_dim": 15_36,
        "width_coef": 1.2,
        "depth_coef": 1.4,
        "image_size": 3_00,
        "dropout_rate": 0.3,
        "dw_padding": [5, 18],
    },
    "b4": {
        "hidden_dim": 17_92,
        "width_coef": 1.4,
        "depth_coef": 1.8,
        "image_size": 3_80,
        "dropout_rate": 0.4,
        "dw_padding": [6],
    },
    "b5": {
        "hidden_dim": 20_48,
        "width_coef": 1.6,
        "depth_coef": 2.2,
        "image_size": 4_56,
        "dropout_rate": 0.4,
        "dw_padding": [13, 27],
    },
    "b6": {
        "hidden_dim": 23_04,
        "width_coef": 1.8,
        "depth_coef": 2.6,
        "image_size": 5_28,
        "dropout_rate": 0.5,
        "dw_padding": [31],
    },
    "b7": {
        "hidden_dim": 25_60,
        "width_coef": 2.0,
        "depth_coef": 3.1,
        "image_size": 6_00,
        "dropout_rate": 0.5,
        "dw_padding": [18],
    },
}






def      a__ (				__lowercase   )	->	List[Any]:
				_A					  =			EfficientNetConfig()
				_A					  =			CONFIG_MAP[model_name]["hidden_dim"]
				_A					  =			CONFIG_MAP[model_name]["width_coef"]
				_A					  =			CONFIG_MAP[model_name]["depth_coef"]
				_A					  =			CONFIG_MAP[model_name]["image_size"]
				_A					  =			CONFIG_MAP[model_name]["dropout_rate"]
				_A					  =			CONFIG_MAP[model_name]["dw_padding"]

				_A					  =			"huggingface/label-files"
				_A					  =			"imagenet-1k-id2label.json"
				_A					  =			1000
				_A					  =			json.load(open(hf_hub_download(lowercase__     ,			lowercase__     ,			repo_type="dataset"   )     ,			"r"   )   )
				_A					  =			{int(lowercase__   ): v for k, v in idalabel.items()}

				_A					  =			idalabel
				_A					  =			{v: k for k, v in idalabel.items()}
				return config






def      a__ (				)	->	Any:
				_A					  =			"http://images.cocodataset.org/val2017/000000039769.jpg"
				_A					  =			Image.open(requests.get(lowercase__     ,			stream=lowercase__   ).raw   )
				return im






def      a__ (				__lowercase   )	->	Union[str, Any]:
				_A					  =			CONFIG_MAP[model_name]["image_size"]
				_A					  =			EfficientNetImageProcessor(
				    size={"height": size, "width": size}     ,			image_mean=[0.485, 0.456, 0.406]     ,			image_std=[0.47_853_944, 0.4_732_864, 0.47_434_163]     ,			do_center_crop=lowercase__     ,			)
				return preprocessor






def      a__ (				__lowercase   )	->	str:
				_A					  =			[v.split("_"   )[0].split("block"   )[1] for v in original_param_names if v.startswith("block"   )]
				_A					  =			sorted(set(lowercase__   )   )
				_A					  =			len(lowercase__   )
				_A					  =			{b: str(lowercase__   ) for b, i in zip(lowercase__     ,			range(lowercase__   )   )}

				_A					  =			[]
				rename_keys.append(("stem_conv/kernel:0", "embeddings.convolution.weight")   )
				rename_keys.append(("stem_bn/gamma:0", "embeddings.batchnorm.weight")   )
				rename_keys.append(("stem_bn/beta:0", "embeddings.batchnorm.bias")   )
				rename_keys.append(("stem_bn/moving_mean:0", "embeddings.batchnorm.running_mean")   )
				rename_keys.append(("stem_bn/moving_variance:0", "embeddings.batchnorm.running_var")   )

				for b in block_names:
								_A					  =			block_name_mapping[b]
								rename_keys.append((f"""block{b}_expand_conv/kernel:0""", f"""encoder.blocks.{hf_b}.expansion.expand_conv.weight""")   )
								rename_keys.append((f"""block{b}_expand_bn/gamma:0""", f"""encoder.blocks.{hf_b}.expansion.expand_bn.weight""")   )
								rename_keys.append((f"""block{b}_expand_bn/beta:0""", f"""encoder.blocks.{hf_b}.expansion.expand_bn.bias""")   )
								rename_keys.append(
								    (f"""block{b}_expand_bn/moving_mean:0""", f"""encoder.blocks.{hf_b}.expansion.expand_bn.running_mean""")   )
								rename_keys.append(
								    (f"""block{b}_expand_bn/moving_variance:0""", f"""encoder.blocks.{hf_b}.expansion.expand_bn.running_var""")   )
								rename_keys.append(
								    (f"""block{b}_dwconv/depthwise_kernel:0""", f"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight""")   )
								rename_keys.append((f"""block{b}_bn/gamma:0""", f"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight""")   )
								rename_keys.append((f"""block{b}_bn/beta:0""", f"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias""")   )
								rename_keys.append(
								    (f"""block{b}_bn/moving_mean:0""", f"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean""")   )
								rename_keys.append(
								    (f"""block{b}_bn/moving_variance:0""", f"""encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var""")   )

								rename_keys.append((f"""block{b}_se_reduce/kernel:0""", f"""encoder.blocks.{hf_b}.squeeze_excite.reduce.weight""")   )
								rename_keys.append((f"""block{b}_se_reduce/bias:0""", f"""encoder.blocks.{hf_b}.squeeze_excite.reduce.bias""")   )
								rename_keys.append((f"""block{b}_se_expand/kernel:0""", f"""encoder.blocks.{hf_b}.squeeze_excite.expand.weight""")   )
								rename_keys.append((f"""block{b}_se_expand/bias:0""", f"""encoder.blocks.{hf_b}.squeeze_excite.expand.bias""")   )
								rename_keys.append(
								    (f"""block{b}_project_conv/kernel:0""", f"""encoder.blocks.{hf_b}.projection.project_conv.weight""")   )
								rename_keys.append((f"""block{b}_project_bn/gamma:0""", f"""encoder.blocks.{hf_b}.projection.project_bn.weight""")   )
								rename_keys.append((f"""block{b}_project_bn/beta:0""", f"""encoder.blocks.{hf_b}.projection.project_bn.bias""")   )
								rename_keys.append(
								    (f"""block{b}_project_bn/moving_mean:0""", f"""encoder.blocks.{hf_b}.projection.project_bn.running_mean""")   )
								rename_keys.append(
								    (f"""block{b}_project_bn/moving_variance:0""", f"""encoder.blocks.{hf_b}.projection.project_bn.running_var""")   )

				rename_keys.append(("top_conv/kernel:0", "encoder.top_conv.weight")   )
				rename_keys.append(("top_bn/gamma:0", "encoder.top_bn.weight")   )
				rename_keys.append(("top_bn/beta:0", "encoder.top_bn.bias")   )
				rename_keys.append(("top_bn/moving_mean:0", "encoder.top_bn.running_mean")   )
				rename_keys.append(("top_bn/moving_variance:0", "encoder.top_bn.running_var")   )

				_A					  =			{}
				for item in rename_keys:
								if item[0] in original_param_names:
												_A					  =			"efficientnet." + item[1]

				_A					  =			"classifier.weight"
				_A					  =			"classifier.bias"
				return key_mapping






def      a__ (				__lowercase     ,			__lowercase     ,			__lowercase   )	->	Tuple:







				for key, value in tf_params.items():
								if "normalization" in key:
												continue

								_A					  =			key_mapping[key]
								if "_conv" in key and "kernel" in key:
												_A					  =			torch.from_numpy(lowercase__   ).permute(3     ,			2     ,			0     ,			1   )
								elif "depthwise_kernel" in key:
												_A					  =			torch.from_numpy(lowercase__   ).permute(2     ,			3     ,			0     ,			1   )
								elif "kernel" in key:
												_A					  =			torch.from_numpy(np.transpose(lowercase__   )   )
								else:
												_A					  =			torch.from_numpy(lowercase__   )

								# Replace HF parameters with original TF model parameters
								assert hf_params[hf_key].shape == new_hf_value.shape
								hf_params[hf_key].copy_(lowercase__   )






@torch.no_grad()
def      a__ (				__lowercase     ,			__lowercase     ,			__lowercase     ,			__lowercase   )	->	str:
				_A					  =			model_classes[model_name](
				    include_top=lowercase__     ,			weights="imagenet"     ,			input_tensor=lowercase__     ,			input_shape=lowercase__     ,			pooling=lowercase__     ,			classes=1000     ,			classifier_activation="softmax"     ,			)

				_A					  =			original_model.trainable_variables
				_A					  =			original_model.non_trainable_variables
				_A					  =			{param.name: param.numpy() for param in tf_params}
				for param in tf_non_train_params:
								_A					  =			param.numpy()
				_A					  =			list(tf_params.keys()   )

				# Load HuggingFace model
				_A					  =			get_efficientnet_config(lowercase__   )
				_A					  =			EfficientNetForImageClassification(lowercase__   ).eval()
				_A					  =			hf_model.state_dict()

				# Create src-to-dst parameter name mapping dictionary
				print("Converting parameters..."   )
				_A					  =			rename_keys(lowercase__   )
				replace_params(lowercase__     ,			lowercase__     ,			lowercase__   )

				# Initialize preprocessor and preprocess input image
				_A					  =			convert_image_processor(lowercase__   )
				_A					  =			preprocessor(images=prepare_img()     ,			return_tensors="pt"   )

				# HF model inference
				hf_model.eval()
				with torch.no_grad():
								_A					  =			hf_model(**lowercase__   )
				_A					  =			outputs.logits.detach().numpy()

				# Original model inference
				_A					  =			False
				_A					  =			CONFIG_MAP[model_name]["image_size"]
				_A					  =			prepare_img().resize((image_size, image_size)     ,			resample=PIL.Image.NEAREST   )
				_A					  =			image.img_to_array(lowercase__   )
				_A					  =			np.expand_dims(lowercase__     ,			axis=0   )
				_A					  =			original_model.predict(lowercase__   )

				# Check whether original and HF model outputs match  -> np.allclose
				assert np.allclose(lowercase__     ,			lowercase__     ,			atol=1E-3   ), "The predicted logits are not the same."
				print("Model outputs match!"   )

				if save_model:
								# Create folder to save model
								if not os.path.isdir(lowercase__   ):
												os.mkdir(lowercase__   )
								# Save converted model and image processor
								hf_model.save_pretrained(lowercase__   )
								preprocessor.save_pretrained(lowercase__   )

				if push_to_hub:
								# Push model and image processor to hub
								print(f"""Pushing converted {model_name} to the hub..."""   )
								_A					  =			f"""efficientnet-{model_name}"""
								preprocessor.push_to_hub(lowercase__   )
								hf_model.push_to_hub(lowercase__   )


if __name__ == "__main__":
		a_       							=    argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    "--model_name",
		    default="b0",
		    type=str,
		    help="Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].",
		)
		parser.add_argument(
		    "--pytorch_dump_folder_path",
		    default="hf_model",
		    type=str,
		    help="Path to the output PyTorch model directory.",
		)
		parser.add_argument("--save_model", action="store_true", help="Save model to local")
		parser.add_argument("--push_to_hub", action="store_true", help="Push model and image processor to the hub")

		a_       							=    parser.parse_args()
		convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)



"""simple docstring"""


import os
import re
import warnings
from shutil import copyfile
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer


if TYPE_CHECKING:
		from ...tokenization_utils_base import TextInput
from ...utils import logging


a_       							=    logging.get_logger(__name__)

a_       							=    {"vocab_file": "spiece.model"}

a_       							=    {
    "vocab_file": {
        "t5-small": "https://huggingface.co/t5-small/resolve/main/spiece.model",
        "t5-base": "https://huggingface.co/t5-base/resolve/main/spiece.model",
        "t5-large": "https://huggingface.co/t5-large/resolve/main/spiece.model",
        "t5-3b": "https://huggingface.co/t5-3b/resolve/main/spiece.model",
        "t5-11b": "https://huggingface.co/t5-11b/resolve/main/spiece.model",
    }
}


# TODO(PVP) - this should be removed in Transformers v5
a_       							=    {
    "t5-small": 5_12,
    "t5-base": 5_12,
    "t5-large": 5_12,
    "t5-3b": 5_12,
    "t5-11b": 5_12,
}

a_       							=    "▁"
class snake_case      (  _UpperCamelCase):
		__UpperCamelCase           =			VOCAB_FILES_NAMES
		__UpperCamelCase           =			PRETRAINED_VOCAB_FILES_MAP
		__UpperCamelCase           =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		__UpperCamelCase           =			['input_ids', 'attention_mask']







		def __init__(					self      :       List[str]    ,   a__      :       Optional[int]    ,   a__      :       Union[str, Any]="</s>"    ,   a__      :       Union[str, Any]="<unk>"    ,   a__      :       str="<pad>"    ,   a__      :       Optional[int]=1_00    ,   a__      :       List[Any]=None    ,   a__      :       Optional[Dict[str, Any]] = None    ,   a__      :       Any=True    ,   **a__      :       Optional[int]    ,   )       ->						None:





						'''simple docstring'''







						if extra_ids > 0 and additional_special_tokens is None:
										_A					  =			[F"""<extra_id_{i}>""" for i in range(a__		)]
						elif extra_ids > 0 and additional_special_tokens is not None:
										# Check that we have the right number of extra_id special tokens
										_A					  =			len(set(filter(lambda a__		: bool("extra_id" in str(a__		)		)    ,   a__		)		)		)
										if extra_tokens != extra_ids:
														raise ValueError(
														    F"""Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are"""
														    " provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids"
														    " tokens"		)
						if legacy:
										logger.warning_once(
										    F"""You are using the legacy behaviour of the {self.__class__}. This means that tokens that come after special tokens will not be properly handled. We recommend you to"""
										    " read the related pull request available at https://github.com/huggingface/transformers/pull/24565"		)

						_A					  =			legacy
						_A					  =			{} if sp_model_kwargs is None else sp_model_kwargs

						super().__init__(
						    eos_token=a__    ,   unk_token=a__    ,   pad_token=a__    ,   extra_ids=a__    ,   additional_special_tokens=a__    ,   sp_model_kwargs=self.sp_model_kwargs    ,   legacy=a__    ,   **a__    ,   )

						_A					  =			vocab_file
						_A					  =			extra_ids

						_A					  =			spm.SentencePieceProcessor(**self.sp_model_kwargs		)
						self.sp_model.Load(a__		)







		@staticmethod
		def  a_ (					a__      :       List[str]    ,   a__      :       Optional[int]    ,   a__      :       Tuple		)       ->						Tuple:





						'''simple docstring'''







						if pretrained_model_name_or_path in TaTokenizer.max_model_input_sizes:
										_A					  =			TaTokenizer.max_model_input_sizes[pretrained_model_name_or_path]
										if init_max_model_length is not None and init_max_model_length != max_model_length:
														return init_max_model_length
										elif init_max_model_length is None:
														warnings.warn(
														    "This tokenizer was incorrectly instantiated with a model max length of"
														    F""" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this"""
														    " behavior is kept to avoid breaking backwards compatibility when padding/encoding with"
														    " `truncation is True`.\n- Be aware that you SHOULD NOT rely on"
														    F""" {pretrained_model_name_or_path} automatically truncating your input to"""
														    F""" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences"""
														    F""" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with"""
														    " `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please"
														    " instantiate this tokenizer with `model_max_length` set to your preferred value."    ,   a__    ,   )

						return max_model_length







		@property
		def  a_ (					self      :       List[Any]		)       ->						Dict:





						'''simple docstring'''







						return self.sp_model.get_piece_size() + self._extra_ids







		def  a_ (					self      :       Dict		)       ->						Optional[Any]:





						'''simple docstring'''







						_A					  =			{self.convert_ids_to_tokens(a__		): i for i in range(self.vocab_size		)}
						vocab.update(self.added_tokens_encoder		)
						return vocab







		def  a_ (					self      :       Optional[Any]    ,   a__      :       List[int]    ,   a__      :       Optional[List[int]] = None    ,   a__      :       bool = False		)       ->						List[int]:





						'''simple docstring'''







						if already_has_special_tokens:
										return super().get_special_tokens_mask(
										    token_ids_a=a__    ,   token_ids_a=a__    ,   already_has_special_tokens=a__		)

						# normal case: some special tokens
						if token_ids_a is None:
										return ([0] * len(a__		)) + [1]
						return ([0] * len(a__		)) + [1] + ([0] * len(a__		)) + [1]







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





						'''simple docstring'''







						return list(
						    set(filter(lambda a__		: bool(re.search(r"<extra_id_\d+>"    ,   a__		)		) is not None    ,   self.additional_special_tokens		)		)		)







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





						'''simple docstring'''







						return [self._convert_token_to_id(a__		) for token in self.get_sentinel_tokens()]







		def  a_ (					self      :       List[Any]    ,   a__      :       List[int]		)       ->						List[int]:





						'''simple docstring'''







						if len(a__		) > 0 and token_ids[-1] == self.eos_token_id:
										warnings.warn(
										    F"""This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated"""
										    " eos tokens being added."		)
										return token_ids
						else:
										return token_ids + [self.eos_token_id]







		def  a_ (					self      :       int    ,   a__      :       List[int]    ,   a__      :       Optional[List[int]] = None		)       ->						List[int]:





						'''simple docstring'''







						_A					  =			[self.eos_token_id]

						if token_ids_a is None:
										return len(token_ids_a + eos		) * [0]
						return len(token_ids_a + eos + token_ids_a + eos		) * [0]







		def  a_ (					self      :       Union[str, Any]    ,   a__      :       List[int]    ,   a__      :       Optional[List[int]] = None		)       ->						List[int]:





						'''simple docstring'''







						_A					  =			self._add_eos_if_not_present(a__		)
						if token_ids_a is None:
										return token_ids_a
						else:
										_A					  =			self._add_eos_if_not_present(a__		)
										return token_ids_a + token_ids_a







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





						'''simple docstring'''







						_A					  =			self.__dict__.copy()
						_A					  =			None
						return state







		def __setstate__(					self      :       int    ,   a__      :       Optional[int]		)       ->						Union[str, Any]:





						'''simple docstring'''







						_A					  =			d

						# for backward compatibility
						if not hasattr(self    ,   "sp_model_kwargs"		):
										_A					  =			{}

						_A					  =			spm.SentencePieceProcessor(**self.sp_model_kwargs		)
						self.sp_model.Load(self.vocab_file		)







		def  a_ (					self      :       int    ,   a__      :       "TextInput"    ,   **a__      :       List[str]		)       ->						List[str]:





						'''simple docstring'''







						if not self.legacy:
										_A					  =			SPIECE_UNDERLINE + text.replace(a__    ,   " "		)
						return super().tokenize(a__    ,   **a__		)







		def  a_ (					self      :       str    ,   a__      :       Dict    ,   **a__      :       Optional[int]		)       ->						Any:





						'''simple docstring'''







						if not self.legacy:
										_A					  =			text.startswith(a__		)
										if is_first:
														_A					  =			text[1:]

						_A					  =			self.sp_model.encode(a__    ,   out_type=a__		)

						if not self.legacy and not is_first and not text.startswith(" "		) and tokens[0].startswith(a__		):
										_A					  =			([tokens[0][1:]] if len(tokens[0]		) > 1 else []) + tokens[1:]
						return tokens







		def  a_ (					self      :       int    ,   a__      :       List[Any]		)       ->						List[str]:





						'''simple docstring'''







						if token.startswith("<extra_id_"		):
										_A					  =			re.match(r"<extra_id_(\d+)>"    ,   a__		)
										_A					  =			int(match.group(1		)		)
										return self.vocab_size - num - 1
						return self.sp_model.piece_to_id(a__		)







		def  a_ (					self      :       Dict    ,   a__      :       Union[str, Any]		)       ->						Any:





						'''simple docstring'''







						if index < self.sp_model.get_piece_size():
										_A					  =			self.sp_model.IdToPiece(a__		)
						else:
										_A					  =			F"""<extra_id_{self.vocab_size - 1 - index}>"""
						return token







		def  a_ (					self      :       Optional[int]    ,   a__      :       Tuple		)       ->						List[str]:





						'''simple docstring'''







						_A					  =			[]
						_A					  =			""
						_A					  =			False
						for token in tokens:
										# make sure that special tokens are not decoded using sentencepiece model
										if token in self.all_special_tokens:
														if not prev_is_special:
																		out_string += " "
														out_string += self.sp_model.decode(a__		) + token
														_A					  =			True
														_A					  =			[]
										else:
														current_sub_tokens.append(a__		)
														_A					  =			False
						out_string += self.sp_model.decode(a__		)
						return out_string.strip()







		def  a_ (					self      :       Dict    ,   a__      :       str    ,   a__      :       Optional[str] = None		)       ->						Tuple[str]:





						'''simple docstring'''







						if not os.path.isdir(a__		):
										logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""		)
										return
						_A					  =			os.path.join(
						    a__    ,   (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]		)

						if os.path.abspath(self.vocab_file		) != os.path.abspath(a__		) and os.path.isfile(self.vocab_file		):
										copyfile(self.vocab_file    ,   a__		)
						elif not os.path.isfile(self.vocab_file		):
										with open(a__    ,   "wb"		) as fi:
														_A					  =			self.sp_model.serialized_model_proto()
														fi.write(a__		)

						return (out_vocab_file,)