基于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 }
}