






'''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


lowerCamelCase_												=					logging.get_logger(__name__)


# General docstring
lowerCamelCase_												=					'''MobileNetV1Config'''

# Base docstring
lowerCamelCase_												=					'''google/mobilenet_v1_1.0_224'''
lowerCamelCase_												=					[1, 10_24, 7, 7]

# Image classification docstring
lowerCamelCase_												=					'''google/mobilenet_v1_1.0_224'''
lowerCamelCase_												=					'''tabby, tabby cat'''


lowerCamelCase_												=					[
    '''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 					__lowercase						(			__lowercase   ,  __lowercase   ,  __lowercase=None       )       ->						List[str]:

      '''simple docstring'''


      _A			     =	{}

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

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

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

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

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

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

      return tf_to_pt_map

def 					__lowercase						(			__lowercase   ,  __lowercase   ,  __lowercase       )       ->						Dict:

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

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

      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

            _A			     =	tf_weights[name]

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

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

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

def 					__lowercase						(			__lowercase   ,  __lowercase       )       ->						torch.Tensor:

      '''simple docstring'''


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

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

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

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

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

class    _UpperCAmelCase			(     nn.Module						):




   """simple docstring"""

   def __init__(		self :	int      ,      __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      ,      ):





         '''simple docstring'''





         super().__init__()
         _A			     =	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.'''		)

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

         _A			     =	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:
               _A			     =	nn.BatchNormad(
                   num_features=__UpperCAmelCase      ,      eps=config.layer_norm_eps      ,      momentum=0.9997      ,      affine=__UpperCAmelCase      ,      track_running_stats=__UpperCAmelCase      ,      )
         else:
               _A			     =	None

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

   def 						lowerCAmelCase (		self :	str      ,      __UpperCAmelCase :	torch.Tensor		):





         '''simple docstring'''





         if self.config.tf_padding:
               _A			     =	apply_tf_padding(__UpperCAmelCase      ,      self.convolution		)
         _A			     =	self.convolution(__UpperCAmelCase		)
         if self.normalization is not None:
               _A			     =	self.normalization(__UpperCAmelCase		)
         if self.activation is not None:
               _A			     =	self.activation(__UpperCAmelCase		)
         return features





class    _UpperCAmelCase			(     snake_case_						):




   """simple docstring"""

   snake_case    				=						MobileNetVaConfig
   snake_case    				=						load_tf_weights_in_mobilenet_va
   snake_case    				=						'''mobilenet_v1'''
   snake_case    				=						'''pixel_values'''
   snake_case    				=						False

   def 						lowerCAmelCase (		self :	List[str]      ,      __UpperCAmelCase :	Union[nn.Linear, nn.Convad]		):





         '''simple docstring'''





         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		)





lowerCamelCase_												=					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.
'''

lowerCamelCase_												=					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.'''							,    snake_case_							,    )
class    _UpperCAmelCase			(     snake_case_						):




   """simple docstring"""

   def __init__(		self :	List[Any]      ,      __UpperCAmelCase :	MobileNetVaConfig      ,      __UpperCAmelCase :	bool = True		):





         '''simple docstring'''





         super().__init__(__UpperCAmelCase		)
         _A			     =	config

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

         _A			     =	MobileNetVaConvLayer(
             __UpperCAmelCase      ,      in_channels=config.num_channels      ,      out_channels=__UpperCAmelCase      ,      kernel_size=3      ,      stride=2      ,      )

         _A			     =	[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

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

               if strides[i] == 2 or i == 0:
                     depth *= 2
                     _A			     =	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      ,      )		)

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

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

   def 						lowerCAmelCase (		self :	List[str]      ,      __UpperCAmelCase :	List[str]		):





         '''simple docstring'''





         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 						lowerCAmelCase (		self :	List[str]      ,      __UpperCAmelCase :	Optional[torch.Tensor] = None      ,      __UpperCAmelCase :	Optional[bool] = None      ,      __UpperCAmelCase :	Optional[bool] = None      ,      ):





         '''simple docstring'''





         _A			     =	(
             output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
         )
         _A			     =	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"		)

         _A			     =	self.conv_stem(__UpperCAmelCase		)

         _A			     =	() if output_hidden_states else None

         for i, layer_module in enumerate(self.layer		):
               _A			     =	layer_module(__UpperCAmelCase		)

               if output_hidden_states:
                     _A			     =	all_hidden_states + (hidden_states,)

         _A			     =	hidden_states

         if self.pooler is not None:
               _A			     =	torch.flatten(self.pooler(__UpperCAmelCase		)      ,      start_dim=1		)
         else:
               _A			     =	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(
    '''
    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    '''							,    snake_case_							,    )
class    _UpperCAmelCase			(     snake_case_						):




   """simple docstring"""

   def __init__(		self :	List[str]      ,      __UpperCAmelCase :	MobileNetVaConfig		):





         '''simple docstring'''





         super().__init__(__UpperCAmelCase		)

         _A			     =	config.num_labels
         _A			     =	MobileNetVaModel(__UpperCAmelCase		)

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

         # Classifier head
         _A			     =	nn.Dropout(config.classifier_dropout_prob      ,      inplace=__UpperCAmelCase		)
         _A			     =	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 						lowerCAmelCase (		self :	Union[str, Any]      ,      __UpperCAmelCase :	Optional[torch.Tensor] = None      ,      __UpperCAmelCase :	Optional[bool] = None      ,      __UpperCAmelCase :	Optional[torch.Tensor] = None      ,      __UpperCAmelCase :	Optional[bool] = None      ,      ):





         '''simple docstring'''





         _A			     =	return_dict if return_dict is not None else self.config.use_return_dict

         _A			     =	self.mobilenet_va(__UpperCAmelCase      ,      output_hidden_states=__UpperCAmelCase      ,      return_dict=__UpperCAmelCase		)

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

         _A			     =	self.classifier(self.dropout(__UpperCAmelCase		)		)

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

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

         if not return_dict:
               _A			     =	(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'''







from __future__ import annotations

from random import choice





def 					UpperCAmelCase_		(__a      :    str ):






   """simple docstring"""

   return choice(__a )





def 					UpperCAmelCase_		(__a      :    list[int] ,	__a      :    int ):






   """simple docstring"""

   _a				:	Dict			=   random_pivot(__a )

   # partition based on pivot
   # linear time
   _a				:	Optional[int]			=   [e for e in lst if e < pivot]
   _a				:	List[str]			=   [e for e in lst if e > pivot]

   # if we get lucky, pivot might be the element we want.
   # we can easily see this:
   # small (elements smaller than k)
   # + pivot (kth element)
   # + big (elements larger than k)
   if len(__a ) == k - 1:
      return pivot
   # pivot is in elements bigger than k
   elif len(__a ) < k - 1:
      return kth_number(__a ,	k - len(__a ) - 1 )
   # pivot is in elements smaller than k
   else:
      return kth_number(__a ,	__a )


if __name__ == "__main__":
  import doctest

  doctest.testmod()



