VGG, perceptual loss in keras
I am wondering if it is possible to add a custom model to the 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 am actually using the VGG network on a loser, so just trying to understand the mechanics of keras.
+3
source to share
1 answer
The usual way to do this is to add VGG to the end of your model, making sure all layers have it before compiling it trainable=False
.
Then you recalculate your Y_train.
Let's assume 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 adding to each other:
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 same mainModel and lossModel weights, and training this model will affect other models.
Make sure lossModel is not trained before compiling:
lossModel.trainable = False
for l in lossModel.layers:
l.trainable = False
fullModel.compile(loss='mse',optimizer=....)
Now set up your training data:
fullYTrain = lossModel.predict(originalYTrain)
And finally, do the training:
fullModel.fit(xTrain, fullYTrain, ....)
+3
source to share