o
    #j>                     @   s"  d dl Z d dlZd dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZ g Zddd	d
ddZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZdd Zd&ddZd&ddZd&d d!Zd&d"d#Zd&d$d%ZdS )'    N)nn)	ParamAttr)AdaptiveAvgPool2D	AvgPool2D	BatchNormConv2DDropoutLinear	MaxPool2D)Uniform)get_weights_path_from_url)zYhttps://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DenseNet121_pretrained.pdparamsZ db1b239ed80a905290fd8b01d3af08e4)zYhttps://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DenseNet161_pretrained.pdparamsZ 62158869cb315098bd25ddbfd308a853)zYhttps://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DenseNet169_pretrained.pdparamsZ 82cc7c635c3f19098c748850efb2d796)zYhttps://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DenseNet201_pretrained.pdparamsZ 16ca29565a7712329cf9e36e02caaf58)zYhttps://paddle-imagenet-models-name.bj.bcebos.com/dygraph/DenseNet264_pretrained.pdparamsZ 3270ce516b85370bba88cfdd9f60bff4)densenet121densenet161densenet169densenet201densenet264c                       .   e Zd Z				d fdd	Zdd Z  ZS )	BNACConvLayer   r   reluc              
      s8   t    t||d| _t||||||t dd| _d S )NactFZin_channelsZout_channelskernel_sizestridepaddinggroupsweight_attr	bias_attr)super__init__r   _batch_normr   r   _convselfnum_channelsnum_filtersfilter_sizer   padr   r   	__class__ ^/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/vision/models/densenet.pyr    ;   s   

zBNACConvLayer.__init__c                 C      |  |}| |}|S N)r!   r"   r$   inputyr+   r+   r,   forwardS      

zBNACConvLayer.forwardr   r   r   r   __name__
__module____qualname__r    r2   __classcell__r+   r+   r)   r,   r   :       r   c                       $   e Zd Z fddZdd Z  ZS )
DenseLayerc                    sZ   t    || _t||| dddd| _t|| |dddd| _|r+t|dd| _d S d S )Nr   r   r%   r&   r'   r(   r      Zdownscale_in_infer)pmode)r   r    dropoutr   bn_ac_func1bn_ac_func2r   dropout_func)r$   r%   growth_ratebn_sizerA   r)   r+   r,   r    Z   s&   
zDenseLayer.__init__c                 C   s:   |  |}| |}| jr| |}tj||gdd}|S )Nr   )Zaxis)rB   rC   rA   rD   paddleconcat)r$   r0   convr+   r+   r,   r2   q   s   


zDenseLayer.forwardr5   r+   r+   r)   r,   r<   Y   s    r<   c                       s(   e Zd Z	d fdd	Zdd Z  ZS )
DenseBlockNc           	         sb   t    || _g | _|}t|D ]}| j| | d|d  t||||d || }qd S )N_r   )r%   rE   rF   rA   )r   r    rA   dense_layer_funcrangeappendadd_sublayerr<   )	r$   r%   
num_layersrF   rE   rA   nameZpre_channellayerr)   r+   r,   r    {   s$   

zDenseBlock.__init__c                 C   s   |}| j D ]}||}q|S r.   )rL   )r$   r0   rI   funcr+   r+   r,   r2      s   

zDenseBlock.forwardr.   r5   r+   r+   r)   r,   rJ   z   s    rJ   c                       r;   )TransitionLayerc                    s2   t    t||dddd| _tdddd| _d S )Nr   r   r=      r   r   r   )r   r    r   conv_ac_funcr   
pool2d_avg)r$   r%   num_output_featuresr)   r+   r,   r       s   
zTransitionLayer.__init__c                 C   r-   r.   )rW   rX   r/   r+   r+   r,   r2      r3   zTransitionLayer.forwardr5   r+   r+   r)   r,   rT      s    rT   c                       r   )	ConvBNLayerr   r   r   c              
      s8   t    t||||||t dd| _t||d| _d S )NFr   r   )r   r    r   r   r"   r   r!   r#   r)   r+   r,   r       s   


zConvBNLayer.__init__c                 C   r-   r.   )r"   r!   r/   r+   r+   r,   r2      r3   zConvBNLayer.forwardr4   r5   r+   r+   r)   r,   rZ      r:   rZ   c                       s4   e Zd ZdZ					d fdd	Zd	d
 Z  ZS )DenseNeta  DenseNet model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_.

    Args:
        layers (int, optional): Layers of DenseNet. Default: 121.
        bn_size (int, optional): Expansion of growth rate in the middle layer. Default: 4.
        dropout (float, optional): Dropout rate. Default: :math:`0.0`.
        num_classes (int, optional): Output dim of last fc layer. If num_classes <= 0, last fc layer
            will not be defined. Default: 1000.
        with_pool (bool, optional): Use pool before the last fc layer or not. Default: True.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of DenseNet model.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.vision.models import DenseNet

            >>> # Build model
            >>> densenet = DenseNet()

            >>> x = paddle.rand([1, 3, 224, 224])
            >>> out = densenet(x)

            >>> print(out.shape)
            [1, 1000]
    y                Tc                    s  t    || _|| _g d}||v sJ d| d| ddg dfddg d	fddg d
fddg dfddg dfd}|| \}}	}
td|ddddd| _tdddd| _|
| _g | _	g | _
|}|}t|
D ]O\}}| j	| d|d  t||||	|dt|d  d |||	  }|}|t|
d kr| j
| d|d  dt||d d |d }|d }qit|dd| _| jrtd| _| jdkrdt|d  }t||tt| |dt d| _d S d S )N)r\              zsupported layers are z but input layer is @       )            `   0   )rf   rg   $   rh   )rf   rg   re   re   )rf   rg   rk   re   )rf   rg   rd   rk   r>      rU   r   )r%   r&   r'   r   r(   r   r   rV   Zdb_conv_rI   )r%   rP   rF   rE   rA   rQ   Ztr_convZ_blk)r%   rY   r   r   g      ?)Zinitializer)r   r   )r   r    num_classes	with_poolrZ   
conv1_funcr
   
pool2d_maxblock_configdense_block_func_listtransition_func_list	enumeraterN   rO   rJ   strlenrT   r   
batch_normr   rX   mathsqrtr	   r   r   out)r$   layersrF   rA   rn   ro   Zsupported_layersZdensenet_specZnum_init_featuresrE   rr   Zpre_num_channelsZnum_featuresirP   Zstdvr)   r+   r,   r       s   

	

zDenseNet.__init__c                 C   s   |  |}| |}t| jD ]\}}| j| |}|t| jd kr*| j| |}q| |}| jr8| 	|}| j
dkrJtj|ddd}| |}|S )Nr   r   )Z
start_axisZ	stop_axis)rp   rq   ru   rr   rs   rw   rt   rx   ro   rX   rn   rG   flattenr{   )r$   r0   rI   r}   rP   r1   r+   r+   r,   r2   <  s   





zDenseNet.forward)r\   r]   r^   r_   T)r6   r7   r8   __doc__r    r2   r9   r+   r+   r)   r,   r[      s     Rr[   c                 K   s^   t dd|i|}|r-| tv sJ |  dtt|  d t|  d }t|}|| |S )Nr|   zJ model do not have a pretrained model now, you should set pretrained=Falser   r   r+   )r[   
model_urlsr   rG   loadZset_dict)archr|   
pretrainedkwargsmodelZweight_pathparamr+   r+   r,   	_densenetQ  s   


r   Fc                 K      t dd| fi |S )a  DenseNet 121-layer model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
            on ImageNet. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`DenseNet <api_paddle_vision_models_DenseNet>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of DenseNet 121-layer model.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.vision.models import densenet121

            >>> # Build model
            >>> model = densenet121()

            >>> # Build model and load imagenet pretrained weight
            >>> # model = densenet121(pretrained=True)

            >>> x = paddle.rand([1, 3, 224, 224])
            >>> out = model(x)

            >>> print(out.shape)
            [1, 1000]
    r   r\   r   r   r   r+   r+   r,   r   a     r   c                 K   r   )a  DenseNet 161-layer model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
            on ImageNet. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`DenseNet <api_paddle_vision_models_DenseNet>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of DenseNet 161-layer model.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.vision.models import densenet161

            >>> # Build model
            >>> model = densenet161()

            >>> # Build model and load imagenet pretrained weight
            >>> # model = densenet161(pretrained=True)

            >>> x = paddle.rand([1, 3, 224, 224])
            >>> out = model(x)

            >>> print(out.shape)
            [1, 1000]
    r   r`   r   r   r+   r+   r,   r     r   r   c                 K   r   )a  DenseNet 169-layer model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
            on ImageNet. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`DenseNet <api_paddle_vision_models_DenseNet>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of DenseNet 169-layer model.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.vision.models import densenet169

            >>> # Build model
            >>> model = densenet169()

            >>> # Build model and load imagenet pretrained weight
            >>> # model = densenet169(pretrained=True)

            >>> x = paddle.rand([1, 3, 224, 224])
            >>> out = model(x)

            >>> print(out.shape)
            [1, 1000]
    r   ra   r   r   r+   r+   r,   r     r   r   c                 K   r   )a  DenseNet 201-layer model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
            on ImageNet. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`DenseNet <api_paddle_vision_models_DenseNet>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of DenseNet 201-layer model.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.vision.models import densenet201

            >>> # Build model
            >>> model = densenet201()

            >>> # Build model and load imagenet pretrained weight
            >>> # model = densenet201(pretrained=True)
            >>> x = paddle.rand([1, 3, 224, 224])
            >>> out = model(x)

            >>> print(out.shape)
            [1, 1000]
    r   rb   r   r   r+   r+   r,   r     s   r   c                 K   r   )a  DenseNet 264-layer model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_.

    Args:
        pretrained (bool, optional): Whether to load pre-trained weights. If True, returns a model pre-trained
            on ImageNet. Default: False.
        **kwargs (optional): Additional keyword arguments. For details, please refer to :ref:`DenseNet <api_paddle_vision_models_DenseNet>`.

    Returns:
        :ref:`api_paddle_nn_Layer`. An instance of DenseNet 264-layer model.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.vision.models import densenet264

            >>> # Build model
            >>> model = densenet264()

            >>> # Build model and load imagenet pretrained weight
            >>> # model = densenet264(pretrained=True)

            >>> x = paddle.rand([1, 3, 224, 224])
            >>> out = model(x)

            >>> print(out.shape)
            [1, 1000]
    r   rc   r   r   r+   r+   r,   r     r   r   )F) ry   rG   r   Zpaddle.base.param_attrr   Z	paddle.nnr   r   r   r   r   r	   r
   Zpaddle.nn.initializerr   Zpaddle.utils.downloadr   __all__r   ZLayerr   r<   rJ   rT   rZ   r[   r   r   r   r   r   r   r+   r+   r+   r,   <module>   s6   $	! 

!
!
! 