Created
June 24, 2021 06:38
-
-
Save Yuvnish017/66756a66ef9b7bdc061bdd6d6a2fb315 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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