I am facing some problems to write the getitem() 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.
The structure of the files looks like this: "/content/drive/MyDrive/mris/9114036/11288003"
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.
The code I tried to execute was:
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
The error I got when I tried to access a sample from the dataset was:
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in open(fp, mode)
2841
2842 if filename: ->
2843 fp = builtins.open(filename, "rb")
2844 exclusive_fp = True
2845 IsADirectoryError: [Errno 21] Is a directory: '/content/drive/MyDrive/mris/9114036'
which seems logical.
I tried to use os.walk
to get in the 11288003 directory where the images are, but it didn't work. Most likely my whole approach is wrong.
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 ?