-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss.py
35 lines (25 loc) · 1.23 KB
/
loss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from keras.models import Model
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.applications.vgg19 import VGG19
import keras.backend as K
def l1_loss(y_true, y_pred):
return K.mean(K.abs(y_pred - y_true))
def l2_loss(y_true, y_pred):
return K.mean(K.square(y_pred - y_true))
def img_preprocess(img):
img = (img + 1.) * 127.5
return preprocess_input(img)
def perceptual_loss(y_true, y_pred):
y_true = img_preprocess(y_true)
y_pred = img_preprocess(y_pred)
vgg = VGG16(include_top=False, weights='imagenet', input_shape=(256,256,3))
loss_model = Model(inputs=vgg.input, outputs=vgg.get_layer('block3_conv3').output)
loss_model.trainable = False
return 0.006 * K.mean(K.square(loss_model(y_true) - loss_model(y_pred)))
def l1_perceptual_loss(y_true, y_pred):
y_true = img_preprocess(y_true)
y_pred = img_preprocess(y_pred)
vgg = VGG16(include_top=False, weights='imagenet', input_shape=(256,256,3))
loss_model = Model(inputs=vgg.input, outputs=vgg.get_layer('block3_conv3').output)
loss_model.trainable = False
return 0.006 * K.mean(K.square(loss_model(y_true) - loss_model(y_pred))) + K.mean(K.abs(y_pred - y_true), axis=-1)