VGG, perceptual loss in keras

Question:

I’m wondering if it’s possible to add a custom model to a loss function in keras. For example:

def model_loss(y_true, y_pred):
    inp = Input(shape=(128, 128, 1))
    x = Dense(2)(inp)
    x = Flatten()(x)

    model = Model(inputs=[inp], outputs=[x])
    a = model(y_pred)
    b = model(y_true)

    # calculate MSE
    mse = K.mean(K.square(a - b))
    return mse

This is a simplified example. I’ll actually be using a VGG net in the loss, so just trying to understand the mechanics of keras.

Asked By: William Falcon

||

Answers:

The usual way of doing that is appending your VGG to the end of your model, making sure all its layers have trainable=False before compiling.

Then you recalculate your Y_train.

Suppose you have these models:

mainModel - the one you want to apply a loss function    
lossModel - the one that is part of the loss function you want   

Create a new model appending one to another:

from keras.models import Model

lossOut = lossModel(mainModel.output) #you pass the output of one model to the other

fullModel = Model(mainModel.input,lossOut) #you create a model for training following a certain path in the graph. 

This model will have the exact same weights of mainModel and lossModel, and training this model will affect the other models.

Make sure lossModel is not trainable before compiling:

lossModel.trainable = False
for l in lossModel.layers:
    l.trainable = False

fullModel.compile(loss='mse',optimizer=....)

Now adjust your data for training:

fullYTrain = lossModel.predict(originalYTrain)

And finally do the training:

fullModel.fit(xTrain, fullYTrain, ....)
Answered By: Daniel Möller

This is old but I’m going to answer it because no one did directly. You definitely can call another model in a custom loss, and I actually think it’s much easier than adding the model to the end of your main model and creating a whole new one and a whole new set of training labels.

Here is an example that both calls a model and an outside function that we define –

def normalize_tensor(in_feat):
    norm_factor = tf.math.sqrt(tf.keras.backend.sum(in_feat**2, axis=-1, keepdims=True))
    return in_feat / (norm_factor + 1e-10)

def VGGLoss(y_true, y_pred):
    true = vgg(preprocess_input(y_true * 255))
    pred = vgg(preprocess_input(y_pred * 255))

    t = normalize_tensor(true[i])
    p = normalize_tensor(pred[i])
    vggLoss = tf.math.reduce_mean(tf.math.square(t - p))

    return vggLoss

vgg() just calls the vgg16 model with no head.
preprocess_input is a keras function that normalizes inputs to be used in the vgg model (here we are assuming your model outputs an image in 0-1 range, then we multiply by 255 to get 0-255 range for vgg).
normalize_tensor takes the vgg activations and makes them have a magnitude of 1 for each channel, otherwise your loss will be massive.

Answered By: Frobot