原文:http://blog.askfermi.me/2016/09/27/diy-prisma/

大约2个月前的一天,一款叫做PRISMA的应用突然刷爆了朋友圈,后来还出现了叫做Ostagram之类的更丰富的应用,它可以让一张照片变成世界名画的风格。实话说,这款app突然火起来还是很让我惊讶的,因为之前也恰好看到了相关的论文,和一个开源的实现。而且在6月的《互联网编程》的课上还有一位同学实现了出来。今天,我们就来一起来实现一个高级版的PRISMA,不仅仅是世界名画,任意两幅图片,我们都能将它们融合在一起。

由于这不是一篇太学术意义上的科普文章,因此本文中会介绍相关的论文,和一些开源的项目,并利用这个开源的项目来实现一个简单的类PRISMA应用。算是一篇踏坑纪实。这篇文章将只会实现后台的一部分。

原理

PRISMA工作在一种叫做卷积神经网络的理论之上,论文可以按此:A Neural Algorithm of Artistic Style,我们的这个项目根据的是这篇论文在torch上的一个实现,作者也将其开源在了Github上了:Neural Style。我们将其安装在我们的系统上,在做一些简单的操作就可以完成类似PRISMA的操作了。

硬件配置

PRISMA所用的卷积神经网络(CNN)通常都对计算机的性能有着较高的要求,在科研和工业环境中,通常需要使用较高配置的显卡来进行基于CUDA的运算才可以在较快的时间内完成。Neural Style的作者也提供了对CUDA的支持,因此有一块较好的显卡是比较推荐的配置。

根据测试,大约需要6~8G的内存,才可以较好地在CPU模式下运行Neural Style。

强烈不建议在小内存的主机上运行这一程序。

安装

Neural Style的作者提供了安装文档,然而,还是会经常遇到一些问题。推荐的安装流程如下(以Ubuntu为例):

升级GCC

GCC 5是必备的组件之一。最初我使用gcc 4.8和gcc 4.9都失败了,这是特别坑的一点,只有使用gcc 5以上的版本才可以正常编译。

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-5 g++-5
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60 --slave /usr/bin/g++ g++ /usr/bin/g++-5

之后使用gcc -v就可以看到当前的版本,若为5就可以进行下面的步骤了。

安装Torch及依赖

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

执行最后一条之后就会开始自动安装torch,在安装结束之后,会自动将环境变量信息写入bashrc,我们只需要source ~/.bashrc就可以使其生效,之后,在命令行中输入th,若出现 Torch,就表示安装成功了。

安装loadcaffe
loadcaffe 可以在Torch中加载Caffe的网络,也是一个经常使用的库。它依赖Google的Protocol Buffer Library,所以要先安装它们 sudo apt-get install libprotobuf-dev protobuf-compiler 我们可以通过luarocks(一个lua的Package Manager)来安装loadcaffe luarocks install loadcaffe

安装 Neural-Style

先从github上把仓库clone下来

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

之后下载提前训练好的神经网络数据,这个数据会比较大sh models/download_models.sh 下载结束之后,就基本可以开始使用了。

使用

最基础的使用:th neural_style.lua -style_image <image.jpg> -content_image <image.jpg> 就可以用默认的参数来输出融合后的图像了。我们也可以为其增加参数来实现不同的功能,基本可以实现PRISMA的各项功能:

Options:

  • image_size: Maximum side length (in pixels) of of the generated image. Default is 512.
  • style_blend_weights: The weight for blending the style of multiple style images, as a comma-separated list, such as -style_blend_weights 3,7. By default all style images are equally weighted.
  • gpu: Zero-indexed ID of the GPU to use; for CPU mode set -gpu to -1.

Optimization options:

  • content_weight: How much to weight the content reconstruction term. Default is 5e0.
  • style_weight: How much to weight the style reconstruction term. Default is 1e2.
  • tv_weight: Weight of total-variation (TV) regularization; this helps to smooth the image. Default is 1e-3. Set to 0 to disable TV regularization.
  • num_iterations: Default is 1000.
  • init: Method for generating the generated image; one of random or image. Default is random which uses a noise initialization as in the paper; image initializes with the content image.
  • optimizer: The optimization algorithm to use; either lbfgs or adam; default is lbfgs. L-BFGS tends to give better results, but uses more memory. Switching to ADAM will reduce memory usage; when using ADAM you will probably need to play with other parameters to get good results, especially the style weight, content weight, and learning rate; you may also want to normalize gradients when using ADAM.
  • learning_rate: Learning rate to use with the ADAM optimizer. Default is 1e1.
  • normalize_gradients: If this flag is present, style and content gradients from each layer will be L1 normalized. Idea from andersbll/neural_artistic_style.

Output options:

  • output_image: Name of the output image. Default is out.png.
  • print_iter: Print progress every print_iter iterations. Set to 0 to disable printing.
  • save_iter: Save the image every save_iter iterations. Set to 0 to disable saving intermediate results.

Layer options:

  • content_layers: Comma-separated list of layer names to use for content reconstruction. Default is relu4_2.
  • style_layers: Comma-separated list of layer names to use for style reconstruction. Default is relu1_1,relu2_1,relu3_1,relu4_1,relu5_1.

Other options:

  • style_scale: Scale at which to extract features from the style image. Default is 1.0.
  • original_colors: If you set this to 1, then the output image will keep the colors of the content image.
  • proto_file: Path to the deploy.txt file for the VGG Caffe model.
  • model_file: Path to the .caffemodel file for the VGG Caffe model. Default is the original VGG-19 model; you can also try the normalized VGG-19 model used in the paper.
  • pooling: The type of pooling layers to use; one of max or avg. Default is max. The VGG-19 models uses max pooling layers, but the paper mentions that replacing these layers with average pooling layers can improve the results. I haven’t been able to get good results using average pooling, but the option is here.
  • backend: nn, cudnn, or clnn. Default is nn. cudnn requires cudnn.torch and may reduce memory usage. clnn requires cltorch and clnn
  • cudnn_autotune: When using the cuDNN backend, pass this flag to use the built-in cuDNN autotuner to select the best convolution algorithms for your architecture. This will make the first iteration a bit slower and can take a bit more memory, but may significantly speed up the cuDNN backend.

运行结果

我对示例中的两幅图片进行了测试,分别使用50次迭代,100次迭代和200次迭代来达到不同的效果。效果图如下:

Content Image:

Style Image:

50次迭代:

100次迭代:

200次迭代:

自己写一个PRISMA 让两张图片融合起来相关推荐

  1. (C语言)写一个函数,实现两个字符串的比较, 即自己写一个strcmp函数,函数原型为int strcmp(const char* p1, const char* p2)

    写一个函数,实现两个字符串的比较, 即自己写一个strcmp函数,函数原型为int strcmp(const char* p1, const char* p2);设p1指向字符串s1,p2指向字符串s ...

  2. python两张图片融合_两张脸融合在一起长啥样?Opencv-Python 来告诉你!

    1,Image Morphing 介绍 图像融合简单来说,通过把图像设置为不同的透明度,把两张图像融合为一张图像(一般要求图像需要等尺寸),公式如下: 可以根据这个公式尝试实现一下融合技术,利用 Op ...

  3. python两张图片融合_python实现两张图片的像素融合

    本文实例为大家分享了python实现两张图片像素融合的具体代码,供大家参考,具体内容如下 通过计算两张图片的颜色直方图特征,利用直方图对图片的颜色进行融合. import numpy as np im ...

  4. Java写一个接口和两个类

    编写一个接口和两个类,要求如下: (1)设计一个学生功能的接口,实现平均成绩的计算和基本信息的输出. (2)设计一个学生类实现了学生接口,学生类的基本信息有:学号.姓名.年龄.各科成绩(假设一共5门课 ...

  5. C语言 写一个函数求两个数的较大值

    代码: #include<stdio.h>int maximum(int a,int b) {return((a > b) ? a : b ); }void main() { int ...

  6. c语言有参有类最小公倍数,【C语言】写一个函数,并调用该函数求两个整数的最大公约数和最小公倍数...

    程序分析: 在数学中,两个数的最小公倍数=两个数的乘积/两数的最大公约数. 求两个数的最大公约数,运用辗转相除法:已知两个整数M和N,假定M>N,则求M%N. 如果余数为0,则N即为所求:如果余 ...

  7. matlab两张图片合成一张_两张图片合成一幅画意作品的简单方法

    在<教你制作一幅画意摄影作品>的文章中,介绍了Snapseed(指划修图)和ToolWiz Photos(理理相册)两个App中用手机或平板制作画意摄影作品的基本方法,现在再来进一步试试用 ...

  8. 自己写个 Prisma

    Sirajology的视频链接 前一段时间特别火的 Prisma 大家都玩了么,看了这篇文章后,你也可以自己写一个 Prisma 迷你版了. 这个 idea 最开始起源于 Google Researc ...

  9. java简易计算器考察什么_练习:用java写一个简易计算器

    初学java中的选择,判断,循环和方法的概念,写一个简易的两位数计算器 思路推荐: 写4个方法:加减乘除 利用循环+switch进行用户交互 传递需要操作的两个数 输出结果 package com.j ...

最新文章

  1. unity3d界面部分英文翻译—新手
  2. Razor 也可说是一个asp.net模板引擎,用不着学习 T4 了
  3. 两台电脑间大量数据拷贝的快捷方法
  4. python3.7是什么_Python 3.7 有什么新变化
  5. dept在Java里面_EmpDeptManager 在JavaEE环境下搭建三大框架体系实现员工的增删改查系统 Develop 261万源代码下载- www.pudn.com...
  6. 根据某一字段值去重查找出所有字段的数据
  7. kmplayer安卓版外部编码器_KMPlayer下载
  8. springboot 代码自动生成器
  9. As a good Java programer
  10. html仿百度贴吧,利用Canvas模仿百度贴吧客户端loading小球的方法示例
  11. javaktv点歌系统项目(java点歌系统)java点歌管理系统
  12. WIN7 SP1 0x8007000D错误
  13. 软工网络15团队作业9——项目验收与总结
  14. 呼吸灯(Verilog)
  15. 小波学习笔记——MATLAB
  16. 开启 Zookeeper 四字命令(is not executed because it is not in the whitelist)
  17. 【第三方互联】9、新浪微博(sina)授权第三方登录
  18. android应用卸载后,出现的本地数据库内容未删除现象
  19. 腾讯公司称为用户安全扫描硬盘文件
  20. 【加密与解密】Openssl 生成的RSA秘钥如被C#使用解密

热门文章

  1. 移动应用界面设计的尺寸规范
  2. 德鲁克日志读后感之八十八
  3. 安全生产双重预防体系建设数字化解决方案
  4. Centos7制作Openstack下win7 镜像
  5. 十大经典排序算法及比较与分析 ( 动画演示 ) ( 可视化工具 )
  6. 用友BIP产品矩阵亮相首届中小企业数字化转型大会,数智创新驱动企业高效成长
  7. 平塘天眼和大数据有什么关系_“中国天眼”选址贵州平塘的缘由
  8. 删除windows文件右击打开方式中的无效程序选项
  9. 荒岛求生html5母狼攻,荒岛求生各资源作用及获取方法解析 荒岛求生资源怎么获得...
  10. 西游记中如来佛祖来降服孙悟空时为什么说可惜他的真面目