o
    Me°C  ã                   @   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
Zed Zed Zed Zed Zed Zed Zeƒ Zdd„ Zdd„ Zdd„ Z							d4dd„Zeddƒ							d4dd „ƒZed!d"ƒ							d4d#d$„ƒZed%d&ƒ							d4d'd(„ƒZ ed)ƒd5d*d+„ƒZ!ed,ƒd6d.d/„ƒZ"ej#j$d0ej%ej&d1�e!_ ej"j e"_ d2Z'e(ed3ej e' ƒ e(ed3ej e' ƒ e(e d3e j e' ƒ dS )7z‹DenseNet models for Keras.

Reference:
  - [Densely Connected Convolutional Networks](
      https://arxiv.org/abs/1608.06993) (CVPR 2017)
é    N)Úbackend)Úimagenet_utils)Útraining)ÚVersionAwareLayers)Ú
data_utils)Úlayer_utils)Úkeras_exportzFhttps://storage.googleapis.com/tensorflow/keras-applications/densenet/ú1densenet121_weights_tf_dim_ordering_tf_kernels.h5ú7densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5ú1densenet169_weights_tf_dim_ordering_tf_kernels.h5ú7densenet169_weights_tf_dim_ordering_tf_kernels_notop.h5ú1densenet201_weights_tf_dim_ordering_tf_kernels.h5ú7densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5c                 C   s0   t |ƒD ]}t| d|d t|d ƒ d�} q| S )z½A dense block.

    Args:
      x: input tensor.
      blocks: integer, the number of building blocks.
      name: string, block label.

    Returns:
      Output tensor for the block.
    é    Ú_blocké   ©Úname)ÚrangeÚ
conv_blockÚstr)ÚxÚblocksr   Úi© r   úKD:\Projects\ConvertPro\env\Lib\site-packages\keras/applications/densenet.pyÚdense_block?   s    r   c                 C   sŠ   t  ¡ dkrdnd}tj|d|d d�| ƒ} tjd|d d	�| ƒ} tjtt  | ¡| | ƒdd
|d d�| ƒ} tjdd|d d�| ƒ} | S )zËA transition block.

    Args:
      x: input tensor.
      reduction: float, compression rate at transition layers.
      name: string, block label.

    Returns:
      output tensor for the block.
    Úchannels_lasté   r   çõ›ìþä>Z_bn©ÚaxisÚepsilonr   ÚreluZ_relur   FZ_conv©Úuse_biasr   é   Ú_pool©Ústridesr   )	r   Úimage_data_formatÚlayersÚBatchNormalizationÚ
ActivationÚConv2DÚintZ	int_shapeZAveragePooling2D)r   Z	reductionr   Úbn_axisr   r   r   Útransition_blockO   s"   
ÿþüûr1   c                 C   sÈ   t  ¡ dkrdnd}tj|d|d d�| ƒ}tjd|d d	�|ƒ}tjd
| dd|d d�|ƒ}tj|d|d d�|ƒ}tjd|d d	�|ƒ}tj|ddd|d d�|ƒ}tj||d d�| |gƒ} | S )zÓA building block for a dense block.

    Args:
      x: input tensor.
      growth_rate: float, growth rate at dense layers.
      name: string, block label.

    Returns:
      Output tensor for the block.
    r   r   r   r   Z_0_bnr    r#   Z_0_relur   é   FZ_1_convr$   Z_1_bnZ_1_reluZsameZ_2_conv)Úpaddingr%   r   Z_concat)r!   r   )r   r*   r+   r,   r-   r.   ÚConcatenate)r   Zgrowth_rater   r0   Úx1r   r   r   r   i   s2   
ÿþÿþ
ÿþÿþr   TÚimagenetéè  Úsoftmaxc                 C   sš  |dv st jj |¡stdƒ‚|dkr|r|dkrtdƒ‚tj|ddt ¡ ||d�}|d	u r5t	j
|d
�}nt |¡sBt	j
||d�}n|}t ¡ dkrLdnd}	t	jdd�|ƒ}
t	jdddddd�|
ƒ}
t	j|	ddd�|
ƒ}
t	jddd�|
ƒ}
t	jdd�|
ƒ}
t	jdddd�|
ƒ}
t|
| d  d!d�}
t|
d"d#d�}
t|
| d d$d�}
t|
d"d%d�}
t|
| d d&d�}
t|
d"d'd�}
t|
| d d(d�}
t	j|	dd)d�|
ƒ}
t	jddd�|
ƒ}
|rît	jd*d�|
ƒ}
t ||¡ t	j||d+d,�|
ƒ}
n|d-krût	jd*d�|
ƒ}
n|d.k�rt	jd/d�|
ƒ}
|d	u�rt |¡}n|}| g d0¢k�r%tj||
d1d�}n(| g d2¢k�r5tj||
d3d�}n| g d4¢k�rEtj||
d5d�}ntj||
d6d�}|dk�rÁ|�rˆ| g d0¢k�rftjd7td8d9d:�}nT| g d2¢k�rwtjd;td8d<d:�}nC| g d4¢k�r‡tjd=td8d>d:�}n2| g d0¢k�r™tjd?t d8d@d:�}n!| g d2¢k�rªtjdAt!d8dBd:�}n| g d4¢k�rºtjdCt"d8dDd:�}| #|¡ |S |d	u�rË| #|¡ |S )Eaz  Instantiates the DenseNet architecture.

    Reference:
    - [Densely Connected Convolutional Networks](
        https://arxiv.org/abs/1608.06993) (CVPR 2017)

    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 DenseNet, call `tf.keras.applications.densenet.preprocess_input` on your
    inputs before passing them to the model.
    `densenet.preprocess_input` will scale pixels between 0 and 1 and then
    will normalize each channel with respect to the ImageNet dataset statistics.

    Args:
      blocks: numbers of building blocks for the four dense layers.
      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 (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.
      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 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.
      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"`.

    Returns:
      A `keras.Model` instance.
    >   r6   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.r6   r7   zWIf using `weights` as `"imagenet"` with `include_top` as true, `classes` should be 1000éà   r   )Zdefault_sizeZmin_sizeÚdata_formatZrequire_flattenÚweightsN)Úshape)Ztensorr<   r   r   r   )©r   r   r=   )r3   é@   é   r&   Fz
conv1/conv)r)   r%   r   r   zconv1/bnr    r#   z
conv1/relur   )©r   r   r@   Zpool1r(   r   Zconv2g      à?Zpool2Zconv3Zpool3Zconv4Zpool4Zconv5ZbnZavg_poolZpredictions)Z
activationr   ZavgÚmaxZmax_pool©é   é   é   é   Zdensenet121©rC   rD   r   r   Zdensenet169©rC   rD   é0   r   Zdensenet201Zdensenetr	   ÚmodelsZ 9d60b8095a5708f2dcce2bca79d332c7)Zcache_subdirZ	file_hashr   Z d699b8f76981ab1b30698df4c175e90br   Z 1ceb130c1ea1b78c3bf6114dbdfd8807r
   Z 30ee3e1110167f948a6b9946edeeb738r   Z b8c4d4c20dd625c148057b9ff1c1176br   Z c13680b51ded0fb44dff2d8f86ac8bb1)$ÚtfÚioZgfileÚexistsÚ
ValueErrorr   Zobtain_input_shaper   r*   r+   ZInputZis_keras_tensorZZeroPadding2Dr.   r,   r-   ZMaxPooling2Dr   r1   ZGlobalAveragePooling2DZvalidate_activationZDenseZGlobalMaxPooling2Dr   Zget_source_inputsr   ZModelr   Zget_fileÚDENSENET121_WEIGHT_PATHÚDENSENET169_WEIGHT_PATHÚDENSENET201_WEIGHT_PATHÚDENSENET121_WEIGHT_PATH_NO_TOPÚDENSENET169_WEIGHT_PATH_NO_TOPÚDENSENET201_WEIGHT_PATH_NO_TOPZload_weights)r   Úinclude_topr;   Úinput_tensorÚinput_shapeÚpoolingÚclassesÚclassifier_activationZ	img_inputr0   r   ZinputsÚmodelZweights_pathr   r   r   ÚDenseNet‡   sâ   Iÿÿú	
ÿþÿþ


üüü€üüü

ý
r\   z'keras.applications.densenet.DenseNet121zkeras.applications.DenseNet121c              	   C   ó   t g d¢| ||||||ƒS )z*Instantiates the Densenet121 architecture.rB   ©r\   ©rU   r;   rV   rW   rX   rY   rZ   r   r   r   ÚDenseNet121Y  ó   ør`   z'keras.applications.densenet.DenseNet169zkeras.applications.DenseNet169c              	   C   r]   )z*Instantiates the Densenet169 architecture.rG   r^   r_   r   r   r   ÚDenseNet169r  ra   rb   z'keras.applications.densenet.DenseNet201zkeras.applications.DenseNet201c              	   C   r]   )z*Instantiates the Densenet201 architecture.rH   r^   r_   r   r   r   ÚDenseNet201‹  ra   rc   z,keras.applications.densenet.preprocess_inputc                 C   s   t j| |dd�S )NZtorch)r:   Úmode)r   Úpreprocess_input)r   r:   r   r   r   re   ¤  s   ÿre   z.keras.applications.densenet.decode_predictionsé   c                 C   s   t j| |d�S )N)Útop)r   Údecode_predictions)Úpredsrg   r   r   r   rh   «  s   rh   Ú )rd   ÚretÚerroraý  

  Reference:
  - [Densely Connected Convolutional Networks](
      https://arxiv.org/abs/1608.06993) (CVPR 2017)

  Optionally loads weights pre-trained on ImageNet.
  Note that the data format convention used by the model is
  the one specified in your Keras config at `~/.keras/keras.json`.

  Note: each Keras Application expects a specific kind of input preprocessing.
  For DenseNet, call `tf.keras.applications.densenet.preprocess_input` on your
  inputs before passing them to the model.

  Args:
    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 (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.
    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 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.
    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"`.

  Returns:
    A Keras model instance.
Ú__doc__)Tr6   NNNr7   r8   )N)rf   ))rm   Ztensorflow.compat.v2ÚcompatÚv2rK   Zkerasr   Zkeras.applicationsr   Zkeras.enginer   Zkeras.layersr   Zkeras.utilsr   r   Z tensorflow.python.util.tf_exportr   ZBASE_WEIGHTS_PATHrO   rR   rP   rS   rQ   rT   r+   r   r1   r   r\   r`   rb   rc   re   rh   ZPREPROCESS_INPUT_DOCÚformatZPREPROCESS_INPUT_RET_DOC_TORCHZPREPROCESS_INPUT_ERROR_DOCZDOCÚsetattrr   r   r   r   Ú<module>   s²   ÿÿÿÿÿÿÿÿÿÿ 
ø Sÿùÿùÿùý
5