3
Ðñß^  ã               @   sp   d dl Z 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	 ddl
mZ G dd	„ d	eƒZG d
d„ deƒZdS )é    N)ÚFunction)ÚModuleÚ	Parameteré   )Ú*)ÚSparseConvNetTensor)ÚConvolutionFunctionc               @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚRandomizedStrideConvolutionat  
    A bit like Fractional Max Pooling during training, but at test time it
    operates like a regular sparseconvnet.Convolution.

    Don't use with scn.InputBatch.precomute_metadata()!!

    Like regular a sparseconvnet.Convolution, is can be paired with a
    sparseconvnet.Deconvolution module in an UNet style network, to restore
    the input sparsity pattern.
    r   c       	      C   s¤   t j| ƒ || _|| _|| _|| _t||ƒ| _| jjƒ j	ƒ | _
t||ƒ| _d| | | j
 d }ttj| j
||| || ƒjd|ƒƒ| _|r ttj|ƒjƒ ƒ| _d S )Ng       @g      à?r   )r   Ú__init__Ú	dimensionÚgroupsÚnInÚnOutÚtoLongTensorÚfilter_sizeÚprodÚitemÚfilter_volumeÚfilter_strider   ÚtorchÚTensorÚnormal_ÚweightÚzero_Úbias)	Úselfr   r   r   r   r   r   r   Ústd© r   úH/home/yuhw/wc/SparseConvNet/sparseconvnet/randomizedStrideConvolution.pyr
      s    

z$RandomizedStrideConvolution.__init__c          
   C   s®   |j jƒ dks$|j jdƒ| jks$t‚tƒ }|j|_|j| j | j	 d |_|jd | j	 | j |jkj
ƒ snt‚| jrxtntj|j | jt| dƒ|j|j|j| j| j| j	ƒ	|_ |S )Nr   r   r   )ÚfeaturesÚ
ndimensionÚsizer   ÚAssertionErrorr   ÚmetadataÚspatial_sizer   r   ÚallÚtrainingÚ#RandomizedStrideConvolutionFunctionr   Úapplyr   ÚoptionalTensorr   )r   ÚinputÚoutputr   r   r   Úforward*   s"    $
z#RandomizedStrideConvolution.forwardc             C   s  dt | jƒ d t | jƒ d }| jjƒ jƒ | jjƒ jƒ kr†| jjƒ jƒ | jjƒ jƒ kr†|t | jd jƒ ƒ d t | jd jƒ ƒ }n”|d t | jd jƒ ƒ }x*| jdd … D ]}|d t |jƒ ƒ }q°W |d	 t | jd jƒ ƒ }x*| jdd … D ]}|d t |jƒ ƒ }qöW |d
 }|S )NzRandomizedStrideConvolution z->z Cr   ú/ú(r   ú,z)/(ú))Ústrr   r   r   Úmaxr   Úminr   )r   ÚsÚir   r   r   Ú__repr__@   s     .z$RandomizedStrideConvolution.__repr__c             C   s   |d | j  | j S )Nr   )r   r   )r   Úout_sizer   r   r   Úinput_spatial_sizeP   s    z.RandomizedStrideConvolution.input_spatial_sizeN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r,   r6   r8   r   r   r   r   r	      s
   

r	   c               @   s$   e Zd Zedd„ ƒZedd„ ƒZdS )r'   c
             C   sf   |j ƒ }
|| _|| _| j|||||||	ƒ t jtjj||||	|||
||ƒ	7  _t j|
j	ƒ 7  _|
S )N)
ÚnewÚinput_metadatar   Úsave_for_backwardÚsparseconvnetÚforward_pass_multiplyAdd_countÚSCNÚ(RandomizedStrideConvolution_updateOutputÚforward_pass_hidden_statesÚnelement)ÚctxÚinput_featuresr   r   r>   r8   Úoutput_spatial_sizer   r   r   Úoutput_featuresr   r   r   r,   T   s0    
z+RandomizedStrideConvolutionFunction.forwardc             C   sp   | j \}}}}}}}|jƒ }	tj|ƒ}
tj|ƒ}tjj||||| j||	|jƒ ||
|ƒ |	|
t	|ƒd d d d d d f	S )N)
Úsaved_tensorsr=   r   Ú
zeros_liker@   rB   Ú$RandomizedStrideConvolution_backwardr>   Ú
contiguousÚoptionalTensorReturn)rF   Úgrad_outputrG   r8   r   r   rH   r   r   Ú
grad_inputÚgrad_weightÚ	grad_biasr   r   r   Úbackwardz   s"    

z,RandomizedStrideConvolutionFunction.backwardN)r9   r:   r;   Ústaticmethodr,   rS   r   r   r   r   r'   S   s   &r'   )r@   Úsparseconvnet.SCNZtorch.autogradr   Útorch.nnr   r   ÚutilsÚsparseConvNetTensorr   Úconvolutionr   r	   r'   r   r   r   r   Ú<module>   s   E