
    2VhAL              	          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Z	 d+dZd,dZ	 d-dZd,dZ eg d      	 	 	 	 	 	 	 	 d.d       Z eddg      	 	 	 	 	 	 	 	 d/d       Z eddg      	 	 	 	 	 	 	 	 d0d       Z ed d!g      d1d"       Z ed#d$g      d2d%       Zej2                  j5                  d&ej6                  ej8                  '      e_        ej0                  j:                  e_        d(Z eed)ej:                  ez           eed)ej:                  ez           eed)ej:                  ez          y)3    )backend)layers)keras_export)imagenet_utils)
Functional)operation_utils)
file_utilszDhttps://storage.googleapis.com/tensorflow/keras-applications/resnet/) 2cb95161c43110f7111970584f804107 4d473c1dd8becc155b73f8504c6f6626) f1aeb4b969a6efcfb50fad2f0c20cfc5 88cf7a10940856eca736dc7b7e228a21) 100835be76be38e30d865e96f2aaae62 ee4c566cf9a93f14d82f913c2dc6dd0c) 3ef43a0b657b3be2300d5770ece849e0 fac2f116257151a9d068a22e544a4917) 6343647c601c52e1368623803854d971 c0ed64b8031c3730f411d2eb4eea35b5) a49b44d1979771252814e80f8ec446f9 ed17cf2e0169df9d443503ef94b23b33) 67a5b30d522ed92f75a1f16eef299d1a 62527c363bdd9ec598bed41947b379fc) 34fb605428fcc7aa4d62f44404c11509 0f678c91647380debd923963594981b3)resnet50	resnet101	resnet152
resnet50v2resnet101v2resnet152v2	resnext50
resnext101Nc                 
   |dv s#t        j                  |      st        d|       |dk(  r|r|dk7  rt        d|       t        j                  |ddt        j                         ||      }|t        j                  |	      }n/t        j                  |      st        j                  ||
      }n|}t        j                         dk(  rd}nd} t        j                  dd      |      } t        j                  ddd|d      |      }|s; t        j                  |dd      |      } t        j                  dd      |      } t        j                  dd      |      } t        j                  ddd      |      } | |      }|r; t        j                  |dd       |      } t        j                  dd!      |      }|rQ t        j                  d"      |      }t        j                   |	|        t        j"                  ||	d#$      |      }nC|d%k(  r t        j                  d"      |      }n!|d&k(  r t        j$                  d'      |      }|t'        j(                  |      }n|}t+        |||
      }|dk(  r`|t,        v rX|r|d(z   }t,        |   d)   }n|d*z   }t,        |   d   }t        j.                  |t0        |z   d+|,      }|j3                  |       |S ||j3                  |       |S )-aR	  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.
        name: Name of the model.
        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"`.
        name: The name of the model (string).

    Returns:
        A Model instance.
    >   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.  Received: weights=r#     zcIf using `weights='imagenet'` with `include_top=True`, `classes` should be 1000.  Received classes=       )default_sizemin_sizedata_formatrequire_flattenweights)shape)tensorr,   channels_last      )r/   r/   r1   	conv1_padpaddingname@         
conv1_convstridesuse_biasr5   >conv1_bnaxisepsilonr5   relu
conv1_relur5   r0   r0   rF   	pool1_pad
pool1_poolr;   r5   post_bn	post_reluavg_poolpredictions)
activationr5   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)r	   exists
ValueErrorr   obtain_input_shaper   image_data_formatr   Inputis_keras_tensorZeroPadding2DConv2DBatchNormalization
ActivationMaxPooling2DGlobalAveragePooling2Dvalidate_activationDenseGlobalMaxPooling2Dr   get_source_inputsr   WEIGHTS_HASHESget_fileBASE_WEIGHTS_PATHload_weights)stack_fnpreactr<   include_topr+   input_tensorinput_shapepoolingclassesclassifier_activationr5   weights_name	img_inputbn_axisxinputsmodel	file_namerT   weights_paths                       M/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/applications/resnet.pyResNetrz   0   sC   | ))Z->->w-G i	!
 	
 *D  'y*
 	
 !33--/#K LL{3	&&|4LLI$I  "o5H%5KH	A 	Nb!QMaPA
F%%(

 9Ff<8;H%5KHKA<Aq|<Q?AA
F%%(

 8Ff;7::F))z:1= 	**+@'J
FLL 5M

 e>--:>qAA:))z:1=A  22<@ vqt,E 	:LN$B$'OOI&|4Q7I MM  '|4Q7I!**	)!	
 	<( L 
	7#L    c                 l   t        j                         dk(  rd}nd}|rG t        j                  d|z  d||dz         |       } t        j                  |d|dz   	      |      }n| } t        j                  |d||d
z         |       }  t        j                  |d|dz   	      |       }  t        j
                  d|dz         |       }  t        j                  ||d|dz         |       }  t        j                  |d|dz   	      |       }  t        j
                  d|dz         |       }  t        j                  d|z  d|dz         |       }  t        j                  |d|dz   	      |       }  t        j                  |dz         || g      }  t        j
                  d|dz         |       } | S )a  A residual block for ResNet*_v1.

    Args:
        x: Input tensor.
        filters: No of filters in the bottleneck layer.
        kernel_size: Kernel size of the bottleneck layer. Defaults to `3`.
        stride: Stride of the first layer. Defaults to `1`.
        conv_shortcut: Use convolution shortcut if `True`, otherwise
            use identity shortcut. Defaults to `True`
        name(optional): Name of the block

    Returns:
        Output tensor for the residual block.
    r.   r/   r0      _0_convrI   r=   _0_bnr?   _1_conv_1_bnrB   _1_relurD   SAME_2_convr3   _2_bn_2_relu_3_conv_3_bn_add_out)r   rX   r   r\   r]   r^   Add)rt   filterskernel_sizestrideconv_shortcutr5   rs   shortcuts           ry   residual_block_v1r      s   $   "o5
6==KF	1A


6,,(

 Hgq&ti7GHKA	!!hTG^			A 	9&ti'78;A	f4)3C			A	!!hTG^			A 	9&ti'78;A<a'k14)+;<Q?A	!!hTG^			A 	'

v&!}5A5&tf}5a8AHr{   c           
          t        | |||dz         } t        d|dz         D ]   }t        | |d|dz   t        |      z         } " | S )p  A set of stacked residual blocks.

    Args:
        x: Input tensor.
        filters: Number of filters in the bottleneck layer in a block.
        blocks: Number of blocks in the stacked blocks.
        stride1: Stride of the first layer in the first block. Defaults to `2`.
        name: Stack label.

    Returns:
        Output tensor for the stacked blocks.
    _block1r   r5   r8   r0   F_blockr   r5   )r   rangestrrt   r   blocksstride1r5   is         ry   stack_residual_blocks_v1r     sZ     	!WW4);KLA1fqj! 
we$/CF2J

 Hr{   c           	         t        j                         dk(  rd}nd} t        j                  |d|dz         |       } t        j                  d|dz   	      |      }|r& t        j
                  d
|z  d||dz         |      }n$|dkD  r t        j                  d|      |       n| } t        j
                  |ddd|dz         |      }  t        j                  |d|dz         |       }  t        j                  d|dz   	      |       }  t        j                  d|dz         |       }  t        j
                  |||d|dz         |       }  t        j                  |d|dz         |       }  t        j                  d|dz   	      |       }  t        j
                  d
|z  d|dz   	      |       }  t        j                  |dz   	      || g      } | S )a  A residual block for ResNet*_v2.

    Args:
        x: Input tensor.
        filters: No of filters in the bottleneck layer.
        kernel_size: Kernel size of the bottleneck layer. Defaults to `3`.
        stride: Stride of the first layer. Defaults to `1`.
        conv_shortcut: Use convolution shortcut if `True`, otherwise
            use identity shortcut. Defaults to `True`
        name(optional): Name of the block

    Returns:
        Output tensor for the residual block.
    r.   r/   r0   r=   
_preact_bnr?   rB   _preact_relurD   r}   r~   rI   )r;   Fr   r:   r   r   rE   _2_padr3   r   r   r   r   r   )	r   rX   r   r]   r^   r\   r_   r[   r   )	rt   r   r   r   r   r5   rs   rj   r   s	            ry   residual_block_v2r   *  s	   $   "o5V&&hTL-@	F CVvD>,AB6JF
6==KF	1A


 :@!2F6215 		AD94D		A	!!hTG^			A 	9&ti'78;AL%5D8OLQOA	I	 		A	!!hTG^			A 	9&ti'78;A<a'k14)+;<Q?A&

v&!}5AHr{   c           	          t        | |d|dz         } t        d|      D ]  }t        | ||dz   t        |      z         } ! t        | |||dz   t        |      z         } | S )r   Tr   r   r8   r   rD   r   )r   r   r   r   s         ry   stack_residual_blocks_v2r   i  ss     	!WDti?OPA1f IathQ/GHI	773v;)F	A Hr{   )z$keras.applications.resnet50.ResNet50z"keras.applications.resnet.ResNet50zkeras.applications.ResNet50c                 6    d }t        |ddd|| ||||||      S )z'Instantiates the ResNet50 architecture.c                 |    t        | dddd      } t        | ddd	      } t        | d
dd	      } t        | ddd	      S )Nr6   r/   r0   conv2r   r5      r}   conv3rD         conv4   conv5r   rt   s    ry   ri   zResNet50.<locals>.stack_fn  sE    $QAqwG$QQW=$QQW='3@@r{   FTr   )rj   r<   rq   r5   rk   r+   rl   rm   rn   ro   rp   rz   	rk   r+   rl   rm   rn   ro   rp   r5   ri   s	            ry   ResNet50r     s;    &A !3 r{   z#keras.applications.resnet.ResNet101zkeras.applications.ResNet101c                 6    d }t        |dd|d| ||||||      S )z(Instantiates the ResNet101 architecture.c                 |    t        | dddd      } t        | ddd	      } t        | d
dd	      } t        | ddd	      S )Nr6   r/   r0   r   r   r   r}   r   rD   r      r   r   r   r   r   s    ry   ri   zResNet101.<locals>.stack_fn  E    $QAqwG$QQW=$QRg>'3@@r{   FTr   rj   r<   r5   rq   rk   r+   rl   rm   rn   ro   rp   r   r   s	            ry   	ResNet101r     ;    $A  !3 r{   z#keras.applications.resnet.ResNet152zkeras.applications.ResNet152c                 6    d }t        |dd|d| ||||||      S )z(Instantiates the ResNet152 architecture.c                 |    t        | dddd      } t        | ddd	      } t        | d
dd	      } t        | ddd	      S )Nr6   r/   r0   r   r   r      r   rD   r   $   r   r   r   r   r   s    ry   ri   zResNet152.<locals>.stack_fn  r   r{   FTr   r   r   r   s	            ry   	ResNet152r     r   r{   z,keras.applications.resnet50.preprocess_inputz*keras.applications.resnet.preprocess_inputc                 2    t        j                  | |d      S )Ncaffe)r)   mode)r   preprocess_input)rt   r)   s     ry   r   r     s     **	{ r{   z.keras.applications.resnet50.decode_predictionsz,keras.applications.resnet.decode_predictionsc                 0    t        j                  | |      S )N)top)r   decode_predictions)predsr   s     ry   r   r     s     ,,U<<r{    )r   reterroraj
  

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 `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. 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.
        When loading pretrained weights, `classifier_activation` can only
        be `None` or `"softmax"`.
    name: The name of the model (string).

Returns:
    A Model instance.
__doc__)	Tr#   NNNr$   softmaxresnetN)r/   r0   TN)r8   N)r/   r0   FN)Tr#   NNNr$   r   r   )Tr#   NNNr$   r   r   )Tr#   NNNr$   r   r   )N)   ) 	keras.srcr   r   keras.src.api_exportr   keras.src.applicationsr   keras.src.modelsr   keras.src.opsr   keras.src.utilsr	   rg   re   rz   r   r   r   r   r   r   r   r   r   PREPROCESS_INPUT_DOCformatPREPROCESS_INPUT_RET_DOC_CAFFEPREPROCESS_INPUT_ERROR_DOCr   DOCsetattr r{   ry   <module>r      s9     - 1 ' ) & K ;!P #	hX CG6r. DH<~.  #	D -& #	D -& #	D 64 86== *>>EE	55

3
3 F   
 ,>>FF  5n )X--3 4 	9i//#5 6 	9i//#5 6r{   