U
    ­Ê¹j¤A  ã                	   @   sŒ  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	 d dl
Z
d dlmZ d dlm  m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mZmZ d	dlmZ d	dlmZm Z  dddddddddg	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(ej"ee)ddœdd„Z*e+e	e+e+e+e+f e+ee e)ee(d œd!d"„Z,d#ed$d%d&œZ-G d'd„ deƒZ.G d(d„ deƒZ/G d)d„ deƒZ0G d*d„ deƒZ1eƒ e d+e.j2fd,�dd-d.œee. e)ee(d/œd0d„ƒƒZ3eƒ e d+e/j2fd,�dd-d.œee/ e)ee(d/œd1d„ƒƒZ4eƒ e d+e0j2fd,�dd-d.œee0 e)ee(d/œd2d„ƒƒZ5eƒ e d+e1j2fd,�dd-d.œee1 e)ee(d/œd3d„ƒƒZ6dS )4é    N)ÚOrderedDict)Úpartial)ÚAnyÚListÚOptionalÚTuple)ÚTensoré   )ÚImageClassification)Ú_log_api_usage_onceé   )Úregister_modelÚWeightsÚWeightsEnum)Ú_IMAGENET_CATEGORIES)Ú_ovewrite_named_paramÚhandle_legacy_interfaceÚDenseNetÚDenseNet121_WeightsÚDenseNet161_WeightsÚDenseNet169_WeightsÚDenseNet201_WeightsÚdensenet121Údensenet161Údensenet169Údensenet201c                       s¶   e Zd Zdeeeeeddœ‡ fdd„Zee edœdd„Z	ee ed	œd
d„Z
ejjee ed	œdd„ƒZejjee ed	œdd„ƒZejjeed	œdd„ƒZeed	œdd„Z‡  ZS )Ú_DenseLayerFN)Únum_input_featuresÚgrowth_rateÚbn_sizeÚ	drop_rateÚmemory_efficientÚreturnc                    sŒ   t ƒ  ¡  t |¡| _tjdd�| _tj||| dddd�| _t || ¡| _	tjdd�| _
tj|| |ddddd�| _t|ƒ| _|| _d S )NT©Zinplacer   F©Úkernel_sizeÚstrideÚbiasé   ©r%   r&   Úpaddingr'   )ÚsuperÚ__init__ÚnnÚBatchNorm2dÚnorm1ÚReLUÚrelu1ÚConv2dÚconv1Únorm2Úrelu2Úconv2Úfloatr    r!   )Úselfr   r   r   r    r!   ©Ú	__class__© úZ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/models/densenet.pyr,       s    

z_DenseLayer.__init__)Úinputsr"   c                 C   s&   t  |d¡}|  |  |  |¡¡¡}|S ©Nr   )ÚtorchÚcatr3   r1   r/   )r8   r=   Zconcated_featuresÚbottleneck_outputr;   r;   r<   Úbn_function/   s    z_DenseLayer.bn_function)Úinputr"   c                 C   s   |D ]}|j r dS qdS )NTF)Zrequires_grad)r8   rC   Ztensorr;   r;   r<   Úany_requires_grad5   s    z_DenseLayer.any_requires_gradc                    s   ‡ fdd„}t j|f|žŽ S )Nc                     s
   ˆ   | ¡S ©N)rB   )r=   ©r8   r;   r<   Úclosure=   s    z7_DenseLayer.call_checkpoint_bottleneck.<locals>.closure)ÚcpÚ
checkpoint)r8   rC   rG   r;   rF   r<   Úcall_checkpoint_bottleneck;   s    z&_DenseLayer.call_checkpoint_bottleneckc                 C   s   d S rE   r;   ©r8   rC   r;   r;   r<   ÚforwardB   s    z_DenseLayer.forwardc                 C   s   d S rE   r;   rK   r;   r;   r<   rL   F   s    c                 C   s†   t |tƒr|g}n|}| jrD|  |¡rDtj ¡ r8tdƒ‚|  |¡}n
|  	|¡}|  
|  |  |¡¡¡}| jdkr‚tj|| j| jd�}|S )Nz%Memory Efficient not supported in JITr   )ÚpÚtraining)Ú
isinstancer   r!   rD   r?   ÚjitZis_scriptingÚ	ExceptionrJ   rB   r6   r5   r4   r    ÚFZdropoutrN   )r8   rC   Zprev_featuresrA   Únew_featuresr;   r;   r<   rL   L   s    



)F)Ú__name__Ú
__module__Ú__qualname__Úintr7   Úboolr,   r   r   rB   rD   r?   rP   ZunusedrJ   Z_overload_methodrL   Ú__classcell__r;   r;   r9   r<   r      s$    ÿ    þr   c                	       sD   e Zd ZdZd
eeeeeeddœ‡ fdd„Zeedœdd	„Z	‡  Z
S )Ú_DenseBlockr	   FN)Ú
num_layersr   r   r   r    r!   r"   c           	         sJ   t ƒ  ¡  t|ƒD ]2}t|||  ||||d�}|  d|d  |¡ qd S )N)r   r   r    r!   zdenselayer%dr   )r+   r,   Úranger   Ú
add_module)	r8   r[   r   r   r   r    r!   ÚiÚlayerr9   r;   r<   r,   c   s    	

ûz_DenseBlock.__init__)Úinit_featuresr"   c                 C   s6   |g}|   ¡ D ]\}}||ƒ}| |¡ qt |d¡S r>   )ÚitemsÚappendr?   r@   )r8   r`   ÚfeaturesÚnamer_   rS   r;   r;   r<   rL   w   s
    z_DenseBlock.forward)F)rT   rU   rV   Ú_versionrW   r7   rX   r,   r   rL   rY   r;   r;   r9   r<   rZ   `   s   	 ùørZ   c                       s&   e Zd Zeeddœ‡ fdd„Z‡  ZS )Ú_TransitionN)r   Únum_output_featuresr"   c                    sN   t ƒ  ¡  t |¡| _tjdd�| _tj||dddd�| _tj	ddd�| _
d S )NTr#   r   Fr$   r	   )r%   r&   )r+   r,   r-   r.   Znormr0   Úrelur2   ÚconvZ	AvgPool2dÚpool)r8   r   rg   r9   r;   r<   r,   €   s
    
z_Transition.__init__)rT   rU   rV   rW   r,   rY   r;   r;   r9   r<   rf      s   rf   c                
       sR   e Zd ZdZdeeeeeef eeeeed	d
œ‡ fdd„Ze	e	dœdd„Z
‡  ZS )r   aK  Densenet-BC model class, based on
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_.

    Args:
        growth_rate (int) - how many filters to add each layer (`k` in paper)
        block_config (list of 4 ints) - how many layers in each pooling block
        num_init_features (int) - the number of filters to learn in the first convolution layer
        bn_size (int) - multiplicative factor for number of bottle neck layers
          (i.e. bn_size * k features in the bottleneck layer)
        drop_rate (float) - dropout rate after each dense layer
        num_classes (int) - number of classification classes
        memory_efficient (bool) - If True, uses checkpointing. Much more memory efficient,
          but slower. Default: *False*. See `"paper" <https://arxiv.org/pdf/1707.06990.pdf>`_.
    é    ©é   é   é   é   é@   é   r   éè  FN)r   Úblock_configÚnum_init_featuresr   r    Únum_classesr!   r"   c                    s’  t ƒ  ¡  t| ƒ t tdtjd|ddddd�fdt |¡fdtjd	d
�fdtj	dddd�fgƒ¡| _
|}t|ƒD ]|\}	}
t|
|||||d�}| j
 d|	d  |¡ ||
|  }|	t|ƒd krrt||d d�}| j
 d|	d  |¡ |d }qr| j
 dt |¡¡ t ||¡| _|  ¡ D ]r}t|tjƒ�r<tj |j¡ nNt|tjƒ�rltj |jd¡ tj |jd¡ nt|tjƒ�rtj |jd¡ �qd S )NZconv0r(   é   r	   Fr)   Znorm0Zrelu0Tr#   Zpool0r   )r%   r&   r*   )r[   r   r   r   r    r!   zdenseblock%d)r   rg   ztransition%dZnorm5r   )r+   r,   r   r-   Ú
Sequentialr   r2   r.   r0   Z	MaxPool2drc   Ú	enumeraterZ   r]   Úlenrf   ZLinearÚ
classifierÚmodulesrO   ÚinitZkaiming_normal_ÚweightZ	constant_r'   )r8   r   rt   ru   r   r    rv   r!   Znum_featuresr^   r[   ÚblockZtransÚmr9   r;   r<   r,   ˜   sJ    
üÿÿú
zDenseNet.__init__)Úxr"   c                 C   s>   |   |¡}tj|dd�}t |d¡}t |d¡}|  |¡}|S )NTr#   )r   r   r   )rc   rR   rh   Zadaptive_avg_pool2dr?   Úflattenr{   )r8   r�   rc   Úoutr;   r;   r<   rL   Ô   s    

zDenseNet.forward)rk   rl   rq   rr   r   rs   F)rT   rU   rV   Ú__doc__rW   r   r7   rX   r,   r   rL   rY   r;   r;   r9   r<   r   ˆ   s&          ø÷<)ÚmodelÚweightsÚprogressr"   c                 C   sl   t  d¡}|j|dd�}t| ¡ ƒD ]8}| |¡}|r$| d¡| d¡ }|| ||< ||= q$|  |¡ d S )Nz]^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$T)r‡   Z
check_hashr   r	   )ÚreÚcompileZget_state_dictÚlistÚkeysÚmatchÚgroupZload_state_dict)r…   r†   r‡   ÚpatternZ
state_dictÚkeyÚresZnew_keyr;   r;   r<   Ú_load_state_dictÝ   s    ÿ
r‘   )r   rt   ru   r†   r‡   Úkwargsr"   c                 K   sH   |d k	rt |dt|jd ƒƒ t| ||f|Ž}|d k	rDt|||d� |S )Nrv   Ú
categories)r…   r†   r‡   )r   rz   Úmetar   r‘   )r   rt   ru   r†   r‡   r’   r…   r;   r;   r<   Ú	_densenetð   s    r•   )é   r–   z*https://github.com/pytorch/vision/pull/116z'These weights are ported from LuaTorch.)Zmin_sizer“   ZrecipeZ_docsc                	   @   s>   e Zd Zedeedd�edddddœid	d
dœ–d�ZeZdS )r   z<https://download.pytorch.org/models/densenet121-a639ec97.pthéà   ©Z	crop_sizeih¿y úImageNet-1Kg²�ï§Æ›R@g‘í|?5þV@©zacc@1zacc@5gyé&1¬@g¸…ëQØ>@©Z
num_paramsZ_metricsZ_opsÚ
_file_size©ÚurlZ
transformsr”   N©	rT   rU   rV   r   r   r
   Ú_COMMON_METAÚIMAGENET1K_V1ÚDEFAULTr;   r;   r;   r<   r     s   
þÿöýc                	   @   s>   e Zd Zedeedd�edddddœid	d
dœ–d�ZeZdS )r   z<https://download.pytorch.org/models/densenet161-8d451a50.pthr—   r˜   i(£µr™   gF¶óýÔHS@g¤p=
×cW@rš   g¶óýÔxé@gV-²�—[@r›   r�   NrŸ   r;   r;   r;   r<   r     s   
þÿöýc                	   @   s>   e Zd Zedeedd�edddddœid	d
dœ–d�ZeZdS )r   z<https://download.pytorch.org/models/densenet169-b2777c0a.pthr—   r˜   ihç× r™   gfffffæR@gÝ$�•3W@rš   gáz®Gá
@g´Èv¾ŸZK@r›   r�   NrŸ   r;   r;   r;   r<   r   3  s   
þÿöýc                	   @   s>   e Zd Zedeedd�edddddœid	d
dœ–d�ZeZdS )r   z<https://download.pytorch.org/models/densenet201-c1103571.pthr—   r˜   ihc1r™   gÓMbX9S@gHáz®WW@rš   gD‹lçû)@gZd;ßWS@r›   r�   NrŸ   r;   r;   r;   r<   r   G  s   
þÿöýZ
pretrained)r†   T)r†   r‡   )r†   r‡   r’   r"   c                 K   s   t  | ¡} tddd| |f|ŽS )a{  Densenet-121 model from
    `Densely Connected Convolutional Networks <https://arxiv.org/abs/1608.06993>`_.

    Args:
        weights (:class:`~torchvision.models.DenseNet121_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.DenseNet121_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.densenet.DenseNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/densenet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.DenseNet121_Weights
        :members:
    rk   rl   rq   )r   Úverifyr•   ©r†   r‡   r’   r;   r;   r<   r   [  s    
c                 K   s   t  | ¡} tddd| |f|ŽS )a{  Densenet-161 model from
    `Densely Connected Convolutional Networks <https://arxiv.org/abs/1608.06993>`_.

    Args:
        weights (:class:`~torchvision.models.DenseNet161_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.DenseNet161_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.densenet.DenseNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/densenet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.DenseNet161_Weights
        :members:
    é0   )rm   rn   é$   ro   é`   )r   r£   r•   r¤   r;   r;   r<   r   u  s    
c                 K   s   t  | ¡} tddd| |f|ŽS )a{  Densenet-169 model from
    `Densely Connected Convolutional Networks <https://arxiv.org/abs/1608.06993>`_.

    Args:
        weights (:class:`~torchvision.models.DenseNet169_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.DenseNet169_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.densenet.DenseNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/densenet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.DenseNet169_Weights
        :members:
    rk   )rm   rn   rk   rk   rq   )r   r£   r•   r¤   r;   r;   r<   r   �  s    
c                 K   s   t  | ¡} tddd| |f|ŽS )a{  Densenet-201 model from
    `Densely Connected Convolutional Networks <https://arxiv.org/abs/1608.06993>`_.

    Args:
        weights (:class:`~torchvision.models.DenseNet201_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.DenseNet201_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.densenet.DenseNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/densenet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.DenseNet201_Weights
        :members:
    rk   )rm   rn   r¥   rk   rq   )r   r£   r•   r¤   r;   r;   r<   r   ©  s    
)7rˆ   Úcollectionsr   Ú	functoolsr   Útypingr   r   r   r   r?   Ztorch.nnr-   Ztorch.nn.functionalZ
functionalrR   Ztorch.utils.checkpointÚutilsrI   rH   r   Ztransforms._presetsr
   r   Z_apir   r   r   Ú_metar   Z_utilsr   r   Ú__all__ÚModuler   Z
ModuleDictrZ   rx   rf   r   rX   r‘   rW   r•   r    r   r   r   r   r¡   r   r   r   r   r;   r;   r;   r<   Ú<module>   sr   ÷A	Uùü$$$