一張照片,AI生成抽象畫(CLIPasso專案安裝使用) | 機器學習系列

劍客·阿良發表於2022-03-23

前言


最近看到一個比較有意思的專案,可以將照片生成對應的抽象畫。

專案地址: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雲盤,懂得都懂。所以我把模型放到我的百度雲盤上,需要的也可以從下面的連結下載。

連結: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.——《了不起的蓋茨比》

如果本文對你有幫助的話,點個贊吧,謝謝!

file

本人CSDN主頁地址:劍客阿良_ALiang的主頁

一起學習,一起進步。

相關文章