Histogram for 3D tensor with negative weights

username: Sentient07
cooked: <p>Hello.</p>
<p>I have a <code>(B, N, 3)</code> pointsets and <code>(B, N, 1)</code> pointwise features which I wish to voxelize. The goal of this voxelisation is to be able to query from the voxel, (approximated) feature at any given point with minimal computation time. For example, given a new point and I wish to compute its feature, with a voxel representation, I can interpolate from neighbouring nodes of the given point, which is easy to compute knowing the edge information. Alternatively, I can use KNN , but when N is large (typically 1million), this operation is quite slow.</p>
<p>I can construct my voxels from pointset using <code>torch.histogram</code>, but how can I fill those voxels with my features instead of binary values? I can possibly use the weights option in <code>torch.histogram</code> however, my point-wise features contain negative values while I can imagine the histogram weights to be positive. Can someone please help me out?</p>
<p>Thank you.</p>

username: tom
cooked: <p>Depending on the numbers B, N, and the size of the voxel grid here, one option might be to do <a href="https://en.wikipedia.org/wiki/Kernel_regression" rel="noopener nofollow ugc">kernel regression</a>. This is quite computationally intensive during voxelization, but in contrast to binning like histograms do, does interpolation, too.</p>
<p>A blunt way to mimic what a histogram does is to quantize the coordinates into integers starting from 0 and then use them as the indices of a sparse tensor. These are allowed to have duplicate indices. Then tensor.coalesce() will sum all values at a given coordinate.</p>
<p>Of course, there so many ways to do this, but these are the ones I’d try first.</p>
<p>Best regards</p>
<p>Thomas</p>

username: Sentient07
cooked: <p>Hello Thomas,</p>
<p>Thanks for the reply.</p>
<blockquote>
<p>A blunt way to mimic what a histogram does is to quantize the coordinates into integers starting from 0 and then use them as the indices of a sparse tensor. These are allowed to have duplicate indices. Then tensor.coalesce() will sum all values at a given coordinate.</p>
</blockquote>
<p>Could you please elaborate on the quantizing part?  If suppose my batched point cloud <code>A = torch.randn((32, 10000, 3))</code> is quantized to a variable <code>B</code> (an integer) such that <code>B.shape == (32, 10000, 3)</code> with each value pointing to a place where corresponding point in A falls into the grid of shape <code>(64,64,64)</code> that would be excellent for my usecase.</p>
<p>With those indices, I can then do <a href="https://pytorch-scatter.readthedocs.io/en/1.4.0/functions/mean.html#torch_scatter.scatter_mean" rel="noopener nofollow ugc">scatter_mean</a> over my feature tensor.</p>
<p>Thank you</p>

username: Sentient07
cooked: <p>Hello <a class="mention" href="/u/ptrblck">@ptrblck</a> can you please help me out? Thanks</p>

username: tom
cooked: <p>So if you just scale your coordinates to fall into the range 0 &lt;= x_i &lt; 64 and take int, it should work…<br>
I’m not sure whether scatter_mean supports multidimensional indices, so you might then multiply x_0 * (64 * 64) + x_1 * 64 + x_2 and to get a 64**3 tensor from scatter_mean that you can then view as a 64, 64, 64 tensor.</p>
<p>Best regards</p>
<p>Thomas</p>

username: Sentient07
cooked: <p>Excellent, that makes sense. Thank you, Tom!</p>