3
Ðñß^	  ã               @   s\   d Z ddlZddlmZ ddlmZ ddlT ddlm	Z	 G dd	„ d	eƒZ
G d
d„ deƒZdS )ag  
Function to convert a SparseConvNet hidden layer to a dense convolutional
layer. Put a SparseToDense convolutional layer (or an ActivePooling layer) at
the top of your sparse network. The output can then pass to a dense
convolutional layers or (if the spatial dimensions have become trivial) a
linear classifier.

Parameters:
dimension : of the input field,
é    N)ÚFunction)ÚModuleé   )Ú*)ÚSparseConvNetTensorc               @   s$   e Zd Zedd„ ƒZedd„ ƒZdS )ÚSparseToDenseFunctionc             C   s8   || _ || _| j||ƒ |jƒ }tjj|||||ƒ |S )N)Úinput_metadataÚ	dimensionÚsave_for_backwardÚnewÚsparseconvnetÚSCNÚSparseToDense_updateOutput)ÚctxÚinput_featuresr   Úspatial_sizer	   ÚnPlanesÚoutput© r   ú:/home/yuhw/wc/SparseConvNet/sparseconvnet/sparseToDense.pyÚforward   s    zSparseToDenseFunction.forwardc             C   s:   |j ƒ }| j\}}tjj|| j|||jƒ ƒ |d d d d fS )N)r   Úsaved_tensorsr   r   ÚSparseToDense_updateGradInputr   Ú
contiguous)r   Úgrad_outputÚ
grad_inputr   r   r   r   r   Úbackward.   s    

zSparseToDenseFunction.backwardN)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr   r   r   r   r   r   r      s   r   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚSparseToDensec             C   s   t j| ƒ || _|| _d S )N)r   Ú__init__r	   r   )Úselfr	   r   r   r   r   r"   <   s    
zSparseToDense.__init__c             C   s   t j|j|j|j| j| jƒS )N)r   ÚapplyÚfeaturesÚmetadatar   r	   r   )r#   Úinputr   r   r   r   A   s    zSparseToDense.forwardc             C   s   |S )Nr   )r#   Úout_sizer   r   r   Úinput_spatial_sizeI   s    z SparseToDense.input_spatial_sizec             C   s    dt | jƒ d t | jƒ d S )NzSparseToDense(ú,ú))Ústrr	   r   )r#   r   r   r   Ú__repr__L   s    zSparseToDense.__repr__N)r   r   r   r"   r   r)   r-   r   r   r   r   r!   ;   s   r!   )Ú__doc__Úsparseconvnet.SCNr   Ztorch.autogradr   Útorch.nnr   ÚutilsÚsparseConvNetTensorr   r   r!   r   r   r   r   Ú<module>   s   "