a
    Sic                     @   s  d 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  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ddddddddd d!d"d#d$d%Zd&gd&d'gg d(d)gd)gd)d*gd*gd+Zd,d-d.d/d0d.d)d1d.d2d-d.gd,d-d.d/d0d.d)d3d.d2d-d.gd,d-d.d/d4d.d)d5d.d2d-d.gd,d-d.d/d6d.d)d5d.d2d-d.gd,d0d.d/d7d.d)d8d.d2d0d.gd,d0d.d/d5d.d)d9d.d2d0d.gd,d0d.d/d:d.d)d;d.d2d0d.gd+Zd<d=d>d?d@dAZdBZddCdDZdeeedHdIdJZ de!ee!dMdNdOZ"de!e!e#eeeeedQdRdSZ$deeeeedTdUdVZ%dWdX Z&e!dYdZd[Z'd\d] Z(de!eeeee!f  eeef dddedfZ)edgdhddidjZ*edkdlddmdnZ+edodpddqdrZ,edsdtddudvZ-edwdxddydzZ.ed{d|dd}d~Z/edddddZ0eddddZ1eddddZ2ej2j e2_ ej3djde*_ ej3dnde+_ ej3drde,_ ej3dvde-_ ej3dzde._ ej3d~de/_ ej3dde0_ dS )zResNet-RS models for Keras.

Reference:
- [Revisiting ResNets: Improved Training and Scaling Strategies](
    https://arxiv.org/pdf/2103.07579.pdf)
    N)Callable)Dict)List)Union)backend)layers)imagenet_utils)training)
data_utils)layer_utils)keras_exportzGhttps://storage.googleapis.com/tensorflow/keras-applications/resnet_rs/Z 544b3434d00efc199d66e9058c7f3379Z 82d5b90c5ce9d710da639d6216d0f979Z eb285be29ab42cf4835ff20a5e3b5d23Z f9a0f6b85faa9c3db2b6e233c4eebb5bZ 8d72a301ed8a6f11a47c4ced4396e338Z 5fbf7ac2155cb4d5a6180ee9e3aa8704Z 31a46a92ab21b84193d0d71dd8c3d03bZ dc8b2cba2005552eafa3167f00dc2133Z ba6271b99bdeb4e7a9b15c05964ef4adZ fa79794252dbe47c89130f65349d654aZ a76930b741884e09ce90fa7450747d5fZ bbdb3994718dfc0d1cd45d7eff3f3d9cZ 20d575825ba26176b03cb51012a367a8Z 2c42ecb22e35f3e23d2f70babce0a2aaZ f4a039dc3c421321b7fc240494574a68Z 6e44b55025bbdff8f51692a023143d66Z 7ccb858cc738305e8ceb3c0140bee393Z ab0c1f9079d2f85a9facbd2c88aa6079Z ae0eb9bed39e64fc8d7e0db4018dc7e8Z fe6217c32be8305b1889657172b98884Z 69d9d925319f00a8bdd4af23c04e4102Z 90daa68cd26c95aa6c5d25451e095529)zresnet-rs-101-i160.h5zresnet-rs-101-i160_notop.h5zresnet-rs-101-i192.h5zresnet-rs-101-i192_notop.h5zresnet-rs-152-i192.h5zresnet-rs-152-i192_notop.h5zresnet-rs-152-i224.h5zresnet-rs-152-i224_notop.h5zresnet-rs-152-i256.h5zresnet-rs-152-i256_notop.h5zresnet-rs-200-i256.h5zresnet-rs-200-i256_notop.h5zresnet-rs-270-i256.h5zresnet-rs-270-i256_notop.h5zresnet-rs-350-i256.h5zresnet-rs-350-i256_notop.h5zresnet-rs-350-i320.h5zresnet-rs-350-i320_notop.h5zresnet-rs-420-i320.h5zresnet-rs-420-i320_notop.h5zresnet-rs-50-i160.h5zresnet-rs-50-i160_notop.h5      )r         r   i@  )2   e           ^    @      )input_filtersnum_repeats         i         $         5   H   ,   W   VarianceScalingg       @fan_outtruncated_normal)scalemodedistribution)
class_nameconfiga(  Instantiates the {name} architecture.

    Reference:
    [Revisiting ResNets: Improved Training and Scaling Strategies](
    https://arxiv.org/pdf/2103.07579.pdf)

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

    Args:
        depth: Depth of ResNet network.
        input_shape: optional shape tuple. 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.
        bn_momentum: Momentum parameter for Batch Normalization layers.
        bn_epsilon: Epsilon parameter for Batch Normalization layers.
        activation: activation function.
        se_ratio: Squeeze and Excitation layer ratio.
        dropout_rate: dropout rate before final classifier layer.
        drop_connect_rate: dropout rate at skip connections.
        include_top: whether to include the fully-connected layer at the top of
        the network.
        block_args: list of dicts, parameters to construct block modules.
        model_name: name of the model.
        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.
        weights: one of `None` (random initialization), `'imagenet'`
            (pre-training on ImageNet), or the path to the weights file to be
            loaded.  Note: one model can have multiple imagenet variants
            depending on input shape it was trained with. For input_shape
            224x224 pass `imagenet-i224` as argument. By default, highest input
            shape weights are downloaded.
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to
            use as image input for the model.
        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.
        include_preprocessing: Boolean, whether to include the preprocessing
            layer (`Rescaling`) at the bottom of the network. Defaults to
            `True`.  Note: Input image is normalized by ImageNet mean and
            standard deviation.

    Returns:
        A `keras.Model` instance.
c                    s2   du rt d}d|  fdd}|S )z Conv2D block with fixed padding.NZconv_c              	      s:   dkrt | } tj dkr(dnddtd| S )N   samevalidF)filterskernel_sizestridespaddinguse_biaskernel_initializername)fixed_paddingr   Conv2DCONV_KERNEL_INITIALIZER)inputsr3   r4   r9   r5    X/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/applications/resnet_rs.pyapply   s    
z!Conv2DFixedPadding.<locals>.applyr   get_uid)r3   r4   r5   r9   counterrA   r?   r>   r@   Conv2DFixedPadding   s
    

rE           h㈵>relubn_momentum
bn_epsilon
activationc                    s2   du rt d}d|  fdd}|S )zResNet-D type STEM block.NZstem_c                    s8  t  dkrdnd}tdddd d| }tj|d d	|}tj d
 d|}tdddd d|}tj|d d	|}tj d d|}tdddd d|}tj|d d	|}tj d d|}tdddd d|}tj|d d	|}tj d d|}|S )Nchannels_lastr   r0          Z_stem_conv_1r3   r4   r5   r9   Z_stem_batch_norm_1axismomentumepsilonr9   Z_stem_act_1r9   Z_stem_conv_2Z_stem_batch_norm_2Z_stem_act_2r   Z_stem_conv_3Z_stem_batch_norm_3Z_stem_act_3Z_stem_conv_4Z_stem_batch_norm_4Z_stem_act_4)r   image_data_formatrE   r   BatchNormalization
Activation)r=   bn_axisxrL   rK   rJ   r9   r?   r@   rA      st    zSTEM.<locals>.applyrB   )rJ   rK   rL   r9   rD   rA   r?   r[   r@   STEM   s
    

4r\         ?r0   )
in_filtersse_ratioexpand_ratioc                    sH   t  dkrdnd du r0t d}d|  fdd}|S )zSqueeze and Excitation block.rM   r   r0   NZse_c              
      s   t jd d| } dkr.|jd ddf}ndd|jd f}t j|d d|}tdtd  }t j|ddgddgtddd	d
 d|}t jd  ddgddgtdddd d|}t j| |gd dS )NZ_se_squeezerU   r0   Z_se_reshaper   r1   TrH   Z
_se_reduce)r3   r4   r5   r8   r6   r7   rL   r9   sigmoidZ
_se_expandZ
_se_excite)	r   GlobalAveragePooling2DshapeReshapemaxintr;   r<   multiply)r=   rZ   se_shapeZnum_reduced_filtersrY   r`   r^   r9   r_   r?   r@   rA   1  sF    	zSE.<locals>.apply)r   rV   rC   )r^   r_   r`   r9   rD   rA   r?   rj   r@   SE(  s    

$rk   皙?)r3   r5   use_projectionrJ   rK   rL   r_   survival_probabilityc	              	      s<   du rt d}	d|	  f	dd}
|
S )z7Bottleneck block variant for residual networks with BN.NZblock_0_c                    s  t  dkrdnd}| }rd }dkr`tjdddd d	| }t|ddd
 d|}nt|dd
 d| }tj|d d|}tddd d| }tj|d d|}tj d d|}tdd d|}tj|d d|}tj d d|}td ddd d|}tj|d d|}d  k rpdk rn ntd d|}rtjdd d|}t	 ||g}tj d d|S )NrM   r   r0   r   rO   )rO   rO   r1   Z_projection_pooling)	pool_sizer5   r6   r9   Z_projection_convrP   Z_projection_batch_normrQ   Z_conv_1Zbatch_norm_1Z_act_1rU   Z_conv_2Z_batch_norm_2Z_act_2Z_conv_3Z_batch_norm_3r   _se)r_   r9   )Nr0   r0   r0   Z_drop)noise_shaper9   Z_output_act)
r   rV   r   AveragePooling2DrE   rW   rX   rk   DropoutAdd)r=   rY   shortcutfilters_outrZ   	rL   rK   rJ   r3   r9   r_   r5   rn   rm   r?   r@   rA   h  s    zBottleneckBlock.<locals>.applyrB   )r3   r5   rm   rJ   rK   rL   r_   rn   r9   rD   rA   r?   rw   r@   BottleneckBlockX  s
    

Xrx   )r_   rK   rJ   rL   rn   c	              	      s<   du rt d}	d|	  f	dd}
|
S )z0Create one group of blocks for the ResNet model.NZblock_group_c                    s`   t d d d	| }tdD ].}t dd d| d d	|}q,|S )	NTZ	_block_0_)	r3   r5   rm   r_   rK   rJ   rL   rn   r9   r0   F_block__)	r3   r5   rm   r_   rL   rK   rJ   rn   r9   )rx   range)r=   rZ   i	rL   rK   rJ   r3   r9   r   r_   r5   rn   r?   r@   rA     s8    

zBlockGroup.<locals>.applyrB   )r3   r5   r   r_   rK   rJ   rL   rn   r9   rD   rA   r?   r}   r@   
BlockGroup  s
    

r~   c                 C   s   | t | | S )z@Get survival probability based on block number and initial rate.)floatZ	init_rate	block_numZtotal_blocksr?   r?   r@   get_survival_probability  s    r   )target_limitc                 C   s   t  }|| k rt |  dS )z9Increase default recursion limit to create larger models.N)sysgetrecursionlimitsetrecursionlimit)r   Zcurrent_limitr?   r?   r@   allow_bigger_recursion  s    r   c                 C   s8   |d }|d }|| }t j||f||ffd| }|S )zKPad the input along the spatial dimensions independently of input
    size.r0   rO   )r6   )r   ZeroPadding2D)r=   r4   Z	pad_totalZpad_begpad_endpadded_inputsr?   r?   r@   r:     s    r:   皙?T	resnet-rsimagenet  softmax)depthrL   
block_argsclassifier_activationc           "      C   s  t |  }|dkr&t|}| d| }dd |D }|h |dv sltjj|sltd|  d| d| ||v r|r|d	krtd
| d| tj|ddt	
 ||d}|du rtj|d}nt	|stj||d}n|}t	
 dkrdnd}|}|rB||d  }tjdd|}|dkrBtjg dg d|d|}t|||d|}|	du rft|  }	t|	D ]d\}}t||d t|	d d}t|d ||dkrdnd|d ||||d|d  d d!	|}qn|r(tjd"d#|}|dkrtj|d$d#|}t|| tj||d%d&|}n6|d'krDtjd"d#|}n|d(kr^tjd)d#|}|durtt|}n|}tj|||
d#}||v r|d*d+ }|
 d*| }|s|d,7 }| d-}t | } t!j"|| d.t#| d/}!|$|! n|dur|$| |S )0a"  Build Resnet-RS model, given provided parameters.

    Args:
        depth: Depth of ResNet network.
        input_shape: optional shape tuple. 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.
        bn_momentum: Momentum parameter for Batch Normalization layers.
        bn_epsilon: Epsilon parameter for Batch Normalization layers.
        activation: activation function.
        se_ratio: Squeeze and Excitation layer ratio.
        dropout_rate: dropout rate before final classifier layer.
        drop_connect_rate: dropout rate at skip connections.
        include_top: whether to include the fully-connected layer at the top of
          the network.
        block_args: list of dicts, parameters to construct block modules.
        model_name: name of the model.
        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.
        weights: one of `None` (random initialization), `'imagenet'`
          (pre-training on ImageNet), or the path to the weights file to be
          loaded. Note- one model can have multiple imagenet variants depending
          on input shape it was trained with. For input_shape 224x224 pass
          `imagenet-i224` as argument. By default, highest input shape weights
          are downloaded.
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to
          use as image input for the model.
        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.
        include_preprocessing: Boolean, whether to include the preprocessing
          layer (`Rescaling`) at the bottom of the network. Defaults to `True`.
          Note- Input image is normalized by ImageNet mean and standard
          deviation.

    Returns:
        A `tf.keras.Model` instance.

    Raises:
        ValueError: in case of invalid argument for `weights`, or invalid input
            shape.
        ValueError: if `classifier_activation` is not `softmax` or `None` when
            using a pretrained top layer.
    r   z-ic                 S   s   g | ]}d | qS )z
imagenet-ir?   ).0rZ   r?   r?   r@   
<listcomp>^      zResNetRS.<locals>.<listcomp>NzThe `weights` argument should be either `None` (random initialization), `'imagenet'` (pre-training on ImageNet, with highest available input shape), or the path to the weights file to be loaded. For ResNetRSz- the following weight variants are available z% (default=highest). Received weights=r   z-If using `weights` as `'imagenet'` or any of zH with `include_top` as true, `classes` should be 1000. Received classes=r   rN   )default_sizemin_sizedata_formatrequire_flattenweights)rd   )tensorrd   rM   r   r0   gp?)r+   )g
ףp=
?gv/?gCl?)gg٪?g.?gQ?)meanvariancerR   rI   rO   r   r   r   r   r~   rz   )	r3   rL   r5   r   r_   rJ   rK   rn   r9   avg_poolrU   top_dropoutpredictions)rL   r9   avgrf   max_pool-ra   Z_notopz.h5models)fnameorigincache_subdir	file_hash)%DEPTH_TO_WEIGHT_VARIANTSrf   tfiogfileexists
ValueErrorr   obtain_input_shaper   rV   r   Inputis_keras_tensor	RescalingNormalizationr\   
BLOCK_ARGS	enumerater   lenr~   rc   rs   validate_activationDenseGlobalMaxPooling2Dr   get_source_inputsr	   ModelsplitBASE_WEIGHTS_URLr
   get_fileWEIGHT_HASHESload_weights)"r   input_shaperJ   rK   rL   r_   dropout_ratedrop_connect_rateinclude_topr   
model_namepoolingr   input_tensorclassesr   include_preprocessingZavailable_weight_variantsZmax_input_shapeZweights_allow_list	img_inputrY   rZ   num_channelsr|   argsrn   r=   modelZweights_input_shapeZweights_namefilenamedownload_urlweights_pathr?   r?   r@   ResNetRS  s    I
	












r   z'keras.applications.resnet_rs.ResNetRS50zkeras.applications.ResNetRS50c                 C   s    t d| dd||||||d|dS )zBuild ResNet-RS50 model.r   rF   r]   zresnet-rs-50r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r?   r?   r@   
ResNetRS50  s    r   z(keras.applications.resnet_rs.ResNetRS101zkeras.applications.ResNetRS101c                 C   s    t d| dd||||||d|dS )zBuild ResNet-RS101 model.r   rF   r]   zresnet-rs-101r   r   r   r?   r?   r@   ResNetRS101  s    r   z(keras.applications.resnet_rs.ResNetRS152zkeras.applications.ResNetRS152c                 C   s    t d| dd||||||d|dS )zBuild ResNet-RS152 model.r   rF   r]   zresnet-rs-152r   r   r   r?   r?   r@   ResNetRS152  s    r   z(keras.applications.resnet_rs.ResNetRS200zkeras.applications.ResNetRS200c                 C   s    t d| dd||||||d|dS )zBuild ResNet-RS200 model.r   皙?r]   zresnet-rs-200r   r   r   r?   r?   r@   ResNetRS200:  s    r   z(keras.applications.resnet_rs.ResNetRS270zkeras.applications.ResNetRS270c                 C   s(   t d td| dd||||||d|dS )zBuild ResNet-RS-270 model.i  r   r   r]   zresnet-rs-270r   r   r   r   r?   r?   r@   ResNetRS270X  s    r   z(keras.applications.resnet_rs.ResNetRS350zkeras.applications.ResNetRS350c                 C   s(   t d td| dd||||||d|dS )zBuild ResNet-RS350 model.i  r   r   皙?zresnet-rs-350r   r   r   r?   r?   r@   ResNetRS350w  s    r   z(keras.applications.resnet_rs.ResNetRS420zkeras.applications.ResNetRS420c                 C   s(   t d td| dd||||||d|dS )zBuild ResNet-RS420 model.i  r   r   r   zresnet-rs-420)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r?   r?   r@   ResNetRS420  s    r   z-keras.applications.resnet_rs.preprocess_inputc                 C   s   | S )a  A placeholder method for backward compatibility.

    The preprocessing logic has been included in the ResnetRS 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 `tf.Tensor`.
      data_format: Optional data format of the image tensor/array. Defaults to
        None, in which case the global setting
        `tf.keras.backend.image_data_format()` is used (unless you changed it,
        it defaults to "channels_last").{mode}

    Returns:
      Unchanged `numpy.array` or `tf.Tensor`.
    r?   )rZ   r   r?   r?   r@   preprocess_input  s    r   z/keras.applications.resnet_rs.decode_predictions   c                 C   s   t j| |dS )N)top)r   decode_predictions)predsr   r?   r?   r@   r     s    r   rU   )N)rF   rG   rH   N)r]   r0   N)rF   rG   rH   r]   rl   N)r]   rG   rF   rH   rl   N)NrF   rG   rH   r]   r]   r   TNr   Nr   Nr   r   T)Tr   r   NNNr   T)Tr   r   NNNr   T)Tr   r   NNNr   T)Tr   r   NNNr   T)Tr   r   NNNr   T)Tr   r   NNNr   T)Tr   r   NNNr   T)N)r   )4__doc__r   typingr   r   r   r   Ztensorflow.compat.v2compatv2r   kerasr   r   Zkeras.applicationsr   keras.enginer	   keras.utilsr
   r    tensorflow.python.util.tf_exportr   r   r   r   r   r<   BASE_DOCSTRINGrE   r   strr\   rg   rk   boolrx   r~   r   r   r:   r   r   r   r   r   r   r   r   r   r   formatr?   r?   r?   r@   <module>   s  -	L
    C 4      o      0                
 S                                                        
