这篇文章通过一个具体的例子来看一下使用torch可以做的有趣的事情。
事前准备
- https://liumiaocn.blog.csdn.net/article/details/109848433
liumiaocn:fast-neural-style liumiao$ th
______ __ | Torch7
/_ __/__ ________/ / | Scientific computing for Lua.
/ / / _ \/ __/ __/ _ \ | Type ? for help
/_/ \___/_/ \__/_//_/ | https://github.com/torch
| http://torch.ch
th>
步骤1: 下载图像风格迁移代码
执行 命令:git clone https://github.com/jcjohnson/fast-neural-style
liumiaocn:~ liumiao$ git clone https://github.com/jcjohnson/fast-neural-style
Cloning into 'fast-neural-style'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 256 (delta 0), reused 0 (delta 0), pack-reused 253
Receiving objects: 100% (256/256), 18.65 MiB | 7.14 MiB/s, done.
Resolving deltas: 100% (109/109), done.
liumiaocn:~ liumiao$
步骤2: 下载预先训练好的模型
liumiaocn:~ liumiao$ cd fast-neural-style/
liumiaocn:fast-neural-style liumiao$ ls
README.md fast_neural_style.lua print_options.lua slow_neural_style.lua webcam_demo.lua
doc images requirements.txt test
fast_neural_style models scripts train.lua
liumiaocn:fast-neural-style liumiao$ sh models/download_style_transfer_models.sh
...省略
liumiaocn:fast-neural-style liumiao$ find models/ -name '*.t7'
models//instance_norm/mosaic.t7
models//instance_norm/candy.t7
models//instance_norm/the_scream.t7
models//instance_norm/feathers.t7
models//instance_norm/la_muse.t7
models//instance_norm/udnie.t7
models//eccv16/starry_night.t7
models//eccv16/la_muse.t7
models//eccv16/composition_vii.t7
models//eccv16/the_wave.t7
liumiaocn:fast-neural-style liumiao$
注意:此步骤中下载脚本可能会下载失败,可以通过其他方式比如浏览器下载之后移动到相应位置即可。
结果验证
示例1: 黑猫警长
- 原始图片
- 执行命令:
liumiaocn:fast-neural-style liumiao$ th fast_neural_style.lua \
> -model models//eccv16/starry_night.t7 \
> -input_image liumiaocn.jpg \
> -output_image newstyle-liumiaocn.png
Writing output image to newstyle-liumiaocn.png
liumiaocn:fast-neural-style liumiao$
- 风格变换之后
注:图像太小,拉伸后很诡异
示例2: 芝加哥
- 原始图片
- 执行命令
liumiaocn:fast-neural-style liumiao$ time -p th fast_neural_style.lua \
> -model models//eccv16/starry_night.t7 \
> -input_image city.jpg \
> -output_image newstyle-city.png
Writing output image to newstyle-city.png
real 7.60
user 9.32
sys 4.79
liumiaocn:fast-neural-style liumiao$
- 风格变换之后