def get_texture_loss_for_layer(x, s, l):
with tf.name_scope('get_style_loss_for_layer'):
# Compute gram matrices using the activated filter maps of the art and generated images
x_layer_maps = getattr(x, l)
t_layer_maps = getattr(s, l)
x_layer_gram = convert_to_gram(x_layer_maps)
t_layer_gram = convert_to_gram(t_layer_maps)
# Make sure the feature map dimensions are the same
assert_equal_shapes = tf.assert_equal(x_layer_maps.get_shape(), t_layer_maps.get_shape())
with tf.control_dependencies([assert_equal_shapes]):
# Compute and return the normalized gram loss using the gram matrices
shape = x_layer_maps.get_shape().as_list()
size = reduce(lambda a, b: a * b, shape) ** 2
gram_loss = get_l2_norm_loss(x_layer_gram - t_layer_gram)
return gram_loss / size
# Compute total variation regularization loss term given a variable image (x) and its shape
评论列表
文章目录