




"""simple docstring"""






import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase__						=			logging.get_logger(__name__)

UpperCamelCase__						=			{
    '''microsoft/unispeech-sat-base-100h-libri-ft''': (
        '''https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'''
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}




class   a__			(       UpperCamelCase_  ):
						snake_case__             = '''unispeech-sat'''





						def __init__(							self      :	Dict     ,a__      :	Union[str, Any]=32     ,a__      :	Optional[Any]=768     ,a__      :	Tuple=12     ,a__      :	Optional[int]=12     ,a__      :	Optional[Any]=3072     ,a__      :	List[str]="gelu"     ,a__      :	List[Any]=0.1     ,a__      :	int=0.1     ,a__      :	int=0.1     ,a__      :	Optional[Any]=0.0     ,a__      :	Dict=0.0     ,a__      :	Any=0.1     ,a__      :	Optional[int]=0.1     ,a__      :	int=0.02     ,a__      :	int=1E-5     ,a__      :	List[str]="group"     ,a__      :	Union[str, Any]="gelu"     ,a__      :	List[Any]=(512, 512, 512, 512, 512, 512, 512)     ,a__      :	int=(5, 2, 2, 2, 2, 2, 2)     ,a__      :	List[Any]=(10, 3, 3, 3, 3, 2, 2)     ,a__      :	Optional[int]=False     ,a__      :	Tuple=128     ,a__      :	Any=16     ,a__      :	Optional[int]=False     ,a__      :	str=True     ,a__      :	Optional[Any]=0.05     ,a__      :	Optional[int]=10     ,a__      :	int=2     ,a__      :	List[Any]=0.0     ,a__      :	int=10     ,a__      :	str=0     ,a__      :	Optional[int]=320     ,a__      :	Optional[int]=2     ,a__      :	Tuple=0.1     ,a__      :	List[str]=100     ,a__      :	Union[str, Any]=256     ,a__      :	List[str]=256     ,a__      :	List[Any]=0.1     ,a__      :	Any="mean"     ,a__      :	Optional[int]=False     ,a__      :	List[Any]=False     ,a__      :	Union[str, Any]=256     ,a__      :	Tuple=(512, 512, 512, 512, 1500)     ,a__      :	Union[str, Any]=(5, 3, 3, 1, 1)     ,a__      :	str=(1, 2, 3, 1, 1)     ,a__      :	Optional[Any]=512     ,a__      :	Optional[int]=0     ,a__      :	str=1     ,a__      :	List[Any]=2     ,a__      :	str=504     ,**a__      :	Dict     ,)						->       Tuple:




											"""simple docstring"""







											super().__init__(**a__     ,pad_token_id=a__     ,bos_token_id=a__     ,eos_token_id=a__)
											_lowerCAmelCase:str									=  hidden_size
											_lowerCAmelCase:Optional[int]									=  feat_extract_norm
											_lowerCAmelCase:Union[str, Any]									=  feat_extract_activation
											_lowerCAmelCase:int									=  list(a__)
											_lowerCAmelCase:Union[str, Any]									=  list(a__)
											_lowerCAmelCase:Dict									=  list(a__)
											_lowerCAmelCase:Union[str, Any]									=  conv_bias
											_lowerCAmelCase:Dict									=  num_conv_pos_embeddings
											_lowerCAmelCase:List[Any]									=  num_conv_pos_embedding_groups
											_lowerCAmelCase:Any									=  len(self.conv_dim)
											_lowerCAmelCase:Union[str, Any]									=  num_hidden_layers
											_lowerCAmelCase:Optional[Any]									=  intermediate_size
											_lowerCAmelCase:Optional[int]									=  hidden_act
											_lowerCAmelCase:Any									=  num_attention_heads
											_lowerCAmelCase:Union[str, Any]									=  hidden_dropout
											_lowerCAmelCase:Optional[Any]									=  attention_dropout
											_lowerCAmelCase:Optional[Any]									=  activation_dropout
											_lowerCAmelCase:str									=  feat_proj_dropout
											_lowerCAmelCase:Optional[Any]									=  final_dropout
											_lowerCAmelCase:List[str]									=  layerdrop
											_lowerCAmelCase:List[str]									=  layer_norm_eps
											_lowerCAmelCase:Any									=  initializer_range
											_lowerCAmelCase:Tuple									=  vocab_size
											_lowerCAmelCase:Tuple									=  num_clusters
											_lowerCAmelCase:List[str]									=  do_stable_layer_norm
											_lowerCAmelCase:Union[str, Any]									=  use_weighted_layer_sum

											if (
											    (len(self.conv_stride) != self.num_feat_extract_layers)
											    or (len(self.conv_kernel) != self.num_feat_extract_layers)
											    or (len(self.conv_dim) != self.num_feat_extract_layers)
											):
																raise ValueError(
																    '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='''
																    ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='''
																    F' {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,'
																    F' `len(config.conv_kernel) = {len(self.conv_kernel)}`.')

											# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
											_lowerCAmelCase:Any									=  apply_spec_augment
											_lowerCAmelCase:Union[str, Any]									=  mask_time_prob
											_lowerCAmelCase:Optional[int]									=  mask_time_length
											_lowerCAmelCase:Union[str, Any]									=  mask_time_min_masks
											_lowerCAmelCase:List[str]									=  mask_feature_prob
											_lowerCAmelCase:Tuple									=  mask_feature_length
											_lowerCAmelCase:Optional[Any]									=  mask_feature_min_masks

											# parameters for pretraining with codevector quantized representations
											_lowerCAmelCase:Any									=  num_codevectors_per_group
											_lowerCAmelCase:Tuple									=  num_codevector_groups
											_lowerCAmelCase:Any									=  contrastive_logits_temperature
											_lowerCAmelCase:Union[str, Any]									=  feat_quantizer_dropout
											_lowerCAmelCase:Dict									=  num_negatives
											_lowerCAmelCase:str									=  codevector_dim
											_lowerCAmelCase:Optional[int]									=  proj_codevector_dim
											_lowerCAmelCase:Union[str, Any]									=  diversity_loss_weight

											# ctc loss
											_lowerCAmelCase:Union[str, Any]									=  ctc_loss_reduction
											_lowerCAmelCase:Dict									=  ctc_zero_infinity

											# SequenceClassification-specific parameter. Feel free to ignore for other classes.
											_lowerCAmelCase:List[Any]									=  classifier_proj_size

											# XVector-specific parameters. Feel free to ignore for other classes.
											_lowerCAmelCase:Union[str, Any]									=  list(a__)
											_lowerCAmelCase:Optional[int]									=  list(a__)
											_lowerCAmelCase:Any									=  list(a__)
											_lowerCAmelCase:int									=  xvector_output_dim







						@property
						def       __UpperCamelCase     (							self      :	int)						->       Optional[int]:




											"""simple docstring"""







											return functools.reduce(operator.mul     ,self.conv_stride     ,1)












"""simple docstring"""






import argparse
import json
import re
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    MobileNetVaConfig,
    MobileNetVaForImageClassification,
    MobileNetVaImageProcessor,
    load_tf_weights_in_mobilenet_va,
)
from transformers.utils import logging


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





def 							UpperCAmelCase		(		snake_case			:					Optional[int]    ):
					_lowerCAmelCase:List[Any]									=  MobileNetVaConfig(layer_norm_eps=0.0_01    )

					if "_quant" in model_name:
										raise ValueError('''Quantized models are not supported.'''    )

					_lowerCAmelCase:List[Any]									=  re.match(R'''^mobilenet_v1_([^_]*)_([^_]*)$'''      ,       snake_case    )
					if matches:
										_lowerCAmelCase:str									=  float(matches[1]    )
										_lowerCAmelCase:str									=  int(matches[2]    )

					# The TensorFlow version of MobileNetV1 predicts 1001 classes instead of
					# the usual 1000. The first class (index 0) is "background".
					_lowerCAmelCase:int									=  1001
					_lowerCAmelCase:Union[str, Any]									=  '''imagenet-1k-id2label.json'''
					_lowerCAmelCase:Optional[int]									=  '''huggingface/label-files'''
					_lowerCAmelCase:List[str]									=  json.load(open(hf_hub_download(snake_case      ,       snake_case      ,       repo_type='''dataset'''    )      ,       '''r'''    )    )
					_lowerCAmelCase:Union[str, Any]									=  {int(snake_case    ) + 1: v for k, v in idalabel.items()}
					_lowerCAmelCase:Optional[Any]									=  '''background'''
					_lowerCAmelCase:Optional[Any]									=  idalabel
					_lowerCAmelCase:List[Any]									=  {v: k for k, v in idalabel.items()}

					return config





def 							UpperCAmelCase		(		):
					_lowerCAmelCase:Tuple									=  '''http://images.cocodataset.org/val2017/000000039769.jpg'''
					_lowerCAmelCase:Optional[Any]									=  Image.open(requests.get(snake_case      ,       stream=snake_case    ).raw    )
					return im





@torch.no_grad()
def 							UpperCAmelCase		(		snake_case			:					Any      ,       snake_case			:					Union[str, Any]      ,       snake_case			:					List[Any]      ,       snake_case			:					Any=False    ):
					_lowerCAmelCase:List[str]									=  get_mobilenet_va_config(snake_case    )

					# Load 🤗 model
					_lowerCAmelCase:Any									=  MobileNetVaForImageClassification(snake_case    ).eval()

					# Load weights from TensorFlow checkpoint
					load_tf_weights_in_mobilenet_va(snake_case      ,       snake_case      ,       snake_case    )

					# Check outputs on an image, prepared by MobileNetV1ImageProcessor
					_lowerCAmelCase:Dict									=  MobileNetVaImageProcessor(
					    crop_size={'''width''': config.image_size, '''height''': config.image_size}      ,       size={'''shortest_edge''': config.image_size + 32}      ,       )
					_lowerCAmelCase:List[str]									=  image_processor(images=prepare_img()      ,       return_tensors='''pt'''    )
					_lowerCAmelCase:List[str]									=  model(**snake_case    )
					_lowerCAmelCase:int									=  outputs.logits

					assert logits.shape == (1, 1001)

					if model_name == "mobilenet_v1_1.0_224":
										_lowerCAmelCase:Union[str, Any]									=  torch.tensor([-4.17_39, -1.12_33, 3.12_05]    )
					elif model_name == "mobilenet_v1_0.75_192":
										_lowerCAmelCase:Any									=  torch.tensor([-3.94_40, -2.31_41, -0.33_33]    )
					else:
										_lowerCAmelCase:Dict									=  None

					if expected_logits is not None:
										assert torch.allclose(logits[0, :3]      ,       snake_case      ,       atol=1e-4    )

					Path(snake_case    ).mkdir(exist_ok=snake_case    )
					print(F'Saving model {model_name} to {pytorch_dump_folder_path}'    )
					model.save_pretrained(snake_case    )
					print(F'Saving image processor to {pytorch_dump_folder_path}'    )
					image_processor.save_pretrained(snake_case    )

					if push_to_hub:
										print('''Pushing to the hub...'''    )
										_lowerCAmelCase:List[str]									=  '''google/''' + model_name
										image_processor.push_to_hub(snake_case    )
										model.push_to_hub(snake_case    )


if __name__ == "__main__":
				UpperCamelCase__						=			argparse.ArgumentParser()
				# Required parameters
				parser.add_argument(
				    '''--model_name''',
				    default='''mobilenet_v1_1.0_224''',
				    type=str,
				    help='''Name of the MobileNetV1 model you\'d like to convert. Should in the form \'mobilenet_v1_<depth>_<size>\'.''',
				)
				parser.add_argument(
				    '''--checkpoint_path''', required=True, type=str, help='''Path to the original TensorFlow checkpoint (.ckpt file).'''
				)
				parser.add_argument(
				    '''--pytorch_dump_folder_path''', required=True, type=str, help='''Path to the output PyTorch model directory.'''
				)
				parser.add_argument(
				    '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.'''
				)

				UpperCamelCase__						=			parser.parse_args()
				convert_movilevit_checkpoint(
				    args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub
				)






