前言
最近看到一個比較有意思的項目,可以將照片生成對應的抽象畫。
項目地址:github地址
看一下項目提供的效果圖。
是不是很有意思,我果斷把項目弄下來自己玩了玩。下面我分享一下安裝使用的過程,以供大家參考。
安裝
我們先看一下作者給出的安裝說明。
作者給出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調試使用。如果有感興趣的可以參考
如果不需要類似調試的話,可以去掉這部分的命令。
拉取項目
cd /home
git clone https://github.com/yael-vinker/CLIPasso.git
cd CLIPasso/
目錄結構如下
使用
在使用之前我們先看一下作者給的一些參數解釋。
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,就不細說了。
樣例一
下面是准備的第一張圖,背景為純白色。
執行命令如下
python run_object_sketching.py --target_file "changjinglu.jpg"
執行結果
看一下效果
可以看出跑了3次。
還挺有意思的,大致的輪廓還是比較清晰的。
樣例二
第二張圖我挑了一張有背景的。
執行命令如下
python run_object_sketching.py --target_file "redainiao.jpg" --num_strokes 8 --num_sketches 1 --mask_object 1 --fix_scale 1
參數上,我減少了筆畫數,並且需要扣除一下背景以及保留原比例。
執行結果
看一下效果
整體形象還是非常不錯的,很清晰的看出鳥的輪廓。
樣例三
第三幅圖我找了一張蒙娜麗莎的畫像,主要想看看不扣背景,油畫的抽象畫效果。
執行命令如下
python run_object_sketching.py --target_file "mengnalisha.jpg" --num_strokes 32 --num_sketches 1 --fix_scale 1
參數上,我增加了一些筆畫,保留比例且不扣背景。
執行結果
看一下效果
哈哈哈,很有喜感,雖然效果不是很好,可能和我設置的筆畫數太少有關。留個懸念,大家有興趣可以加多一些筆畫數看看效果。
補充(很關鍵)
注意看一下這部分代碼。
在容器化部署安裝過程中,鏡像是帶有該預處理模型的。如果pip安裝的話,需要自己下載,google雲盤,懂得都懂。所以我把模型放到我的百度雲盤上,需要的也可以從下面的鏈接下載。
鏈接:https://pan.baidu.com/s/1sHDJu8XS9atyJdcNcfnfNw
提取碼: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.——《了不起的蓋茨比》
如果本文對你有幫助的話,點個贊吧,謝謝!
本人CSDN主頁地址:劍客阿良_ALiang的主頁
一起學習,一起進步。