How to create 3D custom dataloader

saint_codded: <p>I am trying to create a custom dataloader for 3D data in pytorch. I have chunked data of size (10,1,10,512,512) meaning (N, C, D, H, W).<br>
Any idea?..please assist</p>

ptrblck: <p><a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html">This tutorial</a> shows you an example on how to write a custom <code>Dataset</code>. Did you already check it and if so, are you stuck somewhere?</p>

saint_codded: <p>Thank you so much for your response, i could do that for 2D data. However, i am currently working on 3D lung CT scans, i am actually aiming to train a classification model using a 3D CNN.<br>
I was able to stack the entire data in a numpy array. For example, for 10 paients with 10 CT slices each, i stacked it into an array of (10, 1,10,512,512)=(N,C,D,H,W)<br>
I could also do a train/validation split. But i am stuck on how i will match each patient data to the corresponding label of either normal or diseased and how i could join both data and label to form a data loader. The labels are in a csv file as either 0 or 1.<br>
I hope you understand. And am sorry this is already looking like a long essay.<br>
Thank you for taking time to assist me with this.</p>

J_Johnson: <p>You can build a custom dataloader, if you plan to store the data in GPU or CPU RAM. And then just create an index that you can shuffle and use to get the batches. Here is an example:</p>
<aside class="quote quote-modified" data-post="2" data-topic="165070">
  <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/j_johnson/48/55494_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/use-case-for-loading-the-entire-dataset-into-ram/165070/2">Use case for loading the entire dataset into RAM</a> <a class="badge-category__wrapper " href="/c/data/37"><span data-category-id="37" style="--category-badge-color: #F7941D; --category-badge-text-color: #FFFFFF;" data-drop-close="true" class="badge-category " title="Topics related to DataLoader, Dataset, torch.utils.data, pytorch/data, and TorchArrow."><span class="badge-category__name">data</span></span></a>
  </div>
  <blockquote>
    Just a suggestion: don’t use the vanilla dataloader for this. If you can fit the data all into ram, just turn it into a tensor for each set of data, shape being: 
self.dataset.size()=([data_index, ...])

Where each data_index is a sample. 
Then create an indexer, either in NumPy or Pytorch. Here is an example for NumPy. 
rng=np.random.default_rng()

...
self.data_index=np.arange(self.dataset.size()[0])

Then define a shuffler class you can call whenever you want to shuffle the index: 
def shuffl…
  </blockquote>
</aside>


saint_codded: <p>Thank you <a class="mention" href="/u/j_johnson">@J_Johnson</a>. Well, I am a beginner so working my way through your suggestion to fit my situation seems a bit hectic. This is the code after chunking the CT scans of 10 patients into a NumPy array. Now my problem is how I will match each patient to a label of either 0 or 1 and create a dataloader to train a network in Pytorch. your assistance would be much appreciated.<br>
Thank you<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/9/4989744f9628a6a1128207ce39d01b672d9f56c9.png" data-download-href="https://discuss.pytorch.org/uploads/default/4989744f9628a6a1128207ce39d01b672d9f56c9" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/9/4989744f9628a6a1128207ce39d01b672d9f56c9.png" alt="image" data-base62-sha1="auxlH2zxVV23I5jA4YloGiGJVmp" width="690" height="119" data-dominant-color="F3F2F2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1118×193 6.35 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

J_Johnson: <p>The trick is you want to keep your data and labels unshuffled. You only shuffle the index and apply it to both. Suppose this time the 4th value of the index is 8 after shuffling. By inserting that index into the 0 dim of both the data and labels, we get back the 8th data sample and 8th label.</p>
<p>In fact, you could probably just copy the example I sent you, throw it into a class, and make an instance of it. It should already generalize to 3D data, as long as your samples are in dim 0.</p>

saint_codded: <p>Thank you again. I think it is my novice self not knowing what to do. I tried to apply the code you suggested, here is what I got. only if you are not tired of me already!!!<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/a/0ab5d4d5f8cc177dee513892fdc562382c31cca2.png" data-download-href="https://discuss.pytorch.org/uploads/default/0ab5d4d5f8cc177dee513892fdc562382c31cca2" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/a/0ab5d4d5f8cc177dee513892fdc562382c31cca2.png" alt="image" data-base62-sha1="1wKluyY5ENSVISXi3M6jkJRtvVg" width="480" height="500" data-dominant-color="F5F5F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">553×575 16.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

J_Johnson: <p>You need a class wrapper to instantiate your data, labels, and the <code>self</code> reference.</p>
<p>Can you copy your code here? I’ll edit it later. Not in front of a computer now.</p>