VGG, perceptual loss in keras
Asked Answered
D

2

7

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.

Decomposition answered 11/5, 2017 at 12:5 Comment(3)
Have you tried it? It sounds simple to try. But I suggest to create the model outside the loss function. Your loss function should start in the line saying a=model(y_pred).Lightproof
But: do you expect that little model to be trained together with the model that contains the loss function?? Then I'd say no way.Lightproof
no, it's a frozen model. this is for VGG lossDecomposition
H
7

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, ....)
Hamza answered 11/5, 2017 at 18:58 Comment(1)
thanks. yeah, i saw this being done in other projects. is there a reason why this can't be added to the symbolic graph in the loss_fn? and operate on y_pred, y_true directly?Decomposition
D
0

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.

Dictaphone answered 11/9, 2022 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.