




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


_snake_case            = logging.get_logger(__name__)


# General docstring
_snake_case            = "MobileNetV1Config"

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

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


_snake_case            = [
    "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        lowerCAmelCase_			(  snake_case_,snake_case_,snake_case_=None  ):
      _A							:  Optional[int]    =		{}

      if isinstance(snake_case_,snake_case_  ):
            _A							:  str    =		model.mobilenet_va
      else:
            _A							:  Any    =		model

      _A							:  List[str]    =		"""MobilenetV1/Conv2d_0/"""
      _A							:  Dict    =		backbone.conv_stem.convolution.weight
      _A							:  Tuple    =		backbone.conv_stem.normalization.bias
      _A							:  int    =		backbone.conv_stem.normalization.weight
      _A							:  int    =		backbone.conv_stem.normalization.running_mean
      _A							:  Tuple    =		backbone.conv_stem.normalization.running_var

      for i in range(13  ):
            _A							:  Union[str, Any]    =		i + 1
            _A							:  Tuple    =		i * 2

            _A							:  int    =		backbone.layer[pt_index]
            _A							:  List[str]    =		f'''MobilenetV1/Conv2d_{tf_index}_depthwise/'''
            _A							:  Optional[int]    =		pointer.convolution.weight
            _A							:  int    =		pointer.normalization.bias
            _A							:  int    =		pointer.normalization.weight
            _A							:  Tuple    =		pointer.normalization.running_mean
            _A							:  Any    =		pointer.normalization.running_var

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

      if isinstance(snake_case_,snake_case_  ):
            _A							:  str    =		"""MobilenetV1/Logits/Conv2d_1c_1x1/"""
            _A							:  Dict    =		model.classifier.weight
            _A							:  Optional[Any]    =		model.classifier.bias

      return tf_to_pt_map







def        lowerCAmelCase_			(  snake_case_,snake_case_,snake_case_  ):
      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							:  Optional[Any]    =		tf.train.list_variables(snake_case_  )
      _A							:  List[str]    =		{}
      for name, shape in init_vars:
            logger.info(f'''Loading TF weight {name} with shape {shape}'''  )
            _A							:  List[Any]    =		tf.train.load_variable(snake_case_,snake_case_  )
            _A							:  Tuple    =		array

      # Build TF to PyTorch weights loading map
      _A							:  Any    =		_build_tf_to_pytorch_map(snake_case_,snake_case_,snake_case_  )

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

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

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

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







def        lowerCAmelCase_			(  snake_case_,snake_case_  ):
      _A    ,     _A							:  Tuple    =		features.shape[-2:]
      _A    ,     _A							:  Any    =		conv_layer.stride
      _A    ,     _A							:  Dict    =		conv_layer.kernel_size

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

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

      _A							:  Dict    =		pad_along_width // 2
      _A							:  Any    =		pad_along_width - pad_left
      _A							:  Optional[int]    =		pad_along_height // 2
      _A							:  Tuple    =		pad_along_height - pad_top

      _A							:  List[str]    =		(pad_left, pad_right, pad_top, pad_bottom)
      return nn.functional.pad(snake_case_,snake_case_,"""constant""",0.0  )







class 		lowercase							(     nn.Module	):

     def __init__(						self	,  _a	,  _a	,  _a	,  _a	,  _a = 1	,  _a = 1	,  _a = False	,  _a = True	,  _a = True	,  )		->							None:
           super().__init__()
           _A							:  List[Any]    =		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							:  Tuple    =		0 if config.tf_padding else int((kernel_size - 1) / 2       )

           _A							:  List[str]    =		nn.Convad(
               in_channels=_a	,  out_channels=_a	,  kernel_size=_a	,  stride=_a	,  padding=_a	,  groups=_a	,  bias=_a	,  padding_mode="""zeros"""	,  )

           if use_normalization:
                 _A							:  Union[str, Any]    =		nn.BatchNormad(
                     num_features=_a	,  eps=config.layer_norm_eps	,  momentum=0.9997	,  affine=_a	,  track_running_stats=_a	,  )
           else:
                 _A							:  Optional[int]    =		None

           if use_activation:
                 if isinstance(_a	,  _a       ):
                       _A							:  List[str]    =		ACTaFN[use_activation]
                 elif isinstance(config.hidden_act	,  _a       ):
                       _A							:  List[str]    =		ACTaFN[config.hidden_act]
                 else:
                       _A							:  Optional[int]    =		config.hidden_act
           else:
                 _A							:  Tuple    =		None



     def       a__			(						self	,  _a       )		->							torch.Tensor:
           if self.config.tf_padding:
                 _A							:  Tuple    =		apply_tf_padding(_a	,  self.convolution       )
           _A							:  Tuple    =		self.convolution(_a       )
           if self.normalization is not None:
                 _A							:  Dict    =		self.normalization(_a       )
           if self.activation is not None:
                 _A							:  str    =		self.activation(_a       )
           return features







class 		lowercase							(     UpperCamelCase__	):
     _a											=		MobileNetVaConfig
     _a											=		load_tf_weights_in_mobilenet_va
     _a											=		"mobilenet_v1"
     _a											=		"pixel_values"
     _a											=		False

     def       a__			(						self	,  _a       )		->							None:
           if isinstance(_a	,  (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(_a	,  nn.BatchNormad       ):
                 module.bias.data.zero_()
                 module.weight.data.fill_(1.0       )


_snake_case            = 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"

_snake_case            = 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.",UpperCamelCase__,)
class 		lowercase							(     UpperCamelCase__	):

     def __init__(						self	,  _a	,  _a = True       )		->							Union[str, Any]:
           super().__init__(_a       )
           _A							:  Any    =		config

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

           _A							:  List[Any]    =		MobileNetVaConvLayer(
               _a	,  in_channels=config.num_channels	,  out_channels=_a	,  kernel_size=3	,  stride=2	,  )

           _A							:  Dict    =		[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

           _A							:  Union[str, Any]    =		nn.ModuleList()
           for i in range(13       ):
                 _A							:  int    =		out_channels

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

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

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

           _A							:  Union[str, Any]    =		nn.AdaptiveAvgPoolad((1, 1)       ) if add_pooling_layer else None

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

     def       a__			(						self	,  _a       )		->							List[Any]:
           raise NotImplementedError



     @add_start_docstrings_to_model_forward(_a       )
     @add_code_sample_docstrings(
         checkpoint=_CHECKPOINT_FOR_DOC	,  output_type=_a	,  config_class=_CONFIG_FOR_DOC	,  modality="""vision"""	,  expected_output=_EXPECTED_OUTPUT_SHAPE	,  )
     def       a__			(						self	,  _a = None	,  _a = None	,  _a = None	,  )		->							Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:
           _A							:  List[Any]    =		(
               output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
           )
           _A							:  Optional[Any]    =		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							:  str    =		self.conv_stem(_a       )

           _A							:  int    =		() if output_hidden_states else None

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

                 if output_hidden_states:
                       _A							:  Optional[Any]    =		all_hidden_states + (hidden_states,)

           _A							:  Optional[int]    =		hidden_states

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







@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    ",UpperCamelCase__,)
class 		lowercase							(     UpperCamelCase__	):

     def __init__(						self	,  _a       )		->							None:
           super().__init__(_a       )

           _A							:  Optional[Any]    =		config.num_labels
           _A							:  Union[str, Any]    =		MobileNetVaModel(_a       )

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

           # Classifier head
           _A							:  List[Any]    =		nn.Dropout(config.classifier_dropout_prob	,  inplace=_a       )
           _A							:  Optional[Any]    =		nn.Linear(_a	,  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(_a       )
     @add_code_sample_docstrings(
         checkpoint=_IMAGE_CLASS_CHECKPOINT	,  output_type=_a	,  config_class=_CONFIG_FOR_DOC	,  expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT	,  )
     def       a__			(						self	,  _a = None	,  _a = None	,  _a = None	,  _a = None	,  )		->							Union[tuple, ImageClassifierOutputWithNoAttention]:
           _A							:  Tuple    =		return_dict if return_dict is not None else self.config.use_return_dict

           _A							:  Tuple    =		self.mobilenet_va(_a	,  output_hidden_states=_a	,  return_dict=_a       )

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

           _A							:  Any    =		self.classifier(self.dropout(_a       )       )

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

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

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

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







from unittest import TestCase

from datasets import Sequence, Value
from datasets.arrow_dataset import Dataset







class 		lowercase							(     UpperCamelCase__	):

     def       a__			(						self       )		->							Optional[int]:
           return [
               {"col_1": 3, "col_2": "a"},
               {"col_1": 2, "col_2": "b"},
               {"col_1": 1, "col_2": "c"},
               {"col_1": 0, "col_2": "d"},
           ]

     def       a__			(						self       )		->							Optional[int]:
           _A							:  Union[str, Any]    =		{"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]}
           return Dataset.from_dict(_a       )

     def       a__			(						self       )		->							Any:
           _A							:  str    =		self._create_example_records()
           _A							:  List[Any]    =		Dataset.from_list(_a       )
           self.assertListEqual(dset.column_names	,  ["""col_1""", """col_2"""]       )
           for i, r in enumerate(_a       ):
                 self.assertDictEqual(_a	,  example_records[i]       )

     def       a__			(						self       )		->							List[str]:
           _A							:  Dict    =		self._create_example_records()
           _A							:  List[str]    =		Dataset.from_list(_a       )
           _A							:  str    =		Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]}       )
           self.assertEqual(dset.info	,  dset_from_dict.info       )

     def       a__			(						self       )		->							str:  # checks what happens with missing columns
           _A							:  List[str]    =		[{"""col_1""": 1}, {"""col_2""": """x"""}]
           _A							:  List[str]    =		Dataset.from_list(_a       )
           self.assertDictEqual(dset[0]	,  {"""col_1""": 1}       )
           self.assertDictEqual(dset[1]	,  {"""col_1""": None}       )  # NB: first record is used for columns

     def       a__			(						self       )		->							Dict:  # checks if the type can be inferred from the second record
           _A							:  List[Any]    =		[{"""col_1""": []}, {"""col_1""": [1, 2]}]
           _A							:  str    =		Dataset.from_list(_a       )
           self.assertEqual(dset.info.features["""col_1"""]	,  Sequence(Value("""int64"""       )       )       )



     def       a__			(						self       )		->							Dict:
           _A							:  List[str]    =		Dataset.from_list([]       )
           self.assertEqual(len(_a       )	,  0       )
           self.assertListEqual(dset.column_names	,  []       )

