title: Load 3D image dataset
body: <p>I am facing some problems to write the <strong>getitem</strong>() function in my dataset class. I am working on a MRI dataset (3D). Each file consists of 160 slices in DICOM format. I have transformed the DICOM files into PNG.</p>
  <p>The structure of the files looks like this: &quot;/content/drive/MyDrive/mris/9114036/11288003&quot;</p>
  <p>Inside the last directory there are the 160 2D slices. The labels are in a .csv file with two columns, one with the id (9114036 for example in the path above) and the other with the grade.</p>
  <p>The code I tried to execute was:</p>
  <pre class="lang-py prettyprint-override"><code>class MyDataset(Dataset):
      
      def __init__(self, csv_file, root_dir, transform = None):
          self.labels_df = pd.read_csv(csv_file, sep = ';')
          self.root_dir = root_dir
          self.transform = transform
      
      def __len__(self):
          return len(self.labels_df)
      
      def __getitem__(self, idx):
          if torch.is_tensor(idx):
              idx = idx.tolist()
          
          img_name = os.path.join(self.root_dir,str(self.labels_df.iloc[idx,0]))
          image = io.imread(img_name, plugin='matplotlib')
          grade = self.labels_df.iloc[idx, 1]
          sample = {'image': image, 'grade': grade}
      
          if self.transform:
              sample = self.transform(sample)
          
          return sample
  </code></pre>
  <p>The error I got when I tried to access a sample from the dataset was:</p>
  <pre><code>/usr/local/lib/python3.7/dist-packages/PIL/Image.py in open(fp, mode) 
  2841 
  2842 if filename: -&gt; 
  2843 fp = builtins.open(filename, &quot;rb&quot;) 
  2844 exclusive_fp = True 
  2845 IsADirectoryError: [Errno 21] Is a directory: '/content/drive/MyDrive/mris/9114036'
  </code></pre>
  <p>which seems logical.</p>
  <p>I tried to use <code>os.walk</code> to get in the 11288003 directory where the images are, but it didn't work. Most likely my whole approach is wrong.</p>
  <p>Does anybody know how to write class dataset for the 3D nature of my data? Should I use another transformation for the DICOM files in the first place ?</p>