a
    SicV                  	   @   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dddddddddZdadFddZdGddZdHd d!ZdId#d$ZdJd%d&ZdKd(d)ZdLd*d+Zed,d-d.dMd/d0Zed1d2dNd3d4Zed5d6dOd7d8Zed9d:dPd;d<Zed=d>dQd@dAZ ej!j"dBej#ej$dCe_ ej j e _ dDZ%e&edEej e%  e&edEej e%  e&edEej e%  dS )RzResNet models for Keras.

Reference:
  - [Deep Residual Learning for Image Recognition](
      https://arxiv.org/abs/1512.03385) (CVPR 2015)
    N)backend)imagenet_utils)training)VersionAwareLayers)
data_utils)layer_utils)keras_exportzDhttps://storage.googleapis.com/tensorflow/keras-applications/resnet/)Z 2cb95161c43110f7111970584f804107Z 4d473c1dd8becc155b73f8504c6f6626)Z f1aeb4b969a6efcfb50fad2f0c20cfc5Z 88cf7a10940856eca736dc7b7e228a21)Z 100835be76be38e30d865e96f2aaae62Z ee4c566cf9a93f14d82f913c2dc6dd0c)Z 3ef43a0b657b3be2300d5770ece849e0Z fac2f116257151a9d068a22e544a4917)Z 6343647c601c52e1368623803854d971Z c0ed64b8031c3730f411d2eb4eea35b5)Z a49b44d1979771252814e80f8ec446f9Z ed17cf2e0169df9d443503ef94b23b33)Z 67a5b30d522ed92f75a1f16eef299d1aZ 62527c363bdd9ec598bed41947b379fc)Z 34fb605428fcc7aa4d62f44404c11509Z 0f678c91647380debd923963594981b3)resnet50	resnet101	resnet152Z
resnet50v2Zresnet101v2Zresnet152v2Z	resnext50Z
resnext101resnetTimagenet  softmaxc                 K   s  d|v r| dant a|r,td|f |dv sJtjj|sJtd|dkrf|rf|	dkrftdtj	|dd	t
 ||d
}|du rtj|d}nt
|stj||d}n|}t
 dkrdnd}tjddd|}tjddd|dd|}|stj|ddd|}tjddd|}tjdd d|}tjddd!d"|}| |}|rxt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rtjd*d|}|durt|}n|}tj|||d}|dkrv|tv rv|r@|d+ }t| d, }n|d- }t| d }tj|t| d.|d/}|| n|dur|| |S )0a  Instantiates the ResNet, ResNetV2, and ResNeXt architecture.

    Args:
      stack_fn: a function that returns output tensor for the
        stacked residual blocks.
      preact: whether to use pre-activation or not
        (True for ResNetV2, False for ResNet and ResNeXt).
      use_bias: whether to use biases for convolutional layers or not
        (True for ResNet and ResNetV2, False for ResNeXt).
      model_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 (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.
      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.
        When loading pretrained weights, `classifier_activation` can only
        be `None` or `"softmax"`.
      **kwargs: For backwards compatibility only.

    Returns:
      A `keras.Model` instance.
    layerszUnknown argument(s): %s>   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       )default_sizemin_sizedata_formatrequire_flattenweightsN)shape)tensorr   channels_last      )r   r   r   Z	conv1_padpaddingname@         Z
conv1_convstridesuse_biasr    >conv1_bnaxisepsilonr    relu
conv1_relur    r   r   r0   Z	pool1_padZ
pool1_poolr%   r    Zpost_bnZ	post_reluavg_poolpredictions)
activationr    avgmaxmax_poolz&_weights_tf_dim_ordering_tf_kernels.h5r   z,_weights_tf_dim_ordering_tf_kernels_notop.h5models)cache_subdir	file_hash) popr   r   
ValueErrortfiogfileexistsr   obtain_input_shaper   image_data_formatInputis_keras_tensorZeroPadding2DConv2DBatchNormalization
ActivationMaxPooling2DGlobalAveragePooling2Dvalidate_activationDenseGlobalMaxPooling2Dr   get_source_inputsr   ModelWEIGHTS_HASHESr   get_fileBASE_WEIGHTS_PATHload_weights)stack_fnpreactr&   
model_nameinclude_topr   input_tensorinput_shapepoolingclassesclassifier_activationkwargs	img_inputbn_axisxinputsmodel	file_namer:   weights_path re   U/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/applications/resnet.pyResNetL   s    >	





rg   r   r   c                 C   sJ  t  dkrdnd}|rPtjd| d||d d| }tj|d|d d	|}n| }tj|d||d
 d| } tj|d|d d	| } tjd|d d| } tj||d|d d| } tj|d|d d	| } tjd|d d| } tjd| d|d d| } tj|d|d d	| } tj|d d|| g} tjd|d d| } | S )a  A residual block.

    Args:
      x: input tensor.
      filters: integer, filters of the bottleneck layer.
      kernel_size: default 3, kernel size of the bottleneck layer.
      stride: default 1, stride of the first layer.
      conv_shortcut: default True, use convolution shortcut if True,
          otherwise identity shortcut.
      name: string, block label.

    Returns:
      Output tensor for the residual block.
    r   r   r      _0_convr1   r'   _0_bnr)   _1_conv_1_bnr,   _1_relur.   SAME_2_convr   _2_bn_2_relu_3_conv_3_bn_add_out)r   rB   r   rF   rG   rH   Add)r`   filterskernel_sizestrideconv_shortcutr    r_   shortcutre   re   rf   block1   sP    



r|   r#   c                 C   sH   t | |||d d} td|d D ] }t | |d|d t| d} q"| S )ad  A set of stacked residual blocks.

    Args:
      x: input tensor.
      filters: integer, filters of the bottleneck layer in a block.
      blocks: integer, blocks in the stacked blocks.
      stride1: default 2, stride of the first layer in the first block.
      name: string, stack label.

    Returns:
      Output tensor for the stacked blocks.
    _block1ry   r    r#   r   F_blockrz   r    )r|   rangestrr`   rw   blocksstride1r    ire   re   rf   stack1(  s    r   Fc           	      C   sf  t  dkrdnd}tj|d|d d| }tjd|d d	|}|rftjd
| d||d d|}n|dkrtjd|d| n| }tj|ddd|d d|} tj|d|d d| } tjd|d d	| } tjd|d d| } tj|||d|d d| } tj|d|d d| } tjd|d d	| } tjd
| d|d d	| } tj|d d	|| g} | S )a  A residual block.

    Args:
        x: input tensor.
        filters: integer, filters of the bottleneck layer.
        kernel_size: default 3, kernel size of the bottleneck layer.
        stride: default 1, stride of the first layer.
        conv_shortcut: default False, use convolution shortcut if True,
          otherwise identity shortcut.
        name: string, block label.

    Returns:
      Output tensor for the residual block.
    r   r   r   r'   Z
_preact_bnr)   r,   Z_preact_relur.   rh   ri   r1   )r%   Frk   r$   rl   rm   r/   _2_padr   ro   rp   rq   rr   ru   )	r   rB   r   rG   rH   rF   rI   rE   rv   )	r`   rw   rx   ry   rz   r    r_   rU   r{   re   re   rf   block2=  sZ    


r   c                 C   s^   t | |d|d d} td|D ]}t | ||d t| d} qt | |||d t| d} | S )ap  A set of stacked residual blocks.

    Args:
        x: input tensor.
        filters: integer, filters of the bottleneck layer in a block.
        blocks: integer, blocks in the stacked blocks.
        stride1: default 2, stride of the first layer in the first block.
        name: string, stack label.

    Returns:
        Output tensor for the stacked blocks.
    Tr}   r   r#   r   r.   r~   )r   r   r   r   re   re   rf   stack2v  s
    r   r   c           
   	      s  t  dkrdnd}|rVtjd| | d|d|d d| }tj|d|d	 d
|}n| }tj|dd|d d| } tj|d|d d
| } tjd|d d| } ||  tjd|d d| } tj|| d|d d| } t | dd }	t 	| t 
|	|  fg} tj fdd|d d| } t 	| t 
|	|fg} 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|| g} tjd|d  d| } | S )!a  A residual block.

    Args:
      x: input tensor.
      filters: integer, filters of the bottleneck layer.
      kernel_size: default 3, kernel size of the bottleneck layer.
      stride: default 1, stride of the first layer.
      groups: default 32, group size for grouped convolution.
      conv_shortcut: default True, use convolution shortcut if True,
          otherwise identity shortcut.
      name: string, block label.

    Returns:
      Output tensor for the residual block.
    r   r   r   r!   Fri   r$   r'   rj   r)   rk   )r&   r    rl   r,   rm   r.   r/   r   r   ro   )r%   depth_multiplierr&   r    Nc                    s   t  fddtD S )Nc                 3   s0   | ](} d d d d d d d d |f V  qd S )Nre   ).0r   r`   re   rf   	<genexpr>      z+block3.<locals>.<lambda>.<locals>.<genexpr>)sumr   r   cr   rf   <lambda>  r   zblock3.<locals>.<lambda>Z	_2_reducerp   rq   rr   rs   rt   ru   )r   rB   r   rF   rG   rH   rE   DepthwiseConv2Dr   reshapeconcatenateLambdarv   )
r`   rw   rx   ry   groupsrz   r    r_   r{   x_shapere   r   rf   block3  s~    





r   c              	   C   sL   t | ||||d d} td|d D ]"}t | ||d|d t| d} q$| S )a  A set of stacked residual blocks.

    Args:
      x: input tensor.
      filters: integer, filters of the bottleneck layer in a block.
      blocks: integer, blocks in the stacked blocks.
      stride1: default 2, stride of the first layer in the first block.
      groups: default 32, group size for grouped convolution.
      name: string, stack label.

    Returns:
      Output tensor for the stacked blocks.
    r}   )ry   r   r    r#   r   Fr   )r   rz   r    )r   r   r   )r`   rw   r   r   r   r    r   re   re   rf   stack3  s    r   z$keras.applications.resnet50.ResNet50z"keras.applications.resnet.ResNet50zkeras.applications.ResNet50c                 K   s*   dd }t |ddd| |||||f
i |S )z'Instantiates the ResNet50 architecture.c                 S   sB   t | ddddd} t | dddd	} t | d
ddd	} t | dddd	S )Nr!   r   r   conv2r   r       rh   conv3r.         conv4   conv5r   r   re   re   rf   rT     s    zResNet50.<locals>.stack_fnFTr	   rg   rW   r   rX   rY   rZ   r[   r]   rT   re   re   rf   ResNet50  s    r   z#keras.applications.resnet.ResNet101zkeras.applications.ResNet101c                 K   s*   dd }t |ddd| |||||f
i |S )z(Instantiates the ResNet101 architecture.c                 S   sB   t | ddddd} t | dddd	} t | d
ddd	} t | dddd	S )Nr!   r   r   r   r   r   rh   r   r.   r      r   r   r   r   r   re   re   rf   rT   &  s    zResNet101.<locals>.stack_fnFTr
   r   r   re   re   rf   	ResNet101  s    r   z#keras.applications.resnet.ResNet152zkeras.applications.ResNet152c                 K   s*   dd }t |ddd| |||||f
i |S )z(Instantiates the ResNet152 architecture.c                 S   sB   t | ddddd} t | dddd	} t | d
ddd	} t | dddd	S )Nr!   r   r   r   r   r      r   r.   r   $   r   r   r   r   r   re   re   rf   rT   I  s    zResNet152.<locals>.stack_fnFTr   r   r   re   re   rf   	ResNet152;  s    r   z,keras.applications.resnet50.preprocess_inputz*keras.applications.resnet.preprocess_inputc                 C   s   t j| |ddS )Ncaffe)r   mode)r   preprocess_input)r`   r   re   re   rf   r   ^  s    r   z.keras.applications.resnet50.decode_predictionsz,keras.applications.resnet.decode_predictions   c                 C   s   t j| |dS )N)top)r   decode_predictions)predsr   re   re   rf   r   h  s    r    )r   reterrora9
  

  Reference:
  - [Deep Residual Learning for Image Recognition](
      https://arxiv.org/abs/1512.03385) (CVPR 2015)

  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 ResNet, call `tf.keras.applications.resnet.preprocess_input` on your
  inputs before passing them to the model.
  `resnet.preprocess_input` will convert the input images from RGB to BGR,
  then will zero-center each color channel with respect to the ImageNet dataset,
  without scaling.

  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__)r   Tr   NNNr   r   )r   r   TN)r#   N)r   r   FN)r#   N)r   r   r   TN)r#   r   N)Tr   NNNr   )Tr   NNNr   )Tr   NNNr   )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   rR   rP   r   rg   r|   r   r   r   r   r   r   r   r   r   r   PREPROCESS_INPUT_DOCformatPREPROCESS_INPUT_RET_DOC_CAFFEPREPROCESS_INPUT_ERROR_DOCDOCsetattrre   re   re   rf   <module>   s   #        
 *
3

9
     
O
                     
<