3
Á3W^ )  ã               @   sò   d dl Z d dlZd dljZd dljjZd dljj	Z
d dlmZ ddlmZ ddddd	gZd
ddddœZ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dd„ Zdd„ Zd"dd„Zd#dd	„Zd$d d„Zd%d!d„ZdS )&é    N)ÚOrderedDicté   )Úload_state_dict_from_urlÚDenseNetÚdensenet121Údensenet169Údensenet201Údensenet161z<https://download.pytorch.org/models/densenet121-a639ec97.pthz<https://download.pytorch.org/models/densenet169-b2777c0a.pthz<https://download.pytorch.org/models/densenet201-c1103571.pthz<https://download.pytorch.org/models/densenet161-8d451a50.pth)r   r   r   r	   c                s   ‡ ‡‡fdd„}|S )Nc                 s    t j| dƒ}ˆ ˆˆ|ƒƒƒ}|S )Nr   )ÚtorchÚcat)ÚinputsZconcated_featuresÚbottleneck_output)ÚconvÚnormÚrelu© úB/tmp/pip-build-7wfi2gtz/torchvision/torchvision/models/densenet.pyÚbn_function   s    z)_bn_function_factory.<locals>.bn_functionr   )r   r   r   r   r   )r   r   r   r   Ú_bn_function_factory   s    r   c                   s&   e Zd Zd‡ fdd„	Zdd„ Z‡  ZS )Ú_DenseLayerFc          
      s¼   t t| ƒjƒ  | jdtj|ƒƒf | jdtjdd�ƒf | jdtj||| dddd�ƒf | jd	tj|| ƒƒf | jd
tjdd�ƒf | jdtj|| |ddddd�ƒf || _|| _	d S )NÚnorm1Úrelu1T)ÚinplaceÚconv1r   F)Úkernel_sizeÚstrideÚbiasÚnorm2Úrelu2Úconv2é   )r   r   Úpaddingr   )
Úsuperr   Ú__init__Ú
add_moduleÚnnÚBatchNorm2dÚReLUÚConv2dÚ	drop_rateÚmemory_efficient)ÚselfÚnum_input_featuresÚgrowth_rateÚbn_sizer)   r*   )Ú	__class__r   r   r#      s    z_DenseLayer.__init__c             G   s|   t | j| j| jƒ}| jr<tdd„ |D ƒƒr<tj|f|žŽ }n||Ž }| j| j	| j
|ƒƒƒ}| jdkrxtj|| j| jd�}|S )Nc             s   s   | ]}|j V  qd S )N)Zrequires_grad)Ú.0Zprev_featurer   r   r   ú	<genexpr>/   s    z&_DenseLayer.forward.<locals>.<genexpr>r   )ÚpÚtraining)r   r   r   r   r*   ÚanyÚcpÚ
checkpointr   r   r   r)   ÚFZdropoutr3   )r+   Zprev_featuresr   r   Únew_featuresr   r   r   Úforward-   s    


z_DenseLayer.forward)F)Ú__name__Ú
__module__Ú__qualname__r#   r9   Ú__classcell__r   r   )r/   r   r      s   r   c                   s&   e Zd Zd‡ fdd„	Zdd„ Z‡  ZS )Ú_DenseBlockFc       	         sR   t t| ƒjƒ  x>t|ƒD ]2}t|||  ||||d�}| jd|d  |ƒ qW d S )N)r-   r.   r)   r*   zdenselayer%dr   )r"   r>   r#   Úranger   r$   )	r+   Ú
num_layersr,   r.   r-   r)   r*   ÚiÚlayer)r/   r   r   r#   ;   s    
z_DenseBlock.__init__c             C   s:   |g}x&| j ƒ D ]\}}||Ž }|j|ƒ qW tj|dƒS )Nr   )Znamed_childrenÚappendr
   r   )r+   Zinit_featuresÚfeaturesÚnamerB   r8   r   r   r   r9   G   s
    z_DenseBlock.forward)F)r:   r;   r<   r#   r9   r=   r   r   )r/   r   r>   :   s   r>   c                   s   e Zd Z‡ fdd„Z‡  ZS )Ú_Transitionc          	      sj   t t| ƒjƒ  | jdtj|ƒƒ | jdtjdd�ƒ | jdtj||dddd�ƒ | jd	tjd
d
d�ƒ d S )Nr   r   T)r   r   r   F)r   r   r   Úpoolé   )r   r   )	r"   rF   r#   r$   r%   r&   r'   r(   Z	AvgPool2d)r+   r,   Únum_output_features)r/   r   r   r#   P   s    z_Transition.__init__)r:   r;   r<   r#   r=   r   r   )r/   r   rF   O   s   rF   c                   s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )r   aI  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   éè  Fc                s–  t t| ƒjƒ  tjtdtjd|ddddd�fdtj|ƒfdtjd	d
�fdtj	dddd�fgƒƒ| _
|}xˆt|ƒD ]|\}	}
t|
|||||d�}| j
jd|	d  |ƒ ||
|  }|	t|ƒd krpt||d d�}| j
jd|	d  |ƒ |d }qpW | j
jdtj|ƒƒ tj||ƒ| _x~| jƒ D ]r}t|tjƒ�r>tjj|jƒ nNt|tjƒ�rntjj|jdƒ tjj|jdƒ nt|tjƒ�rtjj|jdƒ �qW d S )NZconv0r    é   rH   F)r   r   r!   r   Znorm0Zrelu0T)r   Zpool0r   )r   r   r!   )r@   r,   r.   r-   r)   r*   zdenseblock%d)r,   rI   ztransition%dZnorm5r   )r"   r   r#   r%   Ú
Sequentialr   r(   r&   r'   Z	MaxPool2drD   Ú	enumerater>   r$   ÚlenrF   ZLinearÚ
classifierÚmodulesÚ
isinstanceÚinitZkaiming_normal_ZweightZ	constant_r   )r+   r-   Úblock_configÚnum_init_featuresr.   r)   Znum_classesr*   Znum_featuresrA   r@   ÚblockZtransÚm)r/   r   r   r#   i   sB    
zDenseNet.__init__c             C   s>   | j |ƒ}tj|dd�}tj|dƒ}tj|dƒ}| j|ƒ}|S )NT)r   r   )r   r   )rD   r7   r   Zadaptive_avg_pool2dr
   ÚflattenrV   )r+   ÚxrD   Úoutr   r   r   r9   š   s    

zDenseNet.forward©rK   rL   rM   rN   )rJ   ra   rO   rP   r   rQ   F)r:   r;   r<   Ú__doc__r#   r9   r=   r   r   )r/   r   r   Y   s    0c             C   sn   t jdƒ}t||d�}xHt|jƒ ƒD ]8}|j|ƒ}|r$|jdƒ|jdƒ }|| ||< ||= q$W | j|ƒ d S )Nz]^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$)Úprogressr   rH   )ÚreÚcompiler   ÚlistÚkeysÚmatchÚgroupZload_state_dict)ÚmodelZ	model_urlrc   ÚpatternZ
state_dictÚkeyÚresZnew_keyr   r   r   Ú_load_state_dict£   s    

rn   c             K   s(   t |||f|Ž}|r$t|t|  |ƒ |S )N)r   rn   Ú
model_urls)Úarchr-   rZ   r[   Ú
pretrainedrc   Úkwargsrj   r   r   r   Ú	_densenetµ   s    rs   FTc             K   s   t dddd| |f|ŽS )	aÙ  Densenet-121 model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
        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   rJ   rK   rL   rM   rN   rO   )rK   rL   rM   rN   )rs   )rq   rc   rr   r   r   r   r   ½   s    
c             K   s   t dddd| |f|ŽS )	aÙ  Densenet-161 model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
        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	   é0   rK   rL   é$   rM   é`   )rK   rL   ru   rM   )rs   )rq   rc   rr   r   r   r   r	   Ë   s    
c             K   s   t dddd| |f|ŽS )aÙ  Densenet-169 model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
        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   rJ   rK   rL   rO   )rK   rL   rJ   rJ   )rs   )rq   rc   rr   r   r   r   r   Ù   s    
c             K   s   t dddd| |f|ŽS )aÙ  Densenet-201 model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
        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   rJ   rK   rL   rt   rO   )rK   rL   rt   rJ   )rs   )rq   rc   rr   r   r   r   r   ç   s    
)FT)FT)FT)FT)rd   r
   Ztorch.nnr%   Ztorch.nn.functionalZ
functionalr7   Ztorch.utils.checkpointÚutilsr6   r5   Úcollectionsr   r   Ú__all__ro   r   rS   r   ÚModuler>   rF   r   rn   rs   r   r	   r   r   r   r   r   r   Ú<module>   s,   
	
J


