Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Trivial example for Mixed-Gradient-Error and Mean-Gradient-Error
import tensorflow as tf
def MeanGradientError(outputs, targets, weight):
filter_x = tf.tile(tf.expand_dims(tf.constant([[-1, -2, -2], [0, 0, 0], [1, 2, 1]], dtype = outputs.dtype), axis = -1), [1, 1, outputs.shape[-1])
filter_x = tf.tile(tf.expand_dims(filter_x, axis = -1), [1, 1, 1, outputs.shape[-1]])
filter_y = tf.tile(tf.expand_dims(tf.constant([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype = outputs.dtype), axis = -1), [1, 1, targets.shape[-1]])
filter_y = tf.tile(tf.expand_dims(filter_y, axis = -1), [1, 1, 1, targets.shape[-1]])
# output gradient
output_gradient_x = tf.math.square(tf.nn.conv2d(outputs, filter_x, strides = 1, padding = 'SAME'))
output_gradient_y = tf.math.square(tf.nn.conv2d(outputs, filter_y, strides = 1, padding = 'SAME'))
#target gradient
target_gradient_x = tf.math.square(tf.nn.conv2d(targets, filter_x, strides = 1, padding = 'SAME'))
target_gradient_y = tf.math.square(tf.nn.conv2d(targets, filter_y, strides = 1, padding = 'SAME'))
# square
output_gradients = tf.math.sqrt(tf.math.add(output_gradient_x, output_gradient_y))
target_gradients = tf.math.sqrt(tf.math.add(target_gradient_x, target_gradient_y))
# compute mean gradient error
shape = output_gradients.shape[1:3]
mge = tf.math.reduce_sum(tf.math.squared_difference(output_gradients, target_gradients) / (shape[0] * shape[1]))
return mge * weight
x = tf.random.normal(shape = (224, 224, 3)
y = tf.random.normal(shape = (224, 224, 3)
gradient_loss = MeanGradientError(x, y, weight = 0.1)
@HydrogenSulfate

This comment has been minimized.

Copy link

@HydrogenSulfate HydrogenSulfate commented Nov 27, 2020

I guess there is a small problem in your code at line 4, the sobel filter should be [[-1, -2, -1], [0, 0, 0], [1, 2, 1]], but [[-1, -2, -2], [0, 0, 0], [1, 2, 1]] in your code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment