在本教程中,我们将学习如何使用深度学习将图片的风格进行转换。这被称为neural style transfer!这是Leon A. Gatys的论文“A Neural Algorithm of Artistic Style,”中概述的一种技术,这个论很好,你一定要看看它:https://arxiv.org/abs/1508.06576
Neural style transfer 是一种优化方法, 它需要三张图片, 一张是内容图片, 一张是风格图片, 第三张是生成图片(初始值使用内容图片), 通过迭代优化, 最终使得第三张图片结合了第一张图片的内容和第二张图片的风格。
import numpy as np import tensorflow.contrib.keras.api.keras.backend as K from scipy.misc import imsave, imresize from scipy.optimize import fmin_l_bfgs_b from tensorflow.contrib.keras.api.keras.applications import vgg19 from tensorflow.contrib.keras.api.keras.preprocessing.image import load_img, img_to_array from IPython.display import Image
输出(stream):
d:\mysites\deeplearning.ai-master\.env\lib\site-packages\ipykernel_launcher.py:5: DeprecationWarning: `imresize` is deprecated!
`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``skimage.transform.resize`` instead.
"""
model = vgg19.VGG19(include_top=False, weights='imagenet', input_tensor=input_tensor) print('Model loaded')
outputs_dict = dict([(layer.name, layer.output) for layer in model.layers])
输出(stream):
d:\mysites\deeplearning.ai-master\.env\lib\site-packages\ipykernel_launcher.py:5: DeprecationWarning: `imresize` is deprecated!
`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``skimage.transform.resize`` instead.
"""
输出(stream):
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5
80142336/80134624 [==============================] - 70s 1us/step
Model loaded
输出(stream):
d:\mysites\deeplearning.ai-master\.env\lib\site-packages\ipykernel_launcher.py:5: DeprecationWarning: `imresize` is deprecated!
`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``skimage.transform.resize`` instead.
"""
输出(stream):
loss: 1516893568.0
输出(stream):
d:\mysites\deeplearning.ai-master\.env\lib\site-packages\ipykernel_launcher.py:11: DeprecationWarning: `imsave` is deprecated!
`imsave` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``imageio.imwrite`` instead.
# This is added back by InteractiveShellApp.init_path()