# 课程笔记

• 建立内容损失函数 ${J}_{content}\left(C,G\right)$$J_{content}(C,G)$
• 建立风格损失函数 ${J}_{style}\left(S,G\right)$$J_{style}(S,G)$
• 加权组合起来，即总体损失函数 $J\left(G\right)=\alpha {J}_{content}\left(C,G\right)+\beta {J}_{style}\left(S,G\right)$$J(G) = \alpha J_{content}(C,G) + \beta J_{style}(S,G)$.

CNN是对输入的图片进行处理的神经网络，一般有卷积层、池化层、全连接层，每一层都是对图片进行像素级的运算。图片以矩阵的形式输入神经网络，在经过每一层时的输出依然时矩阵，把这个矩阵反转回去得到的图像，就是这一层对图片进行处理后得到的图像。

## 内容损失函数

$\begin{array}{}\text{(1)}& {J}_{content}\left(C,G\right)=\frac{1}{4×{n}_{H}×{n}_{W}×{n}_{C}}\sum _{\text{all entries}}\left({a}^{\left(C\right)}-{a}^{\left(G\right)}{\right)}^{2}\end{array}$

${n}_{H},{n}_{W}$$n_H, n_W$ and ${n}_{C}$$n_C$ 时指定神经网络层的输出矩阵，这里为了方便计算，做举证展开（Unrolled），如下图：

What you should remember:
- The content cost takes a hidden layer activation of the neural network, and measures how different ${a}^{\left(C\right)}$$a^{(C)}$ and ${a}^{\left(G\right)}$$a^{(G)}$ are.
- When we minimize the content cost later, this will help make sure $G$$G$ has similar content as $C$$C$.

## 风格损失函数

${G}_{ij}={v}_{i}^{T}{v}_{j}=np.dot\left({v}_{i},{v}_{j}\right)$
.

$\begin{array}{}\text{(2)}& {J}_{style}^{\left[l\right]}\left(S,G\right)=\frac{1}{4×{{n}_{C}}^{2}×\left({n}_{H}×{n}_{W}{\right)}^{2}}\sum _{i=1}^{{n}_{C}}\sum _{j=1}^{{n}_{C}}\left({G}_{ij}^{\left(S\right)}-{G}_{ij}^{\left(G\right)}{\right)}^{2}\end{array}$

What you should remember:
- The style of an image can be represented using the Gram matrix of a hidden layer’s activations. However, we get even better results combining this representation from multiple different layers. This is in contrast to the content representation, where usually using just a single hidden layer is sufficient.
- Minimizing the style cost will cause the image $G$$G$ to follow the style of the image $S$$S$.

## 总体损失函数

$J\left(G\right)=\alpha {J}_{content}\left(C,G\right)+\beta {J}_{style}\left(S,G\right)$

# pycharm版程序

import os
import sys
import scipy.io
import scipy.misc
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
from PIL import Image
from nst_utils import *
import numpy as np
import tensorflow as tf

import datetime

def compute_content_cost(a_C, a_G):
"""
Computes the content cost

Arguments:
a_C -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing content of the image C
a_G -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing content of the image G

Returns:
J_content -- scalar that you compute using equation 1 above.
"""

### START CODE HERE ###
# Retrieve dimensions from a_G (≈1 line)
m, n_H, n_W, n_C = a_G.get_shape().as_list()                # 用 a_G 和 a_C 的区别?

# Reshape a_C and a_G (≈2 lines)
a_C_unrolled = tf.reshape(a_C,[n_H * n_W, n_C])
a_G_unrolled = tf.reshape(a_G,[n_H * n_W, n_C])

# compute the cost with tensorflow (≈1 line)
J_content = tf.reduce_sum(tf.square(tf.subtract(a_C_unrolled, a_G_unrolled))) / (4*n_H*n_W*n_C)
### END CODE HERE ###

return J_content

def gram_matrix(A):
"""
Argument:
A -- matrix of shape (n_C, n_H*n_W)

Returns:
GA -- Gram matrix of A, of shape (n_C, n_C)
"""

### START CODE HERE ### (≈1 line)
GA = tf.matmul(A, A ,transpose_a=False, transpose_b=True)       # 矩阵相乘,后面的flag表示是否对对应矩阵进行转置操作
### END CODE HERE ###

return GA

def compute_layer_style_cost(a_S, a_G):
"""
Arguments:
a_S -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing style of the image S
a_G -- tensor of dimension (1, n_H, n_W, n_C), hidden layer activations representing style of the image G

Returns:
J_style_layer -- tensor representing a scalar value, style cost defined above by equation (2)
"""

### START CODE HERE ###
# Retrieve dimensions from a_G (≈1 line)
m, n_H, n_W, n_C = a_G.get_shape().as_list()

# Reshape the images to have them of shape (n_H*n_W, n_C) (≈2 lines)
a_S = tf.reshape(a_S, [n_W*n_H, n_C])
a_G = tf.reshape(a_G, [n_W*n_H, n_C])

# Computing gram_matrices for both images S and G (≈2 lines)
GS = gram_matrix(tf.transpose(a_S))
GG = gram_matrix(tf.transpose(a_G))
# GS = gram_matrix(a_S)
# GG = gram_matrix(a_G)

# Computing the loss (≈1 line)
J_style_layer = tf.reduce_sum(tf.square(tf.subtract(GS, GG))) / (4*tf.to_float(tf.square(n_C*n_H*n_W)))

### END CODE HERE ###

return J_style_layer

def compute_style_cost(model, STYLE_LAYERS):
"""
Computes the overall style cost from several chosen layers

Arguments:
model -- our tensorflow model
STYLE_LAYERS -- A python list containing:
- the names of the layers we would like to extract style from
- a coefficient for each of them

Returns:
J_style -- tensor representing a scalar value, style cost defined above by equation (2)
"""

# initialize the overall style cost
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_style

def total_cost(J_content, J_style, alpha=10, beta=40):
"""
Computes the total cost function

Arguments:
J_content -- content cost coded above
J_style -- style cost coded above
alpha -- hyperparameter weighting the importance of the content cost
beta -- hyperparameter weighting the importance of the style cost

Returns:
J -- total cost as defined by the formula above.
"""

### START CODE HERE ### (≈1 line)
J = alpha * J_content + beta * J_style
### END CODE HERE ###

return J

def model_nn(sess, input_image, num_iterations=200):
# Initialize global variables (you need to run the session on the initializer)
### START CODE HERE ### (1 line)
sess.run(tf.global_variables_initializer())
### END CODE HERE ###

# Run the noisy input image (initial generated image) through the model. Use assign().
### START CODE HERE ### (1 line)
sess.run(model['input'].assign(input_image))
### END CODE HERE ###

for i in range(num_iterations):

# Run the session on the train_step to minimize the total cost
### START CODE HERE ### (1 line)
sess.run(train_step)
### END CODE HERE ###

# Compute the generated image by running the session on the current model['input']
### START CODE HERE ### (1 line)
generated_image = sess.run(model['input'])
### END CODE HERE ###

# Print every 20 iteration.
if i % 20 == 0:
Jt, Jc, Js = sess.run([J, J_content, J_style])
print("Iteration " + str(i) + " :")
print("total cost = " + str(Jt))
print("content cost = " + str(Jc))
print("style cost = " + str(Js))

# save current generated image in the "/output" directory
save_image("out1/3/" + str(i) + ".png", generated_image)

# save last generated image
save_image('out1/3/generated_image.jpg', generated_image)

return generated_image

if __name__ == '__main__':

starttime = datetime.datetime.now()

###############################################
# Reset the graph
tf.reset_default_graph()

# Start interactive session
sess = tf.InteractiveSession()

content_image = reshape_and_normalize_image(content_image)

style_image = reshape_and_normalize_image(style_image)

generated_image = generate_noise_image(content_image)
plt.imshow(generated_image[0])
plt.show()

STYLE_LAYERS = [                                     # style_layers 的作用
('conv1_1', 0.2),
('conv2_1', 0.2),
('conv3_1', 0.2),
('conv4_1', 0.2),
('conv5_1', 0.2)]

# Assign the content image to be the input of the VGG model.
sess.run(model['input'].assign(content_image))

# Select the output tensor of layer conv4_2
out = model['conv4_2']

# Set a_C to be the hidden layer activation from the layer we have selected
a_C = sess.run(out)

# Set a_G to be the hidden layer activation from same layer. Here, a_G references model['conv4_2']
# 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 the content cost
J_content = compute_content_cost(a_C, a_G)

# Assign the input of the model to be the "style" image
sess.run(model['input'].assign(style_image))

# Compute the style cost
J_style = compute_style_cost(model, STYLE_LAYERS)

### START CODE HERE ### (1 line)
J = total_cost(J_content=J_content, J_style=J_style)
### END CODE HERE ###

# define optimizer (1 line)

# define train_step (1 line)
train_step = optimizer.minimize(J)

model_nn(sess, generated_image)

#################################################
endtime = datetime.datetime.now()
print("the running time :" + str((endtime - starttime).seconds))
print("END!")

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120