Search code examples
tensorflowkerasloss-function

Custom Loss Function - Attention to small regions


I want to use a loss function which includes an expression for the area used by the attention model.

My model is a classification model, designed to perform the decision based on a small region of the original image.

So I would like my loss function to be:

Loss = categorical_crossentropy(y_pred, y_true) + alpha*A

where A is the area from the attention model.

How can I create a custom loss function in Keras, which minimizes the sum of the standard loss + some additional function?


Solution

  • Make a model that outputs both things, y_pred and A:

    #blablabla functional API model definition
    model = Model(inputs, [predictions, areaOutput])
    

    Make a custom area loss:

    def areaLoss(trueArea, predArea):
        return predArea
    

    Compile the model with one loss per output, and use alpha as the weight for the area loss:

    model.compile(loss=['categorical_crossentropy', areaLoss], loss_weights=[1,alpha], ...)
    

    Fit with a dummy value for the area as output:

    model.fit(x_train, [y_train, np.zeros((totalSamples,))], ...)