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	 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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é   )Ú*)ÚSparseConvNetTensor)ÚMetadatac               @   s*   e Zd ZdZd
dd„Zdd„ Zdd„ Zd	S )Ú
InputLayeraÀ  
    Takes a tuple (coords, features, batch_size [optional])
    * coords is 2d torch.LongTensor with size
       N x dimension   (batch size == 1)
    or
       N x (dimension+1)  (first d columns are coordinates, last column is batch index)

    * features is a CPU or CUDA float tensor with size

      N x n_feature_planes

    * batch_size if given, set a lower bound on the the number of samples in the output tensor.
    Batch size can normally be inferred from the last column of coords, but this may fail if
    some of the batch items are totally empty.

    In case of repetition in coords:
    mode == 0 if the input is guaranteed to have no duplicates
    mode == 1 to use the last item at each spatial location
    mode == 2 to keep the first item at each spatial location
    mode == 3 to sum feature vectors sharing one spatial location
    mode == 4 to average feature vectors at each spatial location

    Output is a SparseConvNetTensor
    é   c             C   s,   t j| ƒ || _t||ƒ| _|| _d | _d S )N)r   Ú__init__Ú	dimensionÚtoLongTensorÚspatial_sizeÚmodeÚdevice)Úselfr   r   r   © r   ú5/home/yuhw/wc/SparseConvNet/sparseconvnet/ioLayers.pyr   (   s
    
zInputLayer.__init__c             C   s
   || _ | S )N)r   )r   r   r   r   r   Úto/   s    zInputLayer.toc             C   sr   t t| jƒ| jd�}tj| j|j| j|d jƒ jƒ | j	rH|d j
| j	ƒn|d t|ƒdkr^dn|d | jƒ|_|S )N)Úmetadatar   r   r   é   )r   r   r   r   ÚInputLayerFunctionÚapplyr   ÚcpuÚlongr   r   Úlenr   Úfeatures)r   ÚinputÚoutputr   r   r   Úforward3   s    

zInputLayer.forwardN)r
   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r	      s   
r	   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚOutputLayera"  
    Used in conjunction with an InputLayer for 'autoencoder' style networks
    Takes a SparseConvNetTensor and results a float Tensor of size

    N x n_feature_planes

    N is defined by the InputLayer

    Behavior during forward-/back-propagation depends on the InputLayer's mode
    c             C   s   t j| ƒ || _d S )N)r   r   r   )r   r   r   r   r   r   O   s    
zOutputLayer.__init__c             C   s   t j| j|j|jƒ}|S )N)ÚOutputLayerFunctionr   r   r   r   )r   r   r   r   r   r   r   S   s
    zOutputLayer.forwardN)r    r!   r"   r#   r   r   r   r   r   r   r$   D   s   
r$   c               @   s*   e Zd ZdZd
dd„Zdd„ Zdd„ Zd	S )ÚBLInputLayera˜  
    Takes a tuple (coords, features)
    * coords is 3d torch.LongTensor with size
       batch_size x length x dimension

      Coordinates should be >=0, or -1 to indicate 'empty'

    * features is a 3d CPU or CUDA float Tensor with size

      batch_size x length x n_feature_planes

    mode == 0 Assumes that for each coords[i, :], the locations are unique and not 'empty'.
    mode == 1 Use the last item at each spatial location
    mode == 2 Keep the first item at each spatial location
    mode == 3 Sum feature vectors sharing one spatial location
    mode == 4 Average feature vectors at each spatial location

    Output is a SparseConvNetTensor
    r
   c             C   s,   t j| ƒ || _t||ƒ| _|| _d | _d S )N)r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   p   s
    
zBLInputLayer.__init__c             C   s
   || _ | S )N)r   )r   r   r   r   r   r   w   s    zBLInputLayer.toc             C   s\   t t| jƒ| jd�}tj| j|j| j|d jƒ jƒ | j	rH|d j
| j	ƒn|d | jƒ|_|S )N)r   r   r   r   )r   r   r   r   ÚBLInputLayerFunctionr   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   {   s    

zBLInputLayer.forwardN)r
   )r    r!   r"   r#   r   r   r   r   r   r   r   r&   \   s   
r&   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚBLOutputLayeraT  
    Used in conjunction with a BLInputLayer for 'autoencoder' style networks
    Takes a SparseConvNetTensor and results a float Tensor of batch_size

    batch_size x length x n_feature_planes

    batch_size and length are defined by the BLInputLayer

    Behavior during forward-/back-propagation depends on the BLInputLayer's mode
    c             C   s   t j| ƒ || _d S )N)r   r   r   )r   r   r   r   r   r   –   s    
zBLOutputLayer.__init__c             C   s   t j| j|j|jƒ}|S )N)ÚBLOutputLayerFunctionr   r   r   r   )r   r   r   r   r   r   r   š   s
    zBLOutputLayer.forwardN)r    r!   r"   r#   r   r   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   s4   |j ƒ }|| _|| _tjj||||jƒ |||ƒ |S )N)Únewr   Ú	metadata_ÚsparseconvnetÚSCNÚInputLayer_updateOutputÚ
contiguous)	Úctxr   r   r   ÚcoordsÚinput_featuresÚ
batch_sizer   Úoutput_featuresr   r   r   r   ¤   s    
zInputLayerFunction.forwardc             C   s0   |j ƒ }tjj| j||jƒ ƒ d d d d |d d fS )N)r*   r,   r-   ÚInputLayer_updateGradInputr+   r/   )r0   Úgrad_outputÚ
grad_inputr   r   r   Úbackward¼   s    
zInputLayerFunction.backwardN)r    r!   r"   Ústaticmethodr   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   s,   |j ƒ }|| _|| _tjj||jƒ |ƒ |S )N)r*   r+   r   r,   r-   ÚOutputLayer_updateOutputr/   )r0   r   r   r2   r4   r   r   r   r   Ç   s    zOutputLayerFunction.forwardc             C   s0   |j ƒ }|jƒ }tjj| j||jƒ ƒ d d |fS )N)r*   r/   r,   r-   ÚOutputLayer_updateGradInputr+   )r0   r6   r7   r   r   r   r8   ×   s    
zOutputLayerFunction.backwardN)r    r!   r"   r9   r   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   s2   |j ƒ }|| _|| _tjj||||jƒ ||ƒ |S )N)r*   r+   r   r,   r-   ÚBLInputLayer_updateOutputr/   )r0   r   r   r   r1   r2   r   r4   r   r   r   r   ã   s    	zBLInputLayerFunction.forwardc             C   s.   |j ƒ }tjj| j||jƒ ƒ d d d d |d fS )N)r*   r,   r-   ÚBLInputLayer_updateGradInputr+   r/   )r0   r6   r7   r   r   r   r8   ù   s    
zBLInputLayerFunction.backwardN)r    r!   r"   r9   r   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   s,   |j ƒ }|| _|| _tjj||jƒ |ƒ |S )N)r*   r+   r   r,   r-   ÚBLOutputLayer_updateOutputr/   )r0   r   r   r2   r4   r   r   r   r     s    zBLOutputLayerFunction.forwardc             C   s(   |j ƒ }tjj| j||jƒ ƒ d d |fS )N)r*   r,   r-   ÚBLOutputLayer_updateGradInputr+   r/   )r0   r6   r7   r   r   r   r8     s    
zBLOutputLayerFunction.backwardN)r    r!   r"   r9   r   r8   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 )
ÚInputLayerInputc             C   s   ||g| _ d S )N)Úx)r   r1   r   r   r   r   r     s    zInputLayerInput.__init__c             C   s
   | j | S )N)rA   )r   Únr   r   r   Ú__getitem__   s    zInputLayerInput.__getitem__c             C   s   dS )Nr   r   )r   r   r   r   Ú__len__"  s    zInputLayerInput.__len__c             C   s   | j d jƒ | j d< | S )Nr   )rA   Úcuda)r   r   r   r   rE   $  s    zInputLayerInput.cudaN)r    r!   r"   r   rC   rD   rE   r   r   r   r   r@     s   r@   )Úsparseconvnet.SCNr,   Ztorch.autogradr   Útorch.nnr   r   ÚutilsÚsparseConvNetTensorr   r   r   r	   r$   r&   r(   r   r%   r'   r)   Úobjectr@   r   r   r   r   Ú<module>   s   5/#!