o
    Meå=  ã                   @   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aeddƒ							d$dd„ƒZ					d%dd„Zd&dd„Zedƒd'dd„ƒZedƒd(d d!„ƒZejjd"ejejd#�e_ ejj e_ dS ))zÀInception-ResNet V2 model for Keras.

Reference:
  - [Inception-v4, Inception-ResNet and the Impact of
     Residual Connections on Learning](https://arxiv.org/abs/1602.07261)
    (AAAI 2017)
é    N)Úbackend)Úimagenet_utils)Útraining)ÚVersionAwareLayers)Ú
data_utils)Úlayer_utils)Úkeras_exportzQhttps://storage.googleapis.com/tensorflow/keras-applications/inception_resnet_v2/z8keras.applications.inception_resnet_v2.InceptionResNetV2z$keras.applications.InceptionResNetV2TÚimagenetéè  Úsoftmaxc                 K   sF  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dddd�}	t|	dddd�}	t|	ddƒ}	tjddd�|	ƒ}	t|	dddd�}	t|	dddd�}	tjddd�|	ƒ}	t|	ddƒ}
t|	ddƒ}t|ddƒ}t|	ddƒ}t|ddƒ}t|ddƒ}tjdddd�|	ƒ}t|ddƒ}|
|||g}t
 ¡ dkrÕdnd}tj|dd�|ƒ}	tdd ƒD ]
}t|	d!d"|d#�}	qåt|	d$dddd�}
t|	d%dƒ}t|d%dƒ}t|d$dddd�}tjdddd�|	ƒ}|
||g}tj|d&d�|ƒ}	tdd'ƒD ]}t|	d(d)|d#�}	�q+t|	d%dƒ}
t|
d$dddd�}
t|	d%dƒ}t|d*dddd�}t|	d%dƒ}t|d*dƒ}t|d+dddd�}tjdddd�|	ƒ}|
|||g}tj|d,d�|ƒ}	tdd-ƒD ]}t|	d.d/|d#�}	�qˆt|	d0dd/d-d1�}	t|	d2dd3d4�}	| �rÁtjd5d4�|	ƒ}	t ||¡ tj||d6d7�|	ƒ}	n|d8k�rÍt ¡ |	ƒ}	n|d9k�rØt ¡ |	ƒ}	|du�rãt |¡}n|}tj||	d:d4�}|dk�r| �rd;}tj|t| d<d=d>�}nd?}tj|t| d<d@d>�}| |¡ |S |du�r!| |¡ |S )Aa]  Instantiates the Inception-ResNet v2 architecture.

    Reference:
    - [Inception-v4, Inception-ResNet and the Impact of
       Residual Connections on Learning](https://arxiv.org/abs/1602.07261)
      (AAAI 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 InceptionResNetV2, call
    `tf.keras.applications.inception_resnet_v2.preprocess_input`
    on your inputs before passing them to the model.
    `inception_resnet_v2.preprocess_input`
    will scale input pixels between -1 and 1.

    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 `(299, 299, 3)` (with `'channels_last'` data format)
        or `(3, 299, 299)` (with `'channels_first'` data format).
        It should have exactly 3 inputs channels,
        and width and height should be no smaller than 75.
        E.g. `(150, 150, 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"`.
      **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 1000i+  éK   )Zdefault_sizeZmin_sizeÚdata_formatZrequire_flattenÚweightsN)Úshape)Ztensorr   é    é   é   Zvalid)ÚstridesÚpadding)r   é@   )r   éP   é   éÀ   é`   é0   é   ÚsameÚchannels_firstZmixed_5b©ÚaxisÚnameé   gÃõ(\�ÂÅ?Úblock35)ÚscaleÚ
block_typeÚ	block_idxi€  é   Zmixed_6aé   gš™™™™™¹?Úblock17i   i@  Zmixed_7aé
   gš™™™™™É?Úblock8g      ð?)r$   Ú
activationr%   r&   i   Zconv_7b©r!   Zavg_poolZpredictions)r,   r!   ZavgÚmaxZinception_resnet_v2z9inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5ÚmodelsZ e693bd0210a403b3192acc6073ad2e96)Zcache_subdirZ	file_hashz?inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5Z d19885ff4a710c122648d3b5c3b684e4) Úpopr   r   Ú
ValueErrorÚtfÚioZgfileÚexistsr   Zobtain_input_shaper   Úimage_data_formatZInputZis_keras_tensorÚ	conv2d_bnZMaxPooling2DZAveragePooling2DÚConcatenateÚrangeÚinception_resnet_blockZGlobalAveragePooling2DZvalidate_activationZDenseZGlobalMaxPooling2Dr   Zget_source_inputsr   ZModelr   Zget_fileÚBASE_WEIGHT_URLZload_weights)Zinclude_topr   Zinput_tensorZinput_shapeZpoolingÚclassesZclassifier_activationÚkwargsZ	img_inputÚxÚbranch_0Úbranch_1Úbranch_2Zbranch_poolÚbranchesÚchannel_axisr&   ÚinputsÚmodelÚfnameZweights_path© rF   úVD:\Projects\ConvertPro\env\Lib\site-packages\keras/applications/inception_resnet_v2.pyÚInceptionResNetV2+   sæ   Mÿÿú	
ÿ

ÿ
ÿ
ÿÿþ



üÿü

ý
rH   r   r   ÚreluFc                 C   sŒ   t j||||||d�| ƒ} |s-t ¡ dkrdnd}|du rdn|d }	t j|d|	d�| ƒ} |durD|du r7dn|d	 }
t j||
d
�| ƒ} | S )a  Utility function to apply conv + BN.

    Args:
      x: input tensor.
      filters: filters in `Conv2D`.
      kernel_size: kernel size as in `Conv2D`.
      strides: strides in `Conv2D`.
      padding: padding mode in `Conv2D`.
      activation: activation in `Conv2D`.
      use_bias: whether to use a bias in `Conv2D`.
      name: name of the ops; will become `name + '_ac'` for the activation
          and `name + '_bn'` for the batch norm layer.

    Returns:
      Output tensor after applying `Conv2D` and `BatchNormalization`.
    )r   r   Úuse_biasr!   r   r   r   NZ_bnF)r    r$   r!   Ú_acr-   )r   ZConv2Dr   r5   ZBatchNormalizationÚ
Activation)r=   ÚfiltersZkernel_sizer   r   r,   rJ   r!   Zbn_axisZbn_nameZac_namerF   rF   rG   r6     s(   úùÿr6   c                 C   s®  |dkr.t | ddƒ}t | ddƒ}t |ddƒ}t | ddƒ}t |ddƒ}t |ddƒ}|||g}nR|dkrSt | ddƒ}t | d	dƒ}t |d
ddgƒ}t |dddgƒ}||g}n-|dkrxt | ddƒ}t | ddƒ}t |dddgƒ}t |dddgƒ}||g}ntdt|ƒ ƒ‚|d t|ƒ }	t ¡ dkr�dnd}
tj|
|	d d�|ƒ}t |t | ¡|
 ddd|	d d�}tjdd„ t | ¡dd… d|i|	d�| |gƒ} |durÕtj	||	d d�| ƒ} | S )a(  Adds an Inception-ResNet block.

    This function builds 3 types of Inception-ResNet blocks mentioned
    in the paper, controlled by the `block_type` argument (which is the
    block name used in the official TF-slim implementation):
    - Inception-ResNet-A: `block_type='block35'`
    - Inception-ResNet-B: `block_type='block17'`
    - Inception-ResNet-C: `block_type='block8'`

    Args:
      x: input tensor.
      scale: scaling factor to scale the residuals (i.e., the output of passing
        `x` through an inception module) before adding them to the shortcut
        branch. Let `r` be the output from the residual branch, the output of
        this block will be `x + scale * r`.
      block_type: `'block35'`, `'block17'` or `'block8'`, determines the network
        structure in the residual branch.
      block_idx: an `int` used for generating layer names. The Inception-ResNet
        blocks are repeated many times in this network. We use `block_idx` to
        identify each of the repetitions. For example, the first
        Inception-ResNet-A block will have `block_type='block35', block_idx=0`,
        and the layer names will have a common prefix `'block35_0'`.
      activation: activation function to use at the end of the block (see
        [activations](../activations.md)). When `activation=None`, no activation
        is applied
        (i.e., "linear" activation: `a(x) = x`).

    Returns:
        Output tensor for the block.

    Raises:
      ValueError: if `block_type` is not one of `'block35'`,
        `'block17'` or `'block8'`.
    r#   r   r   r   r   r   r)   r   é€   é    é   r+   éà   r'   zXUnknown Inception-ResNet block type. Expects "block35", "block17" or "block8", but got: Ú_r   Z_mixedr   NTZ_conv)r,   rJ   r!   c                 S   s   | d | d |  S )Nr   r   rF   )rC   r$   rF   rF   rG   Ú<lambda>�  s    z(inception_resnet_block.<locals>.<lambda>r$   )Zoutput_shapeÚ	argumentsr!   rK   r-   )
r6   r1   Ústrr   r5   r   r7   Z	int_shapeÚLambdarL   )r=   r$   r%   r&   r,   r>   r?   r@   rA   Z
block_namerB   ÚmixedÚuprF   rF   rG   r9   B  sb   #

þÿÿú	üûr9   z7keras.applications.inception_resnet_v2.preprocess_inputc                 C   s   t j| |dd�S )Nr2   )r   Úmode)r   Úpreprocess_input)r=   r   rF   rF   rG   rZ   ™  s   ÿrZ   z9keras.applications.inception_resnet_v2.decode_predictionsr   c                 C   s   t j| |d�S )N)Útop)r   Údecode_predictions)Úpredsr[   rF   rF   rG   r\      s   r\   Ú )rY   ÚretÚerror)Tr	   NNNr
   r   )r   r   rI   FN)rI   )N)r   )Ú__doc__Ztensorflow.compat.v2ÚcompatÚv2r2   Zkerasr   Zkeras.applicationsr   Zkeras.enginer   Zkeras.layersr   Zkeras.utilsr   r   Z tensorflow.python.util.tf_exportr   r:   r   rH   r6   r9   rZ   r\   ZPREPROCESS_INPUT_DOCÚformatZPREPROCESS_INPUT_RET_DOC_TFZPREPROCESS_INPUT_ERROR_DOCrF   rF   rF   rG   Ú<module>   sT   ÿþù j
ø
.Wý