PaddleGAN icon indicating copy to clipboard operation
PaddleGAN copied to clipboard

PaddleGAN实现的stylegan2中的梯度可能有重大错误!

Open miemie2013 opened this issue 3 years ago • 3 comments

兄弟issue: https://github.com/PaddlePaddle/Paddle/issues/40741

ppgan/models/generators/generator_styleganv2.py中的ModulatedConv2D类的forward()方法中,有以下代码:

        style = self.modulation(style).reshape((batch, 1, in_channel, 1, 1))
        weight = self.scale * self.weight * style

        if self.demodulate:
            demod = paddle.rsqrt((weight * weight).sum([2, 3, 4]) + 1e-8)
            weight = weight * demod.reshape((batch, self.out_channel, 1, 1, 1))

问题复现:

代码地址:https://github.com/miemie2013/ppgan

环境:Python3.9、paddlepaddle-gpu==2.2.0

进入test_grad文件夹。首先运行test2_18_elementwise_grad.py,用了pytorch实现了上面的语句(精简了一下)

    style = styles.reshape((batch_size, 1, in_C, 1, 1))
    weight_pytorch = scale * weight * style
    demod = (weight_pytorch.square().sum(axis=[2, 3, 4]) + 1e-8).rsqrt()
    weight_pytorch = weight_pytorch * demod.reshape((batch_size, out_C, 1, 1, 1))

,跑8次前向,保存输入、输出、损失对styles的梯度等为文件18_grad.npz; 接着,跑test2_18_elementwise_grad_paddle.py,用了paddle实现了上面的语句(精简了一下),

    style = styles.reshape((batch_size, 1, in_C, 1, 1))
    weight = scale * weight * style
    demod = paddle.rsqrt((weight * weight).sum([2, 3, 4]) + 1e-8)
    weight = weight * demod.reshape((batch_size, out_C, 1, 1, 1))

读取18_grad.npz中保存的输入作为输入,跑8次前向。即此时paddle版与pytorch版有相同的输入,我们期望paddle与pytorch有相同的输出、相同的梯度。但是跑8次前向的结果发现,out_pytorch与out_paddle是一样的,dloss_dstyles_pytorch与dloss_dstyles_paddle差异巨大。

系paddle.grad()这个api求出了错误的梯度,错误的梯度将影响stylegan2的训练效果。

miemie2013 avatar Mar 21 '22 03:03 miemie2013

正在跟进中,预计2.3正式版解决这个问题

LielinJiang avatar Apr 27 '22 06:04 LielinJiang

我们在使用基于styleganV2的人脸属性编辑模型的时候,重新复现训练的时候,也发现了相同的问题,期待尽快解决。

simple123456T avatar May 09 '22 05:05 simple123456T

您好,请问问题还是否需要解决,目前相关图像生成能力集成在PaddleMIX中,https://github.com/PaddlePaddle/PaddleMIX/tree/develop 可以在这个repo下提出您的需求

jerrywgz avatar Feb 28 '24 12:02 jerrywgz