哪種髮型更適合你?AI幫你選擇適合你的髮型丨機器學習
前言
最近看到一個開源專案(Barbershop),可以將照片中的髮型更換成另一個,很神奇。先給大家看看專案給出的效果圖。
先說說我在安裝使用該專案的感受,因為作者給的安裝說明太少,我邊看程式碼邊安裝環境花了整整8個小時,順便還在等安裝的過程中,追了10集電視劇《輸贏》。看在我堅持弄了這麼長時間,並把踩過的坑,安裝這麼長時間的份上,我覺著還是給個讚的。
下面開始我的安裝使用記錄。
環境部署
下面進入最坑的部分,首先我們看一下專案給出的安裝說明。
其實只有兩條,conda匯入environment/environment。yaml環境,下載II2S圖片。就這兩句話讓我裝了一晚上,天知道我經歷了啥。坑踩了一邊,可以按照我的流程裝,應該問題不大。
1、匯入environment/environment。yaml環境
先建立conda虛擬環境,使用一下命令:
conda env create -f environment/environment。yaml
2、安裝pytorch
這裡有不會的話,可以看我另一篇文章,有詳細的搭建方法:
機器學習基礎環境部署 | 機器學習系列
3、依賴庫安裝
我就不提我踩了多少坑了,下面我把需要的專案沒提供的依賴,總結髮在下面。
dlib
Ninja
gdown
scikit-image
IPython
opencv-python
版本可以選擇最新的,因為我用最新的跑起來了。
4、 cl。exe環境變數配置
需要安裝一下Microsoft Visual Studio,我選擇的版本是2019版本。
這裡需要注意安裝的時候需要把C++選上。
按照自己的機器位數,選擇路徑新增到環境變數路徑裡。下面是我的路徑。
新增環境變數
5、模型下載
專案在執行的時候其實是會自己下載模型的,但是從google雲盤經常失敗,而且該專案要的模型特別多,大家可以下我的。
模型檔案連結:
https://
docs。qq。com/doc/DV1VkTE
FTYUhrZHRj
首先人臉68特徵資料放到專案的cache目錄內。其次另外的6個pt檔案放到專案的pretrained_models目錄內。
6、髮型資料下載
按照專案給出的地址從雲盤下載到input/face目錄內。
7、程式碼調整
專案align_face。py檔案中,不會檢查模型是否存在,還是會下載,需要調整一下。如下圖:
專案驗證
OK,環境如果你按照我上面說的部署成功了,恭喜。下面我來驗證一下效果。
先看看我準備的照片。
把照片放到unprocessed目錄下,注意編號需要和input/face內的檔案編號不同。
1、預處理照片
首先我們執行命令預處理unprocessed目錄內的照片。命令如下:
python align_face。py
(Barbershop) C:\Users\yi\PycharmProjects\Barbershop>python align_face。py
Downloading Shape Predictor
126。jpg: Number of faces detected: 1
可以看到input/face目錄多了一個檔案,預處理把原照片的人臉處理出來了。
2、換髮型
下面看一下我準備換的髮型是哪張。
換個短髮。執行命令如下:
python main。py ——im_path1 126。png ——im_path2 94。png ——im_path3 94。png ——sign realistic ——smooth 5
看下執行結果
(Barbershop) C:\Users\yi\PycharmProjects\Barbershop>python main。py ——im_path1 126。png ——im_path2 94。png ——im_path3 94。png ——sign realistic ——smooth 5
Loading StyleGAN2 from checkpoint: pretrained_models/ffhq。pt
Setting up Perceptual loss。。。
Loading model from: C:\Users\yi\PycharmProjects\Barbershop\losses\lpips\weights\v0。1\vgg。pth
。。。[net-lin [vgg]] initialized
。。。Done
Number of images: 2
Images: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [10:39<00:00, 319。74s/it]
Number of images: 2
Images: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [02:08<00:00, 64。04s/it]
Loading StyleGAN2 from checkpoint: pretrained_models/ffhq。pt
C:\Users\yi\PycharmProjects\Barbershop\models\Alignment。py:97: UserWarning: where received a uint8 condition tensor。 This behavior is deprecated and will be removed in a
future version of PyTorch。 Use a boolean condition instead。 (Triggered internally at 。。\aten\src\ATen\native\TensorCompare。cpp:328。)
new_target_final = torch。where(OB_region, torch。from_numpy(new_target_inpainted), new_target)
Align Step 2: 0%| | 0/100 [00:00<?, ?it/s]E
:\ProgramData\Anaconda3\envs\Barbershop\lib\site-packages\torch\nn\functional。py:3680: UserWarning: The default behavior for interpolate/upsample with float scale_factor
changed in 1。6。0 to align with other frameworks/libraries, and now uses scale_factor directly, instead of relying on the computed output size。 If you wish to restore th
e old behavior, please set recompute_scale_factor=True。 See the documentation of nn。Upsample for details。
“The default behavior for interpolate/upsample with float scale_factor changed ”
E:\ProgramData\Anaconda3\envs\Barbershop\lib\site-packages\torch\nn\functional。py:3635: UserWarning: Default upsampling behavior when mode=bicubic is changed to align_co
rners=False since 0。4。0。 Please specify align_corners=True if the old behavior is desired。 See the documentation of nn。Upsample for details。
“See the documentation of nn。Upsample for details。”。format(mode)
Loading StyleGAN2 from checkpoint: pretrained_models/ffhq。pt
Setting up Perceptual loss。。。
Loading model from: C:\Users\yi\PycharmProjects\Barbershop\losses\masked_lpips\weights\v0。1\vgg。pth
。。。[net-lin [vgg]] initialized
。。。Done
Setting up Perceptual loss。。。
Loading model from: C:\Users\yi\PycharmProjects\Barbershop\losses\masked_lpips\weights\v0。1\vgg。pth
。。。[net-lin [vgg]] initialized
。。。Done
等了很長時間,結果出來了。最後的效果圖如下
對比一下看看,還是很不錯的吧,我這麼長的安裝時間沒有白費。
總結
該專案最終的效果是非常好的,只是安裝的過程比較繁瑣。而且在我跑最終融合的效果時,我似乎都快聞到顯示卡的焦味了。花費了很多時間,解決了很多問題,也有了很多收穫,成長不外如此。
分享:
生活不能等待別人來安排,要自己去爭取和奮鬥;而不論其結果是喜是悲,但可以慰藉的是,你總不枉在這世界上活了一場。有了這樣的認識,你就會珍重生活,而不會玩世不恭;同時,也會給人自身注入一種強大的內在力量。——《平凡的世界》
如果本文對你有用的話,點個贊吧,謝謝!!!