

"""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, 10_24, 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   _A  (  UpperCamelCase_					:	Union[str, Any],      UpperCamelCase_					:	int,      UpperCamelCase_					:	Optional[int]=None)     ->						int:
   '''simple docstring'''


   __lowercase								=						{}

   if isinstance(UpperCamelCase_,      UpperCamelCase_):
      __lowercase								=						model.mobilenet_va
   else:
      __lowercase								=						model

   __lowercase								=						"MobilenetV1/Conv2d_0/"
   __lowercase								=						backbone.conv_stem.convolution.weight
   __lowercase								=						backbone.conv_stem.normalization.bias
   __lowercase								=						backbone.conv_stem.normalization.weight
   __lowercase								=						backbone.conv_stem.normalization.running_mean
   __lowercase								=						backbone.conv_stem.normalization.running_var

   for i in range(13):
      __lowercase								=						i + 1
      __lowercase								=						i * 2

      __lowercase								=						backbone.layer[pt_index]
      __lowercase								=						F"""MobilenetV1/Conv2d_{tf_index}_depthwise/"""
      __lowercase								=						pointer.convolution.weight
      __lowercase								=						pointer.normalization.bias
      __lowercase								=						pointer.normalization.weight
      __lowercase								=						pointer.normalization.running_mean
      __lowercase								=						pointer.normalization.running_var

      __lowercase								=						backbone.layer[pt_index + 1]
      __lowercase								=						F"""MobilenetV1/Conv2d_{tf_index}_pointwise/"""
      __lowercase								=						pointer.convolution.weight
      __lowercase								=						pointer.normalization.bias
      __lowercase								=						pointer.normalization.weight
      __lowercase								=						pointer.normalization.running_mean
      __lowercase								=						pointer.normalization.running_var

   if isinstance(UpperCamelCase_,      UpperCamelCase_):
      __lowercase								=						"MobilenetV1/Logits/Conv2d_1c_1x1/"
      __lowercase								=						model.classifier.weight
      __lowercase								=						model.classifier.bias

   return tf_to_pt_map




def   _A  (  UpperCamelCase_					:	int,      UpperCamelCase_					:	int,      UpperCamelCase_					:	List[str])     ->						List[str]:
   '''simple docstring'''


   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								=						tf.train.list_variables(UpperCamelCase_)
   __lowercase								=						{}
   for name, shape in init_vars:
      logger.info(F"""Loading TF weight {name} with shape {shape}""")
      __lowercase								=						tf.train.load_variable(UpperCamelCase_,      UpperCamelCase_)
      __lowercase								=						array

   # Build TF to PyTorch weights loading map
   __lowercase								=						_build_tf_to_pytorch_map(UpperCamelCase_,      UpperCamelCase_,      UpperCamelCase_)

   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								=						tf_weights[name]

      if "depthwise_weights" in name:
         logger.info("Transposing depthwise")
         __lowercase								=						np.transpose(UpperCamelCase_,      (2, 3, 0, 1))
      elif "weights" in name:
         logger.info("Transposing")
         if len(pointer.shape) == 2:  # copying into linear layer
            __lowercase								=						array.squeeze().transpose()
         else:
            __lowercase								=						np.transpose(UpperCamelCase_,      (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								=						torch.from_numpy(UpperCamelCase_)

      tf_weights.pop(UpperCamelCase_,      UpperCamelCase_)
      tf_weights.pop(name + "/RMSProp",      UpperCamelCase_)
      tf_weights.pop(name + "/RMSProp_1",      UpperCamelCase_)
      tf_weights.pop(name + "/ExponentialMovingAverage",      UpperCamelCase_)

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




def   _A  (  UpperCamelCase_					:	torch.Tensor,      UpperCamelCase_					:	nn.Convad)     ->						torch.Tensor:
   '''simple docstring'''


   __lowercase		,__lowercase								=						features.shape[-2:]
   __lowercase		,__lowercase								=						conv_layer.stride
   __lowercase		,__lowercase								=						conv_layer.kernel_size

   if in_height % stride_height == 0:
      __lowercase								=						max(kernel_height - stride_height,      0)
   else:
      __lowercase								=						max(kernel_height - (in_height % stride_height),      0)

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

   __lowercase								=						pad_along_width // 2
   __lowercase								=						pad_along_width - pad_left
   __lowercase								=						pad_along_height // 2
   __lowercase								=						pad_along_height - pad_top

   __lowercase								=						(pad_left, pad_right, pad_top, pad_bottom)
   return nn.functional.pad(UpperCamelCase_,      UpperCamelCase_,      "constant",      0.0)




class  _lowerCAmelCase  (     nn.Module       ):







 """simple docstring"""







 def __init__(      self							:      Optional[Any], UpperCAmelCase__							:      MobileNetVaConfig, UpperCAmelCase__							:      int, UpperCAmelCase__							:      int, UpperCAmelCase__							:      int, UpperCAmelCase__							:      Optional[int] = 1, UpperCAmelCase__							:      Optional[int] = 1, UpperCAmelCase__							:      bool = False, UpperCAmelCase__							:      Optional[bool] = True, UpperCAmelCase__							:      Optional[bool or str] = True, ):
    super().__init__()
    __lowercase								=						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								=						0 if config.tf_padding else int((kernel_size - 1) / 2     )

    __lowercase								=						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								=						nn.BatchNormad(
           num_features=UpperCAmelCase__, eps=config.layer_norm_eps, momentum=0.9_997, affine=UpperCAmelCase__, track_running_stats=UpperCAmelCase__, )
    else:
       __lowercase								=						None

    if use_activation:
       if isinstance(UpperCAmelCase__, UpperCAmelCase__     ):
          __lowercase								=						ACTaFN[use_activation]
       elif isinstance(config.hidden_act, UpperCAmelCase__     ):
          __lowercase								=						ACTaFN[config.hidden_act]
       else:
          __lowercase								=						config.hidden_act
    else:
       __lowercase								=						None



 def  _lowercase			(      self							:      Any, UpperCAmelCase__							:      torch.Tensor     ):
    if self.config.tf_padding:
       __lowercase								=						apply_tf_padding(UpperCAmelCase__, self.convolution     )
    __lowercase								=						self.convolution(UpperCAmelCase__     )
    if self.normalization is not None:
       __lowercase								=						self.normalization(UpperCAmelCase__     )
    if self.activation is not None:
       __lowercase								=						self.activation(UpperCAmelCase__     )
    return features







class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 __UpperCAmelCase     : Optional[Any]       		=     MobileNetVaConfig
 __UpperCAmelCase     : Optional[Any]       		=     load_tf_weights_in_mobilenet_va
 __UpperCAmelCase     : Tuple       		=     "mobilenet_v1"
 __UpperCAmelCase     : str       		=     "pixel_values"
 __UpperCAmelCase     : Dict       		=     False


 def  _lowercase			(      self							:      Optional[int], UpperCAmelCase__							:      Union[nn.Linear, nn.Convad]     ):
    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."					,lowercase					,)
class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 def __init__(      self							:      Any, UpperCAmelCase__							:      MobileNetVaConfig, UpperCAmelCase__							:      bool = True     ):
    super().__init__(UpperCAmelCase__     )
    __lowercase								=						config

    __lowercase								=						3_2
    __lowercase								=						max(int(depth * config.depth_multiplier     ), config.min_depth     )

    __lowercase								=						MobileNetVaConvLayer(
        UpperCAmelCase__, in_channels=config.num_channels, out_channels=UpperCAmelCase__, kernel_size=3, stride=2, )

    __lowercase								=						[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

    __lowercase								=						nn.ModuleList()
    for i in range(1_3     ):
       __lowercase								=						out_channels

       if strides[i] == 2 or i == 0:
          depth *= 2
          __lowercase								=						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								=						nn.AdaptiveAvgPoolad((1, 1)     ) if add_pooling_layer else None

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


 def  _lowercase			(      self							:      int, UpperCAmelCase__							:      str     ):
    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  _lowercase			(      self							:      Optional[int], UpperCAmelCase__							:      Optional[torch.Tensor] = None, UpperCAmelCase__							:      Optional[bool] = None, UpperCAmelCase__							:      Optional[bool] = None, ):
    __lowercase								=						(
        output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    )
    __lowercase								=						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								=						self.conv_stem(UpperCAmelCase__     )

    __lowercase								=						() if output_hidden_states else None

    for i, layer_module in enumerate(self.layer     ):
       __lowercase								=						layer_module(UpperCAmelCase__     )

       if output_hidden_states:
          __lowercase								=						all_hidden_states + (hidden_states,)

    __lowercase								=						hidden_states

    if self.pooler is not None:
       __lowercase								=						torch.flatten(self.pooler(UpperCAmelCase__     ), start_dim=1     )
    else:
       __lowercase								=						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    "					,lowercase					,)
class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 def __init__(      self							:      Union[str, Any], UpperCAmelCase__							:      MobileNetVaConfig     ):
    super().__init__(UpperCAmelCase__     )

    __lowercase								=						config.num_labels
    __lowercase								=						MobileNetVaModel(UpperCAmelCase__     )

    __lowercase								=						self.mobilenet_va.layer[-1].convolution.out_channels

    # Classifier head
    __lowercase								=						nn.Dropout(config.classifier_dropout_prob, inplace=UpperCAmelCase__     )
    __lowercase								=						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  _lowercase			(      self							:      int, UpperCAmelCase__							:      Optional[torch.Tensor] = None, UpperCAmelCase__							:      Optional[bool] = None, UpperCAmelCase__							:      Optional[torch.Tensor] = None, UpperCAmelCase__							:      Optional[bool] = None, ):
    __lowercase								=						return_dict if return_dict is not None else self.config.use_return_dict

    __lowercase								=						self.mobilenet_va(UpperCAmelCase__, output_hidden_states=UpperCAmelCase__, return_dict=UpperCAmelCase__     )

    __lowercase								=						outputs.pooler_output if return_dict else outputs[1]

    __lowercase								=						self.classifier(self.dropout(UpperCAmelCase__     )     )

    __lowercase								=						None
    if labels is not None:
       if self.config.problem_type is None:
          if self.num_labels == 1:
             __lowercase								=						"regression"
          elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
             __lowercase								=						"single_label_classification"
          else:
             __lowercase								=						"multi_label_classification"

       if self.config.problem_type == "regression":
          __lowercase								=						MSELoss()
          if self.num_labels == 1:
             __lowercase								=						loss_fct(logits.squeeze(), labels.squeeze()     )
          else:
             __lowercase								=						loss_fct(UpperCAmelCase__, UpperCAmelCase__     )
       elif self.config.problem_type == "single_label_classification":
          __lowercase								=						CrossEntropyLoss()
          __lowercase								=						loss_fct(logits.view(-1, self.num_labels     ), labels.view(-1     )     )
       elif self.config.problem_type == "multi_label_classification":
          __lowercase								=						BCEWithLogitsLoss()
          __lowercase								=						loss_fct(UpperCAmelCase__, UpperCAmelCase__     )

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

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







"""simple docstring"""





import numpy

# List of input, output pairs
_a							=							(
    ((5, 2, 3), 15),
    ((6, 5, 9), 25),
    ((11, 12, 13), 41),
    ((1, 1, 1), 8),
    ((11, 12, 13), 41),
)
_a							=							(((5_15, 22, 13), 5_55), ((61, 35, 49), 1_50))
_a							=							[2, 4, 1, 5]
_a							=							len(train_data)
_a							=							0.009




def   _A  (  UpperCamelCase_					:	str,      UpperCamelCase_					:	List[Any]="train")     ->						Optional[Any]:
   '''simple docstring'''


   return calculate_hypothesis_value(UpperCamelCase_,      UpperCamelCase_) - output(
       UpperCamelCase_,      UpperCamelCase_)




def   _A  (  UpperCamelCase_					:	List[Any])     ->						Union[str, Any]:
   '''simple docstring'''


   __lowercase								=						0
   for i in range(len(UpperCamelCase_) - 1):
      hyp_val += data_input_tuple[i] * parameter_vector[i + 1]
   hyp_val += parameter_vector[0]
   return hyp_val




def   _A  (  UpperCamelCase_					:	Dict,      UpperCamelCase_					:	Optional[int])     ->						Dict:
   '''simple docstring'''


   if data_set == "train":
      return train_data[example_no][1]
   elif data_set == "test":
      return test_data[example_no][1]
   return None




def   _A  (  UpperCamelCase_					:	Dict,      UpperCamelCase_					:	List[str])     ->						int:
   '''simple docstring'''


   if data_set == "train":
      return _hypothesis_value(train_data[example_no][0])
   elif data_set == "test":
      return _hypothesis_value(test_data[example_no][0])
   return None




def   _A  (  UpperCamelCase_					:	Any,      UpperCamelCase_					:	Tuple=m)     ->						int:
   '''simple docstring'''


   __lowercase								=						0
   for i in range(UpperCamelCase_):
      if index == -1:
         summation_value += _error(UpperCamelCase_)
      else:
         summation_value += _error(UpperCamelCase_) * train_data[i][0][index]
   return summation_value




def   _A  (  UpperCamelCase_					:	str)     ->						str:
   '''simple docstring'''


   __lowercase								=						summation_of_cost_derivative(UpperCamelCase_,      UpperCamelCase_) / m
   return cost_derivative_value




def   _A  (  )     ->						List[str]:
   '''simple docstring'''


   global parameter_vector
   # Tune these values to set a tolerance value for predicted output
   __lowercase								=						0.000_002
   __lowercase								=						0
   __lowercase								=						0
   while True:
      j += 1
      __lowercase								=						[0, 0, 0, 0]
      for i in range(0,      len(UpperCamelCase_)):
         __lowercase								=						get_cost_derivative(i - 1)
         __lowercase								=						(
             parameter_vector[i] - LEARNING_RATE * cost_derivative
         )
      if numpy.allclose(
          UpperCamelCase_,      UpperCamelCase_,      atol=UpperCamelCase_,      rtol=UpperCamelCase_,      ):
         break
      __lowercase								=						temp_parameter_vector
   print(("Number of iterations:", j))




def   _A  (  )     ->						int:
   '''simple docstring'''


   for i in range(len(UpperCamelCase_)):
      print(("Actual output value:", output(UpperCamelCase_,      "test")))
      print(("Hypothesis output:", calculate_hypothesis_value(UpperCamelCase_,      "test")))


if __name__ == "__main__":
   run_gradient_descent()
   print('\nTesting gradient descent for a linear hypothesis function.\n')
   test_gradient_descent()




