"""simple docstring"""






from typing import Optional, Union

import torch
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_mobilenet_va import MobileNetVaConfig


__A       =					logging.get_logger(__name__)


# General docstring
__A       =					"MobileNetV1Config"

# Base docstring
__A       =					"google/mobilenet_v1_1.0_224"
__A       =					[1, 1_0_2_4, 7, 7]

# Image classification docstring
__A       =					"google/mobilenet_v1_1.0_224"
__A       =					"tabby, tabby cat"


__A       =					[
    "google/mobilenet_v1_1.0_224",
    "google/mobilenet_v1_0.75_192",
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
]



def SCREAMING_SNAKE_CASE__     (  __UpperCAmelCase			,       __UpperCAmelCase			,       __UpperCAmelCase=None					)    ->    str:
   lowercase__:  str   = {}

   if isinstance(a_			,       a_					):
      lowercase__:  Optional[int]   = model.mobilenet_va
   else:
      lowercase__:  Any   = model

   lowercase__:  int   = '''MobilenetV1/Conv2d_0/'''
   lowercase__:  Union[str, Any]   = backbone.conv_stem.convolution.weight
   lowercase__:  Optional[Any]   = backbone.conv_stem.normalization.bias
   lowercase__:  Dict   = backbone.conv_stem.normalization.weight
   lowercase__:  List[str]   = backbone.conv_stem.normalization.running_mean
   lowercase__:  List[str]   = backbone.conv_stem.normalization.running_var

   for i in range(1_3					):
      lowercase__:  List[Any]   = i + 1
      lowercase__:  List[str]   = i * 2

      lowercase__:  Optional[Any]   = backbone.layer[pt_index]
      lowercase__:  int   = F"""MobilenetV1/Conv2d_{tf_index}_depthwise/"""
      lowercase__:  Optional[int]   = pointer.convolution.weight
      lowercase__:  Optional[int]   = pointer.normalization.bias
      lowercase__:  Optional[Any]   = pointer.normalization.weight
      lowercase__:  Tuple   = pointer.normalization.running_mean
      lowercase__:  Union[str, Any]   = pointer.normalization.running_var

      lowercase__:  List[str]   = backbone.layer[pt_index + 1]
      lowercase__:  Tuple   = F"""MobilenetV1/Conv2d_{tf_index}_pointwise/"""
      lowercase__:  List[str]   = pointer.convolution.weight
      lowercase__:  Tuple   = pointer.normalization.bias
      lowercase__:  Union[str, Any]   = pointer.normalization.weight
      lowercase__:  Optional[int]   = pointer.normalization.running_mean
      lowercase__:  Dict   = pointer.normalization.running_var

   if isinstance(a_			,       a_					):
      lowercase__:  int   = '''MobilenetV1/Logits/Conv2d_1c_1x1/'''
      lowercase__:  Tuple   = model.classifier.weight
      lowercase__:  Optional[Any]   = model.classifier.bias

   return tf_to_pt_map



def SCREAMING_SNAKE_CASE__     (  __UpperCAmelCase			,       __UpperCAmelCase			,       __UpperCAmelCase					)    ->    Optional[int]:
   try:
      import numpy as np
      import tensorflow as tf
   except ImportError:
      logger.error(
          '''Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see '''
          '''https://www.tensorflow.org/install/ for installation instructions.'''					)
      raise

   # Load weights from TF model
   lowercase__:  int   = tf.train.list_variables(a_					)
   lowercase__:  str   = {}
   for name, shape in init_vars:
      logger.info(F"""Loading TF weight {name} with shape {shape}"""					)
      lowercase__:  Optional[int]   = tf.train.load_variable(a_			,       a_					)
      lowercase__:  Tuple   = array

   # Build TF to PyTorch weights loading map
   lowercase__:  int   = _build_tf_to_pytorch_map(a_			,       a_			,       a_					)

   for name, pointer in tf_to_pt_map.items():
      logger.info(F"""Importing {name}"""					)
      if name not in tf_weights:
         logger.info(F"""{name} not in tf pre-trained weights, skipping"""					)
         continue

      lowercase__:  int   = tf_weights[name]

      if "depthwise_weights" in name:
         logger.info('''Transposing depthwise'''					)
         lowercase__:  List[Any]   = np.transpose(a_			,       (2, 3, 0, 1)					)
      elif "weights" in name:
         logger.info('''Transposing'''					)
         if len(pointer.shape					) == 2:  # copying into linear layer
            lowercase__:  Any   = array.squeeze().transpose()
         else:
            lowercase__:  Union[str, Any]   = np.transpose(a_			,       (3, 2, 0, 1)					)

      if pointer.shape != array.shape:
         raise ValueError(F"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched"""					)

      logger.info(F"""Initialize PyTorch weight {name} {array.shape}"""					)
      lowercase__:  List[str]   = torch.from_numpy(a_					)

      tf_weights.pop(a_			,       a_					)
      tf_weights.pop(name + '''/RMSProp'''			,       a_					)
      tf_weights.pop(name + '''/RMSProp_1'''			,       a_					)
      tf_weights.pop(name + '''/ExponentialMovingAverage'''			,       a_					)

   logger.info(F"""Weights not copied to PyTorch model: {', '.join(tf_weights.keys()					)}"""					)
   return model



def SCREAMING_SNAKE_CASE__     (  __UpperCAmelCase			,       __UpperCAmelCase					)    ->    str:
   lowercase__,					lowercase__:  List[Any]   = features.shape[-2:]
   lowercase__,					lowercase__:  Union[str, Any]   = conv_layer.stride
   lowercase__,					lowercase__:  Optional[Any]   = conv_layer.kernel_size

   if in_height % stride_height == 0:
      lowercase__:  Optional[Any]   = max(kernel_height - stride_height			,       0					)
   else:
      lowercase__:  int   = max(kernel_height - (in_height % stride_height)			,       0					)

   if in_width % stride_width == 0:
      lowercase__:  Tuple   = max(kernel_width - stride_width			,       0					)
   else:
      lowercase__:  Tuple   = max(kernel_width - (in_width % stride_width)			,       0					)

   lowercase__:  Union[str, Any]   = pad_along_width // 2
   lowercase__:  Any   = pad_along_width - pad_left
   lowercase__:  Optional[Any]   = pad_along_height // 2
   lowercase__:  Any   = pad_along_height - pad_top

   lowercase__:  Optional[int]   = (pad_left, pad_right, pad_top, pad_bottom)
   return nn.functional.pad(a_			,       a_			,       '''constant'''			,       0.0					)


class        UpperCAmelCase		(nn.Module    ):



    """simple docstring"""

    def __init__(  self       ,     _UpperCAmelCase       ,     _UpperCAmelCase       ,     _UpperCAmelCase       ,     _UpperCAmelCase       ,     _UpperCAmelCase = 1       ,     _UpperCAmelCase = 1       ,     _UpperCAmelCase = False       ,     _UpperCAmelCase = True       ,     _UpperCAmelCase = True       ,     ):
       super().__init__()
       lowercase__:  Tuple   = config

       if in_channels % groups != 0:
          raise ValueError(F"""Input channels ({in_channels}) are not divisible by {groups} groups."""				)
       if out_channels % groups != 0:
          raise ValueError(F"""Output channels ({out_channels}) are not divisible by {groups} groups."""				)

       lowercase__:  int   = 0 if config.tf_padding else int((kernel_size - 1) / 2				)

       lowercase__:  Any   = nn.Convad(
           in_channels=_UpperCAmelCase       ,     out_channels=_UpperCAmelCase       ,     kernel_size=_UpperCAmelCase       ,     stride=_UpperCAmelCase       ,     padding=_UpperCAmelCase       ,     groups=_UpperCAmelCase       ,     bias=_UpperCAmelCase       ,     padding_mode='''zeros'''       ,     )

       if use_normalization:
          lowercase__:  Tuple   = nn.BatchNormad(
              num_features=_UpperCAmelCase       ,     eps=config.layer_norm_eps       ,     momentum=0.9_997       ,     affine=_UpperCAmelCase       ,     track_running_stats=_UpperCAmelCase       ,     )
       else:
          lowercase__:  str   = None

       if use_activation:
          if isinstance(_UpperCAmelCase       ,     _UpperCAmelCase				):
             lowercase__:  Optional[int]   = ACTaFN[use_activation]
          elif isinstance(config.hidden_act       ,     _UpperCAmelCase				):
             lowercase__:  Any   = ACTaFN[config.hidden_act]
          else:
             lowercase__:  List[str]   = config.hidden_act
       else:
          lowercase__:  Optional[int]   = None






    def _snake_case     (  self       ,     _UpperCAmelCase				):
       if self.config.tf_padding:
          lowercase__:  Optional[int]   = apply_tf_padding(_UpperCAmelCase       ,     self.convolution				)
       lowercase__:  List[str]   = self.convolution(_UpperCAmelCase				)
       if self.normalization is not None:
          lowercase__:  Any   = self.normalization(_UpperCAmelCase				)
       if self.activation is not None:
          lowercase__:  str   = self.activation(_UpperCAmelCase				)
       return features



class        UpperCAmelCase		(snake_case_    ):



    """simple docstring"""

    _UpperCAmelCase      :Dict		       =					MobileNetVaConfig
    _UpperCAmelCase      :Dict		       =					load_tf_weights_in_mobilenet_va
    _UpperCAmelCase      :Any		       =					"mobilenet_v1"
    _UpperCAmelCase      :List[Any]		       =					"pixel_values"
    _UpperCAmelCase      :int		       =					False






    def _snake_case     (  self       ,     _UpperCAmelCase				):
       if isinstance(_UpperCAmelCase       ,     (nn.Linear, nn.Convad)				):
          module.weight.data.normal_(mean=0.0       ,     std=self.config.initializer_range				)
          if module.bias is not None:
             module.bias.data.zero_()
       elif isinstance(_UpperCAmelCase       ,     nn.BatchNormad				):
          module.bias.data.zero_()
          module.weight.data.fill_(1.0				)



__A       =					R"\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n"

__A       =					R"\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`MobileNetV1ImageProcessor.__call__`] for details.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n"


@add_start_docstrings(
    "The bare MobileNetV1 model outputting raw hidden-states without any specific head on top."       ,snake_case_       ,)
class        UpperCAmelCase		(snake_case_    ):



    """simple docstring"""

    def __init__(  self       ,     _UpperCAmelCase       ,     _UpperCAmelCase = True				):
       super().__init__(_UpperCAmelCase				)
       lowercase__:  List[str]   = config

       lowercase__:  Optional[Any]   = 32
       lowercase__:  List[str]   = max(int(depth * config.depth_multiplier				)       ,     config.min_depth				)

       lowercase__:  int   = MobileNetVaConvLayer(
           _UpperCAmelCase       ,     in_channels=config.num_channels       ,     out_channels=_UpperCAmelCase       ,     kernel_size=3       ,     stride=2       ,     )

       lowercase__:  Any   = [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

       lowercase__:  Any   = nn.ModuleList()
       for i in range(13				):
          lowercase__:  Optional[int]   = out_channels

          if strides[i] == 2 or i == 0:
             depth *= 2
             lowercase__:  List[str]   = max(int(depth * config.depth_multiplier				)       ,     config.min_depth				)

          self.layer.append(
              MobileNetVaConvLayer(
                  _UpperCAmelCase       ,     in_channels=_UpperCAmelCase       ,     out_channels=_UpperCAmelCase       ,     kernel_size=3       ,     stride=strides[i]       ,     groups=_UpperCAmelCase       ,     )				)

          self.layer.append(
              MobileNetVaConvLayer(
                  _UpperCAmelCase       ,     in_channels=_UpperCAmelCase       ,     out_channels=_UpperCAmelCase       ,     kernel_size=1       ,     )				)

       lowercase__:  List[Any]   = nn.AdaptiveAvgPoolad((1, 1)				) if add_pooling_layer else None

       # Initialize weights and apply final processing
       self.post_init()






    def _snake_case     (  self       ,     _UpperCAmelCase				):
       raise NotImplementedError






    @add_start_docstrings_to_model_forward(_UpperCAmelCase				)
    @add_code_sample_docstrings(
        checkpoint=_CHECKPOINT_FOR_DOC       ,     output_type=_UpperCAmelCase       ,     config_class=_CONFIG_FOR_DOC       ,     modality='''vision'''       ,     expected_output=_EXPECTED_OUTPUT_SHAPE       ,     )
    def _snake_case     (  self       ,     _UpperCAmelCase = None       ,     _UpperCAmelCase = None       ,     _UpperCAmelCase = None       ,     ):
       lowercase__:  str   = (
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       lowercase__:  Dict   = return_dict if return_dict is not None else self.config.use_return_dict

       if pixel_values is None:
          raise ValueError('''You have to specify pixel_values'''				)

       lowercase__:  Optional[Any]   = self.conv_stem(_UpperCAmelCase				)

       lowercase__:  int   = () if output_hidden_states else None

       for i, layer_module in enumerate(self.layer				):
          lowercase__:  int   = layer_module(_UpperCAmelCase				)

          if output_hidden_states:
             lowercase__:  Dict   = all_hidden_states + (hidden_states,)

       lowercase__:  Optional[Any]   = hidden_states

       if self.pooler is not None:
          lowercase__:  Any   = torch.flatten(self.pooler(_UpperCAmelCase				)       ,     start_dim=1				)
       else:
          lowercase__:  List[str]   = None

       if not return_dict:
          return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None				)

       return BaseModelOutputWithPoolingAndNoAttention(
           last_hidden_state=_UpperCAmelCase       ,     pooler_output=_UpperCAmelCase       ,     hidden_states=_UpperCAmelCase       ,     )



@add_start_docstrings(
    "\n    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "       ,snake_case_       ,)
class        UpperCAmelCase		(snake_case_    ):



    """simple docstring"""

    def __init__(  self       ,     _UpperCAmelCase				):
       super().__init__(_UpperCAmelCase				)

       lowercase__:  int   = config.num_labels
       lowercase__:  int   = MobileNetVaModel(_UpperCAmelCase				)

       lowercase__:  Union[str, Any]   = self.mobilenet_va.layer[-1].convolution.out_channels

       # Classifier head
       lowercase__:  Union[str, Any]   = nn.Dropout(config.classifier_dropout_prob       ,     inplace=_UpperCAmelCase				)
       lowercase__:  List[str]   = nn.Linear(_UpperCAmelCase       ,     config.num_labels				) if config.num_labels > 0 else nn.Identity()

       # Initialize weights and apply final processing
       self.post_init()






    @add_start_docstrings_to_model_forward(_UpperCAmelCase				)
    @add_code_sample_docstrings(
        checkpoint=_IMAGE_CLASS_CHECKPOINT       ,     output_type=_UpperCAmelCase       ,     config_class=_CONFIG_FOR_DOC       ,     expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT       ,     )
    def _snake_case     (  self       ,     _UpperCAmelCase = None       ,     _UpperCAmelCase = None       ,     _UpperCAmelCase = None       ,     _UpperCAmelCase = None       ,     ):
       lowercase__:  List[Any]   = return_dict if return_dict is not None else self.config.use_return_dict

       lowercase__:  Tuple   = self.mobilenet_va(_UpperCAmelCase       ,     output_hidden_states=_UpperCAmelCase       ,     return_dict=_UpperCAmelCase				)

       lowercase__:  List[Any]   = outputs.pooler_output if return_dict else outputs[1]

       lowercase__:  Union[str, Any]   = self.classifier(self.dropout(_UpperCAmelCase				)				)

       lowercase__:  List[Any]   = None
       if labels is not None:
          if self.config.problem_type is None:
             if self.num_labels == 1:
                lowercase__:  List[Any]   = '''regression'''
             elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                lowercase__:  Dict   = '''single_label_classification'''
             else:
                lowercase__:  Dict   = '''multi_label_classification'''

          if self.config.problem_type == "regression":
             lowercase__:  Dict   = MSELoss()
             if self.num_labels == 1:
                lowercase__:  List[Any]   = loss_fct(logits.squeeze()       ,     labels.squeeze()				)
             else:
                lowercase__:  str   = loss_fct(_UpperCAmelCase       ,     _UpperCAmelCase				)
          elif self.config.problem_type == "single_label_classification":
             lowercase__:  List[str]   = CrossEntropyLoss()
             lowercase__:  List[str]   = loss_fct(logits.view(-1       ,     self.num_labels				)       ,     labels.view(-1				)				)
          elif self.config.problem_type == "multi_label_classification":
             lowercase__:  Dict   = BCEWithLogitsLoss()
             lowercase__:  Union[str, Any]   = loss_fct(_UpperCAmelCase       ,     _UpperCAmelCase				)

       if not return_dict:
          lowercase__:  Dict   = (logits,) + outputs[2:]
          return ((loss,) + output) if loss is not None else output

       return ImageClassifierOutputWithNoAttention(
           loss=_UpperCAmelCase       ,     logits=_UpperCAmelCase       ,     hidden_states=outputs.hidden_states       ,     )








import copy
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Optional, Union




@dataclass
class 					UpperCamelCase_ :



	'''simple docstring'''




	lowerCAmelCase						=					None
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False
	lowerCAmelCase						=					None
	lowerCAmelCase						=					None
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False
	lowerCAmelCase						=					False
	lowerCAmelCase						=					True
	lowerCAmelCase						=					None
	lowerCAmelCase						=					1
	lowerCAmelCase						=					None
	lowerCAmelCase						=					False
	lowerCAmelCase						=					None
	lowerCAmelCase						=					None




	def 				_UpperCamelCase     (			self   )			->       "DownloadConfig":
								return self.__class__(**{k: copy.deepcopy(a   ) for k, v in self.__dict__.items()}   )





