Skip to content

Instantly share code, notes, and snippets.

@RaphaelMeudec
Last active March 20, 2018 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RaphaelMeudec/18c560ff875cfbe32cc0ff38d3374f1b to your computer and use it in GitHub Desktop.
Save RaphaelMeudec/18c560ff875cfbe32cc0ff38d3374f1b to your computer and use it in GitHub Desktop.
import keras.backend as K
from keras.applications.vgg16 import VGG16
from keras.models import Model
image_shape = (256, 256, 3)
def perceptual_loss(y_true, y_pred):
vgg = VGG16(include_top=False, weights='imagenet', input_shape=image_shape)
loss_model = Model(inputs=vgg.input, outputs=vgg.get_layer('block3_conv3').output)
loss_model.trainable = False
return K.mean(K.square(loss_model(y_true) - loss_model(y_pred)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment