3
Ðñß^  ã               @   sŒ   d dl Zd dlmZ d dlmZmZ ddlT ddlm	Z	 G dd„ deƒZ
G d	d
„ d
e
ƒZG dd„ de
ƒZG dd„ deƒZG dd„ deƒZdS )é    N)ÚFunction)ÚModuleÚ	Parameteré   )Ú*)ÚSparseConvNetTensorc               @   s2   e Zd ZdZddd„Zdd	„ Zd
d„ Zdd„ ZdS )ÚBatchNormalizationaÞ  
    Parameters:
    nPlanes : number of input planes
    eps : small number used to stabilise standard deviation calculation
    momentum : for calculating running average for testing (default 0.99)
    affine : only 'true' is supported at present (default 'true')
    noise : add multiplicative and additive noise during training if >0.
    leakiness : Apply activation def inplace: 0<=leakiness<=1.
    0 for ReLU, values in (0,1) for LeakyReLU, 1 for no activation def.
    ç-Cëâ6?ç®Gáz®ï?Tr   c             C   sŒ   t j| ƒ || _|| _|| _|| _|| _| jdtj	|ƒj
dƒƒ | jdtj	|ƒj
dƒƒ |rˆttj	|ƒj
dƒƒ| _ttj	|ƒj
dƒƒ| _d S )NÚrunning_meanr   Úrunning_varr   )r   Ú__init__ÚnPlanesÚepsÚmomentumÚaffineÚ	leakinessÚregister_bufferÚtorchÚTensorÚfill_r   ÚweightÚbias)Úselfr   r   r   r   r   © r   ú?/home/yuhw/wc/SparseConvNet/sparseconvnet/batchNormalization.pyr      s    
zBatchNormalization.__init__c          
   C   s†   |j jƒ dkr|S |j jdƒ| jks6t| j|j jfƒ‚tƒ }|j|_|j|_t	j
|j t| dƒt| dƒ| j| j| j| j| j| jƒ	|_ |S )Nr   r   r   r   )ÚfeaturesÚnelementÚsizer   ÚAssertionErrorÚshaper   ÚmetadataÚspatial_sizeÚBatchNormalizationFunctionÚapplyÚoptionalTensorr   r   r   r   Útrainingr   )r   ÚinputÚoutputr   r   r   Úforward+   s"    $
zBatchNormalization.forwardc             C   s   |S )Nr   )r   Úout_sizer   r   r   Úinput_spatial_size>   s    z%BatchNormalization.input_spatial_sizec             C   s`   dt | jƒ d t | jƒ d t | jƒ d t | jƒ }| jdkrT|d t | jƒ }|d }|S )Nz
BatchNorm(z,eps=z
,momentum=z,affine=r   z,leakiness=ú))Ústrr   r   r   r   r   )r   Úsr   r   r   Ú__repr__A   s
    8
zBatchNormalization.__repr__N)r	   r
   Tr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r)   r+   r/   r   r   r   r   r      s   
   
r   c               @   s   e Zd Zddd„Zdd„ ZdS )	ÚBatchNormReLUç-Cëâ6?ç®Gáz®ï?c             C   s   t j| |||ddƒ d S )NTr   )r   r   )r   r   r   r   r   r   r   r   K   s    zBatchNormReLU.__init__c             C   s@   dt | jƒ d t | jƒ d t | jƒ d t | jƒ d }|S )NzBatchNormReLU(z,eps=z
,momentum=z,affine=r,   )r-   r   r   r   r   )r   r.   r   r   r   r/   N   s    <zBatchNormReLU.__repr__N)r5   r6   )r0   r1   r2   r   r/   r   r   r   r   r4   J   s   
r4   c               @   s   e Zd Zd	dd„Zdd„ ZdS )
ÚBatchNormLeakyReLUç-Cëâ6?ç®Gáz®ï?çZd;ßOÕ?c             C   s   t j| |||d|ƒ d S )NT)r   r   )r   r   r   r   r   r   r   r   r   U   s    zBatchNormLeakyReLU.__init__c             C   sN   dt | jƒ d t | jƒ d t | jƒ d t | jƒ d t | jƒ d }|S )NzBatchNormLeakyReLU(z,eps=z
,momentum=z,affine=z,leakiness=r,   )r-   r   r   r   r   r   )r   r.   r   r   r   r/   X   s    JzBatchNormLeakyReLU.__repr__N)r8   r9   r:   )r0   r1   r2   r   r/   r   r   r   r   r7   T   s   
r7   c               @   s$   e Zd Zedd„ ƒZedd„ ƒZdS )r#   c
             C   s‚   |j d | _|| _|	| _|jƒ }
|jƒ j| jƒ}|jƒ j| jƒ}tjj	||
||||||||| j| jƒ | j
||
||||||ƒ |
S )Nr   )r    r   Útrainr   ÚnewÚresize_ÚcloneÚsparseconvnetÚSCNÚBatchNormalization_updateOutputÚsave_for_backward)ÚctxÚinput_featuresr   r   r   r   r   r   r;   r   Úoutput_featuresÚsaveMeanÚ
saveInvStdr   r   r   r)   ^   s8    z"BatchNormalizationFunction.forwardc             C   s„   | j \}}}}}}}}	| js t‚|jƒ }
tj|ƒ}tj|ƒ}tjj||
||j	ƒ ||	||||||| j
ƒ |
t|ƒt|ƒd d d d d d f	S )N)Úsaved_tensorsr;   r   r<   r   Ú
zeros_liker?   r@   ÚBatchNormalization_backwardÚ
contiguousr   ÚoptionalTensorReturn)rC   Úgrad_outputrD   rE   r   r   r   r   rF   rG   Ú
grad_inputÚgrad_weightÚ	grad_biasr   r   r   Úbackward‡   s(    	


z#BatchNormalizationFunction.backwardN)r0   r1   r2   Ústaticmethodr)   rQ   r   r   r   r   r#   ]   s   )r#   c               @   s2   e Zd ZdZddd„Zdd„ Zd	d
„ Zdd„ ZdS )ÚMeanOnlyBNLeakyReLUa  
    Parameters:
    nPlanes : number of input planes
    momentum : for calculating running average for testing (default 0.99)
    leakiness : Apply activation def inplace: 0<=leakiness<=1.
    0 for ReLU, values in (0,1) for LeakyReLU, 1 for no activation def.
    Tr   ç®Gáz®ï?c             C   sX   t j| ƒ || _|| _| jdtj|ƒjdƒƒ || _|rNt	tj|ƒjdƒƒ| _
|| _d S )Nr   r   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   ­   s    
zMeanOnlyBNLeakyReLU.__init__c             C   sÌ   |j jƒ dks$|j jdƒ| jks$t‚tƒ }|j|_|j|_| jrŠt	j
ƒ �. |j jdƒ}| j| j |d| j   | _W d Q R X |j | |_ n|j | j |_ | jr¨|| j }| jdkrÈt	jjj|j | jƒ|_ |S )Nr   r   )r   r   r   r   r   r   r!   r"   r&   r   Úno_gradÚmeanr   r   r   r   r   ÚnnÚ
functionalÚ
leaky_relu)r   r'   r(   ÚmrE   r   r   r   r)   »   s    $
&

zMeanOnlyBNLeakyReLU.forwardc             C   s   |S )Nr   )r   r*   r   r   r   r+   Í   s    z&MeanOnlyBNLeakyReLU.input_spatial_sizec             C   s2   dt | jƒ d t | jƒ d t | jƒ d }|S )NzMeanOnlyBatchNorm(z
,momentum=z,leakiness=r,   )r-   r   r   r   )r   r.   r   r   r   r/   Ð   s    .zMeanOnlyBNLeakyReLU.__repr__N)Tr   rT   )r0   r1   r2   r3   r   r)   r+   r/   r   r   r   r   rS   ¥   s     
	rS   )Úsparseconvnet.SCNr?   Útorch.autogradr   Útorch.nnr   r   ÚutilsÚsparseConvNetTensorr   r   r4   r7   r#   rS   r   r   r   r   Ú<module>   s   =
	H