
    2Vh                        d dl Z 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 d	lmZ d
ZddddddddZddddddddddddddddddddddddd ddd ddddddddd dddd d!dddd dd!dd"d#dddgdddddd ddddd$dddd ddddd$dd%dd ddddd&d%d!dddd ddd'd!d(dddd ddd)d(d*dddd ddd$d*d+dddd dgddd,d,dd ddddd&d,ddd ddddd&dd-dd ddddd.d-d/dddd ddd0d/d1dddd ddd2d1d3dddd ddd&d3d4dddd dgddd,d5dd dddddd5d,dd dddddd,ddd ddddddd-dddd ddd$d-d6dddd ddd7d6d/dddd dgddd,d5dd dddddd5d,dd dddddd,ddd ddddddd-dddd ddd$d-d6dddd ddd7d6d/dddd dgddd,d5dd dddddd5d,dd dddddd,ddd ddddddd-dddd ddd$d-d6dddd ddd7d6d/dddd dgddd,d5dd dddddd5d,dd dddddd,ddd ddddddd-dddd ddd$d-d6dddd ddd7d6d/dddd dgd8Zd9d:d;d<d=d>Zd9d?d;d@d=d>ZdAZdB ZdC Z	 	 	 	 	 	 	 	 dhdDZ	 	 	 	 	 	 	 	 dhdEZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 didFZ edGdHg      	 	 	 	 	 	 	 	 	 djdI       Z edJdKg      	 	 	 	 	 	 	 	 	 dkdL       Z edMdNg      	 	 	 	 	 	 	 	 	 dldO       Z edPdQg      	 	 	 	 	 	 	 	 	 dmdR       Z edSdTg      	 	 	 	 	 	 	 	 	 dndU       Z edVdWg      	 	 	 	 	 	 	 	 	 dodX       Z  edYdZg      	 	 	 	 	 	 	 	 	 dpd[       Z!ejE                  d\]      e_#        ejE                  d^]      e_#        ejE                  d_]      e_#        ejE                  d`]      e_#        ejE                  da]      e_#        ejE                  db]      e _#        ejE                  dc]      e!_#         edd      dqde       Z$ edf      drdg       Z%e	jJ                  jF                  e%_#        y)s    N)backend)initializers)layers)keras_export)imagenet_utils)
Functional)operation_utils)
file_utilszMhttps://storage.googleapis.com/tensorflow/keras-applications/efficientnet_v2/) 21ecbf6da12460d5c40bb2f29ceb2188 893217f2bb855e2983157299931e43ff) 069f0534ff22adf035c89e2d9547a9dc 0e80663031ca32d657f9caa404b6ec37) 424e49f28180edbde1e94797771950a7 1dfe2e7a5d45b6632553a8961ea609eb) 1f1fc43bd98a6e4fd8fdfd551e02c7a0 f6abf7b5849ac99a89b50dd3fd532856) e1d88a8495beba45748fedd0cecbe016 af0682fb74e8c54910f2d4393339c070) a3bf6aa3276309f4fc6a34aa114c95cd 1b8dc055df72dde80d614482840fe342) 27e6d408b53c7ebc868fefa357689935 b0b66b5c863aef5b46e8608fe1711615)b0b1b2b3-s-m-l                    )kernel_size
num_repeatinput_filtersoutput_filtersexpand_ratiose_ratiostrides	conv_type   0   @   )r,   r)   r'   r%   r&   r(   r*   r+         g      ?	               P               i0  i       `   
                 i     p      )efficientnetv2-sefficientnetv2-mefficientnetv2-lefficientnetv2-b0efficientnetv2-b1efficientnetv2-b2efficientnetv2-b3VarianceScalingg       @fan_outtruncated_normal)scalemodedistribution)
class_nameconfiggUUUUUU?uniforma   Instantiates the {name} architecture.

Reference:
- [EfficientNetV2: Smaller Models and Faster Training](
    https://arxiv.org/abs/2104.00298) (ICML 2021)

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 EfficientNetV2, by default input preprocessing is included as a part of
the model (as a `Rescaling` layer), and thus
`keras.applications.efficientnet_v2.preprocess_input` is actually a
pass-through function. In this use case, EfficientNetV2 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 EfficientNetV2 models expect their inputs to be
float tensors of pixels with values in the `[-1, 1]` range.

Args:
    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_shape: Optional shape tuple, only to be specified
        if `include_top` is `False`.
        It should have exactly 3 inputs channels.
    pooling: Optional pooling mode for feature extraction
        when `include_top` is `False`. Defaults to None.
        - `None` means that the output of the model will be
            the 4D tensor output of the
            last convolutional layer.
        - `"avg"` means that global average pooling
            will be applied to the output of the
            last convolutional layer, 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 (number of
        ImageNet classes).
    classifier_activation: A string 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.
        Defaults to `"softmax"`.
        When loading pretrained weights, `classifier_activation` can only
        be `None` or `"softmax"`.
    name: The name of the model (string).

Returns:
    A model instance.
c                 p    | |z  } |xs |}t        |t        | |dz  z         |z  |z        }t        |      S )z2Round number of filters based on depth multiplier.r!   )maxint)filterswidth_coefficient	min_depthdepth_divisorminimum_depthnew_filterss         V/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/applications/efficientnet_v2.pyround_filtersra   M  sM      G.MGma''(M9MIK {    c                 D    t        t        j                  || z              S )z2Round number of repeats based on depth multiplier.)rY   mathceilrepeatsdepth_coefficients     r`   round_repeatsri   X  s    tyy*W4566rb   c
                     	 t        j                         dk(  rdnd	t        j                  d      	 	fd}
|
S )z2MBConv block: Mobile Inverted Residual Bottleneck.channels_lastr    r#   block0c                 "   
	z  }	dk7  r~ t        j                  |ddt        dt        j                         ddz         |       } t        j
                  dz         |      } t        j                  dz   	      |      }n| } t        j                  t        dt        j                         dd
z         |      } t        j
                  dz         |      } t        j                  dz   	      |      }dcxk  rdk  rn nt        dt        
z              } t        j                  dz   	      |      }dk(  r|ddf}ndd|f} t        j                  |dz   	      |      } t        j                  |ddt        dz         |      } t        j                  |dddt        dz         |      }t        j                  ||gdz   	      } t        j                  ddt        dt        j                         ddz         |      } t        j
                  dz         |      }dk(  rD
k(  r?r! t        j                  ddz         |      }t        j                  || gdz   	      }|S )Nr#   sameFexpand_convrZ   r%   r+   kernel_initializerpaddingdata_formatuse_biasname	expand_bnaxismomentumru   expand_activationru   dwconv2)r%   r+   depthwise_initializerrr   rs   rt   ru   bn
activationr   
se_squeeze
se_reshape	se_reducerr   r   rq   ru   sigmoid	se_expand	se_exciteproject_conv
project_bnNr#   r#   r#   dropnoise_shaperu   add)r   Conv2DCONV_KERNEL_INITIALIZERr   image_data_formatBatchNormalization
ActivationDepthwiseConv2DrX   rY   GlobalAveragePooling2DReshapemultiplyDropoutr   inputsrZ   x
filters_sesese_shaper   bn_axisbn_momentumr)   r'   r%   ru   r(   r*   r+   survival_probabilitys         r`   applyzMBConvBlock.<locals>.applyo  s   ,.1	#:#557M)	 	A))$K' 	A
 O!!*4:M3MNA A
F""#"9113	!
 
F%%;TD[

 DFjtl/BCAF x1QMH$< =>JH..D<4GHKB!|#Q?q'?Ctl/BCBGB%#:K' B$#:K' B Bd[.@AA	
FMM"6113&	
 	
F%%;TL5H

 a<M^;#FNN( / 	
 

Av;TE\:Arb   r   r   get_uidr'   r(   r)   r%   r+   r*   r   r   r   ru   r   r   s   `````````` @r`   MBConvBlockr   ]  sD     ,,./AaqG|x(] ]~ Lrb   c
                     	 t        j                         dk(  rdnd	t        j                  d      	 	fd}
|
S )z8Fuses the proj conv1x1 and depthwise_conv into a conv2d.rk   r    r#   rl   c                 j   
	z  }	dk7  r~ t        j                  |t        t        j                         dddz         |       } t        j
                  dz         |      } t        j                  dz   	      |      }n| }d
cxk  rdk  rn nt        dt        
z              } t        j                  dz         |      }dk(  r|ddf}ndd|f} t        j                  |dz         |      } t        j                  |ddt        dz         |      } t        j                  |dddt        dz         |      }t        j                  ||gdz         } t        j                  	dk7  rdn	dk7  rdnt        dddz         |      } t        j
                  dz         |      }	dk(  r  t        j                  dz   	      |      }dk(  rD
k(  r?r! t        j                  ddz         |      }t        j                  || gdz         }|S )Nr#   rn   Fro   )r%   r+   rq   rs   rr   rt   ru   rv   rw   rz   r   ru   r   r   r{   r   r   r   r   r   r   r   )r%   r+   rq   rr   rt   ru   r   project_activationr   r   r   r   )r   r   r   r   r   r   r   rX   rY   r   r   r   r   r   r   s         r`   r   zFusedMBConvBlock.<locals>.apply  s   ,.1	'#:#557M)	 	A)){9KA!!%D3F,FA A x1QMH$< =>JH..D<4GHKB!|#Q?q'?Ctl/BCBGB%#:K' B$#:K' B Bd[.@AA
FMM)Q.K%*A6&
 
F%%;TL5H

 1!!%D3G,GA
 a<M^;#FNN( / 	
 

Av;TE\:Arb   r   r   s   `````````` @r`   FusedMBConvBlockr     sD     ,,./AaqG|x(O Ob Lrb   c                 
   |	dk(  r	t         |
   }	|dv s#t        j                  |      st        d|       |dk(  r|r|dk7  rt        d      t	        j
                  ||dt        j                         ||      }|t        j                  |	      }n/t        j                  |      st        j                  ||
      }n|}t        j                         dk(  rdnd}|}|r||dz
     }|
j                  d      d   j                  d      rx|dk(  rs t        j                  d      |      }t        j                         dk(  rdggdggdgggg}dggdggdgggg}ng d}g d} t        j                  |||      |      }n t        j                  dd      |      }t        |	d   d    | ||!      } t        j                   |dd"t"        d#d$d%&      |      } t        j$                  ||d'(      |      } t        j&                  |d)*      |      }t)        j*                  |	      }	d}t-        t/        d+ |	D                    }t1        |	      D ]  \  }}|d,   dkD  sJ t        |d    | ||!      |d <   t        |d-   | ||!      |d-<   t2        t4        d.|j7                  d/         }t9        |j7                  d,      |0      } t;        |       D ]G  }!|!dkD  rd|d1<   |d-   |d <     |dH||||z  |z  d2|dz    t=        |!d3z          d4d5||      }|dz  }I  t        d6| ||!      }" t        j                   |"ddt"        d#t        j                         d$d78      |      } t        j$                  ||d9(      |      } t        j&                  |d:;      |      }|r t        j>                  d<*      |      }|dkD  r t        j@                  |d=*      |      }t	        jB                  ||        t        jD                  ||tF        tI        jJ                  d>      d?@      |      }nC|dAk(  r t        j>                  d<*      |      }n!|dBk(  r t        jL                  dC*      |      }|tO        jP                  |      }#n|}#tS        |#||
*      }$|dk(  rW|rdD}%tT        |   d   }&ndE}%tT        |   d   }&|
|%z   }'t        jV                  |'tX        |'z   dF|&G      }(|$j[                  |(       |$S ||$j[                  |       |$S )Ia	  Instantiates the EfficientNetV2 architecture using given scaling
    coefficients.

    Args:
        width_coefficient: float, scaling coefficient for network width.
        depth_coefficient: float, scaling coefficient for network depth.
        default_size: integer, default input image size.
        dropout_rate: float, dropout rate before final classifier layer.
        drop_connect_rate: float, dropout rate at skip connections.
        depth_divisor: integer, a unit of network width.
        min_depth: integer, minimum number of filters.
        bn_momentum: float. Momentum parameter for Batch Normalization layers.
        activation: activation function.
        blocks_args: list of dicts, parameters to construct block modules.
        name: string, model name.
        include_top: whether to include the fully-connected layer at the top of
            the network.
        weights: 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()`) or
            numpy array to use as image input for the model.
        input_shape: optional shape tuple, only to be specified if `include_top`
            is `False`. It should have exactly 3 inputs channels.
        pooling: 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 layer.
            - "avg" means that global average pooling will be applied to
                the output of the last convolutional layer,
                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.
        classifier_activation: A string 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.
        include_preprocessing: Boolean, whether to include the preprocessing
            layer (`Rescaling`) at the bottom of the network.
            Defaults to `True`.

    Returns:
        A model instance.
    default>   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     zRIf using `weights="imagenet"` with `include_top` as true, `classes` should be 1000r<   )default_sizemin_sizers   require_flattenweights)shape)tensorr   rk   r    r#   -bgp?)rQ   channels_first
ףp=
?v/?Cl?g٪?.?Q?)r   r   r   )r   r   r   )meanvariancerx   g      ?)rQ   offsetr   r'   )rZ   r[   r\   r]   r!   rn   F	stem_conv)rZ   r%   r+   rq   rr   rt   ru   stem_bnrw   stem_activationr{   c              3   &   K   | ]	  }|d      yw)r&   N ).0argss     r`   	<genexpr>z!EfficientNetV2.<locals>.<genexpr>  s     BdtL)Bs   r&   r(   )r   r#   r,   rf   r+   blocka   _)r   r   r   ru   i   top_convrp   top_bntop_activationr   avg_pooltop_dropoutr$   predictions)r   rq   bias_initializerru   avgrX   max_poolz.h5z	_notop.h5models)cache_subdir	file_hashr   ).DEFAULT_BLOCKS_ARGSr
   exists
ValueErrorr   obtain_input_shaper   r   r   Inputis_keras_tensorsplit
startswith	RescalingNormalizationra   r   r   r   r   copydeepcopyfloatsum	enumerater   r   popri   rangechrr   r   validate_activationDenseDENSE_KERNEL_INITIALIZERr   ConstantGlobalMaxPooling2Dr	   get_source_inputsr   WEIGHTS_HASHESget_fileBASE_WEIGHTS_PATHload_weights))r[   rh   r   dropout_ratedrop_connect_rater]   r\   r   r   blocks_argsru   include_topr   input_tensorinput_shapepoolingclassesclassifier_activationinclude_preprocessingweights_name	img_inputr   r   num_channelsr   r   stem_filtersr   blocksir   r   rg   jtop_filtersr   modelfile_suffixr   	file_nameweights_paths)                                            r`   EfficientNetV2r  7  s   J i)$/))Z->->w-G! ")		+
 	
 *D1
 	
 !33!--/#K LL{3	&&|4LLI$I,,./AaqGA #7Q;/::c?2))#.<13D1  y1!4A((*.>> 'eWIy9:j\XJ<8*> -9$$! 	A ?  {2>qAA !A/+#	L	2	 		A	!!	 			A
 	>*+<=a@A --,K	A3BkBBCF[) $4L!A%%% !.)/'	!
_ "/)*/'	"
  $45dhh{6KLHH\*>O
 w 	A 1u"#Y(,-=(>_% %'%6%:V%CQUGCBK=2	
  A FA	-$N  +#	K		2--/		 			A	!!	 			A
 	HZ6FGJA:F))z:1=!@|-@CA**+@'J
FLL,7)2237
  e>--:>qAA:))z:1=A  22<@ vqt,E *K&|4Q7I%K&|4Q7I;&	!**	)!	
 	<( L 
	7#Lrb   z3keras.applications.efficientnet_v2.EfficientNetV2B0z#keras.applications.EfficientNetV2B0c	                 2    t        ddd|| |||||||d      S )N      ?rA   r   r[   rh   r   ru   r   r   r   r   r   r   r   r   r   r  	r   r   r   r   r   r   r   r   ru   s	            r`   EfficientNetV2B0r  F  8    " !33 rb   z3keras.applications.efficientnet_v2.EfficientNetV2B1z#keras.applications.EfficientNetV2B1c	                 2    t        ddd|| |||||||d      S )Nr  皙?   r   r  r  r  s	            r`   EfficientNetV2B1r  h  r  rb   z3keras.applications.efficientnet_v2.EfficientNetV2B2z#keras.applications.EfficientNetV2B2c	                 2    t        ddd|| |||||||d      S )Nr  333333?i  r   r  r  r  s	            r`   EfficientNetV2B2r    r  rb   z3keras.applications.efficientnet_v2.EfficientNetV2B3z#keras.applications.EfficientNetV2B3c	                 2    t        ddd|| |||||||d      S )Nr  gffffff?i,  r   r  r  r  s	            r`   EfficientNetV2B3r    r  rb   z2keras.applications.efficientnet_v2.EfficientNetV2Sz"keras.applications.EfficientNetV2Sc	                 2    t        ddd|| |||||||d      S )Nr  rC   r   r  r  r  s	            r`   EfficientNetV2Sr    r  rb   z2keras.applications.efficientnet_v2.EfficientNetV2Mz"keras.applications.EfficientNetV2Mc	                 2    t        ddd|| |||||||d      S )Nr    r   r  r  r  s	            r`   EfficientNetV2Mr     r  rb   z2keras.applications.efficientnet_v2.EfficientNetV2Lz"keras.applications.EfficientNetV2Lc	                 2    t        ddd|| |||||||d      S )Nr  r  r   r  r  r  s	            r`   EfficientNetV2Lr"    r  rb   r  r{   r  r  r  r  r   r"  z3keras.applications.efficientnet_v2.preprocess_inputc                     | S )a  A placeholder method for backward compatibility.

    The preprocessing logic has been included in the EfficientNetV2 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. Defaults to
            None, in which case the global setting
            `keras.backend.image_data_format()` is used
            (unless you changed it, it defaults to "channels_last").{mode}

    Returns:
        Unchanged `numpy.array` or tensor.
    r   )r   rs   s     r`   preprocess_inputr$  =  s	    & Hrb   z5keras.applications.efficientnet_v2.decode_predictionsc                 0    t        j                  | |      S )N)top)r   decode_predictions)predsr&  s     r`   r'  r'  S  s    ,,U<<rb   )r#   r    r#   r$   ?swishg?N)皙?r+  rF   rF   r)  r*  r   efficientnetv2Tr   NNNr   softmaxTN)	Tr   NNNr   r-  TrJ   )	Tr   NNNr   r-  TrK   )	Tr   NNNr   r-  TrL   )	Tr   NNNr   r-  TrM   )	Tr   NNNr   r-  TrG   )	Tr   NNNr   r-  TrH   )	Tr   NNNr   r-  TrI   )N)r6   )&r   rd   	keras.srcr   r   r   keras.src.api_exportr   keras.src.applicationsr   keras.src.modelsr   keras.src.opsr	   keras.src.utilsr
   r   r   r   r   r   BASE_DOCSTRINGra   ri   r   r   r  r  r  r  r  r  r   r"  format__doc__r$  r'  r   rb   r`   <module>r7     sg	      "  - 1 ' ) &c 3D  		
  		
  		
 !		
  !		
  !		
g=@  		
  		
  		
 !		
  !		
  !		
  !		
{GT  		
  		
  		
 !		
  !		
  !		
  !		
{GT  		
  		
  		
  		
 !		
  !		
g=@  		
  		
  		
  		
 !		
  !		
g=@  		
  		
  		
  		
 !		
  !		
g=@  		
  		
  		
  		
 !		
  !		
g=SG T $*  $! AH7 	qn 	cT 	#)L^ =- #	8 =- #	8 =- #	8 =- #	8 <, #	8 <, #	8 <, #	8 *006H0I  )006H0I  )006H0I  )006H0I  (//5F/G (//5F/G (//5F/G  CD E* EF= G= ,>>FF  rb   