
    2Vhc\                        d dl 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dZ	 	 	 	 	 	 	 	 	 	 	 	 	 d$dZ ed      	 	 	 	 	 	 	 	 	 	 	 	 d%d       Z ed      	 	 	 	 	 	 	 	 	 	 	 	 d&d       Zej)                  d      e_        ej)                  d      e_        d Zd Zd Zd'dZd Zd Z ed       d(d!       Z ed"      d)d#       Zej:                  j*                  e_        y)*    N)backend)layers)keras_export)imagenet_utils)
Functional)operation_utils)
file_utilszJhttps://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v3/) 765b44a33ad4005b3ac83185abf1d0eb 40af19a13ebea4e2ee0c676887f69a2e) 59e551e166be033d707958cf9e29a6a7 07fb09a5933dd0c8eaafa16978110389) 675e7b876c45c57e9e63e6d90a36599c ec5221f64a2f6d1ef965a614bdae7973) cb65d4e5be93758266aa0a7f2c6708b7 ebdb5cc8e0b497cd13a7c275d475c819) 8768d4c2e7dee89b9d02b2d03d65d862 d3e8ec802a04aa4fc771ee12a9a9b836) 99cd97fb2fcdad2bf028eb838de69e37 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
`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.
        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`.
    name: String, the name of the model.

Call arguments:
    inputs: A floating point `numpy.array` or backend-native 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 model instance.
c           
         |dv s#t        j                  |      st        d|       |dk(  r|r|	dk7  rt        d|	       ||	 t        j                  |      }|rwt        j                         dk(  r0|j                  d	   |d	   k7  rXt        d
| d|j                         |j                  d   |d	   k7  r(t        d| d|j                         t        d|d      ||	 t        j                  |       t        j                  |      r^t        j                         dk(  r$|j                  d   }|j                  d   }d||f}n#|j                  d	   }|j                  d   }||df}||t        j                         dk(  rd}nd}t        j                         dk(  rd\  }}nd\  }}||   }||   }|r|r|dk  s|dk  rt        d| d      |dk(  r9|s|dvs|r|dk7  rt        d      ||k7  s|dk7  rt        j                  dd        |t        j                  |!      }n/t        j                  |      st        j                  ||"      }n|}t        j                         dk(  rd	nd#}|rd}t        }d }n
d$}t        }d%}|}|r t        j                   d&d'(      |      } t        j"                  d)dd*d+d,d-.      |      } t        j$                  |d/d0d12      |      } ||      } | ||||      }t'        |j                  |   d3z        }|dkD  rt'        ||z        } t        j"                  |d	d+d,d45      |      } t        j$                  |d/d0d62      |      } ||      }|r t        j(                  d78      |      } t        j"                  |d	d+d7d95      |      } ||      }|d:kD  r t        j*                  |      |      } t        j"                  |	d	d+d;<      |      } t        j,                         |      }t/        j0                  ||        t        j2                  |d=>      |      }nC|
d?k(  r t        j(                  d@A      |      }n!|
dBk(  r t        j4                  dCA      |      }|t        j                  |      }n|}t7        |||A      }|dk(  r~dDj9                  ||rdEndFt;        |            }|rdG|z   dHz   }t<        |   d:   }ndG|z   dIz   }t<        |   d	   }t        j>                  |t@        |z   dJ|K      } |jC                  |        |S ||jC                  |       |S # t        $ rT 	 t        j                  t        j                  |            }n&# t        $ r t        d|dt        |             w xY wY Ow xY w# t        $ r t        d|dt        |      d      w xY w)LN>   NimagenetzThe `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     zfIf using `weights="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   zxWhen backend.image_data_format()=channels_first, input_shape[1] must equal input_tensor.shape[1].  Received input_shape=z, input_tensor.shape=   zGinput_shape[1] must equal input_tensor.shape[2].  Received input_shape=zinput_tensor specified: zis not a keras tensorz	is type: zwhich is not a valid type   channels_last)NNr   )r   NN)r   r   )r   r       z9Input size must be at least 32x32; Received `input_shape=`)g      ?      ?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.)
stacklevel)shape)tensorr#      g      ?g?g      )scaleoffset   )r   r   sameFconv)kernel_sizestridespaddinguse_biasnameMbP?+?conv_bnaxisepsilonmomentumr0      conv_1r,   r.   r/   r0   	conv_1_bnT)keepdimsconv_2r   logitsr,   r.   r0   predictions)
activationr0   avgavg_poolr0   maxmax_poolz{}{}_224_{}_float_minimalistic weights_mobilenet_v3_z.h5z_no_top_v2.h5models)cache_subdir	file_hash)"r	   exists
ValueErrorr   is_keras_tensorr   get_source_inputstypeimage_data_formatr#   warningswarnr   Inputrelu
hard_swish	RescalingConv2DBatchNormalization_depthGlobalAveragePooling2DDropoutFlattenr   validate_activation
ActivationGlobalMaxPooling2Dr   formatstrWEIGHTS_HASHESget_fileBASE_WEIGHT_PATHload_weights)!stack_fnlast_point_chinput_shapealpha
model_typeminimalisticinclude_topweightsinput_tensorclassespoolingdropout_rateclassifier_activationinclude_preprocessingr0   is_input_t_tensorrowscolsrow_axiscol_axis	img_inputchannel_axiskernelrA   se_ratioxlast_conv_chinputsmodel
model_name	file_namerL   weights_paths!                                    S/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/applications/mobilenet_v3.pyMobileNetV3r      s   " ))Z->->w-G  !(y	*
 	
 *D  'y*
 	
 <#;	 ' 7 7 E ((*.>>%%a(KN:$' (3m 4.'--.0   %%a(KN:$''2m 4.'--.	0  *'  |7		##L1 ""<0((*.>>#))!,#))!, $o#))!,#))!,#T1o|3$$&/9)K)K  "o5#(#(x Dx D$)tbyGm1
 	
 *[(2  4<43;MM)  LL{3	&&|4LLI$I1137GG1RL

A<F;t<Q?	
	 		A	!!4%i			A 	1AFJ1A!'',/!34L s{}u45		 		A	!!4%k			A 	1A8F))48;
FMM
  qM!,|,Q/A
FMMF

 FNNQ**+@'J
F,=

 e>--:>qAA:))z:1=A  22<@ vqt,E *(//<RU

 /*<uDI&z215I/*<NI&z215I!**y(!	
 	<( L 
	7#Li  	
$+$;$;#55lC%!   $ 3373E2FH  "	V  	 \"+ 	s6   U 'V/ 	V,(VV,#V%%V,+V,/"Wz#keras.applications.MobileNetV3SmallMobileNetV3Smallc                 B    fd}t        |d| d||||||||	|
|      S )Nc           
      :   f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        | z        S Nr[   drk   s    r   depthz1MobileNetV3Small.<locals>.stack_fn.<locals>.depth      !e)$$    r   r)   r   r   r   g      @   gUUUUUU@   (   r8   r&   0      `      	   
   _inverted_res_blockrV   r   r}   rA   r~   r   rk   s        r   rh   z"MobileNetV3Small.<locals>.stack_fn  sk   	%  1eBiAxqI9eBiAtT1M9eBiAtT1Mq%)VQ*a
  q%)VQ*a
  q%)VQ*a
  q%)VQ*a
  q%)VQ*a
  q%)VQ*a
  q%)VQ*a
  q%)VQ*b
 r   i   smallrD   r   rj   rk   rm   rn   ro   rp   rq   rr   rs   rt   ru   r0   rh   s    `           r   r   r     sE    B  r   z#keras.applications.MobileNetV3LargeMobileNetV3Largec                 B    fd}t        |d| d||||||||	|
|      S )Nc           
         f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                      t        | z        S r   r   r   s    r   r   z1MobileNetV3Large.<locals>.stack_fn.<locals>.depth  r   r   r   r)   r   r   r   r   r   r   r&   r8   P   g      @r   gffffff@r   r   p   r                  r   r   s        r   rh   z"MobileNetV3Large.<locals>.stack_fn  s   	%  1eBiAtT1E1eBiAtT1E1eBiAtT1E1eBiHdAN1eBiHdAN1eBiHdAN1eBiAtZK3b	1az1M3b	1az1M3b	1az1Mq%*aHj"
  q%*aHj"
  q%*fa:r
  q%*fa:r
  q%*fa:r
 r   i   largerD   r   r   s    `           r   r   r     sD    >  r   rD   c                 6     t        j                         |       S r   r   ReLUr   s    r   rV   rV     s    6;;=r   c                 D     t        j                  d      | dz         dz  S )Ng      @g      @gUUUUUU?r   r   s    r   hard_sigmoidr      s!    6;;sAG$	22r   c                 8     t        j                  d      |       S )NrW   )r   r`   r   s    r   rW   rW   $  s    *6\*1--r   c                 j    ||}t        |t        | |dz  z         |z  |z        }|d| z  k  r||z  }|S )Nr   g?)rE   int)vdivisor	min_valuenew_vs       r   r[   r[   /  sJ    		3q7Q;/7:WDEEsQwLr   c                 |    t        j                  d|dz         |       } t        j                  t        ||z        dd|dz         |      } t        j                  |dz   	      |      } t        j                  |dd|d
z         |      }t        |      } t        j                  |dz   	      | |g      }|S )NTsqueeze_excite_avg_pool)r<   r0   r   r*   squeeze_excite_convr?   squeeze_excite_relurD   squeeze_excite_conv_1squeeze_excite_mul)r   r\   rY   r[   r   r   Multiply)r   filtersr~   prefixr   s        r   	_se_blockr   9  s    	%%F%>>		A	w!"++		
 		A 	9"778;A	--		
 		A 	QA;V&::;VQKHAHr   c           	      V   t        j                         dk(  rdnd}| }	d}
| j                  |   }|r_d| d}
 t        j                  t        ||z        ddd|
dz   	      |       }  t        j                  |d
d|
dz         |       }  ||       } |dk(  r4 t        j                  t        j                  | |      |
dz         |       }  t        j                  |||dk(  rdndd|
dz         |       }  t        j                  |d
d|
dz         |       }  ||       } |rt        | t        ||z        ||
      }  t        j                  |ddd|
dz   	      |       }  t        j                  |d
d|
dz         |       } |dk(  r&||k(  r! t        j                  |
dz         |	| g      } | S )Nr   r   r%   expanded_conv__r*   Fexpandr:   r1   r2   	expand_bnr4   r   depthwise_pad)r.   r0   valid	depthwise)r-   r.   r/   r0   depthwise_bnproject
project_bnaddrD   )r   rR   r#   r   rY   r[   rZ   ZeroPadding2Dr   correct_padDepthwiseConv2Dr   Add)r   	expansionr   r,   strider~   rA   block_idr|   shortcutr   	infilterss               r   r   r   O  s     1137GG1RLHF%I!(1-
FMM9y()("
 
F%%+%	

  qM{
F  "..q+>/)
 	 A+7k!	 		A	!!n$		
 		A 	1Aa	I 56&I	i	 		A	!!l"		
 		A {yG++FJJFUN+XqM:Hr   z0keras.applications.mobilenet_v3.preprocess_inputc                     | 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 tensor.
        data_format: Optional data format of the image tensor/array.
            `None` means the global setting
            `keras.config.image_data_format()` is used
            (unless you changed it, it uses `"channels_last"`).
            Defaults to `None`.

    Returns:
        Unchanged `numpy.array` or tensor.
     )r   data_formats     r   preprocess_inputr     s	    ( Hr   z2keras.applications.mobilenet_v3.decode_predictionsc                 0    t        j                  | |      S )N)top)r   decode_predictions)predsr   s     r   r   r     s    ,,U<<r   )Nr    r   FTr   Nr   N皙?softmaxTN)Nr    FTr   Nr   Nr   r   Tr   )Nr    FTr   Nr   Nr   r   Tr   )r   Nr   )r&   )rS   	keras.srcr   r   keras.src.api_exportr   keras.src.applicationsr   keras.src.modelsr   keras.src.opsr   keras.src.utilsr	   rf   rd   BASE_DOCSTRINGr   r   r   rb   __doc__rV   r   rW   r[   r   r   r   r   r   r   r   <module>r      s      - 1 ' ) & Q ))+8j` 
#	zz 34
#	? 5?D 34
#	= 5=@ *006H0I  )006H0I  3.,@F @A B, BC= D= ,>>FF  r   