
    2Vhb                        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	Zd
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ddd"d#dddddgZd$d%d&d'd(d)Zd$d*d&d+d(d)Zd,Zg d-Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 dUd.Z	 	 	 	 	 	 	 	 	 	 dVd/Z ed0d1g      	 	 	 	 	 	 	 	 dWd2       Z ed3d4g      	 	 	 	 	 	 	 	 dXd5       Z ed6d7g      	 	 	 	 	 	 	 	 dYd8       Z ed9d:g      	 	 	 	 	 	 	 	 dZd;       Z ed<d=g      	 	 	 	 	 	 	 	 d[d>       Z ed?d@g      	 	 	 	 	 	 	 	 d\dA       Z edBdCg      	 	 	 	 	 	 	 	 d]dD       Z edEdFg      	 	 	 	 	 	 	 	 d^dG       ZejA                  dHI      e_!        ejA                  dJI      e_!        ejA                  dKI      e_!        ejA                  dLI      e_!        ejA                  dMI      e_!        ejA                  dNI      e_!        ejA                  dOI      e_!        ejA                  dPI      e_!         edQ      d_dR       Z" edS      d`dT       Z#ejF                  jB                  e#_!        y)a    N)backend)layers)keras_export)imagenet_utils)
Functional)operation_utils)
file_utilsz2https://storage.googleapis.com/keras-applications/) 902e53a9f72be733fc0bcb005b3ebbac 50bc09e76180e00e4465e1a485ddc09d) 1d254153d4ab51201f1646940f018540 74c4e6b3e1f6a1eea24c589628592432) b15cce36ff4dcbd00b6dd88e7857a6ad 111f8e2ac8aa800a7a99e3239f7bfb39) ffd1fdc53d0ce67064dc6a9c7960ede0 af6d107764bb5b1abb91932881670226) 18c95ad55216b8f92d7e70b3a046e2fc ebc24e6d6c33eaebbd558eafbeedf1ba) ace28f2a6363774853a83a0b21b9421a 38879255a25d3c92d5e44e04ae6cec6f) 165f6e37dce68623721b423839de8be5 9ecce42647a20130c1f39a5d4cb75743) 8c03f828fec3ef71311cd463b6759d99 cbcfe4450ddf6f3ad90b1b398090fe4a)b0b1b2b3b4b5b6b7             Tg      ?)kernel_sizerepeats
filters_infilters_outexpand_ratioid_skipstridesse_ratio            (   P   p         i@  VarianceScaling       @fan_outtruncated_normal)scalemodedistribution)
class_nameconfiggUUUUUU?uniforma
  Instantiates the {name} architecture.

Reference:
- [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](
    https://arxiv.org/abs/1905.11946) (ICML 2019)

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 EfficientNet, input preprocessing is included as part of the model
(as a `Rescaling` layer), and thus
`keras.applications.efficientnet.preprocess_input` is actually a
pass-through function. EfficientNet models expect their inputs to be float
tensors of pixels with values in the `[0-255]` range.

Args:
    include_top: 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. 1000 is how many
        ImageNet classes there are. 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.
        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.
)gZd;O?gy&1?g?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}|f fd	}fd!|} t        j                  d      |      } t        j                  |      |      }|
dk(  rC t        j                  t        D cg c]  }dt        j                  |      z   c}      |      } t        j                   t	        j"                  |d      d      |      } t        j$                   |d      ddddt&        d      |      } t        j(                  |d      |      } t        j*                  |d      |      }t-        j.                  |      }d}t1        t3        !fd|D                    }t5        |      D ]  \  }}|d    dkD  sJ  ||d!         |d!<    ||d"         |d"<   t7         !|j9                  d                   D ]D  }|dkD  rd|d#<   |d"   |d!<   t;        ||||z  |z  fd$d%|dz    t=        |d&z          d'i|}|dz  }F   t        j$                   |d(      dd)dt&        d*+      |      } t        j(                  |d,      |      } t        j*                  |d-      |      }|	rx t        j>                  d.      |      }|dkD  r t        j@                  |d/      |      }t	        jB                  ||
        t        jD                  ||tF        d01      |      }nC|d2k(  r t        j>                  d.      |      }n!|d3k(  r t        jH                  d4      |      }|tK        jL                  |      }n|}tO        |||      }|
dk(  rW|	rd5}tP        |   d   }nd6}tP        |   d   }||z   }t        jR                  |tT        |z   d7|8      } |jW                  |        |S |
|jW                  |
       |S c c}w )9aJ  Instantiates the EfficientNet architecture.

    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.
      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()`)
          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 `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.

    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.rC     zRIf using `weights="imagenet"` with `include_top` as true, `classes` should be 1000r$   )default_sizemin_sizedata_formatrequire_flattenweights)shape)tensorrJ   channels_lastr"   r#   c                     | z  } t        |t        | |dz  z         |z  |z        }|d| z  k  r||z  }t        |      S )z2Round number of filters based on depth multiplier.r.   g?)maxint)filtersdivisornew_filterswidth_coefficients      S/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/applications/efficientnet.pyround_filtersz#EfficientNet.<locals>.round_filters4  sX    $$S7Q;./7:WD
 w&7"K;    c                 F    t        t        j                  | z              S )z2Round number of repeats based on depth multiplier.)rO   mathceil)r'   depth_coefficients    rT   round_repeatsz#EfficientNet.<locals>.round_repeats?  s    499.89::rV   gp?)axis      ?stem_conv_padpaddingnamer.   validF	stem_conv)r,   r`   use_biaskernel_initializerra   stem_bnr\   ra   stem_activationra   r   c              3   4   K   | ]  } |d            yw)r'   N ).0argsr[   s     rT   	<genexpr>zEfficientNet.<locals>.<genexpr>g  s     N$}T)_5Ns   r'   r(   r)   r,   ra   blocka   _i   sametop_convr`   rd   re   ra   top_bntop_activationavg_pooltop_dropoutpredictions)
activationre   ra   avgrN   max_poolz.h5z	_notop.h5models)cache_subdir	file_hash),DEFAULT_BLOCKS_ARGSr	   exists
ValueErrorr   obtain_input_shaper   image_data_formatr   Inputis_keras_tensor	RescalingNormalizationIMAGENET_STDDEV_RGBrX   sqrtZeroPadding2Dcorrect_padConv2DCONV_KERNEL_INITIALIZERBatchNormalization
Activationcopydeepcopyfloatsum	enumeraterangepopro   chrGlobalAveragePooling2DDropoutvalidate_activationDenseDENSE_KERNEL_INITIALIZERGlobalMaxPooling2Dr   get_source_inputsr   WEIGHTS_HASHESget_fileBASE_WEIGHTS_PATHload_weights)"rS   rZ   rE   dropout_ratedrop_connect_ratedepth_divisorrz   blocks_argsra   include_toprI   input_tensorinput_shapepoolingclassesclassifier_activationweights_name	img_inputbn_axisrU   xstddevbblocksirm   jinputsmodelfile_suffixr   	file_nameweights_pathr[   s"   ``                               @rT   EfficientNetr      s   ~ i)))Z->->w-G<
 	
 *D1
 	
 !33!--/#K LL{3	&&|4LLI$I,,./AaqG'4 	 ;
 	A%%a(A*'*1-A*
F3FGS499V$$G

	**1a0			A	b	2	 		A 	@!!wY?BA=*+<=a@A --,K	A3N+NNOF[) 4I"""*4+=>\+D,?@]}TXXi%89: 	A 1u"#Y%)-%8\"!A%. QUGCBK=2	
 A FA	,	d	2	 		A 	?!!wX>qAA<*+;<Q?A:F))z:1=!@|-@CA**+@'J
FLL,7	

  e>--:>qAA:))z:1=A  22<@ vqt,E *K&|4Q7I%K&|4Q7I;&	!**	)!	
 	<( L 
	7#LI Hs   <R
c           
      `   t        j                         dk(  rdnd}||z  }|dk7  ri t        j                  |dddt        |dz         |       } t        j
                  ||dz   	      |      } t        j                  ||d
z         |      }n| }|dk(  r7 t        j                  t        j                  ||      |dz         |      }d}nd} t        j                  |||dt        |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dt        |dz         |      } t        j
                  ||dz   	      |      }|
rL|dk(  rG||k(  rB|dkD  r! t        j                   |d|dz          |      }t        j"                  || g|d!z         }|S )"a  An inverted residual block.

    Args:
        inputs: input tensor.
        activation: activation function.
        drop_rate: float between 0 and 1, fraction of the input units to drop.
        name: string, block label.
        filters_in: integer, the number of input filters.
        filters_out: integer, the number of output filters.
        kernel_size: integer, the dimension of the convolution window.
        strides: integer, the stride of the convolution.
        expand_ratio: integer, scaling coefficient for the input filters.
        se_ratio: float between 0 and 1, fraction to squeeze the input filters.
        id_skip: boolean.

    Returns:
        output tensor for the block.
    rL   r"   r#   rr   Fexpand_convrt   	expand_bnrg   expand_activationri   r.   
dwconv_padr_   rb   dwconv)r,   r`   rd   depthwise_initializerra   bnrz   r   
se_squeeze
se_reshape	se_reduce)r`   rz   re   ra   sigmoid	se_expand	se_exciteproject_conv
project_bn)Nr#   r#   r#   drop)noise_shapera   add)r   r   r   r   r   r   r   r   r   r   DepthwiseConv2DrN   rO   r   Reshapemultiplyr   r   )r   rz   	drop_ratera   r(   r)   r&   r,   r*   r-   r+   r   rP   r   conv_pad
filters_sesese_shapes                     rT   ro   ro     s   > ,,./AaqG <'Gq
FMM6%
  MF%%79KLQOJFjt6I/IJ1M !|
F  "..q+>$
  	5H_	 		A 	B!!wTD[A!DA?*4,+>?BA 	8qC
X 567
DV**|0CDQGa<AH1gH?V^^H4,+>?C
V]]!6#
 
V]] 6#
  OOQG$*<=		2N"	 		A 	J!!wTL5HI!LA7a<J+$=q=TF]A JJ6{6HrV   z.keras.applications.efficientnet.EfficientNetB0z!keras.applications.EfficientNetB0c                 2    t        dddd|| ||||||d      S )Nr]      皙?r   	ra   r   rI   r   r   r   r   r   r   r   r   rI   r   r   r   r   r   ra   s           rT   EfficientNetB0r   +  8      !3 rV   z.keras.applications.efficientnet.EfficientNetB1z!keras.applications.EfficientNetB1c                 2    t        dddd|| ||||||d      S )Nr]   皙?   r   r   r   r   r   s           rT   EfficientNetB1r   L  r   rV   z.keras.applications.efficientnet.EfficientNetB2z!keras.applications.EfficientNetB2c                 2    t        dddd|| ||||||d      S )Nr   333333?i  333333?r   r   r   r   s           rT   EfficientNetB2r   m  r   rV   z.keras.applications.efficientnet.EfficientNetB3z!keras.applications.EfficientNetB3c                 2    t        dddd|| ||||||d      S )Nr   ffffff?i,  r   r   r   r   r   s           rT   EfficientNetB3r     r   rV   z.keras.applications.efficientnet.EfficientNetB4z!keras.applications.EfficientNetB4c                 2    t        dddd|| ||||||d      S )Nr   ?i|  皙?r   r   r   r   s           rT   EfficientNetB4r     r   rV   z.keras.applications.efficientnet.EfficientNetB5z!keras.applications.EfficientNetB5c                 2    t        dddd|| ||||||d      S )Ng?g@i  r   r   r   r   r   s           rT   EfficientNetB5r     r   rV   z.keras.applications.efficientnet.EfficientNetB6z!keras.applications.EfficientNetB6c                 2    t        dddd|| ||||||d      S )Nr   g@i        ?r    r   r   r   s           rT   EfficientNetB6r     r   rV   z.keras.applications.efficientnet.EfficientNetB7z!keras.applications.EfficientNetB7c                 2    t        dddd|| ||||||d      S )Nr8   g@iX  r   r!   r   r   r   s           rT   EfficientNetB7r     r   rV   r   ri   r   r   r   r   r   r   r   z0keras.applications.efficientnet.preprocess_inputc                     | S )a  A placeholder method for backward compatibility.

    The preprocessing logic has been included in the efficientnet 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.backend.image_data_format()`
            is used (unless you changed it, it uses `"channels_last"`).
            Defaults to `None`.

    Returns:
        Unchanged `numpy.array` or tensor.
    rk   )r   rG   s     rT   preprocess_inputr   =  s	    & HrV   z2keras.applications.efficientnet.decode_predictionsc                 0    t        j                  | |      S )N)top)r   decode_predictions)predsr   s     rT   r   r   S  s    ,,U<<rV   )r   r      swishrB   efficientnetTrC   NNNrD   softmaxN)
r            r$   r%   r"   r#   r#   r   T)TrC   NNNrD   r   efficientnetb0)TrC   NNNrD   r   efficientnetb1)TrC   NNNrD   r   efficientnetb2)TrC   NNNrD   r   efficientnetb3)TrC   NNNrD   r   efficientnetb4)TrC   NNNrD   r   efficientnetb5)TrC   NNNrD   r   efficientnetb6)TrC   NNNrD   r   efficientnetb7)N)r1   )$r   rX   	keras.srcr   r   keras.src.api_exportr   keras.src.applicationsr   keras.src.modelsr   keras.src.opsr   keras.src.utilsr	   r   r   r   r   r   BASE_DOCSTRINGr   r   ro   r   r   r   r   r   r   r   r   format__doc__r   r   rk   rV   rT   <module>r     s       - 1 ' ) &H ;!J 	 	 	 	 	 	 	{G T $*  $! >B ,  	##eT 	pf 8+ #	6 8+ #	6 8+ #	6 8+ #	6 8+ #	6 8+ #	6 8+ #	6 8+ #	6 (..4D.E '..4D.E '..4D.E '..4D.E '..4D.E '..4D.E '..4D.E '..4D.E  @A B* BC= D= ,>>FF  rV   