3d cnn for MRI classification

(Keerat Singh) #1

I’m trying to create a classifier that takes in an MRI (.nii or Nifti file that is a stack of 2D slices) and outputs whether the patient is classified as cognitively normal (CN), mildly cognitively impaired (MCI), or with Alzheimer’s disease (AD). I’m using the ADNI dataset, and my data is stored as follows:

“/home/ubuntu/efs/data/train/CN/” contains all CN .nii files

“/home/ubuntu/efs/data/train/MCI/” contains all MCI .nii files

“/home/ubuntu/efs/data/train/AD/” contains all AD .nii files

I’m having trouble getting the data into a DataBlock structure. I’m following this example : https://nbviewer.jupyter.org/gist/jcreinhold/78943cdeca1c5fca4a5af5d066bd8a8d from this blog post: https://towardsdatascience.com/deep-learning-with-magnetic-resonance-and-computed-tomography-images-e9f32273dcb5.

I want the DataBlock to split the dataset of .nii images into training and validation sets with labels defined by their parent folder.

Here is my code:

import fastai.vision as faiv
import numpy as np
import torch
import nibabel as nib

def open_nii(fn:str) -> faiv.Image:
    """ Return fastai `Image` object created from NIfTI image in file `fn`."""
    x = nib.load(str(fn)).get_data().byteswap().newbyteorder()
    return faiv.Image(torch.Tensor(x))

class NiftiItemList(faiv.PointsItemList):
    """ custom item list for nifti files """
    def open(self, fn:faiv.PathOrStr)->faiv.Image: return open_nii(fn)

class NiftiNiftiList(NiftiItemList):
    """ item list suitable for synthesis tasks """
    _label_cls = NiftiItemList

data_dir = '/home/ubuntu/efs/data/train/'
data = (NiftiNiftiList.from_folder(data_dir, extensions = '.nii')
       .split_by_rand_pct()
       .label_from_folder()
       .databunch())

When I run this, I get:

/home/ubuntu/anaconda3/lib/python3.6/site-packages/fastai/basic_data.py:259: UserWarning: There seems to be something wrong with your dataset, for example, in the first batch can’t access any element of self.train_ds.
Tried: 16,119,280,71,44…
warn(warn_msg)
You can deactivate this warning by passing no_check=True.

and when I type:

data

it returns:

Traceback (most recent call last):
File “/home/ubuntu/anaconda3/lib/python3.6/site-packages/nibabel/loadsave.py”, line 40, in load
stat_result = os.stat(filename)
FileNotFoundError: [Errno 2] No such file or directory: ‘CN’
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “”, line 1, in
File “/home/ubuntu/anaconda3/lib/python3.6/site-packages/fastai/basic_data.py”, line 103, in repr
return f’{self._class_._name_};\n\nTrain: {self.train_ds};\n\nValid: {self.valid_ds};\n\nTest: {self.test_ds}’
File “/home/ubuntu/anaconda3/lib/python3.6/site-packages/fastai/data_block.py”, line 613, in repr
items = [self[i] for i in range(min(5,len(self.items)))]
File “/home/ubuntu/anaconda3/lib/python3.6/site-packages/fastai/data_block.py”, line 613, in
items = [self[i] for i in range(min(5,len(self.items)))]
File “/home/ubuntu/anaconda3/lib/python3.6/site-packages/fastai/data_block.py”, line 648, in _getitem_
if self.item is None: x,y = self.x[idxs],self.y[idxs]
File “/home/ubuntu/anaconda3/lib/python3.6/site-packages/fastai/data_block.py”, line 118, in _getitem_
if isinstance(idxs, Integral): return self.get(idxs)
File “/home/ubuntu/anaconda3/lib/python3.6/site-packages/fastai/vision/data.py”, line 271, in get
res = self.open(fn)
File “/home/ubuntu/efs/code/dataloader.py”, line 24, in open
def open(self, fn:faiv.PathOrStr)->faiv.Image: return open_nii(fn)
File “/home/ubuntu/efs/code/dataloader.py”, line 19, in open_nii
x = nib.load(str(fn)).get_data().byteswap().newbyteorder()
File “/home/ubuntu/anaconda3/lib/python3.6/site-packages/nibabel/loadsave.py”, line 42, in load
raise FileNotFoundError(“No such file or no access: ‘%s’” % filename)
FileNotFoundError: No such file or no access: ‘CN’

I’m not really sure where to go from here. I’d really appreciate any help!

0 Likes