

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


_SCREAMING_SNAKE_CASE					:  List[str]             =      logging.get_logger(__name__)


# General docstring
_SCREAMING_SNAKE_CASE					:  Dict             =      '''MobileNetV1Config'''

# Base docstring
_SCREAMING_SNAKE_CASE					:  Optional[int]             =      '''google/mobilenet_v1_1.0_224'''
_SCREAMING_SNAKE_CASE					:  Tuple             =      [1, 1024, 7, 7]

# Image classification docstring
_SCREAMING_SNAKE_CASE					:  Optional[Any]             =      '''google/mobilenet_v1_1.0_224'''
_SCREAMING_SNAKE_CASE					:  Dict             =      '''tabby, tabby cat'''


_SCREAMING_SNAKE_CASE					:  Any             =      [
    '''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     UpperCAmelCase_       (						_A				,		_A				,		_A=None    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   {}

     if isinstance(_A				,		_A    ):
          SCREAMING_SNAKE_CASE__   =   model.mobilenet_va
     else:
          SCREAMING_SNAKE_CASE__   =   model

     SCREAMING_SNAKE_CASE__   =   '''MobilenetV1/Conv2d_0/'''
     SCREAMING_SNAKE_CASE__   =   backbone.conv_stem.convolution.weight
     SCREAMING_SNAKE_CASE__   =   backbone.conv_stem.normalization.bias
     SCREAMING_SNAKE_CASE__   =   backbone.conv_stem.normalization.weight
     SCREAMING_SNAKE_CASE__   =   backbone.conv_stem.normalization.running_mean
     SCREAMING_SNAKE_CASE__   =   backbone.conv_stem.normalization.running_var

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

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

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

     if isinstance(_A				,		_A    ):
          SCREAMING_SNAKE_CASE__   =   '''MobilenetV1/Logits/Conv2d_1c_1x1/'''
          SCREAMING_SNAKE_CASE__   =   model.classifier.weight
          SCREAMING_SNAKE_CASE__   =   model.classifier.bias

     return tf_to_pt_map
def     UpperCAmelCase_       (						_A				,		_A				,		_A    ):





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

     # Build TF to PyTorch weights loading map
     SCREAMING_SNAKE_CASE__   =   _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

          SCREAMING_SNAKE_CASE__   =   tf_weights[name]

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





     '''simple docstring'''




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

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

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

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

     SCREAMING_SNAKE_CASE__   =   (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						:		List[Any]     ,       __lowerCamelCase						:		MobileNetVaConfig     ,       __lowerCamelCase						:		int     ,       __lowerCamelCase						:		int     ,       __lowerCamelCase						:		int     ,       __lowerCamelCase						:		Optional[int] = 1     ,       __lowerCamelCase						:		Optional[int] = 1     ,       __lowerCamelCase						:		bool = False     ,       __lowerCamelCase						:		Optional[bool] = True     ,       __lowerCamelCase						:		Optional[bool or str] = True     ,       )		->							None:
       super().__init__()
       SCREAMING_SNAKE_CASE__   =   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.''' )

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

       SCREAMING_SNAKE_CASE__   =   nn.Convad(
           in_channels=__lowerCamelCase     ,       out_channels=__lowerCamelCase     ,       kernel_size=__lowerCamelCase     ,       stride=__lowerCamelCase     ,       padding=__lowerCamelCase     ,       groups=__lowerCamelCase     ,       bias=__lowerCamelCase     ,       padding_mode='''zeros'''     ,       )

       if use_normalization:
            SCREAMING_SNAKE_CASE__   =   nn.BatchNormad(
                num_features=__lowerCamelCase     ,       eps=config.layer_norm_eps     ,       momentum=0.9997     ,       affine=__lowerCamelCase     ,       track_running_stats=__lowerCamelCase     ,       )
       else:
            SCREAMING_SNAKE_CASE__   =   None

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







  def 				lowercase_		(							self						:		int     ,       __lowerCamelCase						:		torch.Tensor )		->							torch.Tensor:
       if self.config.tf_padding:
            SCREAMING_SNAKE_CASE__   =   apply_tf_padding(__lowerCamelCase     ,       self.convolution )
       SCREAMING_SNAKE_CASE__   =   self.convolution(__lowerCamelCase )
       if self.normalization is not None:
            SCREAMING_SNAKE_CASE__   =   self.normalization(__lowerCamelCase )
       if self.activation is not None:
            SCREAMING_SNAKE_CASE__   =   self.activation(__lowerCamelCase )
       return features






class 	UpperCAmelCase__		(	A__	):






  """simple docstring"""


  a									= MobileNetVaConfig
  a									= load_tf_weights_in_mobilenet_va
  a									= "mobilenet_v1"
  a									= "pixel_values"
  a									= False






  def 				lowercase_		(							self						:		Any     ,       __lowerCamelCase						:		Union[nn.Linear, nn.Convad] )		->							None:
       if isinstance(__lowerCamelCase     ,       (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(__lowerCamelCase     ,       nn.BatchNormad ):
            module.bias.data.zero_()
            module.weight.data.fill_(1.0 )



_SCREAMING_SNAKE_CASE					:  Any             =      r'''
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
'''

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






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






  """simple docstring"""


  def __init__(							self						:		List[str]     ,       __lowerCamelCase						:		MobileNetVaConfig     ,       __lowerCamelCase						:		bool = True )		->							List[Any]:
       super().__init__(__lowerCamelCase )
       SCREAMING_SNAKE_CASE__   =   config

       SCREAMING_SNAKE_CASE__   =   32
       SCREAMING_SNAKE_CASE__   =   max(int(depth * config.depth_multiplier )     ,       config.min_depth )

       SCREAMING_SNAKE_CASE__   =   MobileNetVaConvLayer(
           __lowerCamelCase     ,       in_channels=config.num_channels     ,       out_channels=__lowerCamelCase     ,       kernel_size=3     ,       stride=2     ,       )

       SCREAMING_SNAKE_CASE__   =   [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

       SCREAMING_SNAKE_CASE__   =   nn.ModuleList()
       for i in range(13 ):
            SCREAMING_SNAKE_CASE__   =   out_channels

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

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

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

       SCREAMING_SNAKE_CASE__   =   nn.AdaptiveAvgPoolad((1, 1) ) if add_pooling_layer else None

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






  def 				lowercase_		(							self						:		Any     ,       __lowerCamelCase						:		Dict )		->							Optional[Any]:
       raise NotImplementedError







  @add_start_docstrings_to_model_forward(__lowerCamelCase )
  @add_code_sample_docstrings(
      checkpoint=_CHECKPOINT_FOR_DOC     ,       output_type=__lowerCamelCase     ,       config_class=_CONFIG_FOR_DOC     ,       modality='''vision'''     ,       expected_output=_EXPECTED_OUTPUT_SHAPE     ,       )
  def 				lowercase_		(							self						:		List[Any]     ,       __lowerCamelCase						:		Optional[torch.Tensor] = None     ,       __lowerCamelCase						:		Optional[bool] = None     ,       __lowerCamelCase						:		Optional[bool] = None     ,       )		->							Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:
       SCREAMING_SNAKE_CASE__   =   (
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       SCREAMING_SNAKE_CASE__   =   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''' )

       SCREAMING_SNAKE_CASE__   =   self.conv_stem(__lowerCamelCase )

       SCREAMING_SNAKE_CASE__   =   () if output_hidden_states else None

       for i, layer_module in enumerate(self.layer ):
            SCREAMING_SNAKE_CASE__   =   layer_module(__lowerCamelCase )

            if output_hidden_states:
                 SCREAMING_SNAKE_CASE__   =   all_hidden_states + (hidden_states,)

       SCREAMING_SNAKE_CASE__   =   hidden_states

       if self.pooler is not None:
            SCREAMING_SNAKE_CASE__   =   torch.flatten(self.pooler(__lowerCamelCase )     ,       start_dim=1 )
       else:
            SCREAMING_SNAKE_CASE__   =   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=__lowerCamelCase     ,       pooler_output=__lowerCamelCase     ,       hidden_states=__lowerCamelCase     ,       )






@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    "						,					A__						,					)
class 	UpperCAmelCase__		(	A__	):






  """simple docstring"""


  def __init__(							self						:		List[Any]     ,       __lowerCamelCase						:		MobileNetVaConfig )		->							None:
       super().__init__(__lowerCamelCase )

       SCREAMING_SNAKE_CASE__   =   config.num_labels
       SCREAMING_SNAKE_CASE__   =   MobileNetVaModel(__lowerCamelCase )

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

       # Classifier head
       SCREAMING_SNAKE_CASE__   =   nn.Dropout(config.classifier_dropout_prob     ,       inplace=__lowerCamelCase )
       SCREAMING_SNAKE_CASE__   =   nn.Linear(__lowerCamelCase     ,       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(__lowerCamelCase )
  @add_code_sample_docstrings(
      checkpoint=_IMAGE_CLASS_CHECKPOINT     ,       output_type=__lowerCamelCase     ,       config_class=_CONFIG_FOR_DOC     ,       expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT     ,       )
  def 				lowercase_		(							self						:		int     ,       __lowerCamelCase						:		Optional[torch.Tensor] = None     ,       __lowerCamelCase						:		Optional[bool] = None     ,       __lowerCamelCase						:		Optional[torch.Tensor] = None     ,       __lowerCamelCase						:		Optional[bool] = None     ,       )		->							Union[tuple, ImageClassifierOutputWithNoAttention]:
       SCREAMING_SNAKE_CASE__   =   return_dict if return_dict is not None else self.config.use_return_dict

       SCREAMING_SNAKE_CASE__   =   self.mobilenet_va(__lowerCamelCase     ,       output_hidden_states=__lowerCamelCase     ,       return_dict=__lowerCamelCase )

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

       SCREAMING_SNAKE_CASE__   =   self.classifier(self.dropout(__lowerCamelCase ) )

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

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

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

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





from functools import reduce

_SCREAMING_SNAKE_CASE					:  Any             =      (
    '''73167176531330624919225119674426574742355349194934'''
    '''96983520312774506326239578318016984801869478851843'''
    '''85861560789112949495459501737958331952853208805511'''
    '''12540698747158523863050715693290963295227443043557'''
    '''66896648950445244523161731856403098711121722383113'''
    '''62229893423380308135336276614282806444486645238749'''
    '''30358907296290491560440772390713810515859307960866'''
    '''70172427121883998797908792274921901699720888093776'''
    '''65727333001053367881220235421809751254540594752243'''
    '''52584907711670556013604839586446706324415722155397'''
    '''53697817977846174064955149290862569321978468622482'''
    '''83972241375657056057490261407972968652414535100474'''
    '''82166370484403199890008895243450658541227588666881'''
    '''16427171479924442928230863465674813919123162824586'''
    '''17866458359124566529476545682848912883142607690042'''
    '''24219022671055626321111109370544217506941658960408'''
    '''07198403850962455444362981230987879927244284909188'''
    '''84580156166097919133875499200524063689912560717606'''
    '''05886116467109405077541002256983155200055935729725'''
    '''71636269561882670428252483600823257530420752963450'''
)
def     UpperCAmelCase_       (						_A = N    ):





     '''simple docstring'''




     return max(
         # mypy cannot properly interpret reduce
         int(reduce(lambda _A				,		_A    : str(int(_A    ) * int(_A    )    )				,		n[i : i + 13]    )    )
         for i in range(len(_A    ) - 12    )    )


if __name__ == "__main__":
       print(F"{solution() = }")


