


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


snake_case__		:     Dict    				=				logging.get_logger(__name__)


# General docstring
snake_case__		:     int    				=				'''MobileNetV1Config'''

# Base docstring
snake_case__		:     List[str]    				=				'''google/mobilenet_v1_1.0_224'''
snake_case__		:     List[Any]    				=				[1, 1_024, 7, 7]

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


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      _snake_case						(						_snake_case				:			int ,   _snake_case				:			Tuple ,   _snake_case				:			Union[str, Any]=None			):
      lowerCAmelCase					:	str       =   {}

      if isinstance(_snake_case ,   _snake_case			):
            lowerCAmelCase					:	Union[str, Any]       =   model.mobilenet_va
      else:
            lowerCAmelCase					:	Optional[int]       =   model

      lowerCAmelCase					:	Dict       =   '''MobilenetV1/Conv2d_0/'''
      lowerCAmelCase					:	int       =   backbone.conv_stem.convolution.weight
      lowerCAmelCase					:	Dict       =   backbone.conv_stem.normalization.bias
      lowerCAmelCase					:	str       =   backbone.conv_stem.normalization.weight
      lowerCAmelCase					:	int       =   backbone.conv_stem.normalization.running_mean
      lowerCAmelCase					:	Dict       =   backbone.conv_stem.normalization.running_var

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

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

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

      if isinstance(_snake_case ,   _snake_case			):
            lowerCAmelCase					:	Optional[Any]       =   '''MobilenetV1/Logits/Conv2d_1c_1x1/'''
            lowerCAmelCase					:	List[Any]       =   model.classifier.weight
            lowerCAmelCase					:	int       =   model.classifier.bias

      return tf_to_pt_map


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

      # Build TF to PyTorch weights loading map
      lowerCAmelCase					:	Optional[int]       =   _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

            lowerCAmelCase					:	Any       =   tf_weights[name]

            if "depthwise_weights" in name:
                  logger.info('''Transposing depthwise'''			)
                  lowerCAmelCase					:	str       =   np.transpose(_snake_case ,   (2, 3, 0, 1)			)
            elif "weights" in name:
                  logger.info('''Transposing'''			)
                  if len(pointer.shape			) == 2:  # copying into linear layer
                        lowerCAmelCase					:	Optional[Any]       =   array.squeeze().transpose()
                  else:
                        lowerCAmelCase					:	Dict       =   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}'''			)
            lowerCAmelCase					:	Union[str, Any]       =   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      _snake_case						(						_snake_case				:			torch.Tensor ,   _snake_case				:			nn.Convad			):
      lowerCAmelCase,      lowerCAmelCase					:	List[Any]       =   features.shape[-2:]
      lowerCAmelCase,      lowerCAmelCase					:	str       =   conv_layer.stride
      lowerCAmelCase,      lowerCAmelCase					:	Optional[int]       =   conv_layer.kernel_size

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

      if in_width % stride_width == 0:
            lowerCAmelCase					:	Union[str, Any]       =   max(kernel_width - stride_width ,   0			)
      else:
            lowerCAmelCase					:	Optional[int]       =   max(kernel_width - (in_width % stride_width) ,   0			)

      lowerCAmelCase					:	Tuple       =   pad_along_width // 2
      lowerCAmelCase					:	Dict       =   pad_along_width - pad_left
      lowerCAmelCase					:	str       =   pad_along_height // 2
      lowerCAmelCase					:	Dict       =   pad_along_height - pad_top

      lowerCAmelCase					:	Any       =   (pad_left, pad_right, pad_top, pad_bottom)
      return nn.functional.pad(_snake_case ,   _snake_case ,   '''constant''' ,   0.0			)



class 		snake_case_(       nn.Module			):






     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__()
           lowerCAmelCase					:	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.'''				)

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

           lowerCAmelCase					:	Tuple       =   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:
                 lowerCAmelCase					:	str       =   nn.BatchNormad(
                     num_features=UpperCamelCase_					,				eps=config.layer_norm_eps					,				momentum=0.9_997					,				affine=UpperCamelCase_					,				track_running_stats=UpperCamelCase_					,				)
           else:
                 lowerCAmelCase					:	Optional[Any]       =   None

           if use_activation:
                 if isinstance(UpperCamelCase_					,				UpperCamelCase_				):
                       lowerCAmelCase					:	Tuple       =   ACTaFN[use_activation]
                 elif isinstance(config.hidden_act					,				UpperCamelCase_				):
                       lowerCAmelCase					:	Any       =   ACTaFN[config.hidden_act]
                 else:
                       lowerCAmelCase					:	Union[str, Any]       =   config.hidden_act
           else:
                 lowerCAmelCase					:	List[str]       =   None






     def      lowerCamelCase__     (	self  :					Dict					,				UpperCamelCase_  :					torch.Tensor				):
           if self.config.tf_padding:
                 lowerCAmelCase					:	List[Any]       =   apply_tf_padding(UpperCamelCase_					,				self.convolution				)
           lowerCAmelCase					:	Dict       =   self.convolution(UpperCamelCase_				)
           if self.normalization is not None:
                 lowerCAmelCase					:	Dict       =   self.normalization(UpperCamelCase_				)
           if self.activation is not None:
                 lowerCAmelCase					:	Any       =   self.activation(UpperCamelCase_				)
           return features



class 		snake_case_(       a__			):
     __UpperCamelCase  =					MobileNetVaConfig
     __UpperCamelCase  =					load_tf_weights_in_mobilenet_va
     __UpperCamelCase  =					'''mobilenet_v1'''
     __UpperCamelCase  =					'''pixel_values'''
     __UpperCamelCase  =					False






     def      lowerCamelCase__     (	self  :					Optional[Any]					,				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				)



snake_case__		:     Optional[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.
'''

snake_case__		:     List[str]    				=				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 		snake_case_(       a__			):






     def __init__(	self  :					Dict					,				UpperCamelCase_  :					MobileNetVaConfig					,				UpperCamelCase_  :					bool = True				):
           super().__init__(UpperCamelCase_				)
           lowerCAmelCase					:	Any       =   config

           lowerCAmelCase					:	List[Any]       =   3_2
           lowerCAmelCase					:	Optional[Any]       =   max(int(depth * config.depth_multiplier				)					,				config.min_depth				)

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

           lowerCAmelCase					:	Any       =   [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

           lowerCAmelCase					:	Optional[int]       =   nn.ModuleList()
           for i in range(1_3				):
                 lowerCAmelCase					:	List[Any]       =   out_channels

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

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

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






     def      lowerCamelCase__     (	self  :					int					,				UpperCamelCase_  :					Dict				):
           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  :					Union[str, Any]					,				UpperCamelCase_  :					Optional[torch.Tensor] = None					,				UpperCamelCase_  :					Optional[bool] = None					,				UpperCamelCase_  :					Optional[bool] = None					,				):
           lowerCAmelCase					:	List[Any]       =   (
               output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
           )
           lowerCAmelCase					:	Tuple       =   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'''				)

           lowerCAmelCase					:	str       =   self.conv_stem(UpperCamelCase_				)

           lowerCAmelCase					:	Tuple       =   () if output_hidden_states else None

           for i, layer_module in enumerate(self.layer				):
                 lowerCAmelCase					:	Optional[int]       =   layer_module(UpperCamelCase_				)

                 if output_hidden_states:
                       lowerCAmelCase					:	Union[str, Any]       =   all_hidden_states + (hidden_states,)

           lowerCAmelCase					:	Any       =   hidden_states

           if self.pooler is not None:
                 lowerCAmelCase					:	int       =   torch.flatten(self.pooler(UpperCamelCase_				)					,				start_dim=1				)
           else:
                 lowerCAmelCase					:	Any       =   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.
    '''							,					a__							,					)
class 		snake_case_(       a__			):






     def __init__(	self  :					Optional[Any]					,				UpperCamelCase_  :					MobileNetVaConfig				):
           super().__init__(UpperCamelCase_				)

           lowerCAmelCase					:	int       =   config.num_labels
           lowerCAmelCase					:	Union[str, Any]       =   MobileNetVaModel(UpperCamelCase_				)

           lowerCAmelCase					:	Union[str, Any]       =   self.mobilenet_va.layer[-1].convolution.out_channels

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

           lowerCAmelCase					:	int       =   self.mobilenet_va(UpperCamelCase_					,				output_hidden_states=UpperCamelCase_					,				return_dict=UpperCamelCase_				)

           lowerCAmelCase					:	int       =   outputs.pooler_output if return_dict else outputs[1]

           lowerCAmelCase					:	Optional[int]       =   self.classifier(self.dropout(UpperCamelCase_				)				)

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

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

           if not return_dict:
                 lowerCAmelCase					:	Tuple       =   (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 PIL import Image

# Define glider example
snake_case__		:     int    				=				[
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
]

# Define blinker example
snake_case__		:     Any    				=				[[0, 1, 0], [0, 1, 0], [0, 1, 0]]


def      _snake_case						(						_snake_case				:			list[list[int]]			):
      lowerCAmelCase					:	Union[str, Any]       =   []
      for i in range(len(_snake_case			)			):
            lowerCAmelCase					:	Any       =   []
            for j in range(len(cells[i]			)			):
                  # Get the number of live neighbours
                  lowerCAmelCase					:	Optional[int]       =   0
                  if i > 0 and j > 0:
                        neighbour_count += cells[i - 1][j - 1]
                  if i > 0:
                        neighbour_count += cells[i - 1][j]
                  if i > 0 and j < len(cells[i]			) - 1:
                        neighbour_count += cells[i - 1][j + 1]
                  if j > 0:
                        neighbour_count += cells[i][j - 1]
                  if j < len(cells[i]			) - 1:
                        neighbour_count += cells[i][j + 1]
                  if i < len(_snake_case			) - 1 and j > 0:
                        neighbour_count += cells[i + 1][j - 1]
                  if i < len(_snake_case			) - 1:
                        neighbour_count += cells[i + 1][j]
                  if i < len(_snake_case			) - 1 and j < len(cells[i]			) - 1:
                        neighbour_count += cells[i + 1][j + 1]

                  # Rules of the game of life (excerpt from Wikipedia):
                  # 1. Any live cell with two or three live neighbours survives.
                  # 2. Any dead cell with three live neighbours becomes a live cell.
                  # 3. All other live cells die in the next generation.
                  #    Similarly, all other dead cells stay dead.
                  lowerCAmelCase					:	str       =   cells[i][j] == 1
                  if (
                      (alive and 2 <= neighbour_count <= 3)
                      or not alive
                      and neighbour_count == 3
                  ):
                        next_generation_row.append(1			)
                  else:
                        next_generation_row.append(0			)

            next_generation.append(_snake_case			)
      return next_generation


def      _snake_case						(						_snake_case				:			list[list[int]] ,   _snake_case				:			int			):
      lowerCAmelCase					:	int       =   []
      for _ in range(_snake_case			):
            # Create output image
            lowerCAmelCase					:	Union[str, Any]       =   Image.new('''RGB''' ,   (len(cells[0]			), len(_snake_case			))			)
            lowerCAmelCase					:	Union[str, Any]       =   img.load()

            # Save cells to image
            for x in range(len(_snake_case			)			):
                  for y in range(len(cells[0]			)			):
                        lowerCAmelCase					:	Optional[int]       =   255 - cells[y][x] * 255
                        lowerCAmelCase					:	List[Any]       =   (colour, colour, colour)

        # Save image
            images.append(_snake_case			)
            lowerCAmelCase					:	Union[str, Any]       =   new_generation(_snake_case			)
      return images


if __name__ == "__main__":
 snake_case__		:     Union[str, Any]    				=				generate_images(GLIDER, 16)
 images[0].save('''out.gif''', save_all=True, append_images=images[1:])


