3
Ðñß^·  ã               @   s@   d dl Z ddlmZ ddlmZ ddlmZ G dd„ deƒZdS )é    Né   )ÚMetadata)ÚtoLongTensor)ÚSparseConvNetTensorc               @   s˜   e Zd Zdd„ Zdd„ Zd"dd„Zd#dd	„Zd$d
d„Zd%dd„Zd&dd„Z	dd„ Z
dd„ Zd'dd„Zd(dd„Zd)dd„Zd*dd„Zd+dd„Zdd „ Zd!S ),Ú
InputBatchc             C   sH   t j| d d |ƒ || _t||ƒ| _tjƒ | _t|ƒ| _	| j	j
| jƒ d S )N)r   Ú__init__Ú	dimensionr   Úspatial_sizeÚtorchÚFloatTensorÚfeaturesr   ÚmetadataÚsetInputSpatialSize)Úselfr   r	   © r   ú7/home/yuhw/wc/SparseConvNet/sparseconvnet/inputBatch.pyr      s    

zInputBatch.__init__c             C   s   | j jƒ  d S )N)r   ÚbatchAddSample)r   r   r   r   Ú
add_sample   s    zInputBatch.add_sampleFc             C   sB   |j ƒ dkr| j| j ƒ dks"t‚| jj| j|jƒ |jƒ |ƒ d S )Nr   )Úminr	   ÚAssertionErrorr   ÚsetInputSpatialLocationr   Ú
contiguous)r   ÚlocationÚvectorÚ	overwriter   r   r   Úset_location   s    "zInputBatch.set_locationc             C   s   | j j| j|||ƒ d S )N)r   r   r   )r   r   r   r   r   r   r   Úset_location_   s    zInputBatch.set_location_c             C   s^   |dd…d| j …f }|jƒ dkr:| jj|ƒ| jƒ dks>t‚| jj| j|jƒ |jƒ |ƒ dS )aÎ  
        To set n locations in d dimensions, locations can be
        - A size (n,d) LongTensor, giving d-dimensional coordinates -- points
          are added to the current sample, or
        - A size (n,d+1) LongTensor; the extra column specifies the sample
          number (within the minibatch of samples).

          Example with d==3 and n==2:
          Set
          locations = LongTensor([[1,2,3],
                                  [4,5,6]])
          to add points to the current sample at (1,2,3) and (4,5,6).
          Set
          locations = LongTensor([[1,2,3,7],
                                  [4,5,6,9]])
          to add point (1,2,3) to sample 7, and (4,5,6) to sample 9 (0-indexed).

        Nr   )	r   r   r	   Ú	expand_asr   r   ÚsetInputSpatialLocationsr   r   )r   Ú	locationsÚvectorsr   Úlr   r   r   Úset_locations"   s    (zInputBatch.set_locationsc             C   s   | j j| j|||ƒ d S )N)r   r   r   )r   r   r    r   r   r   r   Úset_locations_:   s    zInputBatch.set_locations_r   c             C   s   | j j| j||| j|ƒ d S )N)r   ÚaddSampleFromThresholdedTensorr   r	   )r   ÚtensorÚoffsetÚ	thresholdr   r   r   Úadd_sample_from_tensor>   s    z!InputBatch.add_sample_from_tensorc             C   s(   |dkr| j jƒ  |dkr$| j jƒ  dS )zé
        Optional.
        Allows precomputation of 'rulebooks' in data loading threads.
        Use size == 2 if downsizing with size-2 stride-2 operations
        Use size == 3 if downsizing with size-3 stride-2 operations
        é   é   N)r   ÚgenerateRuleBooks2s2ÚgenerateRuleBooks3s2)r   Úsizer   r   r   Úprecompute_metadataF   s    
zInputBatch.precompute_metadatac             C   s   | j jƒ  d S )N)r   r   )r   r   r   r   Ú	addSampleS   s    zInputBatch.addSamplec             C   s:   |j ƒ dkr| j| j ƒ dks"t‚| jj| j|||ƒ d S )Nr   )r   r	   r   r   r   r   )r   r   r   r   r   r   r   ÚsetLocationV   s    "zInputBatch.setLocationc             C   s   | j j| j|||ƒ d S )N)r   r   r   )r   r   r   r   r   r   r   ÚsetLocation_[   s    zInputBatch.setLocation_c             C   sV   |d d …d | j …f }|jƒ dkr:| jj|ƒ| jƒ dks>t‚| jj| j|||ƒ d S )Nr   )r   r   r	   r   r   r   r   r   )r   r   r    r   r!   r   r   r   ÚsetLocations_   s    (zInputBatch.setLocationsc             C   s   | j j| j|t|ƒ d S )N)r   r   r   r    )r   r   r   r   r   r   r   ÚsetLocations_e   s    zInputBatch.setLocations_c             C   s   | j j| j||| j|ƒ d S )N)r   r$   r   r	   )r   r%   r&   r'   r   r   r   ÚaddSampleFromTensori   s    zInputBatch.addSampleFromTensorc             C   s(   |dkr| j jƒ  |dkr$| j jƒ  dS )zé
        Optional.
        Allows precomputation of 'rulebooks' in data loading threads.
        Use size == 2 if downsizing with size-2 stride-2 operations
        Use size == 3 if downsizing with size-3 stride-2 operations
        r)   r*   N)r   r+   r,   )r   r-   r   r   r   ÚprecomputeMetadataq   s    
zInputBatch.precomputeMetadataN)F)F)F)F)r   )F)F)F)F)r   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r"   r#   r(   r.   r/   r0   r1   r2   r3   r4   r5   r   r   r   r   r      s   









r   )r
   r   r   Úutilsr   ÚsparseConvNetTensorr   r   r   r   r   r   Ú<module>   s   