前面介绍了利用卷积神经网络实现图像风格迁移的算法原理和基于TensroFlow 的代码实现,这篇博客对前面的代码做了一些改变,设置了一个 image resize 函数,这样可以处理任意size的 input image,而且我们尝试利用 L-BFGS 优化算法替代之前的 Adam 优化算法,对卷积层以及pooling层函数做了修改。

转载出处

import numpy as np
import scipy.io
import scipy.misc
from scipy.misc import imresize, imread
import tensorflow as tf###############################################################################
# Constants for the image input and output.
################################################################################ Output folder for the images.
OUTPUT_DIR = 'output/'
# Style image to use.
STYLE_IMAGE = 'images/the_scream.jpg'
# Content image to use.
CONTENT_IMAGE = 'images/Taipei101.jpg'
# Image dimensions constants.
IMAGE_WIDTH = 600
IMAGE_HEIGHT = 400
COLOR_CHANNELS = 3###############################################################################
# Algorithm constants
###############################################################################
# Noise ratio. Percentage of weight of the noise for intermixing with the
# content image.
NOISE_RATIO = 0.5
# Number of iterations to run.
ITERATIONS = 500
# Constant to put more emphasis on content loss.
alpha = 1
# Constant to put more emphasis on style loss.
beta = 500
VGG_Model = 'imagenet-vgg-verydeep-19.mat'
MEAN_VALUES = np.array([123.68, 116.779, 103.939]).reshape((1, 1, 1, 3))CONTENT_LAYERS = [('conv4_2', 1.)]
STYLE_LAYERS = [('conv1_1', 0.2), ('conv2_1', 0.2), ('conv3_1', 0.2), ('conv4_1', 0.2), ('conv5_1', 0.2)]def generate_noise_image(content_image, noise_ratio = NOISE_RATIO):"""Returns a noise image intermixed with the content image at a certain ratio."""noise_image = np.random.uniform(-20, 20,(1, IMAGE_HEIGHT, IMAGE_WIDTH, COLOR_CHANNELS)).astype('float32')# White noise image from the content representation. Take a weighted average# of the valuesimg = noise_image * noise_ratio + content_image * (1 - noise_ratio)return imgdef load_image(path):image = imread(path)image = imresize(image, (IMAGE_HEIGHT, IMAGE_WIDTH))image = np.reshape(image, ((1,) + image.shape))# Input to the VGG net expects the mean to be subtracted.image = image - MEAN_VALUESreturn imagedef save_image(path, image):# Output should add back the mean.image = image + MEAN_VALUES# Get rid of the first useless dimension, what remains is the image.image = image[0]image = np.clip(image, 0, 255).astype('uint8')scipy.misc.imsave(path, image)def get_weight_bias(vgg_layers, layer_i):weights = vgg_layers[layer_i][0][0][2][0][0]w = tf.constant(weights)bias = vgg_layers[layer_i][0][0][2][0][1]b = tf.constant(np.reshape(bias, (bias.size)))layer_name = vgg_layers[layer_i][0][0][0]print layer_namereturn w, bdef conv_relu_layer(layer_input, nwb):conv_val = tf.nn.conv2d(layer_input, nwb[0], strides=[1, 1, 1, 1], padding='SAME')relu_val = tf.nn.relu(conv_val + nwb[1])return relu_valdef pool_layer(pool_style, layer_input):if pool_style == 'avg':return tf.nn.avg_pool(layer_input, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')elif pool_style == 'max':return  tf.nn.max_pool(layer_input, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')def build_vgg19(path):net = {}vgg_rawnet = scipy.io.loadmat(path)vgg_layers = vgg_rawnet['layers'][0]net['input'] = tf.Variable(np.zeros((1, IMAGE_HEIGHT, IMAGE_WIDTH, 3)).astype('float32'))net['conv1_1'] = conv_relu_layer(net['input'], get_weight_bias(vgg_layers, 0))net['conv1_2'] = conv_relu_layer(net['conv1_1'], get_weight_bias(vgg_layers, 2))net['pool1'] = pool_layer('avg', net['conv1_2'])net['conv2_1'] = conv_relu_layer(net['pool1'], get_weight_bias(vgg_layers, 5))net['conv2_2'] = conv_relu_layer(net['conv2_1'], get_weight_bias(vgg_layers, 7))net['pool2'] = pool_layer('max', net['conv2_2'])net['conv3_1'] = conv_relu_layer(net['pool2'], get_weight_bias(vgg_layers, 10))net['conv3_2'] = conv_relu_layer(net['conv3_1'], get_weight_bias(vgg_layers, 12))net['conv3_3'] = conv_relu_layer(net['conv3_2'], get_weight_bias(vgg_layers, 14))net['conv3_4'] = conv_relu_layer(net['conv3_3'], get_weight_bias(vgg_layers, 16))net['pool3'] = pool_layer('avg', net['conv3_4'])net['conv4_1'] = conv_relu_layer(net['pool3'], get_weight_bias(vgg_layers, 19))net['conv4_2'] = conv_relu_layer(net['conv4_1'], get_weight_bias(vgg_layers, 21))net['conv4_3'] = conv_relu_layer(net['conv4_2'], get_weight_bias(vgg_layers, 23))net['conv4_4'] = conv_relu_layer(net['conv4_3'], get_weight_bias(vgg_layers, 25))net['pool4'] = pool_layer('max', net['conv4_4'])net['conv5_1'] = conv_relu_layer(net['pool4'], get_weight_bias(vgg_layers, 28))net['conv5_2'] = conv_relu_layer(net['conv5_1'], get_weight_bias(vgg_layers, 30))net['conv5_3'] = conv_relu_layer(net['conv5_2'], get_weight_bias(vgg_layers, 32))net['conv5_4'] = conv_relu_layer(net['conv5_3'], get_weight_bias(vgg_layers, 34))net['pool5'] = pool_layer('avg', net['conv5_4'])return netdef content_layer_loss(p, x):M = p.shape[1] * p.shape[2]N = p.shape[3]loss = (1. / (2 * N * M)) * tf.reduce_sum(tf.pow((x - p), 2))return lossdef content_loss_func(sess, net):layers = CONTENT_LAYERStotal_content_loss = 0.0for layer_name, weight in layers:p = sess.run(net[layer_name])x = net[layer_name]total_content_loss += content_layer_loss(p, x)*weighttotal_content_loss /= float(len(layers))return total_content_lossdef gram_matrix(x, area, depth):x1 = tf.reshape(x, (area, depth))g = tf.matmul(tf.transpose(x1), x1)return gdef style_layer_loss(a, x):M = a.shape[1] * a.shape[2]N = a.shape[3]A = gram_matrix(a, M, N)G = gram_matrix(x, M, N)loss = (1. / (4 * N ** 2 * M ** 2)) * tf.reduce_sum(tf.pow((G - A), 2))return lossdef style_loss_func(sess, net):layers = STYLE_LAYERStotal_style_loss = 0.0for layer_name, weight in layers:a = sess.run(net[layer_name])x = net[layer_name]total_style_loss += style_layer_loss(a, x) * weighttotal_style_loss /= float(len(layers))return total_style_lossdef main():net = build_vgg19(VGG_Model)sess = tf.Session()sess.run(tf.initialize_all_variables())content_img = load_image(CONTENT_IMAGE)style_img = load_image(STYLE_IMAGE)sess.run([net['input'].assign(content_img)])cost_content = content_loss_func(sess, net)sess.run([net['input'].assign(style_img)])cost_style = style_loss_func(sess, net)total_loss = alpha * cost_content + beta * cost_styleoptimizer = tf.contrib.opt.ScipyOptimizerInterface(total_loss, method='L-BFGS-B',options={'maxiter': ITERATIONS,'disp': 0})init_img = generate_noise_image(content_img)sess.run(tf.initialize_all_variables())sess.run(net['input'].assign(init_img))optimizer.minimize(sess)mixed_img = sess.run(net['input'])filename = 'output/out.png'save_image(filename, mixed_img)if __name__ == '__main__':main()

tensorflow 风格迁移二相关推荐

  1. tensorflow 风格迁移

    参考来源: https://github.com/ckmarkoh/neuralart_tensorflow https://github.com/log0/neural-style-painting ...

  2. 将 TensorFlow 移植到 Android手机,实现物体识别、行人检测和图像风格迁移详细教程

    2017/02/23 更新 贴一个TensorFlow 2017开发者大会的Mobile专题演讲 移动和嵌入式TensorFlow 这里面有重点讲到本文介绍的三个例子,以及其他的移动和嵌入式方面的TF ...

  3. 度学习实践:如何使用Tensorflow实现快速风格迁移?

    一.风格迁移简介 风格迁移(Style Transfer)是深度学习众多应用中非常有趣的一种,如图,我们可以使用这种方法把一张图片的风格"迁移"到另一张图片上: 然而,原始的风格迁 ...

  4. TensorFlow练手项目三:使用VGG19迁移学习实现图像风格迁移

    使用VGG19迁移学习实现图像风格迁移 2020.3.15 更新: 使用Python 3.7 + TensorFlow 2.0的实现: 有趣的深度学习--使用TensorFlow 2.0实现图片神经风 ...

  5. 深度学习项目二: 图像的风格迁移和图像的快速风格迁移 (含数据和所需源码)

    图像风格迁移是指,将一幅内容图的内容,和一幅或多幅风格图的风格融合在一起,从而生成一些有意思的图片 一:传统的图像风格迁移 为了让我们生成的迁移图在风格上与风格图片尽可能相似,在内容上尽可能与内容图相 ...

  6. 使用Tensorflow实现图片风格迁移,圆梦名画

    一,前期基础知识储备 1)Prisma - 图片风格迁移的鼻祖: 照片可以记录生活的瞬间,变成一幅幅的回忆:而 Prisma 则是可以让瞬间的回忆变成永恒的名画!我们平常用手机随意拍出来的照片效果看起 ...

  7. 卷积神经网络:(二)风格迁移——原理部分

    卷积神经网络:(二)风格迁移--原理部分 引言 本文是在第一步配置完环境后基础上运行的.使用的为系统直装的python环境(在anaconda环境下一样适用,后面注意的点会提示的.).若想查看环境配置 ...

  8. tensorflow学习笔记九:将 TensorFlow 移植到 Android手机,实现物体识别、行人检测和图像风格迁移详细教程

    2017/02/23 更新 贴一个TensorFlow 2017开发者大会的Mobile专题演讲 移动和嵌入式TensorFlow 这里面有重点讲到本文介绍的三个例子,以及其他的移动和嵌入式方面的TF ...

  9. 图像迁移风格保存模型_用TensorFlow.js在浏览器中部署可进行任意图像风格迁移的模型...

    风格迁移一直是很多读者感兴趣的内容之一,近日,网友ReiichiroNakano公开了自己的一个实现:用TensorFlow.js在浏览器中部署可进行任意图像风格迁移的模型.让我们一起去看看吧! Gi ...

最新文章

  1. python for循环九九乘法表_python—用for循环、while循环和一句话打印九九乘法表
  2. 卷积神经网络, Convolutional Neural Networks , CNN
  3. 粗题⼈不考你没学过的算法
  4. 函的红头文件格式制作_Excel实现批量制作年会邀请函,这个方法,你一定不能错过...
  5. linux 查看进程_Linux怎么查看和监控每个进程的实时流量
  6. C++/C--动态二维数组的内存分配与释放【转载】
  7. Python3爬虫入门之Urllib库的用法
  8. java.lang.Object是如何成为默认父类的
  9. CART算法原理及实现
  10. Windows 平台上使用 cwRsync做文件同步
  11. MaNGOS大芒果服务端源码研究(一)——环境安装与源码下载
  12. 关闭windows defender
  13. 关于Excel表格导出方法--application/vnd.ms-excel
  14. 密钥文件读取保存为String,并转PublicKey/PrivateKey
  15. 关于DBA的一些学习(一)
  16. Laravel 接受Ajax的POST请求
  17. Unity高级功能—自发光材质的制作
  18. Win7下连远程桌面 窗口 全屏 切换
  19. 补鞋匠迈尔鲁夫的故事(二)
  20. 三菱四节传送带控制梯形图_三菱PLC用步进指令实现四节传送带的模拟控制.pdf...

热门文章

  1. BCH虫洞项目有多安全以及WHC究竟能干什么?
  2. 【安全牛学习笔记】拒绝服务***工具
  3. AGG第二课 代码框架以及命名规则
  4. 解密亚洲诚信如何做到HTTPS的最佳安全实践
  5. liunx 分割合并文件
  6. TypeError: decoding Unicode is not supported
  7. 限制部分Postfix用户只能内部收发的例子(完整版)
  8. 从Handler.post(Runnable r)再一次梳理Android的消息机制(以及handler的内存泄露)
  9. 51Nod-2149子串水题find
  10. linux netlink 编程示例(一)内核端