a
    SicC                     @   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d5dd Zed!d"d6d#d$Zed%d&d7d'd(Z ed)d8d*d+Z!ed,d9d.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 ):zDenseNet 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.h57densenet121_weights_tf_dim_ordering_tf_kernels_notop.h51densenet169_weights_tf_dim_ordering_tf_kernels.h57densenet169_weights_tf_dim_ordering_tf_kernels_notop.h51densenet201_weights_tf_dim_ordering_tf_kernels.h57densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5c                 C   s0   t |D ]"}t| d|d t|d  d} q| S )zA 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   W/var/www/html/django/DPS/env/lib/python3.9/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 )zA 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   F_convuse_biasr      _poolstridesr   )	r   image_data_formatlayersBatchNormalization
ActivationConv2Dint	int_shapeAveragePooling2D)r   	reductionr   bn_axisr   r   r   transition_blockO   s"    
r5   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 )zA 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_relusameZ_2_conv)paddingr&   r   _concat)r!   r   )r   r+   r,   r-   r.   r/   Concatenate)r   Zgrowth_rater   r4   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 rjt	j
|d
}nt|st	j
||d}n|}t dkrd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|
}
|rt	jd*d|
}
t|| t	j||d+d,|
}
n6|d-krt	jd*d|
}
n|d.krt	jd/d|
}
|d	ur*t|}n|}| g d0krNtj||
d1d}nP| g d2krntj||
d3d}n0| g d4krtj||
d5d}ntj||
d6d}|dkr|r| g d0krtjd7td8d9d:}nB| g d2krtjd;td8d<d:}n | g d4krxtjd=td8d>d:}nd| g d0kr6tjd?t d8d@d:}nB| g d2krXtjdAt!d8dBd:}n | g d4krxtjdCt"d8dDd:}|#| n|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.
    >   Nr<   zThe `weights` argument should be either `None` (random initialization), `imagenet` (pre-training on ImageNet), or the path to the weights file to be loaded.r<   r=   zWIf using `weights` as `"imagenet"` with `include_top` as true, `classes` should be 1000   r   )default_sizemin_sizedata_formatrequire_flattenweightsN)shape)tensorrE   r   r   r   )r   r   rG   )r8   @      r'   Fz
conv1/conv)r*   r&   r   r   zconv1/bnr    r#   z
conv1/relur   )r   r   rJ   Zpool1r)   r   Zconv2g      ?Zpool2Zconv3Zpool3Zconv4Zpool4Zconv5bnavg_poolpredictions)
activationr   avgmaxmax_pool            Zdensenet121rS   rT   r   r   Zdensenet169rS   rT   0   r   Zdensenet201densenetr	   modelsZ 9d60b8095a5708f2dcce2bca79d332c7)cache_subdir	file_hashr   Z d699b8f76981ab1b30698df4c175e90br   Z 1ceb130c1ea1b78c3bf6114dbdfd8807r
   Z 30ee3e1110167f948a6b9946edeeb738r   Z b8c4d4c20dd625c148057b9ff1c1176br   Z c13680b51ded0fb44dff2d8f86ac8bb1)$tfiogfileexists
ValueErrorr   obtain_input_shaper   r+   r,   Inputis_keras_tensorZeroPadding2Dr/   r-   r.   MaxPooling2Dr   r5   GlobalAveragePooling2Dvalidate_activationDenseGlobalMaxPooling2Dr   get_source_inputsr   Modelr   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_TOPload_weights)r   include_toprD   input_tensorinput_shapepoolingclassesclassifier_activation	img_inputr4   r   inputsmodelweights_pathr   r   r   DenseNet   s    I	






r   z'keras.applications.densenet.DenseNet121zkeras.applications.DenseNet121c              	   C   s   t g d| ||||||S )z*Instantiates the Densenet121 architecture.rR   r   rv   rD   rw   rx   ry   rz   r{   r   r   r   DenseNet121Y  s    r   z'keras.applications.densenet.DenseNet169zkeras.applications.DenseNet169c              	   C   s   t g d| ||||||S )z*Instantiates the Densenet169 architecture.rW   r   r   r   r   r   DenseNet169r  s    r   z'keras.applications.densenet.DenseNet201zkeras.applications.DenseNet201c              	   C   s   t g d| ||||||S )z*Instantiates the Densenet201 architecture.rX   r   r   r   r   r   DenseNet201  s    r   z,keras.applications.densenet.preprocess_inputc                 C   s   t j| |ddS )Ntorch)rB   mode)r   preprocess_input)r   rB   r   r   r   r     s    r   z.keras.applications.densenet.decode_predictions   c                 C   s   t j| |dS )N)top)r   decode_predictions)predsr   r   r   r   r     s    r    )r   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__)Tr<   NNNr=   r>   )Tr<   NNNr=   r>   )Tr<   NNNr=   r>   )Tr<   NNNr=   r>   )N)r   ))r   Ztensorflow.compat.v2compatv2r^   kerasr   Zkeras.applicationsr   keras.enginer   keras.layersr   keras.utilsr   r    tensorflow.python.util.tf_exportr   BASE_WEIGHTS_PATHro   rr   rp   rs   rq   rt   r,   r   r5   r   r   r   r   r   r   r   PREPROCESS_INPUT_DOCformatPREPROCESS_INPUT_RET_DOC_TORCHPREPROCESS_INPUT_ERROR_DOCZDOCsetattrr   r   r   r   <module>   s           
 S                     
5