当前位置:   article > 正文

实战二:手把手教你图像风格迁移_风格迁移项目怎么做

风格迁移项目怎么做

                    实战二:手把手教你图像风格迁移

一、简介

图像风格迁移是指,将一幅内容图的内容,和一幅或多幅风格图融合在一起,从而生成一些有意思的图片。

有兴趣的可以看一下外文文献 Leon A. Gatys' paper, A Neural Algorithm of Artistic Style

https://arxiv.org/abs/1508.06576(ps访问需外网,可参考后面的操作)

例子:

二、方法一TensorFlow实现图像风格迁移

1. 准备

安装包:

pip install numpy scipy tensorflow keras

图片准备:

准备好内容图片以及风格图片

2. 原理

为了将风格图的风格和内容图的内容进行融合,所生成的图片,在内容应当尽可能接近内容图,在风格中应当尽可能接近风格图。

因此需要定义内容损失函数和风格损失函数,经过加权后作为总的损失函数。

           实现步骤如下:

   a.随机产生一张图片

   b.在每轮迭代中,根据总的损失函数,调整图片的像素值

   c.经过多轮迭代,得到优化后的图片

 

3.内容损失函数

 两张图片在内容中相似,不能仅仅靠简单的纯像素比较

 CNN具有抽象和理解图像的能力,因此可以考虑将各个卷积层的输出作为图像的内容。

 以VGG19为例,其中包括了多个卷积层、池化层,以及最后的全连接层。

这里使用conv4_2的输出作为图像的内容表示,定义内容损失函数如下

4.风格损失函数

风格是一个很难说清楚的概念,可能是笔触、纹理、结构、布局、用色等等。

这里使用卷积层各个特征图之间的互相关作为图像的风格,以conv1_1为例

  • 共包含64个特征图即feature map,或者说图像的深度、通道的个数
  • 每个特征图都是对上一层输出的一种理解,可以类比成64个人对同一幅画的不同理解
  • 这些人可能分别偏好印象派、现代主义、超现实主义、表现主义等不同风格
  • 当图像是某一种风格时,可能这一部分人很欣赏,但那一部分人不喜欢
  • 当图像是另一种风格时,可能这一部分人不喜欢,但那一部分人很欣赏
  • 64个人之间理解的差异,可以用特征图的互相关表示,这里使用Gram矩阵计算互相关
  • 不同的风格会导致差异化的互相关结果

Gram矩阵的计算如下,如果有64个特征图,那么Gram矩阵的大小便是64 × 64,第i行第j列的值表示第i个特征图和第j个特征图之间的互相关,用内积计算

风格损失函数定义如下,对多个卷积层的风格表示差异进行加权

 

5.损失函数

总的损失函数即内容损失函数和风格损失函数的加权,不同的权重会导致不同的迁移效果。

6.TensorFlow代码实现

  1. # -*- coding: utf-8 -*-
  2. # 加载库
  3. import tensorflow as tf
  4. import numpy as np
  5. import scipy.io
  6. import scipy.misc
  7. import os
  8. import time
  9. def the_current_time():
  10. print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))))
  11. # 定义一些变量
  12. CONTENT_IMG = 'content.jpg'
  13. STYLE_IMG = 'style5.jpg'
  14. OUTPUT_DIR = 'neural_style_transfer_tensorflow/'
  15. if not os.path.exists(OUTPUT_DIR):
  16. os.mkdir(OUTPUT_DIR)
  17. IMAGE_W = 800
  18. IMAGE_H = 600
  19. COLOR_C = 3
  20. NOISE_RATIO = 0.7
  21. BETA = 5
  22. ALPHA = 100
  23. VGG_MODEL = 'imagenet-vgg-verydeep-19.mat'
  24. MEAN_VALUES = np.array([123.68, 116.779, 103.939]).reshape((1, 1, 1, 3))
  25. # 加载VGG19模型
  26. def load_vgg_model(path):
  27. '''
  28. Details of the VGG19 model:
  29. - 0 is conv1_1 (3, 3, 3, 64)
  30. - 1 is relu
  31. - 2 is conv1_2 (3, 3, 64, 64)
  32. - 3 is relu
  33. - 4 is maxpool
  34. - 5 is conv2_1 (3, 3, 64, 128)
  35. - 6 is relu
  36. - 7 is conv2_2 (3, 3, 128, 128)
  37. - 8 is relu
  38. - 9 is maxpool
  39. - 10 is conv3_1 (3, 3, 128, 256)
  40. - 11 is relu
  41. - 12 is conv3_2 (3, 3, 256, 256)
  42. - 13 is relu
  43. - 14 is conv3_3 (3, 3, 256, 256)
  44. - 15 is relu
  45. - 16 is conv3_4 (3, 3, 256, 256)
  46. - 17 is relu
  47. - 18 is maxpool
  48. - 19 is conv4_1 (3, 3, 256, 512)
  49. - 20 is relu
  50. - 21 is conv4_2 (3, 3, 512, 512)
  51. - 22 is relu
  52. - 23 is conv4_3 (3, 3, 512, 512)
  53. - 24 is relu
  54. - 25 is conv4_4 (3, 3, 512, 512)
  55. - 26 is relu
  56. - 27 is maxpool
  57. - 28 is conv5_1 (3, 3, 512, 512)
  58. - 29 is relu
  59. - 30 is conv5_2 (3, 3, 512, 512)
  60. - 31 is relu
  61. - 32 is conv5_3 (3, 3, 512, 512)
  62. - 33 is relu
  63. - 34 is conv5_4 (3, 3, 512, 512)
  64. - 35 is relu
  65. - 36 is maxpool
  66. - 37 is fullyconnected (7, 7, 512, 4096)
  67. - 38 is relu
  68. - 39 is fullyconnected (1, 1, 4096, 4096)
  69. - 40 is relu
  70. - 41 is fullyconnected (1, 1, 4096, 1000)
  71. - 42 is softmax
  72. '''
  73. vgg = scipy.io.loadmat(path)
  74. vgg_layers = vgg['layers']
  75. def _weights(layer, expected_layer_name):
  76. W = vgg_layers[0][layer][0][0][2][0][0]
  77. b = vgg_layers[0][layer][0][0][2][0][1]
  78. layer_name = vgg_layers[0][layer][0][0][0][0]
  79. assert layer_name == expected_layer_name
  80. return W, b
  81. def _conv2d_relu(prev_layer, layer, layer_name):
  82. W, b = _weights(layer, layer_name)
  83. W = tf.constant(W)
  84. b = tf.constant(np.reshape(b, (b.size)))
  85. return tf.nn.relu(tf.nn.conv2d(prev_layer, filter=W, strides=[1, 1, 1, 1], padding='SAME') + b)
  86. def _avgpool(prev_layer):
  87. return tf.nn.avg_pool(prev_layer, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
  88. graph = {}
  89. graph['input'] = tf.Variable(np.zeros((1, IMAGE_H, IMAGE_W, COLOR_C)), dtype='float32')
  90. graph['conv1_1'] = _conv2d_relu(graph['input'], 0, 'conv1_1')
  91. graph['conv1_2'] = _conv2d_relu(graph['conv1_1'], 2, 'conv1_2')
  92. graph['avgpool1'] = _avgpool(graph['conv1_2'])
  93. graph['conv2_1'] = _conv2d_relu(graph['avgpool1'], 5, 'conv2_1')
  94. graph['conv2_2'] = _conv2d_relu(graph['conv2_1'], 7, 'conv2_2')
  95. graph['avgpool2'] = _avgpool(graph['conv2_2'])
  96. graph['conv3_1'] = _conv2d_relu(graph['avgpool2'], 10, 'conv3_1')
  97. graph['conv3_2'] = _conv2d_relu(graph['conv3_1'], 12, 'conv3_2')
  98. graph['conv3_3'] = _conv2d_relu(graph['conv3_2'], 14, 'conv3_3')
  99. graph['conv3_4'] = _conv2d_relu(graph['conv3_3'], 16, 'conv3_4')
  100. graph['avgpool3'] = _avgpool(graph['conv3_4'])
  101. graph['conv4_1'] = _conv2d_relu(graph['avgpool3'], 19, 'conv4_1')
  102. graph['conv4_2'] = _conv2d_relu(graph['conv4_1'], 21, 'conv4_2')
  103. graph['conv4_3'] = _conv2d_relu(graph['conv4_2'], 23, 'conv4_3')
  104. graph['conv4_4'] = _conv2d_relu(graph['conv4_3'], 25, 'conv4_4')
  105. graph['avgpool4'] = _avgpool(graph['conv4_4'])
  106. graph['conv5_1'] = _conv2d_relu(graph['avgpool4'], 28, 'conv5_1')
  107. graph['conv5_2'] = _conv2d_relu(graph['conv5_1'], 30, 'conv5_2')
  108. graph['conv5_3'] = _conv2d_relu(graph['conv5_2'], 32, 'conv5_3')
  109. graph['conv5_4'] = _conv2d_relu(graph['conv5_3'], 34, 'conv5_4')
  110. graph['avgpool5'] = _avgpool(graph['conv5_4'])
  111. return graph
  112. # 定义内容损失函数
  113. def content_loss_func(sess, model):
  114. def _content_loss(p, x):
  115. N = p.shape[3]
  116. M = p.shape[1] * p.shape[2]
  117. return (1 / (4 * N * M)) * tf.reduce_sum(tf.pow(x - p, 2))
  118. return _content_loss(sess.run(model['conv4_2']), model['conv4_2'])
  119. # 定义风格损失函数
  120. STYLE_LAYERS = [('conv1_1', 0.5), ('conv2_1', 1.0), ('conv3_1', 1.5), ('conv4_1', 3.0), ('conv5_1', 4.0)]
  121. def style_loss_func(sess, model):
  122. def _gram_matrix(F, N, M):
  123. Ft = tf.reshape(F, (M, N))
  124. return tf.matmul(tf.transpose(Ft), Ft)
  125. def _style_loss(a, x):
  126. N = a.shape[3]
  127. M = a.shape[1] * a.shape[2]
  128. A = _gram_matrix(a, N, M)
  129. G = _gram_matrix(x, N, M)
  130. return (1 / (4 * N ** 2 * M ** 2)) * tf.reduce_sum(tf.pow(G - A, 2))
  131. return sum([_style_loss(sess.run(model[layer_name]), model[layer_name]) * w for layer_name, w in STYLE_LAYERS])
  132. # 随机产生一张初始照片
  133. def generate_noise_image(content_image, noise_ratio=NOISE_RATIO):
  134. noise_image = np.random.uniform(-20, 20, (1, IMAGE_H, IMAGE_W, COLOR_C)).astype('float32')
  135. input_image = noise_image * noise_ratio + content_image * (1 - noise_ratio)
  136. return input_image
  137. # 加载图片
  138. def load_image(path):
  139. image = scipy.misc.imread(path)
  140. image = scipy.misc.imresize(image, (IMAGE_H, IMAGE_W))
  141. image = np.reshape(image, ((1, ) + image.shape))
  142. image = image - MEAN_VALUES
  143. return image
  144. # 保存图片
  145. def save_image(path, image):
  146. image = image + MEAN_VALUES
  147. image = image[0]
  148. image = np.clip(image, 0, 255).astype('uint8')
  149. scipy.misc.imsave(path, image)
  150. # 调用以上函数并训练模型
  151. the_current_time()
  152. with tf.Session() as sess:
  153. content_image = load_image(CONTENT_IMG)
  154. style_image = load_image(STYLE_IMG)
  155. model = load_vgg_model(VGG_MODEL)
  156. input_image = generate_noise_image(content_image)
  157. sess.run(tf.global_variables_initializer())
  158. sess.run(model['input'].assign(content_image))
  159. content_loss = content_loss_func(sess, model)
  160. sess.run(model['input'].assign(style_image))
  161. style_loss = style_loss_func(sess, model)
  162. total_loss = BETA * content_loss + ALPHA * style_loss
  163. optimizer = tf.train.AdamOptimizer(2.0)
  164. train = optimizer.minimize(total_loss)
  165. sess.run(tf.global_variables_initializer())
  166. sess.run(model['input'].assign(input_image))
  167. ITERATIONS = 2000
  168. for i in range(ITERATIONS):
  169. sess.run(train)
  170. if i % 100 == 0:
  171. output_image = sess.run(model['input'])
  172. the_current_time()
  173. print('Iteration %d' % i)
  174. print('Cost: ', sess.run(total_loss))
  175. save_image(os.path.join(OUTPUT_DIR, 'output_%d.jpg' % i), output_image)

7.结果展示

(1)运行

python main_tf

(2)运行过程

可以看出在本机的cpu上跑非常慢,很慢,,,,,

(3)结果展示

三、方法二TensorFlow实现快速图像风格迁移(Fast Neural Style Transfer)

1.简介

使用TensorFlow实现快速图像风格迁移(Fast Neural Style Transfer)

2.准备

风格图及原图

3.原理

在之前介绍的图像风格迁移中,可以根据内容图片和风格图片优化输入图片,使内容损失函数和风格损失函数尽可能小。

使用快速图像风格迁移可大大缩短生成一张迁移图片所需的时间,其模型结构如下,包括转换网络和损失网络。

  • 风格图片是固定的,而内容图片是可变的输入,因此以上模型用于将任意图片快速转换为指定风格的图片
  • 转换网络:参数需要训练,将内容图片转换成迁移图片
  • 损失网络:计算迁移图片和风格图片之间的风格损失,以及迁移图片和原始内容图片之间的内容损失。
  • 经过训练后,转换网络所生成的迁移图片,在内容和输入的内容图片相似,在风格上和指定的风格图片相似
  • 进行推断时,仅使用转换网络,输入内容图片,即可得到对应的迁移图片
  • 如果有多个风格图片,对每个风格分别训练一个模型即可。

4.实现代码

  1. # -*- coding: utf-8 -*-
  2. import tensorflow as tf
  3. import numpy as np
  4. from imageio import imread, imsave
  5. import os
  6. import time
  7. def the_current_time():
  8. print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))))
  9. style = 'udnie'
  10. model = 'samples_%s' % style
  11. content_image = 'sjtu.jpg'
  12. result_image = 'sjtu_%s.jpg' % style
  13. X_image = imread(content_image)
  14. sess = tf.Session()
  15. sess.run(tf.global_variables_initializer())
  16. saver = tf.train.import_meta_graph(os.path.join(model, 'fast_style_transfer.meta'))
  17. saver.restore(sess, tf.train.latest_checkpoint(model))
  18. graph = tf.get_default_graph()
  19. X = graph.get_tensor_by_name('X:0')
  20. g = graph.get_tensor_by_name('transformer/g:0')
  21. the_current_time()
  22. gen_img = sess.run(g, feed_dict={X: [X_image]})[0]
  23. gen_img = np.clip(gen_img, 0, 255) / 255.
  24. imsave(result_image, gen_img)
  25. the_current_time()

 

5.运行结果展示

(1)过程展示

速度很快,在本机CPU中5s左右就完成了

(2)结果展示

四、方法三TensorFlow实现图像风格迁移

前面都是在CPU上跑的程序,可我就想试试在GPU上跑,怎么办?怎么办?

那么方法来了,谷歌提供了Google Colob可以免费试用GPU来跑代码,那么赶快来试试吧!!!

1.外网访问

谷歌访问助手破解版,无需设置推广网站的地址

https://github.com/haotian-wang/google-access-helper

2.Google Colab免费GPU服务器使用教程

参考一个博主提供的文章

https://blog.csdn.net/cocoaqin/article/details/79184540

3.例子

在此过程中,我们按照下列步骤中进行

a.可视化数据

b.基本预处理/准备数据

c.设置损失功能

d.创建模型

e.优化损失功能

4.代码展示

(1)下载图片到文件中

  1. #下载图片到文件中
  2. import os
  3. img_dir = '/tmp/nst'
  4. if not os.path.exists(img_dir):
  5. os.makedirs(img_dir)
  6. !wget --quiet -P /tmp/nst/ https://upload.wikimedia.org/wikipedia/commons/d/d7/Green_Sea_Turtle_grazing_seagrass.jpg
  7. !wget --quiet -P /tmp/nst/ https://upload.wikimedia.org/wikipedia/commons/0/0a/The_Great_Wave_off_Kanagawa.jpg
  8. !wget --quiet -P /tmp/nst/ https://upload.wikimedia.org/wikipedia/commons/b/b4/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg
  9. !wget --quiet -P /tmp/nst/ https://upload.wikimedia.org/wikipedia/commons/0/00/Tuebingen_Neckarfront.jpg
  10. !wget --quiet -P /tmp/nst/ https://upload.wikimedia.org/wikipedia/commons/6/68/Pillars_of_creation_2014_HST_WFC3-UVIS_full-res_denoised.jpg
  11. !wget --quiet -P /tmp/nst/ https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/1024px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg

(2)运行代码

  1. # 导入和配置模块
  2. import matplotlib.pyplot as plt
  3. import matplotlib as mpl
  4. mpl.rcParams['figure.figsize'] = (10,10)
  5. mpl.rcParams['axes.grid'] = False
  6. import numpy as np
  7. from PIL import Image
  8. import time
  9. import functools
  10. import IPython.display
  11. import tensorflow as tf
  12. import tensorflow.contrib.eager as tfe
  13. from tensorflow.python.keras.preprocessing import image as kp_image
  14. from tensorflow.python.keras import models
  15. from tensorflow.python.keras import losses
  16. from tensorflow.python.keras import layers
  17. from tensorflow.python.keras import backend as K
  18. tf.enable_eager_execution()
  19. print("Eager execution: {}".format(tf.executing_eagerly()))
  20. # 定义全局变量
  21. content_path = '/tmp/nst/Green_Sea_Turtle_grazing_seagrass.jpg'
  22. style_path = '/tmp/nst/The_Great_Wave_off_Kanagawa.jpg'
  23. # 定义加载图片函数
  24. def load_img(path_to_img):
  25. max_dim = 512
  26. img = Image.open(path_to_img)
  27. long = max(img.size)
  28. scale = max_dim/long
  29. img = img.resize((round(img.size[0]*scale), round(img.size[1]*scale)), Image.ANTIALIAS)
  30. img = kp_image.img_to_array(img)
  31. # We need to broadcast the image array such that it has a batch dimension
  32. img = np.expand_dims(img, axis=0)
  33. return img
  34. # 定义显示图片的函数
  35. def imshow(img, title=None):
  36. # Remove the batch dimension
  37. out = np.squeeze(img, axis=0)
  38. # Normalize for display
  39. out = out.astype('uint8')
  40. plt.imshow(out)
  41. if title is not None:
  42. plt.title(title)
  43. plt.imshow(out)
  44. # 定义显示原内容图像和风格图像的函数
  45. def display():
  46. plt.figure(figsize=(10,10))
  47. content = load_img(content_path).astype('uint8')
  48. style = load_img(style_path).astype('uint8')
  49. plt.subplot(1, 2, 1)
  50. imshow(content, 'Content Image')
  51. plt.subplot(1, 2, 2)
  52. imshow(style, 'Style Image')
  53. plt.show()
  54. # 定义加载和处理图像函数
  55. def load_and_process_img(path_to_img):
  56. img = load_img(path_to_img)
  57. img = tf.keras.applications.vgg19.preprocess_input(img)
  58. return img
  59. # 定义优化输出的函数
  60. def deprocess_img(processed_img):
  61. x = processed_img.copy()
  62. if len(x.shape) == 4:
  63. x = np.squeeze(x, 0)
  64. assert len(x.shape) == 3, ("Input to deprocess image must be an image of "
  65. "dimension [1, height, width, channel] or [height, width, channel]")
  66. if len(x.shape) != 3:
  67. raise ValueError("Invalid input to deprocessing image")
  68. # perform the inverse of the preprocessiing step
  69. x[:, :, 0] += 103.939
  70. x[:, :, 1] += 116.779
  71. x[:, :, 2] += 123.68
  72. x = x[:, :, ::-1]
  73. x = np.clip(x, 0, 255).astype('uint8')
  74. return x
  75. # 内容层
  76. # Content layer where will pull our feature maps
  77. content_layers = ['block5_conv2']
  78. # 风格层
  79. # Style layer we are interested in
  80. style_layers = ['block1_conv1',
  81. 'block2_conv1',
  82. 'block3_conv1',
  83. 'block4_conv1',
  84. 'block5_conv1'
  85. ]
  86. num_content_layers = len(content_layers)
  87. num_style_layers = len(style_layers)
  88. # 构建模型
  89. def get_model():
  90. """ Creates our model with access to intermediate layers.
  91. This function will load the VGG19 model and access the intermediate layers.
  92. These layers will then be used to create a new model that will take input image
  93. and return the outputs from these intermediate layers from the VGG model.
  94. Returns:
  95. returns a keras model that takes image inputs and outputs the style and
  96. content intermediate layers.
  97. """
  98. # Load our model. We load pretrained VGG, trained on imagenet data
  99. vgg = tf.keras.applications.vgg19.VGG19(include_top=False, weights='imagenet')
  100. vgg.trainable = False
  101. # Get output layers corresponding to style and content layers
  102. style_outputs = [vgg.get_layer(name).output for name in style_layers]
  103. content_outputs = [vgg.get_layer(name).output for name in content_layers]
  104. model_outputs = style_outputs + content_outputs
  105. # Build model
  106. return models.Model(vgg.input, model_outputs)
  107. # 定义内容损失函数
  108. def get_content_loss(base_content, target):
  109. return tf.reduce_mean(tf.square(base_content - target))
  110. # 定义函数
  111. def gram_matrix(input_tensor):
  112. # We make the image channels first
  113. channels = int(input_tensor.shape[-1])
  114. a = tf.reshape(input_tensor, [-1, channels])
  115. n = tf.shape(a)[0]
  116. gram = tf.matmul(a, a, transpose_a=True)
  117. return gram / tf.cast(n, tf.float32)
  118. # 定义风格损失函数
  119. def get_style_loss(base_style, gram_target):
  120. """Expects two images of dimension h, w, c"""
  121. # height, width, num filters of each layer
  122. # We scale the loss at a given layer by the size of the feature map and the number of filters
  123. height, width, channels = base_style.get_shape().as_list()
  124. gram_style = gram_matrix(base_style)
  125. return tf.reduce_mean(tf.square(gram_style - gram_target))# / (4. * (channels ** 2) * (width * height) ** 2)
  126. # 定义一个反馈函数
  127. def get_feature_representations(model, content_path, style_path):
  128. """Helper function to compute our content and style feature representations.
  129. This function will simply load and preprocess both the content and style
  130. images from their path. Then it will feed them through the network to obtain
  131. the outputs of the intermediate layers.
  132. Arguments:
  133. model: The model that we are using.
  134. content_path: The path to the content image.
  135. style_path: The path to the style image
  136. Returns:
  137. returns the style features and the content features.
  138. """
  139. # Load our images in
  140. content_image = load_and_process_img(content_path)
  141. style_image = load_and_process_img(style_path)
  142. # batch compute content and style features
  143. style_outputs = model(style_image)
  144. content_outputs = model(content_image)
  145. # Get the style and content feature representations from our model
  146. style_features = [style_layer[0] for style_layer in style_outputs[:num_style_layers]]
  147. content_features = [content_layer[0] for content_layer in content_outputs[num_style_layers:]]
  148. return style_features, content_features
  149. # 定义计算损失函数
  150. def compute_loss(model, loss_weights, init_image, gram_style_features, content_features):
  151. """This function will compute the loss total loss.
  152. Arguments:
  153. model: The model that will give us access to the intermediate layers
  154. loss_weights: The weights of each contribution of each loss function.
  155. (style weight, content weight, and total variation weight)
  156. init_image: Our initial base image. This image is what we are updating with
  157. our optimization process. We apply the gradients wrt the loss we are
  158. calculating to this image.
  159. gram_style_features: Precomputed gram matrices corresponding to the
  160. defined style layers of interest.
  161. content_features: Precomputed outputs from defined content layers of
  162. interest.
  163. Returns:
  164. returns the total loss, style loss, content loss, and total variational loss
  165. """
  166. style_weight, content_weight = loss_weights
  167. # Feed our init image through our model. This will give us the content and
  168. # style representations at our desired layers. Since we're using eager
  169. # our model is callable just like any other function!
  170. model_outputs = model(init_image)
  171. style_output_features = model_outputs[:num_style_layers]
  172. content_output_features = model_outputs[num_style_layers:]
  173. style_score = 0
  174. content_score = 0
  175. # Accumulate style losses from all layers
  176. # Here, we equally weight each contribution of each loss layer
  177. weight_per_style_layer = 1.0 / float(num_style_layers)
  178. for target_style, comb_style in zip(gram_style_features, style_output_features):
  179. style_score += weight_per_style_layer * get_style_loss(comb_style[0], target_style)
  180. # Accumulate content losses from all layers
  181. weight_per_content_layer = 1.0 / float(num_content_layers)
  182. for target_content, comb_content in zip(content_features, content_output_features):
  183. content_score += weight_per_content_layer* get_content_loss(comb_content[0], target_content)
  184. style_score *= style_weight
  185. content_score *= content_weight
  186. # Get total loss
  187. loss = style_score + content_score
  188. return loss, style_score, content_score
  189. # 定义计算渐变函数
  190. def compute_grads(cfg):
  191. with tf.GradientTape() as tape:
  192. all_loss = compute_loss(**cfg)
  193. # Compute gradients wrt input image
  194. total_loss = all_loss[0]
  195. return tape.gradient(total_loss, cfg['init_image']), all_loss
  196. # 定义优化循环函数
  197. def run_style_transfer(content_path,
  198. style_path,
  199. num_iterations=1000,
  200. content_weight=1e3,
  201. style_weight=1e-2):
  202. # We don't need to (or want to) train any layers of our model, so we set their
  203. # trainable to false.
  204. model = get_model()
  205. for layer in model.layers:
  206. layer.trainable = False
  207. # Get the style and content feature representations (from our specified intermediate layers)
  208. style_features, content_features = get_feature_representations(model, content_path, style_path)
  209. gram_style_features = [gram_matrix(style_feature) for style_feature in style_features]
  210. # Set initial image
  211. init_image = load_and_process_img(content_path)
  212. init_image = tfe.Variable(init_image, dtype=tf.float32)
  213. # Create our optimizer
  214. opt = tf.train.AdamOptimizer(learning_rate=5, beta1=0.99, epsilon=1e-1)
  215. # For displaying intermediate images
  216. iter_count = 1
  217. # Store our best result
  218. best_loss, best_img = float('inf'), None
  219. # Create a nice config
  220. loss_weights = (style_weight, content_weight)
  221. cfg = {
  222. 'model': model,
  223. 'loss_weights': loss_weights,
  224. 'init_image': init_image,
  225. 'gram_style_features': gram_style_features,
  226. 'content_features': content_features
  227. }
  228. # For displaying
  229. num_rows = 2
  230. num_cols = 5
  231. display_interval = num_iterations/(num_rows*num_cols)
  232. start_time = time.time()
  233. global_start = time.time()
  234. norm_means = np.array([103.939, 116.779, 123.68])
  235. min_vals = -norm_means
  236. max_vals = 255 - norm_means
  237. imgs = []
  238. for i in range(num_iterations):
  239. grads, all_loss = compute_grads(cfg)
  240. loss, style_score, content_score = all_loss
  241. opt.apply_gradients([(grads, init_image)])
  242. clipped = tf.clip_by_value(init_image, min_vals, max_vals)
  243. init_image.assign(clipped)
  244. end_time = time.time()
  245. if loss < best_loss:
  246. # Update best loss and best image from total loss.
  247. best_loss = loss
  248. best_img = deprocess_img(init_image.numpy())
  249. if i % display_interval== 0:
  250. start_time = time.time()
  251. # Use the .numpy() method to get the concrete numpy array
  252. plot_img = init_image.numpy()
  253. plot_img = deprocess_img(plot_img)
  254. imgs.append(plot_img)
  255. IPython.display.clear_output(wait=True)
  256. IPython.display.display_png(Image.fromarray(plot_img))
  257. print('Iteration: {}'.format(i))
  258. print('Total loss: {:.4e}, '
  259. 'style loss: {:.4e}, '
  260. 'content loss: {:.4e}, '
  261. 'time: {:.4f}s'.format(loss, style_score, content_score, time.time() - start_time))
  262. print('Total time: {:.4f}s'.format(time.time() - global_start))
  263. IPython.display.clear_output(wait=True)
  264. plt.figure(figsize=(14,4))
  265. for i,img in enumerate(imgs):
  266. plt.subplot(num_rows,num_cols,i+1)
  267. plt.imshow(img)
  268. plt.xticks([])
  269. plt.yticks([])
  270. return best_img, best_loss
  271. # 定义结果展示函数
  272. def show_results(best_img, content_path, style_path, show_large_final=True):
  273. plt.figure(figsize=(10, 5))
  274. content = load_img(content_path)
  275. style = load_img(style_path)
  276. plt.subplot(1, 2, 1)
  277. imshow(content, 'Content Image')
  278. plt.subplot(1, 2, 2)
  279. imshow(style, 'Style Image')
  280. if show_large_final:
  281. plt.figure(figsize=(10, 10))
  282. plt.imshow(best_img)
  283. plt.title('Output Image')
  284. plt.show()
  285. # 显示结果
  286. best, best_loss = run_style_transfer(content_path,
  287. style_path, num_iterations=1000)
  288. Image.fromarray(best)
  289. show_results(best, content_path, style_path)

5.结果展示

(1)过程展示

 

(2)最终结果

五、项目地址

链接:https://pan.baidu.com/s/1MydlfdfLRYUigZVhKwj7kw

提取码:utmt

github链接

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/530385
推荐阅读
相关标签
  

闽ICP备14008679号