Last active
September 20, 2018 08:31
-
-
Save daa233/56b574289924fcfd6123907cecedd105 to your computer and use it in GitHub Desktop.
Total-Variation Loss https://github.com/mohamedkeid/Style-Transfer-Algorithm/blob/master/src/style_transfer.py#L106-L119
This file contains hidden or 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
TOTAL_VARIATION_SMOOTHING = 1.5 | |
# Compute total variation regularization loss term given a variable image (x) and its shape | |
def get_total_variation(x, shape): | |
with tf.name_scope('get_total_variation'): | |
# Get the dimensions of the variable image | |
height = shape[1] | |
width = shape[2] | |
size = reduce(lambda a, b: a * b, shape) ** 2 | |
# Disjoin the variable image and evaluate the total variation | |
x_cropped = x[:, :height - 1, :width - 1, :] | |
left_term = tf.square(x[:, 1:, :width - 1, :] - x_cropped) | |
right_term = tf.square(x[:, :height - 1, 1:, :] - x_cropped) | |
smoothed_terms = tf.pow(left_term + right_term, TOTAL_VARIATION_SMOOTHING / 2.) | |
return tf.reduce_sum(smoothed_terms) / size |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment