但是我们如何知道它们是否相关呢?
如果两个filters激活之间的点积大,则说两个通道是相关的,如果它很小则是不相关的 。以数学方式:
风格图像的Gram矩阵(S):
这里k \'和k \'表示层l的不同filters或通道 。我们将其称为Gkk \' [l][S] 。

文章插图
用于风格图像的Gram矩阵
生成图像的Gram矩阵(G):
这里k和k\'代表层L的不同filters或通道 。让我们称之为Gkk\'[l] [G] 。

文章插图
生成图像的Gram矩阵
现在,我们可以定义风格损失:
风格与生成图像的成本函数是风格图像的Gram矩阵与生成图像的Gram矩阵之差的平方 。

文章插图
风格成本函数
现在,让我们定义神经风格迁移的总损失 。
总损失函数:总是内容和风格图像的成本之和 。在数学上,它可以表示为:

文章插图
神经风格迁移的总损失函数
您可能已经注意到上述等式中的Alpha和beta 。它们分别用于衡量内容成本和风格成本 。通常,它们在生成的输出图像中定义每个成本的权重 。
一旦计算出损失,就可以使用反向传播使这种损失最小化,反向传播又将我们随机生成的图像优化为有意义的艺术品 。
使用tensorflow实现神经风格迁移Python示例代码:#Importing Required librariesimport osimport sysimport scipy.miscimport scipy.ioimport matplotlib.pyplot as pltimport numpy as npfrom matplotlib.pyplot import imshowfrom PIL import Imagefrom nst_utils import *#Before running this cell Please download the VGG-19 weights from this site: https://www.mathworks.com/help/deeplearning/ref/vgg19.html#bvo3tw1-5#and paste it in the pretrained_model foldermodel = load_vgg_model("pretrained_model/imagenet-vgg-verydeep-19.mat")content_image=scipy.misc.imread("images\YOUR CONTENT IMAGE NAME")#imshow(content_image)#Since the style and the content image should be of same size#Use this to resize either style or content imagefrom PIL import Imagefrom matplotlib.pyplot import imshowimage = Image.open(\'images\IMAGE TO BE RESIZED\')image = image.resize((300,500), Image.ANTIALIAS)imshow(image)image.save("images\NAME OF RESIZED IMAGE")

文章插图

文章插图
style_image=scipy.misc.imread("images\style1.jpg")imshow(style_image)

文章插图
辅助函数
def compute_content_cost(a_C,a_G): m,n_H,n_W,n_C=a_G.get_shape().as_list()a_C_unrolled=tf.transpose(a_C) a_G_unrolled=tf.transpose(a_G)#Content_cost: J_content=(1/(4 * n_H * n_W * n_C)) * tf.reduce_sum(tf.pow((a_C_unrolled - a_G_unrolled),2)) return J_contentdef gram_matrix(A): GA=tf.matmul(A,tf.transpose(A)) return GAdef compute_layer_style_cost(a_S,a_G): m,n_H,n_W,n_C=a_G.get_shape().as_list()#Reshape: a_S=tf.transpose(tf.reshape(a_S,[n_H * n_W, n_C])) a_G=tf.transpose(tf.reshape(a_G,[n_H * n_W, n_C]))#Gram matrix: GS=gram_matrix(a_S) GG=gram_matrix(a_G)#Cost: J_style=(1/(4 * (n_C)**2 * (n_H * n_W)**2)) * tf.reduce_sum(tf.pow((GS - GG),2))return J_styleSTYLE_LAYERS = [ (\'conv1_1\', 0.2), (\'conv2_1\', 0.2), (\'conv3_1\', 0.2), (\'conv4_1\', 0.2), (\'conv5_1\', 0.2)]def compute_style_cost(model,STYLE_LAYERS): J_style = 0 for layer_name, coeff in STYLE_LAYERS: # Select the output tensor of the currently selected layer out = model[layer_name] # Set a_S to be the hidden layer activation from the layer we have selected, by running the session on out a_S = sess.run(out) # Set a_G to be the hidden layer activation from same layer. Here, a_G references model[layer_name]# and isn\'t evaluated yet. Later in the code, we\'ll assign the image G as the model input, so that # when we run the session, this will be the activations drawn from the appropriate layer, with G as input. a_G = out# Compute style_cost for the current layer J_style_layer = compute_layer_style_cost(a_S, a_G) # Add coeff * J_style_layer of this layer to overall style cost J_style += coeff * J_style_layer return J_styledef total_cost(J_content, J_style, alpha = 10, beta = 40): J = alpha * J_content + beta * J_style return J
- 最新孕妇血糖标准 孕妇血糖正常值标准
- 神经鞘磷脂从哪里提取 神经鞘磷脂是什么
- 大便的地方出血了怎么回事 大便时出血是什么原因
- 喉炎的症状是什么 喉炎的早期症状表现
- 风水金钟图片 风水里的金钟是什么
- 冰封之心是什么意思 冰冻之心
- 什么是生殖系统畸形 生殖道畸形是什么症状
- 容易做梦的人是什么原因 突然经常做梦是什么原因
- 淘宝直播卖货扣点吗?直播规则和注意事项是什么?
- 太空系列蛋糕 太空蛋糕是什么
特别声明:本站内容均来自网友提供或互联网,仅供参考,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
