
    2VhsF                        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 ed
dg      	 	 	 	 	 	 	 	 	 dd       Zd ZddZ ed      dd       Z ed      dd       Zej(                  j+                  dej,                  ej.                        e_        ej&                  j0                  e_        y)    N)backend)layers)keras_export)imagenet_utils)
Functional)operation_utils)
file_utilszJhttps://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v2/z+keras.applications.mobilenet_v2.MobileNetV2zkeras.applications.MobileNetV2c	           
         |dv s$t        j                  |      st        d| d      |dk(  r|r|dk7  rt        d| d      | |	 t        j                  |      }	|	ryt        j                         d	k(  r1|j                  d
   | d
   k7  r\t        d|j                   d|  d      |j                  d   | d
   k7  r+t        d|j                   d|  d      t        d| d      | |	 t        j                  |       | t        j                  |      sd}
n| t        j                  |      rt        j                         d	k(  r|j                  d   }|j                  d   }n|j                  d
   }|j                  d   }||k(  r|dv r|}
nBd}
n?| d}
n:t        j                         d	k(  r| d
   }| d   }n
| d   }| d
   }||k(  r|dv r|}
nd}
t        j                  | 
dt        j                         ||      } t        j                         dk(  rd\  }}nd\  }}| |   }| |   }|dk(  r5|dvrt        d| d      ||k7  s|dvrd}t        j                  dd       |t        j                  |       }n/t        j                  |      st        j                  ||        }n|}t        j                         d	k(  rd
nd!}t!        d|z  d"      } t        j"                  |dd#d$d%d&'      |      } t        j$                  |d(d)d*+      |      } t        j&                  d,d-.      |      }t)        |d/|d
d
d0      }t)        |d1|dd2d
0      }t)        |d1|d
d2d0      }t)        |d|dd2d0      }t)        |d|d
d2d30      }t)        |d|d
d2d40      }t)        |d5|dd2d20      }t)        |d5|d
d2d60      }t)        |d5|d
d2d"0      }t)        |d5|d
d2d70      }t)        |d8|d
d2d90      }t)        |d8|d
d2d:0      }t)        |d8|d
d2d;0      }t)        |d<|dd2d=0      }t)        |d<|d
d2d>0      }t)        |d<|d
d2d?0      }t)        |d@|d
d2d/0      }|dAkD  rt!        dB|z  d"      }ndB} t        j"                  |d
d%dCD      |      } t        j$                  |d(d)dE+      |      } t        j&                  d,dF.      |      }|rO t        j*                         |      }t        j,                  ||        t        j.                  ||dGH      |      }n?|dIk(  r t        j*                         |      }n|dJk(  r t        j0                         |      }|t        j                  |      }n|}|	dK|dLdM| }t3        |||.      }|dk(  r|rKdNt5        t7        |            z   dMz   t5        |      z   dOz   }t8        |z   }t        j:                  ||dPQ      }nMdNt5        t7        |            z   dMz   t5        |      z   dRz   dOz   }t8        |z   }t        j:                  ||dPQ      }|j=                  |       |S ||j=                  |       |S # t        $ rV 	 t        j                  t        j                  |            }	n(# t        $ r t        d| dt        |       d      w xY wY Tw xY w# t        $ r t        d| dt        |             w xY w)Sa  Instantiates the MobileNetV2 architecture.

    MobileNetV2 is very similar to the original MobileNet,
    except that it uses inverted residual blocks with
    bottlenecking features. It has a drastically lower
    parameter count than the original MobileNet.
    MobileNets support any input size greater
    than 32 x 32, with larger image sizes
    offering better performance.

    Reference:
    - [MobileNetV2: Inverted Residuals and Linear Bottlenecks](
        https://arxiv.org/abs/1801.04381) (CVPR 2018)

    This function returns a Keras image classification model,
    optionally loaded with weights pre-trained on ImageNet.

    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 MobileNetV2, call
    `keras.applications.mobilenet_v2.preprocess_input`
    on your inputs before passing them to the model.
    `mobilenet_v2.preprocess_input` will scale input pixels between -1 and 1.

    Args:
        input_shape: Optional shape tuple, only to be specified if `include_top`
            is `False` (otherwise the input shape has to be `(224, 224, 3)`
            (with `"channels_last"` data format) or `(3, 224, 224)`
            (with `"channels_first"` data format).
            It should have exactly 3 inputs channels, and width and
            height should be no smaller than 32. E.g. `(200, 200, 3)` would
            be one valid value. Defaults to `None`.
            `input_shape` will be ignored if the `input_tensor` is provided.
        alpha: Controls the width of the network. This is known as the width
            multiplier in the MobileNet paper.
            - 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. Defaults to `1.0`.
        include_top: Boolean, whether to include the fully-connected layer
            at the top of the network. Defaults to `True`.
        weights: One of `None` (random initialization), `"imagenet"`
            (pre-training on ImageNet), or the path to the weights file
            to be loaded. Defaults to `"imagenet"`.
        input_tensor: Optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model. `input_tensor` is useful
            for sharing inputs between multiple different networks.
            Defaults to `None`.
        pooling: Optional pooling mode for feature extraction when `include_top`
            is `False`.
            - `None` (default) 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: Optional number of classes to classify images into,
            only to be specified if `include_top` is `True`, and if
            no `weights` argument is specified. Defaults to `1000`.
        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"`.
        name: String, the name of the model.

    Returns:
        A model instance.
    >   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   z|input_shape[1] must equal shape(input_tensor)[1] when `image_data_format` is `channels_first`; Received `input_tensor.shape=z`, `input_shape=   zNinput_tensor.shape[2] must equal input_shape[1]; Received `input_tensor.shape=z;input_tensor is not a Keras tensor; Received `input_tensor=z9input_tensor must be a valid Keras tensor type; Received z	 of type       )`            r   r       )default_sizemin_sizedata_formatrequire_flattenweightschannels_last)r   r   )r   r   )gffffff?g      ?g      ?      ?g?gffffff?zIf imagenet weights are being loaded, alpha must be one of `0.35`, `0.50`, `0.75`, `1.0`, `1.3` or `1.4` only; Received `alpha=z`input_shape` is undefined or non-square, or `rows` is not in [96, 128, 160, 192, 224]. Weights for input shape (224, 224) will be loaded as the default.)
stacklevel)shape)tensorr       )r   r   sameFConv1)kernel_sizestridespaddinguse_biasnameMbP?+?bn_Conv1axisepsilonmomentumr*         @
Conv1_relur*      )filtersalphastride	expansionblock_id            @      	   r   
         r            i@  r   i   Conv_1)r&   r)   r*   	Conv_1_bnout_relupredictions)
activationr*   avgmaxmobilenetv2_z0.2f_0mobilenet_v2_weights_tf_dim_ordering_tf_kernels_z.h5models)cache_subdir_no_top)r	   exists
ValueErrorr   is_keras_tensorr   get_source_inputstypeimage_data_formatr    r   obtain_input_shapewarningswarnr   Input_make_divisibleConv2DBatchNormalizationReLU_inverted_res_blockGlobalAveragePooling2Dvalidate_activationDenseGlobalMaxPooling2Dr   strfloatBASE_WEIGHT_PATHget_fileload_weights)input_shaper7   include_topr   input_tensorpoolingclassesclassifier_activationr*   is_input_t_tensorr   rowscolsrow_axiscol_axis	img_inputchannel_axisfirst_block_filtersxlast_block_filtersinputsmodel
model_nameweight_pathweights_paths                            S/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/applications/mobilenet_v2.pyMobileNetV2r      s   @ ))Z->->w-G! ")		,
 	
 *DDDK9AO
 	
 <#;	 ' 7 7 E ((*.>>%%a(KN:$8 (--. /**5a	9   %%a(KN:$8'--. /((3}A7  **6q:  |7	##L1 w'>'>|'LL W%<%<\%J((*.>>#))!,#))!,#))!,#))!,t|(@ @#" 
	 $$&*::q>Dq>Dq>Dq>D4<D$<<LL 33!--/#K   "o5#(#(x Dx D*99$ %*7!-  4<4'??DMM)  LL{3	&&|4LLI$I1137GG1RL)"u*a8		 	A	!!4%j			A 	,Cl+A.A	2U1A	A 		2U1A	A 		2U1A	A 		2U1A	A 		2U1A	A 		2U1A	A 		2U1A	A 		2U1A	A 		2U1A	A 		2U1A	A 		2U1B	A 		2U1B	A 		2U1B	A 		3eAR	A 		3eAR	A 		3eAR	A 		3eAR	A s{,TE\1=!	E			A	!!4%k			A 	*Cj)!,A+F))+A.**+@'J
FLL 5M


 e/--/2A+))+A.A  22<@ |eD\4&1vqt,E *BeEl#$ d) 	  +Z7K%..KhL
 CeEl#$ d) 	
   +Z7K%..KhL 	<( L 
	7#LW  
		$+$;$;#55lC%!   $\N4484F3GqJ  "
	N  	(>43E2FH 	s6   	Y -Z6 	Z3(ZZ3%Z,,Z32Z36$[c           
         t        j                         dk(  rdnd}| j                  |   }t        ||z        }t	        |d      }	| }
d| d}|rj t        j                  ||z  dddd	|d
z         |
      }
 t        j                  |dd|dz         |
      }
 t        j                  d|dz         |
      }
nd}|dk(  r4 t        j                  t        j                  |
d      |dz         |
      }
 t        j                  d|d	d|dk(  rdnd|dz         |
      }
 t        j                  |dd|dz         |
      }
 t        j                  d|dz         |
      }
 t        j                  |	dddd	|dz         |
      }
 t        j                  |dd|dz         |
      }
||	k(  r&|dk(  r! t        j                  |dz         | |
g      S |
S ) zInverted ResNet block.r   r   r"   r#   block_rP   r$   FNexpand)r&   r(   r)   rL   r*   r+   r,   	expand_BNr.   r2   expand_relur4   expanded_conv_r   r   pad)r(   r*   valid	depthwise)r&   r'   rL   r)   r(   r*   depthwise_BNdepthwise_reluproject
project_BNadd)r   rZ   r    intr_   r   r`   ra   rb   ZeroPadding2Dr   correct_padDepthwiseConv2DAdd)r}   r9   r8   r7   r6   r:   ry   in_channelspointwise_conv_filterspointwise_filtersr{   prefixs               r   rc   rc     sC   1137GG1RL,,|,K 51 ((>BAhZq!F
FMM#("
 
F%%+%	

  :FKK&="89!<! {
F  "..q!46E>

	 A+7k!	 		A	!!n$		
 		A 	9Cf'778;A	i	 		A	!!l"		
 		A ''FaK.vzzv~.{;;H    c                 j    ||}t        |t        | |dz  z         |z  |z        }|d| z  k  r||z  }|S )Nr   g?)rN   r   )vdivisor	min_valuenew_vs       r   r_   r_     sJ    		3q7Q;/7:WDEEsQwLr   z0keras.applications.mobilenet_v2.preprocess_inputc                 2    t        j                  | |d      S )Ntf)r   mode)r   preprocess_input)r{   r   s     r   r   r     s    **	{ r   z2keras.applications.mobilenet_v2.decode_predictionsc                 0    t        j                  | |      S )N)top)r   decode_predictions)predsr   s     r   r   r     s    ,,U<<r    )r   reterror)	Nr   Tr   NNr   softmaxN)N)r>   )r\   	keras.srcr   r   keras.src.api_exportr   keras.src.applicationsr   keras.src.modelsr   keras.src.opsr   keras.src.utilsr	   rj   r   rc   r_   r   r   PREPROCESS_INPUT_DOCformatPREPROCESS_INPUT_RET_DOC_TFPREPROCESS_INPUT_ERROR_DOC__doc__ r   r   <module>r      s       - 1 ' ) & Q 
 5( 
#	yyxHV @A B BC= D= *>>EE	22

3
3 F   
 ,>>FF  r   