神经网络风格画 Neural Style Art

[TOC]

前言

博主看到了一篇知乎上有关于代码实现照片滤镜的文章
安利一个很火的 Github 滤镜项目
照片风格迁移
7 个 App 可让你的照片变成艺术品

Neural Style 项目

博主目前的水平还看不懂整个项目的代码跟底层算法实现到底是什么原理
google+baidu了一番很是想要亲自体验下这个算法,美化一下自己的照片。
下边是github上neural-style项目的主页:
neural-style | github
安装说明:
neural-style | github | 安装使用说明

项目部署前准备

可以了解到该项目代码基于 Torch,并已经在 Ubuntu 14.04 LTS 系统测试。目前尚未搜索到在windows上跑起来的案例。
并且项目介绍说到GPU下跑的要比CPU效果好,可是博主对于GPU模式并没有研究,遂查了很多案例,博主准备在windows7下利用VMare安装Ubuntu进行项目部署。

Ubuntu桌面入门指南

深度学习neural-style环境搭建 | 简书

Windws下载VM中安装Ubuntu

怎样在VMware虚拟机中使用安装并设置Ubuntu系统 | 百度经验

VM中Ubuntu的网络配置

请参考
虚拟机安装Ubuntu的上网设置(有线网络和无线网络)
需要注意的是Ubuntu在VM下NAT模式网络连接注意本机需要开启两个服务
VMware NAT Service
VMware DHCP Service(可不开)

项目部署安装说明

详细请参考github中项目简介,过程博主出现各种问题,主要google+百度+翻遍issues解决,感触就是别放弃

确认部署的Linux机器拥有git和cmake

没有的通过命令
sudo apt-get install git安装git
cmake安装直接google

步骤一:Install torch7

cd ~/ curl -s https://raw.githubusercontent.com/torch/ezinstall/master/install-deps | bash git clone https://github.com/torch/distro.git ~/torch --recursive cd ~/torch; ./install.sh source ~/.bashrc

还有 sudo apt-get install libreadline-dev

步骤二:安装loadcaffe

sudo apt-get install libprotobuf-dev protobuf-compiler

luarocks install loadcaffe

步骤三:安装neural-style

克隆项目

cd ~/ git clone https://github.com/jcjohnson/neural-style.git cd neural-style

下载模型

sh models/download_models.sh

CPU模式下运行测试

th neural_style.lua -gpu -1 -print_iter 1
上一行是官方给的测试语句,博主在解决以上所有步骤后在此步骤卡了好长时间。
第一次,终端提示RAM不够,重启VM更改设置增到2G内存。RAM报错解决。
第二次,终端报错 No LuaRocks module found for cutorch,找到对应IssuesNo LuaRocks module found for cutorch遂发现Troch7装载过程有问题,重装。

测试输出解释

官方给出的样例输出即为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[libprotobuf WARNING google/protobuf/io/coded_stream.cc:505] Reading dangerously large protocol message.  If the message turns out to be larger than 1073741824 bytes, parsing will be halted for security reasons.  To increase the limit (or to disable these warnings), see CodedInputStream::SetTotalBytesLimit() in google/protobuf/io/coded_stream.h.
[libprotobuf WARNING google/protobuf/io/coded_stream.cc:78] The total number of bytes read was 574671192
Successfully loaded models/VGG_ILSVRC_19_layers.caffemodel
conv1_1: 64 3 3 3
conv1_2: 64 64 3 3
conv2_1: 128 64 3 3
conv2_2: 128 128 3 3
conv3_1: 256 128 3 3
conv3_2: 256 256 3 3
conv3_3: 256 256 3 3
conv3_4: 256 256 3 3
conv4_1: 512 256 3 3
conv4_2: 512 512 3 3
conv4_3: 512 512 3 3
conv4_4: 512 512 3 3
conv5_1: 512 512 3 3
conv5_2: 512 512 3 3
conv5_3: 512 512 3 3
conv5_4: 512 512 3 3
fc6: 1 1 25088 4096
fc7: 1 1 4096 4096
fc8: 1 1 4096 1000
WARNING: Skipping content loss
Iteration 1 / 1000
Content 1 loss: 2091178.593750
Style 1 loss: 30021.292114
Style 2 loss: 700349.560547
Style 3 loss: 153033.203125
Style 4 loss: 12404635.156250
Style 5 loss: 656.860304
Total loss: 15379874.666090
Iteration 2 / 1000
Content 1 loss: 2091177.343750
Style 1 loss: 30021.292114
Style 2 loss: 700349.560547
Style 3 loss: 153033.203125
Style 4 loss: 12404633.593750
Style 5 loss: 656.860304
Total loss: 15379871.853590

其中这段是Troch输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
conv1_1: 64 3 3 3
conv1_2: 64 64 3 3
conv2_1: 128 64 3 3
conv2_2: 128 128 3 3
conv3_1: 256 128 3 3
conv3_2: 256 256 3 3
conv3_3: 256 256 3 3
conv3_4: 256 256 3 3
conv4_1: 512 256 3 3
conv4_2: 512 512 3 3
conv4_3: 512 512 3 3
conv4_4: 512 512 3 3
conv5_1: 512 512 3 3
conv5_2: 512 512 3 3
conv5_3: 512 512 3 3
conv5_4: 512 512 3 3
fc6: 1 1 25088 4096
fc7: 1 1 4096 4096
fc8: 1 1 4096 1000

真正的Neural Style项目输出才是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
WARNING: Skipping content loss	
Iteration 1 / 1000
Content 1 loss: 2091178.593750
Style 1 loss: 30021.292114
Style 2 loss: 700349.560547
Style 3 loss: 153033.203125
Style 4 loss: 12404635.156250
Style 5 loss: 656.860304
Total loss: 15379874.666090
Iteration 2 / 1000
Content 1 loss: 2091177.343750
Style 1 loss: 30021.292114
Style 2 loss: 700349.560547
Style 3 loss: 153033.203125
Style 4 loss: 12404633.593750
Style 5 loss: 656.860304
Total loss: 15379871.853590

关于WARNING: Skipping content loss此句,博主本机上并没有出现此类信息
代码显示如下,此为Neural Style输出代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Setting up style layer  	2	:	relu1_1	
Setting up style layer 7 : relu2_1
Setting up style layer 12 : relu3_1
Setting up style layer 21 : relu4_1
Setting up content layer 23 : relu4_2
Setting up style layer 30 : relu5_1
Capturing content targets
nn.Sequential {
[input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> (11) -> (12) -> (13) -> (14) -> (15) -> (16) -> (17) -> (18) -> (19) -> (20) -> (21) -> (22) -> (23) -> (24) -> (25) -> (26) -> (27) -> (28) -> (29) -> (30) -> (31) -> (32) -> (33) -> (34) -> (35) -> (36) -> (37) -> output]
(1): nn.TVLoss
(2): nn.SpatialConvolution(3 -> 64, 3x3, 1,1, 1,1)
(3): nn.ReLU
(4): nn.StyleLoss
(5): nn.SpatialConvolution(64 -> 64, 3x3, 1,1, 1,1)
(6): nn.ReLU
(7): nn.SpatialMaxPooling(2x2, 2,2)
(8): nn.SpatialConvolution(64 -> 128, 3x3, 1,1, 1,1)
(9): nn.ReLU
(10): nn.StyleLoss
(11): nn.SpatialConvolution(128 -> 128, 3x3, 1,1, 1,1)
(12): nn.ReLU
(13): nn.SpatialMaxPooling(2x2, 2,2)
(14): nn.SpatialConvolution(128 -> 256, 3x3, 1,1, 1,1)
(15): nn.ReLU
(16): nn.StyleLoss
(17): nn.SpatialConvolution(256 -> 256, 3x3, 1,1, 1,1)
(18): nn.ReLU
(19): nn.SpatialConvolution(256 -> 256, 3x3, 1,1, 1,1)
(20): nn.ReLU
(21): nn.SpatialConvolution(256 -> 256, 3x3, 1,1, 1,1)
(22): nn.ReLU
(23): nn.SpatialMaxPooling(2x2, 2,2)
(24): nn.SpatialConvolution(256 -> 512, 3x3, 1,1, 1,1)
(25): nn.ReLU
(26): nn.StyleLoss
(27): nn.SpatialConvolution(512 -> 512, 3x3, 1,1, 1,1)
(28): nn.ReLU
(29): nn.ContentLoss
(30): nn.SpatialConvolution(512 -> 512, 3x3, 1,1, 1,1)
(31): nn.ReLU
(32): nn.SpatialConvolution(512 -> 512, 3x3, 1,1, 1,1)
(33): nn.ReLU
(34): nn.SpatialMaxPooling(2x2, 2,2)
(35): nn.SpatialConvolution(512 -> 512, 3x3, 1,1, 1,1)
(36): nn.ReLU
(37): nn.StyleLoss
}

第三次,基于以上运行后,出现 已杀死killed ,查看Issues中出现此问题Ends abruptly with the message: Killed
在命令末尾加上 -image_size 256 即可,原因好像是内存分配问题导致进程被杀死。
截止到此,成功跑起来,运行代码。

th neural_style.lua -style_image examples/inputs/test1.jpg -content_image examples/inputs/test0.jpg -gpu -1 -image_size 256

以下 Iteration 50 / 1000 表示迭代周期,在CPU模式下进行,大概50分钟可以算出来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Capturing style target 1	
Running optimization with L-BFGS
<optim.lbfgs> creating recyclable direction/step/history buffers
Iteration 50 / 1000
Content 1 loss: 3137684.062500
Style 1 loss: 24579.841614
Style 2 loss: 402988.671875
Style 3 loss: 206954.541016
Style 4 loss: 1519436.718750
Style 5 loss: 4940.230560
Total loss: 5296584.066315
Iteration 100 / 1000
Content 1 loss: 2293927.968750
Style 1 loss: 10807.684326
Style 2 loss: 104236.169434
Style 3 loss: 72638.903809
Style 4 loss: 696868.261719
Style 5 loss: 4750.508499
Total loss: 3183229.496536
Iteration 150 / 1000
Content 1 loss: 2009629.531250
Style 1 loss: 6019.753647
Style 2 loss: 53772.943115
Style 3 loss: 46527.423096
Style 4 loss: 598785.595703
Style 5 loss: 5068.376923
Total loss: 2719803.623734
Iteration 200 / 1000
Content 1 loss: 1873752.343750
Style 1 loss: 4074.698257
Style 2 loss: 33790.167236
Style 3 loss: 36095.117188
Style 4 loss: 563354.492188
Style 5 loss: 5103.339767
Total loss: 2516170.158386
Iteration 250 / 1000
Content 1 loss: 1798739.218750
Style 1 loss: 3052.088928
Style 2 loss: 24353.424072
Style 3 loss: 31327.365112
Style 4 loss: 547528.515625
Style 5 loss: 5063.606644
Total loss: 2410064.219131

项目主页还有关于详细命令设置
th neural_style.lua -style_image examples/inputs/picasso_selfport1907.jpg -content_image examples/inputs/brad_pitt.jpg -output_image profile.png -model_file models/nin_imagenet_conv.caffemodel -proto_file models/train_val.prototxt -gpu 0 -backend clnn -num_iterations 1000 -seed 123 -content_layers relu0,relu3,relu7,relu12 -style_layers relu0,relu3,relu7,relu12 -content_weight 10 -style_weight 1000 -image_size 512 -optimizer adam

-gpu -1 表示没有显卡运行

GPU模式下运行

博主有气无力,硬件太烂,显卡还不是NAVID的,只是搜罗了几篇,供以后赞考
深度学习neural-style环境搭建 | 简书

文章作者: HibisciDai
文章链接: http://hibiscidai.com/2017/04/08/2017-04-08-神经网络风格画-Neural-Style-Art/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 HibisciDai
好用、实惠、稳定的梯子,点击这里