a
    hA                     @   sv  d dl Z d dlmZ d dlmZ d dl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 g 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&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Z/G d&d' d'eZ0e ed(e-j1fd)dd*d+ee- e'ee&d,d-d.Z2e ed(e.j1fd)dd*d+ee. e'ee&d,d/d0Z3e ed(e/j1fd)dd*d+ee/ e'ee&d,d1d2Z4e ed(e0j1fd)dd*d+ee0 e'ee&d,d3d4Z5dS )5    N)OrderedDict)partial)AnyOptional)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__ I/var/www/auris/lib/python3.9/site-packages/torchvision/models/densenet.pyr*       s    

z_DenseLayer.__init__)inputsr    c                 C   s&   t |d}| | | |}|S Nr
   )torchcatr1   r/   r-   )r6   r;   Zconcated_featuresbottleneck_outputr9   r9   r:   bn_function/   s    z_DenseLayer.bn_function)inputr    c                 C   s   |D ]}|j r dS qdS )NTF)Zrequires_grad)r6   rA   Ztensorr9   r9   r:   any_requires_grad5   s    z_DenseLayer.any_requires_gradc                    s$    fdd}t j|g|R ddiS )Nc                     s
     | S N)r@   )r;   r6   r9   r:   closure=   s    z7_DenseLayer.call_checkpoint_bottleneck.<locals>.closureZuse_reentrantF)cp
checkpoint)r6   rA   rE   r9   rD   r:   call_checkpoint_bottleneck;   s    z&_DenseLayer.call_checkpoint_bottleneckc                 C   s   d S rC   r9   r6   rA   r9   r9   r:   forwardB   s    z_DenseLayer.forwardc                 C   s   d S rC   r9   rI   r9   r9   r:   rJ   F   s    c                 C   s   t |tr|g}n|}| jrD| |rDtj r8td| |}n
| 	|}| 
| | |}| jdkrtj|| j| jd}|S )Nz%Memory Efficient not supported in JITr   )ptraining)
isinstancer   r   rB   r=   jitZis_scripting	ExceptionrH   r@   r4   r3   r2   r   FZdropoutrL   )r6   rA   Zprev_featuresr?   new_featuresr9   r9   r:   rJ   L   s    



)F)__name__
__module____qualname__intr5   boolr*   listr   r@   rB   r=   rN   ZunusedrH   Z_overload_methodrJ   __classcell__r9   r9   r7   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)	r6   rZ   r   r   r   r   r   ilayerr7   r9   r:   r*   c   s    	

z_DenseBlock.__init__)init_featuresr    c                 C   s6   |g}|   D ]\}}||}|| qt|dS r<   )itemsappendr=   r>   )r6   r_   featuresnamer^   rQ   r9   r9   r:   rJ   w   s
    z_DenseBlock.forward)F)rR   rS   rT   _versionrU   r5   rV   r*   r   rJ   rX   r9   r9   r7   r:   rY   `   s   	 rY   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normr.   relur0   convZ	AvgPool2dpool)r6   r   rf   r7   r9   r:   r*      s
    
z_Transition.__init__)rR   rS   rT   rU   r*   rX   r9   r9   r7   r:   re      s   re   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(   )rZ   r   r   r   r   r   zdenseblock%d)r   rf   ztransition%dZnorm5r   )r)   r*   r	   r+   
Sequentialr   r0   r,   r.   Z	MaxPool2drb   	enumeraterY   r\   lenre   ZLinear
classifiermodulesrM   initZkaiming_normal_ZweightZ	constant_r%   )r6   r   rs   rt   r   r   ru   r   Znum_featuresr]   rZ   blockZtransmr7   r9   r:   r*      sJ    

zDenseNet.__init__)xr    c                 C   s>   |  |}tj|dd}t|d}t|d}| |}|S )NTr!   )r
   r
   r
   )rb   rP   rg   Zadaptive_avg_pool2dr=   flattenrz   )r6   r   rb   outr9   r9   r:   rJ      s    

zDenseNet.forward)rj   rk   rp   rq   r   rr   F)rR   rS   rT   __doc__rU   tupler5   rV   r*   r   rJ   rX   r9   r9   r7   r:   r      s&          <r   )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_dictrW   keysmatchgroupZload_state_dict)r   r   r   patternZ
state_dictkeyresZnew_keyr9   r9   r:   _load_state_dict   s    
r   )r   rs   rt   r   r   kwargsr    c                 K   sL   |d urt |dt|jd  t| ||fi |}|d urHt|||d |S )Nru   
categories)r   r   r   )r   ry   metar   r   )r   rs   rt   r   r   r   r   r9   r9   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                	   @   sB   e Zd Zedeeddi 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_sizeihy ImageNet-1KgƛR@g|?5V@zacc@1zacc@5gy&1@gQ>@Z
num_paramsZ_metricsZ_ops
_file_sizeurlZ
transformsr   N	rR   rS   rT   r   r   r   _COMMON_METAIMAGENET1K_V1DEFAULTr9   r9   r9   r:   r     s"   
r   c                	   @   sB   e Zd Zedeeddi 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   gFHS@gp=
cW@r   gx@gV-[@r   r   Nr   r9   r9   r9   r:   r     s"   
r   c                	   @   sB   e Zd Zedeeddi 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   gfffffR@g$3W@r   gzG
@gvZK@r   r   Nr   r9   r9   r9   r:   r   3  s"   
r   c                	   @   sB   e Zd Zedeeddi 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   gMbX9S@gHzWW@r   gDl)@gZd;WS@r   r   Nr   r9   r9   r9   r:   r   G  s"   
r   Z
pretrained)r   T)r   r   )r   r   r   r    c                 K   s"   t | } tddd| |fi |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:
    rj   rk   rp   )r   verifyr   r   r   r   r9   r9   r:   r   [  s    
r   c                 K   s"   t | } tddd| |fi |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   )rl   rm   $   rn   `   )r   r   r   r   r9   r9   r:   r   u  s    
r   c                 K   s"   t | } tddd| |fi |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:
    rj   )rl   rm   rj   rj   rp   )r   r   r   r   r9   r9   r:   r     s    
r   c                 K   s"   t | } tddd| |fi |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:
    rj   )rl   rm   r   rj   rp   )r   r   r   r   r9   r9   r:   r     s    
r   )6r   collectionsr   	functoolsr   typingr   r   r=   Ztorch.nnr+   Ztorch.nn.functionalZ
functionalrP   Ztorch.utils.checkpointutilsrG   rF   r   Ztransforms._presetsr   r	   Z_apir   r   r   Z_metar   _utilsr   r   __all__Moduler   Z
ModuleDictrY   rw   re   r   rV   r   rU   r   r   r   r   r   r   r   r   r   r   r   r   r9   r9   r9   r:   <module>   s`   A	U$$$