3D volumes Dataset to 2D slices Dataset

nmvr: <p>Hello,</p>
<p>I currently have a set of 3D nifti images that I’m loading into a Dataset object using <a class="mention" href="/u/monai">@MONAI</a> .<br>
However, instead of having a Dataset object composed of multiple volumes, I wish to have a 2D dataset composed of all the slices from all the volumes.</p>
<p>Is there a way to load the data this way or change the Dataset object after loading?<br>
Or, in case it is not possible, is there a way to change the batches so that instead of batching out volumes, it batches out slices?<br>
<em>e.g</em> if batch size is 2, instead of 2 volumes it would send out all slices from the 2 volumes.</p>
<p>Thanks for the help!</p>

ptrblck: <aside class="quote no-group" data-username="nmvr" data-post="1" data-topic="112971">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nmvr/48/33908_2.png" class="avatar"> nmvr:</div>
<blockquote>
<p>Or, in case it is not possible, is there a way to change the batches so that instead of batching out volumes, it batches out slices?<br>
<em>e.g</em> if batch size is 2, instead of 2 volumes it would send out all slices from the 2 volumes.</p>
</blockquote>
</aside>
<p>Could you explain what the difference would be in these two cases, i.e. how the batch shape would look like?</p>

nmvr: <p>Of course.<br>
So, assuming the volumes were 128x128x32, greyscale and batch was 2, instead of the batch being<br>
<code>[2,1,32,128,128]</code> they would be <code>[64,1,128,128]</code>.<br>
I’m unsure if this sort of conversion even makes sense in any scenario, but I thought it could be an alternative assume there was no function to read and save the volumes as slices on the Dataset object.</p>

ptrblck: <p>This “flattening” operation can be performed by:</p>
<pre><code class="lang-python">x = torch.randn(2, 1, 32, 128, 128)
x = x.permute(0, 2, 1, 3, 4)
x = x.view(-1, *x.size()[2:])
print(x.shape)
&gt; torch.Size([64, 1, 128, 128])
</code></pre>
<p>This could be an easy way to change the input format.<br>
The alternative approach would be to open the volume, grab some slices, and return only these.<br>
However, the logic inside the <code>__getitem__</code> method would be a bit more complicated, as you would have to e.g. reuse the same volume to load the missing slices and would have to map the passed <code>index</code> somehow to this logic or use a custom sampler etc.</p>

sinAshish: <p>Hi, I have a situation opposite to this problem.<br>
I have a point cloud of dim :<code>Bx3xN</code><br>
and I want to convert it to a voxel of dim: <code>Bx3xRxRxR</code><br>
how can I do this efficiently in pytorch?</p>

ptrblck: <p>I’m not aware of any built-in PyTorch methods to work with point clouds and their transformations, so I would assume you might want to use a 3rd party library for it.</p>

sinAshish: <p>could you suggest any?</p>

nmvr: <p>The flattening solution sounds great!<br>
I’m new to torch so Im sorry if this follow-up question doesn’t make much sense but, to use your solutions on 2D models the only part that would need to be changed would be the training loop logic correct? When iterating each batch, I would flatten it and go from there, correct?</p>

ptrblck: <p>Yes, the 4-dimensional input tensor would fit into <code>*2D</code> modules, such as <code>nn.Conv2d</code>.</p>
<p><a class="mention" href="/u/sinashish">@sinAshish</a> PyTorch Geometric might have some methods for point clouds, but I haven’t used it so far.</p>