文章目录
style-GAN Generator 代码解析
PART 2 of Sefa study:
对于style-GAN Generator部分的代码解析
-
style-GAN论文讲解[参考](StyleGAN-基于样式的生成对抗网络(论文阅读总结) - 知乎 (zhihu.com))
-
代码中每个模块的原理作用[解析参考](StyleGAN 和 StyleGAN2 的深度理解 - 知乎 (zhihu.com))
-
styleGAN2有部分添加,而styleGAN的Synthesis部分参考PGGAN
-
下面主要是梳理代码思路
<
代码主要分为两个部分:
- Mapping network
- Synthesis network
Mapping主要网络组块在 c l a s s D e n s e B l o c k ( ) class\quad DenseBlock() classDenseBlock()
Synthesis主要的网络组块在 c l a s s C o n v B l o c k ( ) class\quad ConvBlock() classConvBlock()依靠输入参数(‘position’)不同进行调整
最后组装为 c l a s s S t y l e G A N G e n e r a t o r ( n n . M o d u l e ) class\,StyleGANGenerator(nn.Module) classStyleGANGenerator(nn.Module)
StyleGAN部分
分辨率resolution在 2 3 ∼ 2 10 2^3\sim 2^{10} 23∼210
将潜变量空间$z\in Z(dim=512)\rightarrow w\in W(dim=512) $
n u m _ l a y e r s = l o g 2 ( 2 ⋅ r e s 4 ) ∗ 2 num\_layers=log_2(\frac{2\cdot res}{4})*2 num_layers=log2(42⋅res)∗2
-
输入 z : d i m = 2 , s h a p e = [ z . s h a p e [ 0 ] , 512 ] z:dim=2,shape=[z.shape[0],512] z:dim=2,shape=[z.shape[0],512]
-
先过mapping层得到dict{‘z’: z;‘label’:None;‘w’:w}
其中 z = p i x e l _ n o r m ( z ) z=pixel\_norm(z) z=pixel_norm(z)
z z z过8层 F C FC FC,维度不变Linear+relu 变成w
保持输入输出channels=512, w : [ z . s h a p e [ 0 ] , 512 ] w:[z.shape[0],512] w:[z.shape[0],512]
self.add_module(f'dense{ i}', DenseBlock(in_channels=in_channels, out_channels=out_channels, use_wscale=self.use_wscale,#=True lr_mul=self.lr_mul))#=0.01
-
两个tricks(option):
-
truncation(self.training=True进行自学习,用truncation技巧处理每层得到的w)
ps:其中使用all_gather函数源自.sync_op.py文件,主要把分布运算的w汇总求均值 w ˉ \bar w wˉ
-
style_mix (有0.9的可能性在任意mapping层以后用新的z在后面层产生的w代替原来这些层的w)
-
-
一层truncation:
- l a y e r _ n u m = l o g 2 ( 2 ⋅ r e s o l u t i o n 4 ) ∗ 2 layer\_num=log_2(\frac{2\cdot resolution}{4})*2 layer_num=log2(42⋅re