o
    Mey_  ã                   @   sˆ  d Z ddlm  mZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ dZdddddddœZeƒ ZdZ												d9dd„Zedƒ											d:dd „ƒZed!ƒ											d:d"d#„ƒZejd d$�e_ ejd#d$�e_ d%d&„ Zd'd(„ Zd)d*„ Zd;d,d-„Zd.d/„ Z d0d1„ Z!ed2ƒd<d3d4„ƒZ"ed5ƒd=d7d8„ƒZ#e	j#j e#_ dS )>zMobileNet v3 models for Keras.é    N)Úbackend)Úmodels)Úimagenet_utils)ÚVersionAwareLayers)Ú
data_utils)Úlayer_utils)Ú
tf_logging)Úkeras_exportzJhttps://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v3/)Z 765b44a33ad4005b3ac83185abf1d0ebZ 40af19a13ebea4e2ee0c676887f69a2e)Z 59e551e166be033d707958cf9e29a6a7Z 07fb09a5933dd0c8eaafa16978110389)Z 675e7b876c45c57e9e63e6d90a36599cZ ec5221f64a2f6d1ef965a614bdae7973)Z cb65d4e5be93758266aa0a7f2c6708b7Z ebdb5cc8e0b497cd13a7c275d475c819)Z 8768d4c2e7dee89b9d02b2d03d65d862Z d3e8ec802a04aa4fc771ee12a9a9b836)Z 99cd97fb2fcdad2bf028eb838de69e37Z cde8136e733e811080d9fcd8a252f7e4)zlarge_224_0.75_floatzlarge_224_1.0_floatz large_minimalistic_224_1.0_floatzsmall_224_0.75_floatzsmall_224_1.0_floatz small_minimalistic_224_1.0_floata±  Instantiates the {name} architecture.

  Reference:
  - [Searching for MobileNetV3](
      https://arxiv.org/pdf/1905.02244.pdf) (ICCV 2019)

  The following table describes the performance of MobileNets v3:
  ------------------------------------------------------------------------
  MACs stands for Multiply Adds

  |Classification Checkpoint|MACs(M)|Parameters(M)|Top1 Accuracy|Pixel1 CPU(ms)|
  |---|---|---|---|---|
  | mobilenet_v3_large_1.0_224              | 217 | 5.4 |   75.6   |   51.2  |
  | mobilenet_v3_large_0.75_224             | 155 | 4.0 |   73.3   |   39.8  |
  | mobilenet_v3_large_minimalistic_1.0_224 | 209 | 3.9 |   72.3   |   44.1  |
  | mobilenet_v3_small_1.0_224              | 66  | 2.9 |   68.1   |   15.8  |
  | mobilenet_v3_small_0.75_224             | 44  | 2.4 |   65.4   |   12.8  |
  | mobilenet_v3_small_minimalistic_1.0_224 | 65  | 2.0 |   61.9   |   12.2  |

  For image classification use cases, see
  [this page for detailed examples](
    https://keras.io/api/applications/#usage-examples-for-image-classification-models).

  For transfer learning use cases, make sure to read the
  [guide to transfer learning & fine-tuning](
    https://keras.io/guides/transfer_learning/).

  Note: each Keras Application expects a specific kind of input preprocessing.
  For MobileNetV3, by default input preprocessing is included as a part of the
  model (as a `Rescaling` layer), and thus
  `tf.keras.applications.mobilenet_v3.preprocess_input` is actually a
  pass-through function. In this use case, MobileNetV3 models expect their
  inputs to be float tensors of pixels with values in the [0-255] range.
  At the same time, preprocessing as a part of the model (i.e. `Rescaling`
  layer) can be disabled by setting `include_preprocessing` argument to False.
  With preprocessing disabled MobileNetV3 models expect their inputs to be float
  tensors of pixels with values in the [-1, 1] range.

  Args:
    input_shape: Optional shape tuple, to be specified if you would
      like to use a model with an input image resolution that is not
      (224, 224, 3).
      It should have exactly 3 inputs channels (224, 224, 3).
      You can also omit this option if you would like
      to infer input_shape from an input_tensor.
      If you choose to include both input_tensor and input_shape then
      input_shape will be used if they match, if the shapes
      do not match then we will throw an error.
      E.g. `(160, 160, 3)` would be one valid value.
    alpha: controls the width of the network. This is known as the
      depth multiplier in the MobileNetV3 paper, but the name is kept for
      consistency with MobileNetV1 in Keras.
      - If `alpha` < 1.0, proportionally decreases the number
          of filters in each layer.
      - If `alpha` > 1.0, proportionally increases the number
          of filters in each layer.
      - If `alpha` = 1, default number of filters from the paper
          are used at each layer.
    minimalistic: In addition to large and small models this module also
      contains so-called minimalistic models, these models have the same
      per-layer dimensions characteristic as MobilenetV3 however, they don't
      utilize any of the advanced blocks (squeeze-and-excite units, hard-swish,
      and 5x5 convolutions). While these models are less efficient on CPU, they
      are much more performant on GPU/DSP.
    include_top: Boolean, whether to include the fully-connected
      layer at the top of the network. Defaults to `True`.
    weights: String, one of `None` (random initialization),
      'imagenet' (pre-training on ImageNet),
      or the path to the weights file to be loaded.
    input_tensor: Optional Keras tensor (i.e. output of
      `layers.Input()`)
      to use as image input for the model.
    pooling: String, optional pooling mode for feature extraction
      when `include_top` is `False`.
      - `None` means that the output of the model
          will be the 4D tensor output of the
          last convolutional block.
      - `avg` means that global average pooling
          will be applied to the output of the
          last convolutional block, and thus
          the output of the model will be a
          2D tensor.
      - `max` means that global max pooling will
          be applied.
    classes: Integer, optional number of classes to classify images
      into, only to be specified if `include_top` is True, and
      if no `weights` argument is specified.
    dropout_rate: fraction of the input units to drop on the last layer.
    classifier_activation: A `str` or callable. The activation function to use
      on the "top" layer. Ignored unless `include_top=True`. Set
      `classifier_activation=None` to return the logits of the "top" layer.
      When loading pretrained weights, `classifier_activation` can only
      be `None` or `"softmax"`.
    include_preprocessing: Boolean, whether to include the preprocessing
      layer (`Rescaling`) at the bottom of the network. Defaults to `True`.

  Call arguments:
    inputs: A floating point `numpy.array` or a `tf.Tensor`, 4D with 3 color
      channels, with values in the range [0, 255] if `include_preprocessing`
      is True and in the range [-1, 1] otherwise.

  Returns:
    A `keras.Model` instance.
ç      ð?ÚlargeFTÚimagenetéè  çš™™™™™É?Úsoftmaxc                  C   s\  |dv st jj |¡std|› �ƒ‚|dkr#|r#|	dkr#td|	› �ƒ‚|d ur™|d ur™zt |¡}W n& tyX   z
t t |¡¡}W n tyU   td|dt	|ƒ› �ƒ‚w Y nw |r“t 
¡ dkrzt |¡d	 |d	 krytd
|› dt |¡› �ƒ‚nt |¡d |d	 kr’td|› dt |¡› �ƒ‚ntd|dƒ‚|d u rì|d urìzt |¡ W n ty¹   td|dt	|ƒdƒ‚w t |¡rìt 
¡ dkrÙt |¡d }t |¡d }d||f}nt |¡d	 }t |¡d }||df}|d u rö|d u röd}t 
¡ dk�rd\}}nd\}}|| }|| }|�r&|�r&|dk �s|dk �r&td|› d�ƒ‚|dk�rN|�s3|dv�s;|�r?|dk�r?tdƒ‚||k�sI|dk�rNt d¡ |d u �rZtj|d�}nt |¡�shtj||d �}n|}t 
¡ dk�rsd	nd!}|�rd}t}d }nd"}t}d#}|}|�r“tjd$d%d&�|ƒ}tjd'dd(d)d*d+d,�|ƒ}tj|d-d.d/d0�|ƒ}||ƒ}| ||||ƒ}tt |¡| d1 ƒ}|dk�rÌt|| ƒ}tj|d	d)d*d2d3�|ƒ}tj|d-d.d4d0�|ƒ}||ƒ}|�r/tjd5d6�|ƒ}tj|d	d)d5d7d3�|ƒ}||ƒ}|d8k�rt |¡|ƒ}tj|	d	d)d9d:�|ƒ}t ¡ |ƒ}t ||¡ tj|d;d<�|ƒ}n|
d=k�r=tjd>d?�|ƒ}n|
d@k�rJtjdAd?�|ƒ}|d u�rUt |¡}n|}tj||dB| d?�}|dk�r¢dC ||�rndDndEt |ƒ¡}|�r„dF| dG }t!| d8 }ndF| dH }t!| d	 }t"j#|t$| dI|dJ�}| %|¡ |S |d u�r¬| %|¡ |S )KN>   r   Nz¯The `weights` argument should be either `None` (random initialization), `imagenet` (pre-training on ImageNet), or the path to the weights file to be loaded.  Received weights=r   r   zkIf using `weights` as `"imagenet"` with `include_top` as true, `classes` should be 1000.  Received classes=zinput_tensor: z7is not type input_tensor.  Received type(input_tensor)=Úchannels_firsté   z…When backend.image_data_format()=channels_first, input_shape[1] must equal backend.int_shape(input_tensor)[1].  Received input_shape=z", backend.int_shape(input_tensor)=é   zTinput_shape[1] must equal backend.int_shape(input_tensor)[2].  Received input_shape=zinput_tensor specified: zis not a keras tensorz	is type: zwhich is not a valid typeé   )NNr   Zchannels_last)r   r   )r   r   é    z9Input size must be at least 32x32; Received `input_shape=ú`)g      è?r
   r
   z|If imagenet weights are being loaded, alpha can be one of `0.75`, `1.0` for non minimalistic or `1.0` for minimalistic only.éà   z�`input_shape` is undefined or non-square, or `rows` is not 224. Weights for input shape (224, 224) will be loaded as the default.)Úshape)Ztensorr   éÿÿÿÿé   g      Ð?g€?g      ð¿)ÚscaleÚoffseté   )r   r   ÚsameFZConv)Úkernel_sizeÚstridesÚpaddingÚuse_biasÚnameçü©ñÒMbP?ç+‡ÙÎ÷ï?zConv/BatchNorm©ZaxisÚepsilonZmomentumr"   é   ZConv_1©r   r    r!   r"   zConv_1/BatchNormT)ÚkeepdimsZConv_2r   ZLogits©r   r    r"   ZPredictions)Ú
activationr"   ZavgZavg_pool©r"   ÚmaxZmax_poolZMobilenetV3z{}{}_224_{}_floatZ_minimalisticÚ Zweights_mobilenet_v3_z.h5z_no_top_v2.h5r   )Zcache_subdirÚ	file_hash)&ÚtfÚioZgfileÚexistsÚ
ValueErrorr   Zis_keras_tensorr   Zget_source_inputsÚtypeÚimage_data_formatÚ	int_shapeÚloggingÚwarningÚlayersZInputÚreluÚ
hard_swishZ	RescalingÚConv2DÚBatchNormalizationÚ_depthÚGlobalAveragePooling2DZDropoutZFlattenr   Zvalidate_activationZ
ActivationZGlobalMaxPooling2Dr   ZModelÚformatÚstrÚWEIGHTS_HASHESr   Zget_fileÚBASE_WEIGHT_PATHZload_weights) Ústack_fnZlast_point_chÚinput_shapeÚalphaZ
model_typeÚminimalisticÚinclude_topÚweightsÚinput_tensorÚclassesÚpoolingÚdropout_rateÚclassifier_activationÚinclude_preprocessingZis_input_t_tensorÚrowsÚcolsZrow_axisZcol_axisZ	img_inputÚchannel_axisÚkernelr+   Úse_ratioÚxZlast_conv_chÚinputsÚmodelZ
model_nameÚ	file_namer/   Zweights_path© rY   úOD:\Projects\ConvertPro\env\Lib\site-packages\keras/applications/mobilenet_v3.pyÚMobileNetV3¬   s   üÿþÿÿÿýÿýþýûÿÿ
þüÿÿ	ýûÿ
	

 ÿÿ
ÿ
ÿ
ÿÿ
úùÿþ
ûúÿþûú
ÿþÿþ



ÿü

ý
r[   z#keras.applications.MobileNetV3Smallc                    ó.   ‡ fdd„}t |d| ˆ d||||||||	|
ƒS )Nc              	      s.  ‡ fdd„}t | d|dƒdd|tdƒ} t | d|d	ƒddd tdƒ} t | d
|d	ƒddd tdƒ} t | d|dƒ|d||dƒ} t | d|dƒ|d||dƒ} t | d|dƒ|d||dƒ} t | d|dƒ|d||dƒ} t | d|dƒ|d||dƒ} t | d|dƒ|d||dƒ} t | d|dƒ|d||dƒ} t | d|dƒ|d||dƒ} | S )Nc                    ó   t | ˆ  ƒS ©N©r>   ©Úd©rF   rY   rZ   Údepth´  ó   z1MobileNetV3Small.<locals>.stack_fn.<locals>.depthr   r   r   r   r   g      @é   gUUUUUU@é   é(   r'   r   é0   é   é`   é   é	   é
   ©Ú_inverted_res_blockr:   ©rU   rS   r+   rT   rc   rb   rY   rZ   rD   ³  s:   ÿÿÿÿÿÿÿÿz"MobileNetV3Small.<locals>.stack_fni   Zsmall©r[   ©rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rD   rY   rb   rZ   ÚMobileNetV3Small¥  s"   !òrs   z#keras.applications.MobileNetV3Largec                    r\   )Nc              	      s–  ‡ fdd„}t | d|dƒddd tdƒ} t | d|dƒdd	d tdƒ} t | d|dƒddd td	ƒ} t | d|d
ƒ|d	|tdƒ} t | d|d
ƒ|d|tdƒ} t | d|d
ƒ|d|tdƒ} t | d|dƒdd	d |dƒ} t | d|dƒddd |dƒ} t | d|dƒddd |dƒ} t | d|dƒddd |dƒ} t | d|dƒdd||dƒ} t | d|dƒdd||dƒ} t | d|dƒ|d	||dƒ} t | d|dƒ|d||dƒ} t | d|dƒ|d||dƒ} | S )Nc                    r]   r^   r_   r`   rb   rY   rZ   rc   õ  rd   z1MobileNetV3Large.<locals>.stack_fn.<locals>.depthr   r   r   r   rf   re   r   rg   r   r'   éP   g      @ri   gffffff@rk   rl   ép   rm   é   é    é   é   é   rn   rp   rb   rY   rZ   rD   ô  s6   ÿÿÿÿÿz"MobileNetV3Large.<locals>.stack_fni   r   rq   rr   rY   rb   rZ   ÚMobileNetV3Largeæ  s"   òr{   r,   c                 C   s   t  ¡ | ƒS r^   ©r9   ÚReLU©rU   rY   rY   rZ   r:   )  rd   r:   c                 C   s   t  d¡| d ƒd S )Ng      @g      @gUUUUUUÅ?r|   r~   rY   rY   rZ   Úhard_sigmoid-  s   r   c                 C   s   t  ¡ | t| ƒgƒS r^   )r9   ÚMultiplyr   r~   rY   rY   rZ   r;   1  s   r;   rk   c                 C   sB   |d u r|}t |t| |d  ƒ| | ƒ}|d|  k r||7 }|S )Nr   gÍÌÌÌÌÌì?)r-   Úint)ÚvZdivisorZ	min_valueÚnew_vrY   rY   rZ   r>   <  s   r>   c                 C   sŠ   t jd|d d�| ƒ}t jt|| ƒdd|d d�|ƒ}t j|d d	�|ƒ}t j|dd|d
 d�|ƒ}t|ƒ}t j|d d	�| |gƒ}|S )NTzsqueeze_excite/AvgPool)r)   r"   r   r   zsqueeze_excite/Convr*   zsqueeze_excite/Relur,   zsqueeze_excite/Conv_1zsqueeze_excite/Mul)r9   r?   r<   r>   r}   r   r€   )rV   ÚfiltersrT   ÚprefixrU   rY   rY   rZ   Ú	_se_blockF  s2   ÿþ
üûüûr†   c                 C   sl  t  ¡ dkrdnd}| }	d}
t  | ¡| }|r@d|› d�}
tjt|| ƒddd|
d	 d
�| ƒ} tj|dd|
d d�| ƒ} || ƒ} |dkrStjt 	| |¡|
d d�| ƒ} tj
|||dkr]dndd|
d d�| ƒ} tj|dd|
d d�| ƒ} || ƒ} |r…t| t|| ƒ||
ƒ} tj|ddd|
d d
�| ƒ} tj|dd|
d d�| ƒ} |dkr´||kr´tj|
d d�|	| gƒ} | S )Nr   r   r   zexpanded_conv/Zexpanded_conv_ú/r   FÚexpandr(   r#   r$   zexpand/BatchNormr%   r   zdepthwise/pad)r    r"   ZvalidZ	depthwise)r   r    r!   r"   zdepthwise/BatchNormÚprojectzproject/BatchNormÚAddr,   )r   r5   r6   r9   r<   r>   r=   ZZeroPadding2Dr   Zcorrect_padZDepthwiseConv2Dr†   rŠ   )rU   Z	expansionr„   r   ZstriderT   r+   Zblock_idrR   Zshortcutr…   Z	infiltersrY   rY   rZ   ro   \  sŽ   
ûúüû
þýûúüûûúüûro   z0keras.applications.mobilenet_v3.preprocess_inputc                 C   s   | S )aÜ  A placeholder method for backward compatibility.

    The preprocessing logic has been included in the mobilenet_v3 model
    implementation. Users are no longer required to call this method to
    normalize the input data. This method does nothing and only kept as a
    placeholder to align the API surface between old and new version of model.

    Args:
      x: A floating point `numpy.array` or a `tf.Tensor`.
      data_format: Optional data format of the image tensor/array. Defaults to
        None, in which case the global setting
        `tf.keras.backend.image_data_format()` is used (unless you changed it,
        it defaults to "channels_last").{mode}

    Returns:
      Unchanged `numpy.array` or `tf.Tensor`.
    rY   )rU   Zdata_formatrY   rY   rZ   Úpreprocess_inputŸ  s   r‹   z2keras.applications.mobilenet_v3.decode_predictionsr   c                 C   s   t j| |d�S )N)Útop)r   Údecode_predictions)ÚpredsrŒ   rY   rY   rZ   r�   µ  s   r�   )Nr
   r   FTr   Nr   Nr   r   T)Nr
   FTr   Nr   Nr   r   T)rk   Nr^   )r   )$Ú__doc__Ztensorflow.compat.v2ÚcompatÚv2r0   Zkerasr   r   Zkeras.applicationsr   Zkeras.layersr   Zkeras.utilsr   r   Ztensorflow.python.platformr   r7   Z tensorflow.python.util.tf_exportr	   rC   rB   r9   ZBASE_DOCSTRINGr[   rs   r{   r@   r:   r   r;   r>   r†   ro   r‹   r�   rY   rY   rY   rZ   Ú<module>   s”   ÿëm
ò zõ@õ>

C