我目前正在熟悉 TensorFlow 和机器学习。我正在做一些关于样式转换的教程,现在我有一部分示例代码我无法理解。
我想我明白了主要思想:有三个图像,内容图像,样式图像和混合图像。我们先说一下内容损失,因为如果我能理解这一点,我也会理解风格损失。所以我有内容图像和混合图像(从一些带有一些噪声的分布开始)和 VGG16 模型。
据我所知,我现在应该将内容图像输入到网络中的某个层,并查看该层对于内容图像输入的输出(特征图)是什么。
之后,我还应该将具有混合图像的网络提供给与之前相同的层,并查看混合图像输入该层的输出(特征图)是什么。
然后我应该从这两个输出中计算损失函数,因为我希望混合图像具有与内容图像相似的特征图。
我的问题是我不明白这是如何在网上找到的示例代码中完成的。
示例代码如下: http: //gcucurull.github.io/tensorflow/style-transfer/2016/08/18/neural-art-tf/
但几乎所有的例子都使用了相同的方法。
内容损失定义如下:
def content_loss(cont_out, target_out, layer, content_weight):
# content loss is just the mean square error between the outputs of a given layer
# in the content image and the target image
cont_loss = tf.reduce_sum(tf.square(tf.sub(target_out[layer], cont_out)))
# multiply the loss by its weight
cont_loss = tf.mul(cont_loss, content_weight, name="cont_loss")
return cont_loss
并且是这样调用的:
# compute loss
cont_cost = losses.content_loss(content_out, model, C_LAYER, content_weight)
其中 content_out 是内容图像的输出,model 是使用的模型,C_LAYER 是对我们想要获得输出的层的引用,而 content_weight 是我们乘以的权重。
问题是我不知何故看不到这将混合图像提供给网络的位置。在我看来,“cont_loss”计算内容图像的输出与图层本身之间的均方根。
魔法应该在这里的某个地方:
cont_loss = tf.reduce_sum(tf.square(tf.sub(target_out[layer], cont_out)))
但是我根本找不到这如何在给定层的内容图像的特征图和混合图像的特征图之间产生 RMS。
如果有人能指出我错在哪里并向我解释,内容损失是如何计算的,我将非常感激。
谢谢!