tensorflow 风格迁移
参考来源:
https://github.com/ckmarkoh/neuralart_tensorflow
https://github.com/log0/neural-style-painting/blob/master/TensorFlow%20Implementation%20of%20A%20Neural%20Algorithm%20of%20Artistic%20St
import os
import sys
import numpy as np
import scipy.io
import scipy.misc
import tensorflow as tf# Output folder for the images.
OUTPUT_DIR = 'output/'
# Style image to use.
STYLE_IMAGE = '/images/ocean.jpg'
# Content image to use.
CONTENT_IMAGE = '/images/Taipei101.jpg'
# Image dimensions constants.
IMAGE_WIDTH = 800
IMAGE_HEIGHT = 600
COLOR_CHANNELS = 3###############################################################################
# Algorithm constants
###############################################################################
# 设置随机噪声图像与内容图像的比率
NOISE_RATIO = 0.6
# 设置迭代次数
ITERATIONS = 1000
# 设置内容图像与风格图像的权重
alpha = 1
beta = 500
# 加载VGG-19 MODEL及设定均值
VGG_Model = 'Downloads/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)]# 生成随机噪声图,与content图以一定比率融合
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 = scipy.misc.imread(path)# Resize the image for convnet input, there is no change but just# add an extra dimension.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 build_net(ntype, nin, nwb=None):if ntype == 'conv':return tf.nn.relu(tf.nn.conv2d(nin, nwb[0], strides=[1, 1, 1, 1], padding='SAME') + nwb[1])elif ntype == 'pool':return tf.nn.avg_pool(nin, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')def get_weight_bias(vgg_layers, i):weights = vgg_layers[i][0][0][2][0][0]weights = tf.constant(weights)bias = vgg_layers[i][0][0][2][0][1]bias = tf.constant(np.reshape(bias, (bias.size)))return weights, biasdef 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'] = build_net('conv', net['input'], get_weight_bias(vgg_layers, 0))net['conv1_2'] = build_net('conv', net['conv1_1'], get_weight_bias(vgg_layers, 2))net['pool1'] = build_net('pool', net['conv1_2'])net['conv2_1'] = build_net('conv', net['pool1'], get_weight_bias(vgg_layers, 5))net['conv2_2'] = build_net('conv', net['conv2_1'], get_weight_bias(vgg_layers, 7))net['pool2'] = build_net('pool', net['conv2_2'])net['conv3_1'] = build_net('conv', net['pool2'], get_weight_bias(vgg_layers, 10))net['conv3_2'] = build_net('conv', net['conv3_1'], get_weight_bias(vgg_layers, 12))net['conv3_3'] = build_net('conv', net['conv3_2'], get_weight_bias(vgg_layers, 14))net['conv3_4'] = build_net('conv', net['conv3_3'], get_weight_bias(vgg_layers, 16))net['pool3'] = build_net('pool', net['conv3_4'])net['conv4_1'] = build_net('conv', net['pool3'], get_weight_bias(vgg_layers, 19))net['conv4_2'] = build_net('conv', net['conv4_1'], get_weight_bias(vgg_layers, 21))net['conv4_3'] = build_net('conv', net['conv4_2'], get_weight_bias(vgg_layers, 23))net['conv4_4'] = build_net('conv', net['conv4_3'], get_weight_bias(vgg_layers, 25))net['pool4'] = build_net('pool', net['conv4_4'])net['conv5_1'] = build_net('conv', net['pool4'], get_weight_bias(vgg_layers, 28))net['conv5_2'] = build_net('conv', net['conv5_1'], get_weight_bias(vgg_layers, 30))net['conv5_3'] = build_net('conv', net['conv5_2'], get_weight_bias(vgg_layers, 32))net['conv5_4'] = build_net('conv', net['conv5_3'], get_weight_bias(vgg_layers, 34))net['pool5'] = build_net('pool', 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.train.AdamOptimizer(2.0)init_img = generate_noise_image(content_img)train_op = optimizer.minimize(total_loss)sess.run(tf.initialize_all_variables())sess.run(net['input'].assign(init_img))for it in range(ITERATIONS):sess.run(train_op)if it % 100 == 0:# Print every 100 iteration.mixed_image = sess.run(net['input'])print('Iteration %d' % (it))print('sum : ', sess.run(tf.reduce_sum(mixed_image)))print('cost: ', sess.run(total_loss))if not os.path.exists(OUTPUT_DIR):os.mkdir(OUTPUT_DIR)filename = 'output/%d.png' % (it)save_image(filename, mixed_image)if __name__ == '__main__':main()
提取密码fwat
tensorflow 风格迁移相关推荐
- tensorflow 风格迁移二
前面介绍了利用卷积神经网络实现图像风格迁移的算法原理和基于TensroFlow 的代码实现,这篇博客对前面的代码做了一些改变,设置了一个 image resize 函数,这样可以处理任意size的 i ...
- 将 TensorFlow 移植到 Android手机,实现物体识别、行人检测和图像风格迁移详细教程
2017/02/23 更新 贴一个TensorFlow 2017开发者大会的Mobile专题演讲 移动和嵌入式TensorFlow 这里面有重点讲到本文介绍的三个例子,以及其他的移动和嵌入式方面的TF ...
- 度学习实践:如何使用Tensorflow实现快速风格迁移?
一.风格迁移简介 风格迁移(Style Transfer)是深度学习众多应用中非常有趣的一种,如图,我们可以使用这种方法把一张图片的风格"迁移"到另一张图片上: 然而,原始的风格迁 ...
- TensorFlow从1到2(十三)图片风格迁移
风格迁移 <从锅炉工到AI专家(8)>中我们介绍了一个"图片风格迁移"的例子.因为所引用的作品中使用了TensorFlow 1.x的代码,算法也相对复杂,所以文中没有仔 ...
- tensorflow contrib模块_OpenCV DNN 模块-风格迁移
本文主要介绍OpenCV的DNN模块的使用.OpenCV的DNN模块自从contrib仓库开始,就是只支持推理,不支持训练.但是仅仅只是推理方面,也够强大了.现在OpenCV已经支持TensorFlo ...
- TensorFlow实时任意风格迁移,送女朋友的创意礼物有了
TensorFlow实时任意风格迁移,送女朋友的创意礼物有了 前言 自适应实例规范化 风格迁移网络 编码器结构与实现 通过反射填充(reflection padding)减少块伪影 解码器结构与实现 ...
- TensorFlow练手项目三:使用VGG19迁移学习实现图像风格迁移
使用VGG19迁移学习实现图像风格迁移 2020.3.15 更新: 使用Python 3.7 + TensorFlow 2.0的实现: 有趣的深度学习--使用TensorFlow 2.0实现图片神经风 ...
- 使用Tensorflow实现图片风格迁移,圆梦名画
一,前期基础知识储备 1)Prisma - 图片风格迁移的鼻祖: 照片可以记录生活的瞬间,变成一幅幅的回忆:而 Prisma 则是可以让瞬间的回忆变成永恒的名画!我们平常用手机随意拍出来的照片效果看起 ...
- tensorflow学习笔记九:将 TensorFlow 移植到 Android手机,实现物体识别、行人检测和图像风格迁移详细教程
2017/02/23 更新 贴一个TensorFlow 2017开发者大会的Mobile专题演讲 移动和嵌入式TensorFlow 这里面有重点讲到本文介绍的三个例子,以及其他的移动和嵌入式方面的TF ...
最新文章
- groupadd - 建 立 新 群 组
- mysql按月进行表分区
- LaTeX的表格标题位置
- 区块链技术核心概念与原理讲解-Tiny熊
- 吴恩达机器学习练习4:神经网络学习(反向传播)
- 1.2、获取、创建 ApplicationContextInitializer
- 运动控制卡中伺服电机的规划位置与编码器位置的区别
- PHP数据库扩展 - PDO操作
- 查询检测PhysX 3.2中的场景查询(1)-基础
- VS2010 快捷键设置,快速编码1
- PDF文档全篇免费翻译技巧
- 线性规划中的单纯形法与内点法(原理、步骤以及matlab实现)(一)
- oracle ssd加速,使用Oracle的SSD
- c++17之结构化绑定
- 美团点评 2019校园招聘 后台开发方向
- 多伦多大学计算机专音乐专业,多伦多大学音乐理论专业介绍
- 深度学习在知识图谱的应用
- 安卓 类微信界面实现
- Geography和 Geometry 的区别
- Prediction of Multidrug-Resistant TB from CT Pulmonary Images Based on Deep Learning Techniques论文总结