Skip to content

Instantly share code, notes, and snippets.

@pranjal-joshi
Created April 24, 2017 10:29
Show Gist options
  • Save pranjal-joshi/37eb5fd2d0b8c9b95daad7195cccad90 to your computer and use it in GitHub Desktop.
Save pranjal-joshi/37eb5fd2d0b8c9b95daad7195cccad90 to your computer and use it in GitHub Desktop.
VGG-16 Loss calculation functions
def content_loss(base,final):
return K.sum(K.square(final-base)) # square error
def gram_matrix(x):
features = K.batch_flatten(x) #flatten -> converts multidimension matrix to single dimensional array
gram = K.dot(features,K.transpose(features))
return gram
def style_loss(style,final):
s = gram_matrix(style)
f = gram_matrix(final)
channels = 3 # RGB image
size = img_nrows * img_ncols
return K.sum(K.square(s-f))/(4.0*(channels**2)*(size**2)) # ** is 'raise to' operator in python.
def total_var_loss(x):
a = K.square(x[:,:,:img_nrows-1,:img_ncols-1] - x[:,:,1:,:img_ncols-1])
b = K.square(x[:,:,:img_nrows-1,:img_ncols-1] - x[:,:,:img_nrows-1,1:])
return K.sum(K.pow(a+b,1.25))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment