'Getting Invalid argument: shape of all inputs must match:values[0].shape = [401408] != values[1].shape = [24485888] when using IoU metric in keras

I'm using UNet to train on the TACO dataset, which is in COCO format. I tried training my model with the accuracy metric, only to end up with validation accuracy and accuracy reaching 1.000, which is honestly too good to be true. I was told that accuracy isn't exactly a fitting metric for segmentation problems, which is why I tried using IoU. Unfortunately, I get the following errors:

InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument:  Shapes of all inputs must match: values[0].shape = [401408] != values[1].shape = [24485888]
     [[node confusion_matrix/stack_1 (defined at <ipython-input-7-4238ab807505>:96) ]]
  (1) Invalid argument:  Shapes of all inputs must match: values[0].shape = [401408] != values[1].shape = [24485888]
     [[node confusion_matrix/stack_1 (defined at <ipython-input-7-4238ab807505>:96) ]]
     [[confusion_matrix/stack_1/_96]]

I don't know what I'm doing wrong, since my images are being resized as they are passed into my data generator function by this function:

def getImage(imageObj, img_folder, input_image_size):
    # Read and normalize an image
    train_img = io.imread(img_folder + '/' + imageObj['file_name'])/255.0
    # Resize
    train_img = cv2.resize(train_img, input_image_size)
    if (len(train_img.shape)==3 and train_img.shape[2]==3): # If it is a RGB 3 channel image
        return train_img
    else: # To handle a black and white image, increase dimensions to 3
        stacked_img = np.stack((train_img,)*3, axis=-1)
        return stacked_img

where input_image_size = (224,224). In my UNet model, the input layer is as follows:

##Input Layer
inputs = Input((IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS))

where IMG_WIDTH and IMG_HEIGHT are both 224, and IMG_CHANNELS = 3. I'm also using sparse_categorical_crossentropy for my loss function. I'm pretty new to this, and I don't know what I'm doing wrong. Any help would be much appreciated. Cheers!



Solution 1:[1]

I was facing a similar issue. the problem was with the output layer where the number of filter I had was 1 but the mask was a 3D image hence the filter number was suppose to be 3. maybe for you too, the number of filters in output layers doesn't match the mask dimensions. try changing it

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vedant Joshi