o
    MeêV  ã                	   @   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dHd%d&„Z			'		dJd(d)„ZdKd*d+„Zed,d-d.ƒ						dLd/d0„ƒZed1d2ƒ						dLd3d4„ƒZed5d6ƒ						dLd7d8„ƒZed9d:ƒdMd;d<„ƒZed=d>ƒdNd@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 )Oz�ResNet 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|› �ƒ‚|dv s%tjj |¡s%tdƒ‚|dkr3|r3|	dkr3tdƒ‚tj	|dd	t
 ¡ ||d
�}|du rKtj|d�}nt
 |¡sXtj||d�}n|}t
 ¡ dkrbd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"�|ƒ}| |ƒ}|rº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|}tj|||d�}|dk�r6|tv �r6|�r|d+ }t| d, }n
|d- }t| d }tj|t| d.|d/�}| |¡ |S |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): >   r   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.r   r   zWIf using `weights` as `"imagenet"` with `include_top` as true, `classes` should be 1000éà   é    )Zdefault_sizeZmin_sizeÚdata_formatZrequire_flattenÚweightsN)Úshape)Ztensorr   Úchannels_lasté   é   )©r   r   r   Z	conv1_pad©ÚpaddingÚnameé@   é   é   Z
conv1_conv©ÚstridesÚuse_biasr   çõ›ìþä>Zconv1_bn©ZaxisÚepsilonr   ÚreluZ
conv1_relu©r   ©©r   r   r)   Z	pool1_padZ
pool1_pool©r!   r   Zpost_bnZ	post_reluZavg_poolZpredictions)Z
activationr   ZavgÚmaxZmax_poolz&_weights_tf_dim_ordering_tf_kernels.h5r   z,_weights_tf_dim_ordering_tf_kernels_notop.h5Úmodels)Zcache_subdirÚ	file_hash) Úpopr   r   Ú
ValueErrorÚtfÚioZgfileÚexistsr   Zobtain_input_shaper   Úimage_data_formatZInputZis_keras_tensorÚZeroPadding2DÚConv2DÚBatchNormalizationÚ
ActivationÚMaxPooling2DZGlobalAveragePooling2DZvalidate_activationZDenseZGlobalMaxPooling2Dr   Zget_source_inputsr   ZModelÚWEIGHTS_HASHESr   Zget_fileÚBASE_WEIGHTS_PATHZload_weights)Ústack_fnÚpreactr"   Z
model_nameÚinclude_topr   Úinput_tensorÚinput_shapeÚpoolingÚclassesZclassifier_activationÚkwargsZ	img_inputÚbn_axisÚxZinputsÚmodelÚ	file_namer-   Zweights_path© rG   úID:\Projects\ConvertPro\env\Lib\site-packages\keras/applications/resnet.pyÚResNetL   s¤   >ÿÿú	
ÿÿþÿþÿþÿü

ý
rI   r   r   c                 C   sJ  t  ¡ dkrdnd}|r(t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_convr*   r#   Ú_0_bnr$   Ú_1_convÚ_1_bnr&   Ú_1_relur'   ZSAMEÚ_2_convr   Ú_2_bnÚ_2_reluÚ_3_convÚ_3_bnÚ_addÚ_out)r   r3   r   r5   r6   r7   ÚAdd)rD   ÚfiltersÚkernel_sizeÚstrideÚconv_shortcutr   rC   ÚshortcutrG   rG   rH   Ú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©rZ   r   r   r   FÚ_block©r[   r   )r]   ÚrangeÚstr©rD   rX   ÚblocksÚstride1r   ÚirG   rG   rH   Ústack1(  s   ÿrh   Fc           	      C   sf  t  ¡ dkrdnd}tj|d|d d�| ƒ}tjd|d d	�|ƒ}|r3tjd
| d||d d�|ƒ}n|dkr@t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'   rJ   rK   r*   )r!   FrM   r    rN   rO   r(   Ú_2_padr   rP   rQ   rR   rS   rV   )	r   r3   r   r6   r7   r5   r8   r4   rW   )	rD   rX   rY   rZ   r[   r   rC   r<   r\   rG   rG   rH   Úblock2=  sZ   
ÿþÿþÿÿþ
ÿþûú
ÿþrj   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^   ra   r   r`   r'   r_   )rj   rb   rc   rd   rG   rG   rH   Ústack2v  s
   rk   r   c           
   	      sÚ  t  ¡ dkrdnd}|r+t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   FrK   r    r#   rL   r$   rM   )r"   r   rN   r&   rO   r'   r(   ri   r   rP   )r!   Zdepth_multiplierr"   r   Néÿÿÿÿc                    s   t ‡ fdd„tˆƒD ƒƒS )Nc                 3   s2   � | ]}ˆ d d …d d …d d …d d …|f V  qd S ©NrG   )Ú.0rg   ©rD   rG   rH   Ú	<genexpr>Ä  s   €0 z+block3.<locals>.<lambda>.<locals>.<genexpr>)Úsumrb   ro   ©Úcro   rH   Ú<lambda>Ä  s    zblock3.<locals>.<lambda>Z	_2_reducerQ   rR   rS   rT   rU   rV   )r   r3   r   r5   r6   r7   r4   ZDepthwiseConv2Dr   ZreshapeZconcatenateÚLambdarW   )
rD   rX   rY   rZ   Úgroupsr[   r   rC   r\   Zx_shaperG   rr   rH   Úblock3Š  s~   
ûú
ÿþ
ÿþûú
þý
ÿþÿþ
ÿþrw   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^   )rZ   rv   r   r   r   Fr`   )rv   r[   r   )rw   rb   rc   )rD   rX   re   rf   rv   r   rg   rG   rG   rH   Ústack3Ù  s   ûrx   z$keras.applications.resnet50.ResNet50z"keras.applications.resnet.ResNet50zkeras.applications.ResNet50c                 K   ó*   dd„ }t |ddd| |||||f
i |¤ŽS )z'Instantiates the ResNet50 architecture.c                 S   óB   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©rf   r   é€   rJ   Úconv3r'   é   é   Úconv4é   Úconv5©rh   ro   rG   rG   rH   r;     ó   zResNet50.<locals>.stack_fnFTr	   ©rI   ©r=   r   r>   r?   r@   rA   rB   r;   rG   rG   rH   ÚResNet50ó  s   öõrˆ   z#keras.applications.resnet.ResNet101zkeras.applications.ResNet101c                 K   ry   )z(Instantiates the ResNet101 architecture.c                 S   rz   )Nr   r   r   r{   r|   r}   rJ   r~   r'   r   é   r�   r‚   rƒ   r„   ro   rG   rG   rH   r;   &  r…   zResNet101.<locals>.stack_fnFTr
   r†   r‡   rG   rG   rH   Ú	ResNet101  ó   öõrŠ   z#keras.applications.resnet.ResNet152zkeras.applications.ResNet152c                 K   ry   )z(Instantiates the ResNet152 architecture.c                 S   rz   )Nr   r   r   r{   r|   r}   é   r~   r'   r   é$   r�   r‚   rƒ   r„   ro   rG   rG   rH   r;   I  r…   zResNet152.<locals>.stack_fnFTr   r†   r‡   rG   rG   rH   Ú	ResNet152;  r‹   rŽ   z,keras.applications.resnet50.preprocess_inputz*keras.applications.resnet.preprocess_inputc                 C   s   t j| |dd�S )NZcaffe)r   Úmode)r   Úpreprocess_input)rD   r   rG   rG   rH   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’   rG   rG   rH   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   r   r   TN)r   r   N)Tr   NNNr   rm   )r‘   )'r˜   Ztensorflow.compat.v2ÚcompatÚv2r0   Zkerasr   Zkeras.applicationsr   Zkeras.enginer   Zkeras.layersr   Zkeras.utilsr   r   Z tensorflow.python.util.tf_exportr   r:   r9   r   rI   r]   rh   rj   rk   rw   rx   rˆ   rŠ   rŽ   r�   r“   ZPREPROCESS_INPUT_DOCÚformatZPREPROCESS_INPUT_RET_DOC_CAFFEZPREPROCESS_INPUT_ERROR_DOCZDOCÚsetattrrG   rG   rG   rH   Ú<module>   s¼   ÿã#
õ 
*
3

9
ù
Oýú ÿú ÿú þþý
<