基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer

核心贡献:

1、仿射变换加入:目标函数在优化过程中,进行约束重建后的图像将用局部图像来表示仿射颜色变换的输入,以防止显示扭曲。(光线(颜色)正则化Lm)

2、语义分割加入:基于输入的语义分割的过程,避免内容不匹配的问题,提高了结果的摄影真实性。

平台::linux系统,ubuntu kylin操作系统

软件配置及其依赖库:

Requirements and Prerequisites:

  • Python 2.7.x
  • Now support Tensorflow >= 1.0  (提议选择1.0以上的版本,不要低于,否则报错)
  • 安装 pyyaml:
  • python版本与tensorflow一致

论文链接:[1703.07511] Deep Photo Style Transfer (arxiv.org)

实践步骤:

1、在ubuntu(18.04)下安装Tensorflow

进入ubuntu (kylin)

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第1张图片

 建立一个专门的tensorlow用户用来测试本项目,用户名tensorflowLab

建立用户:

sudo useradd -m tensorflowLab

给建立的用户设置密码:

sudo passwd tensorflowLab

 进入tensorflowLab

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第2张图片

 在root权限下操作

对新用户增加管理员权限

sudo adduser tensorflowLab sudo

也就是开通权限。

更新安装包工具apt

按ctrl+alt+t 打开终端,输入:sudo apt-get update

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第3张图片

 安装文本编辑工具 sudo apt-get install vim (期间配置会有小问题,按报错提示做即可)

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第4张图片

2、安装相关环境和工具

sudo apt-get install python-pip python-dev python-virtualenv

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第5张图片

unmet dependencies 包依赖安装中断,修复    apt --fix-broken install      继续。

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第6张图片

 按y继续安装

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第7张图片

继续安装python依赖,虚拟环境库

sudo apt-get install python-pip python-dev python-virtualenv

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第8张图片

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第9张图片

 至此,依赖python-pip, python-dev, python-virtualenv全部安装完了。

3、创建虚拟环境virtualenv

创建虚拟环境,在家目录~下创建虚拟环境变量tensorflow

virtualenv --system-site-packages ~/tensorflow

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第10张图片

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第11张图片

 报错,python版本要python2。

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第12张图片

 貌似安装好了。

4、进入虚拟环境

进入虚拟环境,

cd ~/tensorflow

并激活

source bin/activate

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第13张图片

5、在虚拟环境安装最新版本的tensorflow

pip install  tensorflow==1.0.0 (升级 Pip install -U pip)

(其实我先安装0.12.0版本,后来报错,原因不同版本对接口的函数不一致)

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第14张图片

pip install tensorflow==0.12.0
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Collecting tensorflow==0.12.0
基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第15张图片

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第16张图片

【运行tensorflow程序只要执行下面两句即可,cd ~/tensorflow,source bin/activate】

6、模型下载

接下啦下载训练好的模型model,  提前训练的。可以下载直接用(不要再重新训练),将下列链接复制到ubuntu下的firefox搜索栏,输入以下地址:

                          http://pan.baidu.com/s/1mi9Ilyo

但是进入网盘,必须事先安装linux下的网盘软件

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第17张图片

选择deb格式安装。

安装linux版的百度网盘,打开直接点击

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第18张图片

如果以上格式无法安装,不如直接在终端下载百度网盘的包

wget https://issuepcdn.baidupcs.com/issue/netdisk/LinuxGuanjia/4.17.7/baidunetdisk_4.17.7_amd64.deb
 

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第19张图片

 下载好了

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第20张图片

 于是在终端安装百度网盘

sudo apt install -y ./baidunetdisk_4.17.7_amd64.deb 
基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第21张图片

 里面报错,修复,按里面修复命令apt --fix-broken install执行

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第22张图片

 要root权限安装,切换用户su root

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第23张图片

 按y继续(其实目的更新版本的包)

以下更新完了

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第24张图片

再重复(现在才安装百度网盘linux版)执行安装命令

 sudo apt-get install -y ./baidunetdisk_4.17.7_amd64.deb

以下就安装好了百度网盘(实质是百度网盘包的一个解压过程)

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第25张图片

 既然安装好了百度网盘,那么就可以下载神经网络训练好的模型model 

输入model的网盘地址:  models_免费高速下载|百度网盘-分享无限制百度网盘为您提供文件的网络备份、同步和分享服务。空间大、速度快、安全稳固,支持教育网加速,支持手机端。注册使用百度网盘即可享受免费存储空间http://pan.baidu.com/s/1mi9Ilyo

链接: https://pan.baidu.com/s/1lCG6vnYUEbBapLaLhSF4DA 提取码: pghx 

下载后将models文件夹放在工程fast-neural-style-tensorflow-master的根目录下

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第26张图片

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第27张图片

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第28张图片

 7、下载项目(图片迁移的工程project)

先进入用户tensorflowLab

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第29张图片

 打开firefox浏览器,输入Github下载地址

在Github上下载项目代码地址,进入  Github地址:GitHub - hzy46/fast-neural-style-tensorflow: A tensorflow implementation for fast neural style!

或链接: https://pan.baidu.com/s/1iFDGeneMZo_g32gdQjZTEw 提取码: rdiv 

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第30张图片

由于工程项目fast-neural-style-tensorflow-master.zip是在cloudcomputation目录下,我希望所有项目放在用户tensorflowLab下,所以采用cp复制,输入以下复制命令就可以将项目复制到tensorflowLab下

sudo cp -rp /home/cloudcomputation/Downloads/fast-neural-style-tensorflow-master.zip  /home/tensorflowLab
 

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第31张图片

 项目压缩包fast-neural-style-tensorflow-master.zip已经在当前目录tensorflowLab, 于是解压在当前文件家tensorflowLab下(先增加该文件的权限:sudo chmod 777 fast-neural-style-tensorflow-master.zip)

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第32张图片

 (颜色由红色变绿色),接下来可以解压,鉴于压缩包为zip,所以采用以下解压命令:

sudo tar -zxvf /home/tensorflowLab/fast-neural-style-tensorflow-master.zip

sudo unzip fast-neural-style-tensorflow-master.zip (实际采用该命令)

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第33张图片

显示解压后的结果:

 基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第34张图片

将训练好的模型models,复制到工程项目下面,使用以下命令完成复制神经网络训练好的模型s:

sudo cp -r  /home/cloudcomputation/Downloads/models   /home/tensorflowLab/fast-neural-style-tensorflow-master

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第35张图片

 基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第36张图片

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第37张图片

完成所有模型和项目工程的文件的复制。!!!

如果第二次开机,一定记得激活虚拟环境,在虚拟环境里面执行工程项目

cd ~/tensorflow

并激活

source bin/activate

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第38张图片

8、模型使用快速训练:

用训练好的模型进行实现快速风格迁移

python eval.py --model_file      --image_file img/test.jpg
your path to wave.ckpt-done      训练好的下载模型的路径
img/test.jpg   用于测试图片而输入测试图片的路径

切记:切换到当前工程项目的目录fast-neural-style-tensorflow-master下!

采用:python eval.py --model_file /models/wave.ckpt-done --image_file img/test.jpg

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第39张图片

 基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第40张图片

执行报错

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第41张图片

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第42张图片

TypeError: Input 'split_dim' of 'Split' Op has type float32 that does not match expected type of int32.
数据输入是浮点数,数据输出为int,矛盾!

TensorFlow 版本不同,函数接口修改导致的问题,Tensorflow的版本 0.12.0改为1.0.0。

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第43张图片

报错,因为路径中多了一个斜杆 /.

(tensorflow) root@cloudcomputation-virtual-machine:/home/tensorflowLab/fast-neural-style-tensorflow-master# python eval.py --model_file /models/wave.ckpt-done --image_file img/wave.jpg
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
INFO:tensorflow:Image size: 928x514
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
W tensorflow/core/framework/op_kernel.cc:993] Invalid argument: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
Traceback (most recent call last):
  File "eval.py", line 76, in
    tf.app.run()
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "eval.py", line 57, in main
    saver.restore(sess, FLAGS.model_file)
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1439, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run
    run_metadata_ptr)
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 965, in _run
    feed_dict_string, options, run_metadata)
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1015, in _do_run
    target_list, options, run_metadata)
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1035, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
     [[Node: save/RestoreV2_15 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/RestoreV2_15/tensor_names, save/RestoreV2_15/shape_and_slices)]]

Caused by op u'save/RestoreV2_15', defined at:
  File "eval.py", line 76, in
    tf.app.run()
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "eval.py", line 53, in main
    saver = tf.train.Saver(tf.global_variables(), write_version=tf.train.SaverDef.V1)
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1051, in __init__
    self.build()
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1081, in build
    restore_sequentially=self._restore_sequentially)
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 675, in build
    restore_sequentially, reshape)
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 402, in _AddRestoreOps
    tensors = self.restore_op(filename_tensor, saveable, preferred_shard)
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 242, in restore_op
    [spec.tensor.dtype])[0])
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_io_ops.py", line 668, in restore_v2
    dtypes=dtypes, name=name)
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
    op_def=op_def)
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2395, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/root/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1264, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Unsuccessful TensorSliceReader constructor: Failed to get matching files on /models/wave.ckpt-done: Not found: /models
     [[Node: save/RestoreV2_15 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/RestoreV2_15/tensor_names, save/RestoreV2_15/shape_and_slices)]]

更正路径,然后继续测试

重新测试:python eval.py --model_file models/wave.ckpt-done --image_file img/wave.jpg

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第44张图片

 测试结果:

INFO:tensorflow:Image size: 928x514
INFO:tensorflow:Elapsed time: 234.052376s
INFO:tensorflow:Done. Please check generated/res.jpg.

生成的文件的目录:generated/res.jpg

9、工程应用实践

1)钱塘江图片,输入图片

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第45张图片

 生成结果;

peed up CPU computations.
INFO:tensorflow:Image size: 787x525
INFO:tensorflow:Elapsed time: 137.216044s
INFO:tensorflow:Done. Please check generated/res.jpg.
基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第46张图片

2)理塘图片,输入图片

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第47张图片

 输出:

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第48张图片

 3)换一种油画风格,理塘图片,输入图片

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第49张图片

输出:

INFO:tensorflow:Image size: 984x927
INFO:tensorflow:Elapsed time: 27.587501s
INFO:tensorflow:Done. Please check generated/res.jpg.
 

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第50张图片

(4)理塘照片,输入梵高的星空风格

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第51张图片

 输出

peed up CPU computations.
INFO:tensorflow:Image size: 984x927
INFO:tensorflow:Elapsed time: 32.754720s
INFO:tensorflow:Done. Please check generated/res.jpg.
 

基于tensorflow下卷积神经网络图片风格迁移_Deep Photo Style Transfer_第52张图片

实践完毕!

你可能感兴趣的:(卷积神经网络,自然语言处理,opencv,神经网络,图像处理,pytorch)