# flake8: noqa
# Lint as: python3
__lowercase										= [
    '''VerificationMode''',
    '''Version''',
    '''disable_progress_bar''',
    '''enable_progress_bar''',
    '''is_progress_bar_enabled''',
    '''experimental''',
]

from .info_utils import VerificationMode
from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled
from .version import Version
from .experimental import experimental






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


__lowercase										= logging.get_logger(__name__)


# General docstring
__lowercase										= '''MobileNetV1Config'''

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

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


__lowercase										= [
    '''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  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE=None     ):
    '''simple docstring'''







    __UpperCamelCase					:Tuple        =		{}

    if isinstance(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
        __UpperCamelCase					:Dict        =		model.mobilenet_va
    else:
        __UpperCamelCase					:str        =		model

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

    for i in range(13     ):
        __UpperCamelCase					:Optional[Any]        =		i + 1
        __UpperCamelCase					:Optional[int]        =		i * 2

        __UpperCamelCase					:List[Any]        =		backbone.layer[pt_index]
        __UpperCamelCase					:Tuple        =		f"""MobilenetV1/Conv2d_{tf_index}_depthwise/"""
        __UpperCamelCase					:Any        =		pointer.convolution.weight
        __UpperCamelCase					:Dict        =		pointer.normalization.bias
        __UpperCamelCase					:List[str]        =		pointer.normalization.weight
        __UpperCamelCase					:Any        =		pointer.normalization.running_mean
        __UpperCamelCase					:List[str]        =		pointer.normalization.running_var

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

    if isinstance(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
        __UpperCamelCase					:Any        =		'''MobilenetV1/Logits/Conv2d_1c_1x1/'''
        __UpperCamelCase					:Union[str, Any]        =		model.classifier.weight
        __UpperCamelCase					:int        =		model.classifier.bias

    return tf_to_pt_map



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

    # Build TF to PyTorch weights loading map
    __UpperCamelCase					:Optional[Any]        =		_build_tf_to_pytorch_map(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_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

        __UpperCamelCase					:Optional[Any]        =		tf_weights[name]

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

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

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



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
    '''simple docstring'''







    __UpperCamelCase  ,	__UpperCamelCase					:str        =		features.shape[-2:]
    __UpperCamelCase  ,	__UpperCamelCase					:Union[str, Any]        =		conv_layer.stride
    __UpperCamelCase  ,	__UpperCamelCase					:Union[str, Any]        =		conv_layer.kernel_size

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

    if in_width % stride_width == 0:
        __UpperCamelCase					:List[str]        =		max(kernel_width - stride_width  ,      0     )
    else:
        __UpperCamelCase					:Tuple        =		max(kernel_width - (in_width % stride_width)  ,      0     )

    __UpperCamelCase					:Any        =		pad_along_width // 2
    __UpperCamelCase					:Tuple        =		pad_along_width - pad_left
    __UpperCamelCase					:Union[str, Any]        =		pad_along_height // 2
    __UpperCamelCase					:str        =		pad_along_height - pad_top

    __UpperCamelCase					:Optional[Any]        =		(pad_left, pad_right, pad_top, pad_bottom)
    return nn.functional.pad(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      '''constant'''  ,      0.0     )


class      lowerCamelCase_					(       nn.Module			):






    '''simple docstring'''






    def __init__(     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase = 1			,			__lowercase = 1			,			__lowercase = False			,			__lowercase = True			,			__lowercase = True			,			)	->     None:
        super().__init__()
        __UpperCamelCase					:str        =		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.""")

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

        __UpperCamelCase					:List[Any]        =		nn.Convad(
            in_channels=__lowercase			,			out_channels=__lowercase			,			kernel_size=__lowercase			,			stride=__lowercase			,			padding=__lowercase			,			groups=__lowercase			,			bias=__lowercase			,			padding_mode='''zeros'''			,			)

        if use_normalization:
            __UpperCamelCase					:str        =		nn.BatchNormad(
                num_features=__lowercase			,			eps=config.layer_norm_eps			,			momentum=0.99_97			,			affine=__lowercase			,			track_running_stats=__lowercase			,			)
        else:
            __UpperCamelCase					:Tuple        =		None

        if use_activation:
            if isinstance(__lowercase			,			__lowercase):
                __UpperCamelCase					:Union[str, Any]        =		ACTaFN[use_activation]
            elif isinstance(config.hidden_act			,			__lowercase):
                __UpperCamelCase					:Dict        =		ACTaFN[config.hidden_act]
            else:
                __UpperCamelCase					:List[Any]        =		config.hidden_act
        else:
            __UpperCamelCase					:Optional[Any]        =		None







    def       UpperCamelCase__       (     self			,			__lowercase)	->     torch.Tensor:
        if self.config.tf_padding:
            __UpperCamelCase					:Any        =		apply_tf_padding(__lowercase			,			self.convolution)
        __UpperCamelCase					:str        =		self.convolution(__lowercase)
        if self.normalization is not None:
            __UpperCamelCase					:Any        =		self.normalization(__lowercase)
        if self.activation is not None:
            __UpperCamelCase					:List[str]        =		self.activation(__lowercase)
        return features


class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    a__     :						List[str]      =				MobileNetVaConfig
    a__     :						Dict      =				load_tf_weights_in_mobilenet_va
    a__     :						Tuple      =				"""mobilenet_v1"""
    a__     :						Optional[Any]      =				"""pixel_values"""
    a__     :						int      =				False







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

__lowercase										= 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."""						,     UpperCAmelCase_						,     )
class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    def __init__(     self			,			__lowercase			,			__lowercase = True)	->     Optional[Any]:
        super().__init__(__lowercase)
        __UpperCamelCase					:List[str]        =		config

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

        __UpperCamelCase					:Union[str, Any]        =		MobileNetVaConvLayer(
            __lowercase			,			in_channels=config.num_channels			,			out_channels=__lowercase			,			kernel_size=3			,			stride=2			,			)

        __UpperCamelCase					:str        =		[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

        __UpperCamelCase					:Any        =		nn.ModuleList()
        for i in range(13):
            __UpperCamelCase					:str        =		out_channels

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

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

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

        __UpperCamelCase					:str        =		nn.AdaptiveAvgPoolad((1, 1)) if add_pooling_layer else None

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







    def       UpperCamelCase__       (     self			,			__lowercase)	->     Union[str, Any]:
        raise NotImplementedError







    @add_start_docstrings_to_model_forward(__lowercase)
    @add_code_sample_docstrings(
        checkpoint=_CHECKPOINT_FOR_DOC			,			output_type=__lowercase			,			config_class=_CONFIG_FOR_DOC			,			modality='''vision'''			,			expected_output=_EXPECTED_OUTPUT_SHAPE			,			)
    def       UpperCamelCase__       (     self			,			__lowercase = None			,			__lowercase = None			,			__lowercase = None			,			)	->     Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:
        __UpperCamelCase					:Union[str, Any]        =		(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        __UpperCamelCase					:str        =		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''')

        __UpperCamelCase					:int        =		self.conv_stem(__lowercase)

        __UpperCamelCase					:List[str]        =		() if output_hidden_states else None

        for i, layer_module in enumerate(self.layer):
            __UpperCamelCase					:Optional[Any]        =		layer_module(__lowercase)

            if output_hidden_states:
                __UpperCamelCase					:int        =		all_hidden_states + (hidden_states,)

        __UpperCamelCase					:Any        =		hidden_states

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


@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.
    """						,     UpperCAmelCase_						,     )
class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






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

        __UpperCamelCase					:int        =		config.num_labels
        __UpperCamelCase					:Optional[int]        =		MobileNetVaModel(__lowercase)

        __UpperCamelCase					:Optional[Any]        =		self.mobilenet_va.layer[-1].convolution.out_channels

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

        __UpperCamelCase					:Tuple        =		self.mobilenet_va(__lowercase			,			output_hidden_states=__lowercase			,			return_dict=__lowercase)

        __UpperCamelCase					:List[str]        =		outputs.pooler_output if return_dict else outputs[1]

        __UpperCamelCase					:Union[str, Any]        =		self.classifier(self.dropout(__lowercase))

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

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

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

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





