o
    i e2?                     @   s0  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d	lmZ dd
lmZ dZda	edd							d'ddZ					d(ddZej G dd de
j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 ),zInception-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)layers)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rdnd}tj|dd|}	tdd D ]
}t|	d!d"|d#}	qt|	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#}	qt|	d0dd/d-d1}	t|	d2dd3d4}	| rtjd5d4|	}	t|| tj||d6d7|	}	n|d8krt |	}	n|d9krt |	}	|durt|}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.
    r   zUnknown argument(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 1000i+  K   )default_sizemin_sizedata_formatrequire_flattenweightsN)shape)tensorr             valid)stridespadding)r   @   )r   P         `   0      samechannels_firstmixed_5baxisname   g(\?block35)scale
block_type	block_idxi     mixed_6a   g?block17i   i@  mixed_7a
   g?block8g      ?)r*   
activationr+   r,   i   conv_7br'   avg_poolpredictions)r4   r'   avgmaxinception_resnet_v2z9inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5models e693bd0210a403b3192acc6073ad2e96)cache_subdir	file_hashz?inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5 d19885ff4a710c122648d3b5c3b684e4) popr   r   
ValueErrortfiogfileexistsr   obtain_input_shaper   image_data_formatInputis_keras_tensor	conv2d_bnMaxPooling2DAveragePooling2DConcatenaterangeinception_resnet_blockGlobalAveragePooling2Dvalidate_activationDenseGlobalMaxPooling2Dr   get_source_inputsr   Modelr   get_fileBASE_WEIGHT_URLload_weights)include_topr   input_tensorinput_shapepoolingclassesclassifier_activationkwargs	img_inputxbranch_0branch_1branch_2branch_poolbrancheschannel_axisr,   inputsmodelfnameweights_path rm   Y/var/www/myenv/lib/python3.10/site-packages/keras/src/applications/inception_resnet_v2.pyInceptionResNetV2-   s   M	











ro   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   N_bnF)r&   r*   r'   _acr6   )r   Conv2Dr   rH   BatchNormalization
Activation)rb   filterskernel_sizer   r   r4   rq   r'   bn_axisbn_nameac_namerm   rm   rn   rK     s(   rK   c                       s0   e Zd Z fddZ fddZdd Z  ZS )CustomScaleLayerc                    s   t  jdi | || _d S )Nrm   )super__init__r*   )selfr*   r`   	__class__rm   rn   r~   F  s   
zCustomScaleLayer.__init__c                    s   t   }|d| ji |S )Nr*   )r}   
get_configupdater*   )r   configr   rm   rn   r   J  s   
zCustomScaleLayer.get_configc                 C   s   |d |d | j   S )Nr   r   )r*   )r   ri   rm   rm   rn   callO  s   zCustomScaleLayer.call)__name__
__module____qualname__r~   r   r   __classcell__rm   rm   r   rn   r|   D  s    r|   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rdnd}
tj|
|	d d|}t |t| |
 ddd|	d d}t|| |g} |durt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   r0   r            r3      r-   zXUnknown Inception-ResNet block type. Expects "block35", "block17" or "block8", but got: _r#   _mixedr%   NT_conv)r4   rq   r'   rs   r6   )
rK   rB   strr   rH   r   rN   	int_shaper|   rv   )rb   r*   r+   r,   r4   rc   rd   re   rg   
block_namerh   mixeduprm   rm   rn   rP   S  sT   #

	rP   z7keras.applications.inception_resnet_v2.preprocess_inputc                 C   s   t j| |ddS )NrC   )r   mode)r   preprocess_input)rb   r   rm   rm   rn   r     s   r   z9keras.applications.inception_resnet_v2.decode_predictionsr!   c                 C   s   t j| |dS )N)top)r   decode_predictions)predsr   rm   rm   rn   r     s   r    )r   reterror)Tr
   NNNr   r   )r   r"   rp   FN)rp   )N)r!   )$__doc__tensorflow.compat.v2compatv2rC   	keras.srcsrckerasr   r   keras_layerskeras.src.applicationsr   keras.src.enginer   keras.src.layersr   keras.src.utilsr   r    tensorflow.python.util.tf_exportr	   rX   ro   rK   utilsregister_keras_serializableLayerr|   rP   r   r   PREPROCESS_INPUT_DOCformatPREPROCESS_INPUT_RET_DOC_TFPREPROCESS_INPUT_ERROR_DOCrm   rm   rm   rn   <module>   s\    j
.
R