Skip to content

Instantly share code, notes, and snippets.

@Yuvnish017
Created June 24, 2021 06:38
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 Yuvnish017/66756a66ef9b7bdc061bdd6d6a2fb315 to your computer and use it in GitHub Desktop.
Save Yuvnish017/66756a66ef9b7bdc061bdd6d6a2fb315 to your computer and use it in GitHub Desktop.
def gram_matrix(x):
x = tf.transpose(x, (2, 0, 1))
features = tf.reshape(x, (tf.shape(x)[0], -1))
gram = tf.matmul(features, tf.transpose(features))
return gram
def style_loss(style, combination):
S = gram_matrix(style)
C = gram_matrix(combination)
channels = 3
size = img_rows * img_cols
return tf.reduce_sum(tf.square(S - C)) / (channels * (3 ** 2) * (size ** 2))
def content_loss(base, combination):
return tf.reduce_sum(tf.square(combination - base))
def total_variation_loss(x):
a = tf.square(x[:, : img_rows - 1, : img_cols - 1, :] - x[:, 1:, : img_cols - 1, :])
b = tf.square(x[:, : img_rows - 1, : img_cols - 1, :] - x[:, : img_rows - 1, 1:, :])
return tf.reduce_sum(tf.pow(a + b, 1.25))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment