Skip to content

Instantly share code, notes, and snippets.

@daa233
Last active September 20, 2018 08:31
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 daa233/56b574289924fcfd6123907cecedd105 to your computer and use it in GitHub Desktop.
Save daa233/56b574289924fcfd6123907cecedd105 to your computer and use it in GitHub Desktop.
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