How to load a list of numpy arrays to pytorch dataset loader?

Hongtao_Zhang: <p>Hi, I am a newbie and I am trying to train using the MRI2D dataset. My data set has saved 2D slices in a matrix form with the shape [500, 3, 64, 64]. When using the dataloader, I got an error: Expected 4-dimensional input for 4-dimensional weight 64 3 3 3 but got 5-dimensional input of size [4, 500, 3, 64, 64] instead.<br>
What should I do? 500 is the number of 2Dmri pictures, and 3 is the number of channels.<br>
I found that it reads the 4D dataset and batches it, adding a latitude. That’s why there are 4 more. What should I do to make a batch in my 500<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/3/93d7ff1cc56cce34d0cd1008b1ee3fddc41cc197.png" data-download-href="https://discuss.pytorch.org/uploads/default/93d7ff1cc56cce34d0cd1008b1ee3fddc41cc197" title="11C3940C-EF39-43F9-BF97-BE1473B8B71F"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/93d7ff1cc56cce34d0cd1008b1ee3fddc41cc197_2_690x124.png" alt="11C3940C-EF39-43F9-BF97-BE1473B8B71F" data-base62-sha1="l5SXqPAWQhCdYi4nqoOmJDEx9Q3" width="690" height="124" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/93d7ff1cc56cce34d0cd1008b1ee3fddc41cc197_2_690x124.png, https://discuss.pytorch.org/uploads/default/original/3X/9/3/93d7ff1cc56cce34d0cd1008b1ee3fddc41cc197.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/3/93d7ff1cc56cce34d0cd1008b1ee3fddc41cc197.png 2x" data-dominant-color="F3F4F7"><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">11C3940C-EF39-43F9-BF97-BE1473B8B71F</span><span class="informations">777×140 18.9 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> <img src="https://discuss.pytorch.org/uploads/default/original/3X/a/f/af6ec37bc780093ab532f17d683fd149cc3f51f1.png" alt="463AC8AD-95EF-4DC1-8C4E-C6E819BF137C" data-base62-sha1="p1WRLL5qYoCLfMVukcR1DibPMl3" width="449" height="486"> <img src="https://discuss.pytorch.org/uploads/default/original/3X/a/0/a09f08924df39cf6b55d38ed9b14ad35e5eb5a63.png" alt="AA53C78C-4FD6-48BB-AA0D-A4395C9C640C" data-base62-sha1="mUV7CzdxvfRJsFa5P4oyjX2IvEn" width="543" height="230"></p>

ptrblck: <p>How would you like to process these MRI images?<br>
If you want to process all 500 slices at once, you might want to use 3-dimensional layers, e.g. <code>nn.Conv3d</code>.<br>
Alternatively, if you would like to use each slice as a sample, you might use the 500 slices directly as the batch dimension.</p>

Hongtao_Zhang: <p>Hello, thank you for your answer.<br>
My idea is: use the 500 slices saved by numpy as the data set, and use it as part of the batch for training. What should I do, because I found that in ‘’ ‘def __getitem __ (self, index):’“<br>
it will iterate 500 slices directly, not from it batch, I expected to get [4, 3, 256, 256] , But it is [4,500,3,256,256]. What should i do</p>

ptrblck: <p>Are you preloading the complete dataset and are the 500 slices the complete set or do you have more files (each with 500 slices)?<br>
If you are preloading the data, you could do it in the <code>__init__</code> method of your <code>Dataset</code> and load each slice as a single sample in <code>__getitem__</code>.<br>
I’m not sure, how <code>ImageDataset1</code> works, as <code>img_lrr</code> and <code>img_hrr</code> seem to be global variables. What shapes do these arrays have?</p>
<p>PS: you can post code snippets by wrapping them in three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

Hongtao_Zhang: <p>Yes, I have already preloaded the complete dataset.<br>
And just now I had finished preparing the dataset through your advice.<br>
Thank you for your answer to the discussion:<a href="https://discuss.pytorch.org/t/input-numpy-ndarray-instead-of-images-in-a-cnn/18797.I">https://discuss.pytorch.org/t/input-numpy-ndarray-instead-of-images-in-a-cnn/18797.I</a> learned a lot.</p>