cvdlab / nn-segmentation-for-lar

Neural networks to segment some type of biomedical images
21 stars 15 forks source link

brain_pipeline.py Saving scans for patient 1... Value 177 is out of range, should be between 0 and 176 #2

Closed mingrui closed 7 years ago

mingrui commented 7 years ago

Hi, this is my complete output:

Loading scans... (155, 240, 240) /media/960EVO/workspace/Data/BRATS2015_Training/HGG/brats_2013_pat0002_1/VSD.Brain.XX.O.MR_Flair.54518/VSD.Brain.XX.O.MR_Flair.54518.mha **************************************************************************************************** (155, 240, 240) /media/960EVO/workspace/Data/BRATS2015_Training/HGG/brats_2013_pat0002_1/VSD.Brain.XX.O.MR_T1.54519/VSD.Brain.XX.O.MR_T1.54519.mha **************************************************************************************************** (155, 240, 240) /media/960EVO/workspace/Data/BRATS2015_Training/HGG/brats_2013_pat0002_1/VSD.Brain.XX.O.MR_T1c.54520/VSD.Brain.XX.O.MR_T1c.54520.mha **************************************************************************************************** (155, 240, 240) /media/960EVO/workspace/Data/BRATS2015_Training/HGG/brats_2013_pat0002_1/VSD.Brain.XX.O.MR_T2.54521/VSD.Brain.XX.O.MR_T2.54521.mha **************************************************************************************************** (155, 240, 240) /media/960EVO/workspace/Data/BRATS2015_Training/HGG/brats_2013_pat0002_1/VSD.Brain_3more.XX.O.OT.54523/VSD.Brain_3more.XX.O.OT.54523.mha **************************************************************************************************** Normalizing slices... Done. Saving scans for patient 1... Traceback (most recent call last): File "brain_pipeline.py", line 237, in <module> save_patient_slices(patients, 'reg') File "brain_pipeline.py", line 204, in save_patient_slices a.save_patient(type_modality, patient_num) File "brain_pipeline.py", line 155, in save_patient for slice_ix in progress(xrange(176)): File "/home/mingrui/anaconda/envs/python2env/lib/python2.7/site-packages/progressbar/bar.py", line 436, in __next__ self.update(self.value + 1) File "/home/mingrui/anaconda/envs/python2env/lib/python2.7/site-packages/progressbar/bar.py", line 541, in update % (value, self.min_value, self.max_value)) ValueError: Value 177 is out of range, should be between 0 and 176

I downloaded my dataset from https://www.smir.ch/, BRATS_2015 Please help! Thank you!

Cesarec88 commented 7 years ago

Hi, the problem is in the format extraction, change it from brain_pipeline.py in read_scans function:

 def read_scans(self):
        """
        goes into each modality in patient directory and loads individual scans.
        transforms scans of same slice into strip of 5 images
        """
        print('Loading scans...')
        slices_by_mode = np.zeros((5, 176, 216, 160))  ---> slices_by_mode = np.zeros((5, 155,240, 240)) 
        slices_by_slice = np.zeros((176, 5, 216, 160)) --->  slices_by_slice = np.zeros((155, 5,240, 240))
        flair = glob(self.path + '/*Flair*/*.mha')
        t2 = glob(self.path + '/*_T2*/*.mha')
        gt = glob(self.path + '/*more*/*.mha')
        t1s = glob(self.path + '/**/*T1*.mha')
        t1_n4 = glob(self.path + '/*T1*/*_n.mha')
        t1 = [scan for scan in t1s if scan not in t1_n4]
        scans = [flair[0], t1[0], t1[1], t2[0], gt[0]]  # directories to each image (5 total)
        if self.n4itk_apply:
            print('-> Applyling bias correction...')
            for t1_path in t1:
                self.n4itk_norm(t1_path)  # normalize files
            scans = [flair[0], t1_n4[0], t1_n4[1], t2[0], gt[0]]
        elif self.n4itk:
            scans = [flair[0], t1_n4[0], t1_n4[1], t2[0], gt[0]]
        for scan_idx in xrange(5):
            # read each image directory, save to self.slices
            print(io.imread(scans[scan_idx], plugin='simpleitk').astype(float).shape)
            print(scans[scan_idx])
            print('*' * 100)
            try:
                slices_by_mode[scan_idx] = io.imread(scans[scan_idx], plugin='simpleitk').astype(float)
            except:
                continue
        for mode_ix in xrange(slices_by_mode.shape[0]):  # modes 1 thru 5
            for slice_ix in xrange(slices_by_mode.shape[1]):  # slices 1 thru 155
                slices_by_slice[slice_ix][mode_ix] = slices_by_mode[mode_ix][slice_ix]  # reshape by slice
        return slices_by_mode, slices_by_slice

NB. Because you are using another file format you will have others problem relative to the image format all over the process ----> solution is to change all images formats in the whole file (especially in the prediction phase)

mingrui commented 7 years ago

Thanks! I will try your solution as soon as possible.

Just to clarify what you meant? The best solution is to make all the images in the BRATS 2015 dataset conform to

       slices_by_mode = np.zeros((5, 176, 216, 160))
       slices_by_slice = np.zeros((176, 5, 216, 160))

By the way what dataset did you use? Thanks!

mingrui commented 7 years ago

hmmm... upon closer inspection,it turns out to be a bug with progress bar:

in __next__ self.update(self.value + 1) File "/home/mingrui/anaconda/envs/python2env/lib/python2.7/site-packages/progressbar/bar.py", line 541, in update % (value, self.min_value, self.max_value)) ValueError: Value 177 is out of range, should be between 0 and 176

for some reason progress bar is not resetting with progress.currval =0

I have removed all progress bar functions for now, and can confirm that the script will process all the data correctly.

Cesarec88 commented 7 years ago

exactely. We used BRats Dataset from 2013