一张照片,AI生成抽象画(CLIPasso项目安装使用) | 机器学习系列

前言

​ 最近看到一个比较有意思的项目,可以将照片生成对应的抽象画。

项目地址:github地址

看一下项目提供的效果图。

file

是不是很有意思,我果断把项目弄下来自己玩了玩。下面我分享一下安装使用的过程,以供大家参考。

安装

我们先看一下作者给出的安装说明。

file

作者给出docker安装模式和pip安装模式,之前很多篇文章我都是使用pip进行安装部署的,这一次我决定偷懒使用docker方式。下面依次说下我的安装过程,给没有这么操作过的同学演示一下。

拉取镜像

按照作者的命令,在准备好的带显卡服务器上拉取镜像。

docker pull yaelvinker/clipasso_docker

这个命令耗时很长,你要多等等。

启动容器

镜像拉取完成后,我使用的启动命令如下:

docker run --name=hy-c -itd --restart=always --gpus all --shm-size 16G -p 2281:22 --log-opt max-size=50m --log-opt max-file=10 yaelvinker/clipasso_docker

没有按照作者提供的命令,我这里做一下说明。

1、主要将外部的显卡映射到容器内。

2、映射出22端口到外部的2281端口,是为了本地pycharm调试使用。如果有感兴趣的可以参考

Pycharm代码docker容器运行调试

如果不需要类似调试的话,可以去掉这部分的命令。

拉取项目

cd /home
git clone https://github.com/yael-vinker/CLIPasso.git
cd CLIPasso/
复制代码

目录结构如下

file

使用

在使用之前我们先看一下作者给的一些参数解释。

Optional arguments:

  • --num_strokes Defines the number of strokes used to create the sketch, which determines the level of abstraction. The default value is set to 16, but for different images, different numbers might produce better results.
  • --mask_object It is recommended to use images without a background, however, if your image contains a background, you can mask it out by using this flag with "1" as an argument.
  • --fix_scale If your image is not squared, it might be cut off, it is recommended to use this flag with 1 as input to automatically fix the scale without cutting the image.
  • --num_sketches As stated in the paper, by default there will be three parallel running scripts to synthesize three sketches and automatically choose the best one. However, for some environments (for example when running on CPU) this might be slow, so you can specify --num_sketches 1 instead.
  • -cpu If you want to run the code on the cpu (not recommended as it might be very slow).

简单说明一下

1、第一个参数--num_strokes主要是用来标识笔画数等级的,默认为16。简而言之,数量越大,画面越详细,数量越小,画面越抽象。

2、第二个参数--mask_object是用来扣除背景的,如果需要的话,使用1。

3、第三个参数--fix_scale是用来配置是否需要切图的,如果不配置的话,项目会先将图片转化为1:1的分辨率,如果想要保持原来的分辨率,需要配置为1。

4、第四个参数--num_sketches,默认为跑3次选最好的一次,如果你只想要1次,配置1就行。

5、第五个参数是否使用CPU,就不细说了。

样例一

下面是准备的第一张图,背景为纯白色。

file

执行命令如下

python run_object_sketching.py --target_file "changjinglu.jpg"
复制代码

执行结果

file

看一下效果

file

可以看出跑了3次。

file

还挺有意思的,大致的轮廓还是比较清晰的。

样例二

第二张图我挑了一张有背景的。

file

执行命令如下

python run_object_sketching.py --target_file "redainiao.jpg" --num_strokes 8 --num_sketches 1 --mask_object 1 --fix_scale 1
复制代码

参数上,我减少了笔画数,并且需要扣除一下背景以及保留原比例。

执行结果

file

看一下效果

file

file

整体形象还是非常不错的,很清晰的看出鸟的轮廓。

样例三

第三幅图我找了一张蒙娜丽莎的画像,主要想看看不扣背景,油画的抽象画效果。

file

执行命令如下

python run_object_sketching.py --target_file "mengnalisha.jpg" --num_strokes 32 --num_sketches 1 --fix_scale 1
复制代码

参数上,我增加了一些笔画,保留比例且不扣背景。

执行结果

file

看一下效果

file

哈哈哈,很有喜感,虽然效果不是很好,可能和我设置的笔画数太少有关。留个悬念,大家有兴趣可以加多一些笔画数看看效果。 

补充(很关键)

注意看一下这部分代码。

file

在容器化部署安装过程中,镜像是带有该预处理模型的。如果pip安装的话,需要自己下载,google云盘,懂得都懂。所以我把模型放到我的百度云盘上,需要的也可以从下面的链接下载。

链接:pan.baidu.com/s/1sHDJu8XS… 提取码:TUAN

总结

很久没有写机器学习这个系列了,其实我自己在github上已经收藏了不少有意思的项目,有时间都会玩玩看。如果有时间还可以把项目改改变成工具分享出来,但是最近比较忙,在研究点新东西。

分享:

Yet high over the city our line of yellow windows must have contributed their share of human secrecy to the casual watcher in the darkening streets, and I was him too, looking up and wondering. I was within and without, simultaneously enchanted and repelled by the inexhaustible variety of life.——《了不起的盖茨比》
复制代码

如果本文对你有帮助的话,点个赞吧,谢谢!

file

本人CSDN主页地址:剑客阿良_ALiang的主页

一起学习,一起进步。

猜你喜欢

转载自juejin.im/post/7078289837149093896