Search code examples
pythontensorflowkerassemantic-segmentation

Error during compiling a Keras model with own loss function


When I've tried to give to compile a model in Keras with a loss function from here, I've got an error

ValueError: Shape must be rank 2 but is rank 1 for 'loss/activation_10_loss/MatMul' (op: 'MatMul') with input shapes: [?], [?].

I've tried to fix this error following this answer.

def get_loss_function(weights):
    def loss(y_pred, y_true):
        return (y_pred - y_true) * weights # or whatever your loss function   should be
    return loss
 model.compile(loss=get_loss_function(conv_weights), optimizer=SGD(lr=0.1))

The easiest way to replicate the issue:

from segmentation_models.metrics import iou_score
from segmentation_models import Unet
import keras

class Losses:

    def __init__(self):
        pass
    @staticmethod
    def IoULoss(targets, inputs, smooth=1e-6):
        logger=logging.getLogger("Losses.IoULoss")
        logger.setLevel(Debug_param.debug_scope())
        # flatten label and prediction tensors
        # logger.critical(("targets.shape",targets.get_shape().as_list(), "inputs.shape",inputs.shape))
        inputs = K.flatten(inputs)
        targets = K.flatten(targets)
        logger.critical(("flatten", "targets.shape", targets.shape, "inputs.shape", inputs.shape))

        intersection = K.sum(K.dot(targets, inputs))
        total = K.sum(targets) + K.sum(inputs)
        union = total - intersection

        IoU = (intersection + smooth) / (union + smooth)
        return 1 - IoU

model = Unet("resnet34", backend=None, classes=1, activation='softmax')
opt = keras.optimizers.Adam(lr=config.lr)
model.compile(loss=Losses.IoULoss, optimizer=opt,
                      metrics=[iou_score, "accuracy"])

How to compile a model with a custom loss function or how to prevent the error?

Python version 3.7.4, keras 2.3.0, TF 1.14, segmentation-models 0.2.1


Solution

  • When I reproduce your error, I see that the problem is happening at the function K.dot(). It looks like Keras expects two rank 2 tensors (i.e. a matrix or a 2D array) for that function. You're making the inputs and targets into 1D tensors (vectors) by using K.flatten(). Here's an example for how you could make a 2D tensor from your data:

    inputs = K.reshape(inputs, [1, -1]) # 1 row, as many columns as needed
    targets = K.reshape(targets, [-1, 1]) # 1 column, as many rows as needed