基於https://arxiv.org/abs/1912.04958的簡單pytorch實現了stylegan2的實現,可以從命令行完全訓練,無需編碼。
以下是一些不存在的花。
這些手也不
這些城市也不是
這些名人也沒有(@yoniker訓練)
您將需要安裝帶有GPU和CUDA的機器。然後pip這樣安裝包裹
$ pip install stylegan2_pytorch如果您使用的是Windows計算機,則據報導,以下命令有效。
$ conda install pytorch torchvision -c python
$ pip install stylegan2_pytorch$ stylegan2_pytorch --data /path/to/images就是這樣。示例圖像將被保存到results/default ,並將模型定期保存到models/default中。
您可以用
$ stylegan2_pytorch --data /path/to/images --name my-project-name您還可以指定應將中間結果和模型檢查點存儲的位置
$ stylegan2_pytorch --data /path/to/images --name my-project-name --results_dir /path/to/results/dir --models_dir /path/to/models/dir您可以將網絡容量(默認為16 )提高以提高發電結果,而成本更多。
$ stylegan2_pytorch --data /path/to/images --network-capacity 256默認情況下,如果培訓被切斷,它將自動從最後一個檢查點文件中恢復。如果您想重新啟動新設置,只需添加一個new標誌
$ stylegan2_pytorch --new --data /path/to/images --name my-project-name --image-size 512 --batch-size 1 --gradient-accumulate-every 16 --network-capacity 10完成培訓後,您可以從最新檢查站產生圖像。
$ stylegan2_pytorch --generate通過潛在空間中的兩個隨機點生成插值的視頻。
$ stylegan2_pytorch --generate-interpolation --interpolation-num-steps 100保存插值的每個單獨框架
$ stylegan2_pytorch --generate-interpolation --save-frames如果先前的檢查點包含一個更好的生成器(通常是在發電機開始在培訓結束時開始降解),則可以從先前的檢查點加載帶有另一個標誌
$ stylegan2_pytorch --generate --load-from {checkpoint number} Stylegan和Biggan中使用的一種技術正在截斷潛在值,以使它們的值接近均值。截斷值很小,樣品以樣品品種的成本而出現越好。您可以用--trunc-psi控制它,其中值通常落在0.5和1之間。默認設置為0.75
$ stylegan2_pytorch --generate --trunc-psi 0.5如果您有一台具有多個GPU的機器,則存儲庫提供一種利用所有機器進行培訓的方法。使用多個GPU,每個批次將在可用的GPU中均勻分配。例如,對於2個GPU,批量大小為32,每個GPU將看到16個樣本。
您只需要添加一個--multi-gpus標誌,所有其他都可以照顧。如果您想限製到特定的GPU,則可以使用CUDA_VISIBLE_DEVICES環境變量來控制可以使用哪些設備。 (例如CUDA_VISIBLE_DEVICES=0,2,3僅設備0、2、3可用)
$ stylegan2_pytorch --data ./data --multi-gpus --batch-size 32 --gradient-accumulate-every 1過去,甘斯需要大量數據來學習如何生成良好。例如,Faces模型從Flickr拍攝了70K高質量的圖像。
但是,在2020年5月,世界各地的研究人員都獨立地融合了一種簡單的技術,以將該數字降低到1-2K 。這個簡單的想法是在培訓期間分化了所有圖像,即產生或真實的圖像。
如果一個人以足夠低的概率增加,則增強物將不會“洩漏”到世代中。
在低數據設置中,您可以將功能與簡單標誌一起使用。
# find a suitable probability between 0. -> 0.7 at maximum
$ stylegan2_pytorch --data ./data --aug-prob 0.25默認情況下,使用的增強是translation和cutout 。如果您想添加color ,則可以使用--aug-types參數進行操作。
# make sure there are no spaces between items!
$ stylegan2_pytorch --data ./data --aug-prob 0.25 --aug-types [translation,cutout,color]您可以將其自定義為您想要的三個組合。從此處複製了可區分的增強代碼並稍作修改。
直到兩個神經網之間的對抗性遊戲崩潰(我們稱此分歧)為止。默認情況下,對於128x128圖像,訓練步驟的數量設置為150000 ,但是如果GAN在培訓結束時不相差,或者您正在以更高的分辨率進行培訓,那麼您肯定希望此數字更高。
$ stylegan2_pytorch --data ./data --image-size 512 --num-train-steps 1000000該框架還允許您在歧視器的指定層(以及發電機的對稱層)中添加有效的自我注意力形式,這將大大改善結果。您負擔得起的關注越多,越好!
# add self attention after the output of layer 1
$ stylegan2_pytorch - - data . / data - - attn - layers 1 # add self attention after the output of layers 1 and 2
# do not put a space after the comma in the list!
$ stylegan2_pytorch - - data . / data - - attn - layers [ 1 , 2 ]透明圖像培訓
$ stylegan2_pytorch --data ./transparent/images/path --transparent您擁有的GPU內存越多,圖像生成就會越大。 NVIDIA建議將高達16GB用於培訓1024x1024圖像。如果您少於此,則可以使用幾個設置,以使該模型合適。
$ stylegan2_pytorch --data /path/to/data
--batch-size 3
--gradient-accumulate-every 5
--network-capacity 16批次尺寸 - 您可以將batch-size降低至1,但是您應該相應地增加gradient-accumulate-every以便網絡看到的微型批量不會太小。這可能會使外行人感到困惑,因此我將考慮如何自動化gradient-accumulate-every選擇。
網絡容量 - 您可以降低神經網絡容量以降低內存需求。請注意,這已被證明可以降低生成性能。
如果這一切都沒有起作用,則可以選擇“輕量級” gan,這將使您能夠在合理的時間內以更大的分辨率進行培訓。
以下是一些步驟,可能有助於使用Amazon Web服務部署。為了使用此功能,您必須提供一個由GPU支持的EC2實例。適當的實例類型將來自P2或P3系列。我(iBoates)嘗試了一個P2.xlarge(最便宜的選擇),並且比使用Google Colab慢,實際上慢。更強大的實例類型可能會更好,但它們更昂貴。您可以在這裡閱讀有關它們的更多信息。
sudo snap install aws-cli --classic
aws configure然後,您將必須輸入AWS訪問鍵,您可以在AWS管理控制台>配置文件>我的安全憑據>訪問密鑰下從管理控制台中檢索。
然後,運行這些命令,或者將它們放入外殼腳本中並執行該命令:
mkdir data
curl -O https://bootstrap.pypa.io/get-pip.py
sudo apt-get install python3-distutils
python3 get-pip.py
pip3 install stylegan2_pytorch
export PATH= $PATH :/home/ubuntu/.local/bin
aws s3 sync s3:// < Your bucket name > ~ /data
cd data
tar -xf ../train.tar.gz現在,您應該可以通過簡單地調用stylegan2_pytorch [args]來訓練。
筆記:
screen窗口上運行此操作,以便一旦登錄SSH會話就不會終止。 多虧了Geteclectic,您現在可以定期計算FID分數!同樣,用一個額外的參數使超級簡單,如下所示。
首先,安裝pytorch_fid軟件包
$ pip install pytorch-fid其次是
$ stylegan2_pytorch --data ./data --calculate-fid-every 5000 FID結果將登錄到./results/{name}/fid_scores.txt name }/fid_scores.txt
如果您想通過以下簡單的ModelLoader類進行編程示例圖像,則可以這樣做。
import torch
from torchvision . utils import save_image
from stylegan2_pytorch import ModelLoader
loader = ModelLoader (
base_dir = '/path/to/directory' , # path to where you invoked the command line tool
name = 'default' # the project name, defaults to 'default'
)
noise = torch . randn ( 1 , 512 ). cuda () # noise
styles = loader . noise_to_styles ( noise , trunc_psi = 0.7 ) # pass through mapping network
images = loader . styles_to_images ( styles ) # call the generator on intermediate style vectors
save_image ( images , './sample.jpg' ) # save your images, or do whatever you desire要將損失記錄到開源實驗跟踪器(AIM)中,您只需要通過這樣的額外標誌即可。
$ stylegan2_pytorch --data ./data --log然後,您需要確保安裝了Docker。按照AIM的說明,您在終端中執行以下操作。
$ aim up然後打開瀏覽器到地址,您應該看到

一篇新論文產生了證據表明,通過簡單地將歧視者認為是假的樣本中的梯度貢獻零,生成器就可以更好地學習,從而實現了新的最新狀態。
$ stylegan2_pytorch - - data . / data - - top - k - training伽馬是一個衰減時間表,可緩慢地將TOPK從整個批處理大小降低到50%的目標分數(也可修改的超參數)。
$ stylegan2_pytorch - - data . / data - - top - k - training - - generate - top - k - frac 0.5 - - generate - top - k - gamma 0.99最近的一篇論文報導瞭如果對鑑別器的中間表示進行了量化,則報告的結果有所改善。儘管我沒有註意到任何戲劇性的更改,但我還是決定將其添加為一個功能,因此其他人可以調查。要使用,您必須指定要量化哪個層。默認字典大小為256 ,也可調節。
# feature quantize layers 1 and 2, with a dictionary size of 512 each
# do not put a space after the comma in the list!
$ stylegan2_pytorch - - data . / data - - fq - layers [ 1 , 2 ] - - fq - dict - size 512我嘗試過對歧視者的對比學習(根據通常的GAN培訓),並可能觀察到提高的穩定性和最終結果的質量。您可以使用一個簡單的標誌打開此實驗功能,如下所示。
$ stylegan2_pytorch - - data . / data - - cl - reg這是在相對論的GAN論文中提出的,以穩定培訓。我的結果好壞參半,但會為那些想嘗試一下的人提供功能。
$ stylegan2_pytorch - - data . / data - - rel - disc - loss默認情況下,StyleGAN體系結構逐漸被逐步採樣時,會導致常數學習的4x4塊。這是一個實驗功能,因此可以從樣式向量w中學到4x4塊。
$ stylegan2_pytorch - - data . / data - - no - const最近的一篇論文提出,真實和假ligits之間的新穎對比損失可以改善其他類型的損失。 (該存儲庫中的默認值是鉸鏈損失,並且該論文顯示出略有改進)
$ stylegan2_pytorch - - data . / data - - dual - contrast - loss stylegan2 + UNET歧視器
我通過一個UNET歧視器獲得了非常好的效果,但是在此存儲庫中,架構上的變化太大了,無法適應。如果您的目標是完美,請隨時嘗試。
如果您希望我對其他一些GAN建築(Biggan)進行皇家待遇,請隨時在我的電子郵件中脫穎而出。很高興聽到您的音調。
感謝Matthew Mann為Tensorflow 2.0的啟發性簡單端口
@article { Karras2019stylegan2 ,
title = { Analyzing and Improving the Image Quality of {StyleGAN} } ,
author = { Tero Karras and Samuli Laine and Miika Aittala and Janne Hellsten and Jaakko Lehtinen and Timo Aila } ,
journal = { CoRR } ,
volume = { abs/1912.04958 } ,
year = { 2019 } ,
} @misc { zhao2020feature ,
title = { Feature Quantization Improves GAN Training } ,
author = { Yang Zhao and Chunyuan Li and Ping Yu and Jianfeng Gao and Changyou Chen } ,
year = { 2020 }
} @misc { chen2020simple ,
title = { A Simple Framework for Contrastive Learning of Visual Representations } ,
author = { Ting Chen and Simon Kornblith and Mohammad Norouzi and Geoffrey Hinton } ,
year = { 2020 }
} @article {,
title = { Oxford 102 Flowers } ,
author = { Nilsback, M-E. and Zisserman, A., 2008 } ,
abstract = { A 102 category dataset consisting of 102 flower categories, commonly occuring in the United Kingdom. Each class consists of 40 to 258 images. The images have large scale, pose and light variations. }
} @article { afifi201911k ,
title = { 11K Hands: gender recognition and biometric identification using a large dataset of hand images } ,
author = { Afifi, Mahmoud } ,
journal = { Multimedia Tools and Applications }
} @misc { zhang2018selfattention ,
title = { Self-Attention Generative Adversarial Networks } ,
author = { Han Zhang and Ian Goodfellow and Dimitris Metaxas and Augustus Odena } ,
year = { 2018 } ,
eprint = { 1805.08318 } ,
archivePrefix = { arXiv }
} @article { shen2019efficient ,
author = { Zhuoran Shen and
Mingyuan Zhang and
Haiyu Zhao and
Shuai Yi and
Hongsheng Li } ,
title = { Efficient Attention: Attention with Linear Complexities } ,
journal = { CoRR } ,
year = { 2018 } ,
url = { http://arxiv.org/abs/1812.01243 } ,
} @article { zhao2020diffaugment ,
title = { Differentiable Augmentation for Data-Efficient GAN Training } ,
author = { Zhao, Shengyu and Liu, Zhijian and Lin, Ji and Zhu, Jun-Yan and Han, Song } ,
journal = { arXiv preprint arXiv:2006.10738 } ,
year = { 2020 }
} @misc { zhao2020image ,
title = { Image Augmentations for GAN Training } ,
author = { Zhengli Zhao and Zizhao Zhang and Ting Chen and Sameer Singh and Han Zhang } ,
year = { 2020 } ,
eprint = { 2006.02595 } ,
archivePrefix = { arXiv }
} @misc { karras2020training ,
title = { Training Generative Adversarial Networks with Limited Data } ,
author = { Tero Karras and Miika Aittala and Janne Hellsten and Samuli Laine and Jaakko Lehtinen and Timo Aila } ,
year = { 2020 } ,
eprint = { 2006.06676 } ,
archivePrefix = { arXiv } ,
primaryClass = { cs.CV }
} @misc { jolicoeurmartineau2018relativistic ,
title = { The relativistic discriminator: a key element missing from standard GAN } ,
author = { Alexia Jolicoeur-Martineau } ,
year = { 2018 } ,
eprint = { 1807.00734 } ,
archivePrefix = { arXiv } ,
primaryClass = { cs.LG }
} @misc { sinha2020topk ,
title = { Top-k Training of GANs: Improving GAN Performance by Throwing Away Bad Samples } ,
author = { Samarth Sinha and Zhengli Zhao and Anirudh Goyal and Colin Raffel and Augustus Odena } ,
year = { 2020 } ,
eprint = { 2002.06224 } ,
archivePrefix = { arXiv } ,
primaryClass = { stat.ML }
} @misc { yu2021dual ,
title = { Dual Contrastive Loss and Attention for GANs } ,
author = { Ning Yu and Guilin Liu and Aysegul Dundar and Andrew Tao and Bryan Catanzaro and Larry Davis and Mario Fritz } ,
year = { 2021 } ,
eprint = { 2103.16748 } ,
archivePrefix = { arXiv } ,
primaryClass = { cs.CV }
}