frankkramer-lab / MIScnn

A framework for Medical Image Segmentation with Convolutional Neural Networks and Deep Learning
GNU General Public License v3.0
400 stars 116 forks source link

IndexError: index 255 is out of bounds for axis 1 with size 3 #93

Open Qcatbot opened 3 years ago

Qcatbot commented 3 years ago

Hello, I use MIScnn repo to segment medical images. My data come from 2018 Atrial Segmentation Challenge. I follow exactly the same way the tutorial says, however, I get the error. Could you please give a clue why this error comes when I test it?

Thank you.

Qcatbot commented 3 years ago

My dataset does not contain multi-class segmentation. Only the left atrial of human heart.

My segmentation mask like follows;

case_00090 [ 0. 255.] case_00091 [ 0. 255.] case_00092 [ 0. 255.] case_00093 [ 0. 255.] case_00094 [ 0. 255.] case_00095 [ 0. 255.] case_00096 [ 0. 255.] case_00097 [ 0. 255.] case_00098 [ 0. 255.] case_00099 [ 0. 255.] [[[[0.] [0.] [0.] ... [0.] [0.] [0.]] Its not very clear whats the problem. Much appreciate if you could guide me. Thank you!

muellerdo commented 3 years ago

Hey @Qcatbot,

you have to preprocess your data to be sparse categorical.

This mean that your segmentation mask should have only increasing class values like 0 (for class 0), 1 (for class 1), ... This grayscale mask [0, 255] instead of [0,1] can not be processed by MIScnn.

Also I'm quite confused why the error states "for axis 1 with size 3". Sounds like keras found more 3 classes instead of 2. Could you provide the full error log and a reproducible example in google colab?

Cheers, Dominik

leonieburg commented 3 years ago

Hi @Qcatbot, I had the same problem. For some reason MIScnn needs to have consecutive values for the classes when training. So I created a subfunction for preprocessing where I changed all values from 255 to 1.

class ChangeValues(Abstract_Subfunction):
   #---------------------------------------------#
   #                Initialization               #
   #---------------------------------------------#
  def __init__(self, val_to_change, change_in):
    self.val_to_change = val_to_change
    self.change_in = change_in
    #---------------------------------------------#
    #                Preprocessing                #
    #---------------------------------------------#
  def preprocessing(self, sample, training=True):
    seg_temp = sample.seg_data
    if(sample.seg_data is not None):
      sample.seg_data = np.where(seg_temp == self.val_to_change, self.change_in, seg_temp)
    #---------------------------------------------#
    #               Postprocessing                #
    #---------------------------------------------#
  def postprocessing(self, sample, prediction):
    pred = prediction
    if prediction is not None:
      prediction = np.where(pred == self.change_in, self.val_to_change, pred)
    return prediction

After that you have to call the function sf_change = ChangeValues(val_to_change=255, change_in=1) and pass sf_change with the other subfunctions you want to use to the Preprocessor.

I hope this solves your problem, Leonie

Qcatbot commented 3 years ago

Hi Muellerdo & Leonie, I will modify according to your suggestions and get back here. Please dont close this discussion. Thank you!

hinatanvir commented 1 year ago

hi @leonieburg , i have encountered the same error however i do not understand clearly how to apply the solution you have proposed. i know i have a label -> -1 is my images that is often treated as 255 , other than that i have 19 classes of interest from 0 to 18. I use a preprocess function that converts label to-catgorical. this is where the problem is occuring. I cannot understand where to position this solution subfunction that you proposed.

error preprocess