【StyleGAN】Robust Unsupervised StyleGAN Image Restoration论文代码复现

论文:https://arxiv.org/abs/2302.06733

代码:https://github.com/yohan-pg/robust-unsupervised

1.创建StyleGAN环境

创建新的虚拟环境StyleGAN,根据论文所给GitHub链接下载代码

git --clone https://github.com/NVlabs/stylegan2-ada-pytorch.git

2. 根据代码安装所需库

(我这里安装的几个库没有截图)

3.解决bug

ImportError: /root/.cache/torch_extensions/py39_cu117/upfirdn2d_plugin/upfirdn2d_plugin.so: cannot open shared object file: No such file or directory

  warnings.warn('Failed to build CUDA kernels for upfirdn2d. Falling back to slow reference implementation. Details:\n\n' + traceback.format_exc())

AttributeError: '_idat' object has no attribute 'fileno'

 终端输入指令:pip install ninja

因为这里的StyleGAN直接使用的预训练好的权重,如果需要将此方法用在自己的数据集上,需要重新训练网络,生成自己数据集的权重。

------------------------------------------------------------分割线----------------------------------------------------------



4.StyleGAN2网络 

文中说了预训练模型需要另一篇文章的StyleGAN2网络 

StyleGAN2 GitHub:https://github.com/NVlabs/stylegan2-ada-pytorch?tab=readme-ov-file

服务器下载StyleGAN2代码:git clone GitHub - NVlabs/stylegan2-ada-pytorch: StyleGAN2-ADA - Official PyTorch implementation

然后根据StyleGAN2中的 README 进行操作。

5.下载对应数据集

我这里先是使用的 MetFaces dataset数据集,按照要求下载图片文件

数据集链接:GitHub - NVlabs/metfaces-dataset

注意:下载时unprocessed会有7个不同的压缩文件。

根据数据集GitHub的介绍,下载完数据集之后需要运行metfaces.py重现数据集,运行过程中可能会出现某个图片不存在的错误,这时候可以把该图片单独下载上传后再重新运行。

6.python dataset_tool.py

更换自己对应的数据集路径

7.训练新网络

7.1 python train.py

我这里更改了代码中的路径,所以不用带GitHub中原教程的后缀。

7.2 安装网络缺少的模块

pip install tensorboard

终端运行:pip install tensorboard

7.3 RuntimeError

 RuntimeError:derivative for aten::grid_sampler_2d_backward is not implemented

问题GitHub链接:https://github.com/pytorch/pytorch/issues/34704

解决方法:根据StyleGAN3新版本:https://github.com/NVlabs/stylegan3/blob/407db86e6fe432540a22515310188288687858fa/torch_utils/ops/grid_sample_gradfix.py更换了torch_utils/ops/conv2d_gradfix.py和 torch_utils/ops/grid_sample_gradfix.py里的内容

出现新的报错:TypeError:'tuple'object is not callable

解决方法:**torch._C._jit_get_operation()**这个函数在pytorch2.0中已经被废弃了,所以需要对其进行替换。

参考这篇博文的内容:https://blog.csdn.net/qq_43826289/article/details/131578788,并使用chatgpt改写代码。

最终将有误代码更改为下面的代码可以实现训练。

class _GridSample2dBackward(torch.autograd.Function):
    @staticmethod
    def forward(ctx, grad_output, input, grid):
        grad_input = grad_grid = None
        if ctx.needs_input_grad[1] or ctx.needs_input_grad[2]:
            grad_input = torch.zeros_like(input)
            grad_grid = torch.zeros_like(grid)
        else:
            grad_input = torch.zeros_like(input)
            grad_grid = torch.zeros_like(grid)
        ctx.save_for_backward(grid)
        return grad_input, grad_grid

8.补充

8.1 training_loop.py

training_loop.py文件中可以启动tensorboard会话,这样每次训练的结果都可以在网页上看到。

安装tensorboard:pip install tensorboard

启动tensorboard:tensorboard --logdir=my_log_dir(my_log_dir是你结果输出的路经)

在端口中可以找到tensorboard输出的端口,然后转到浏览器中查看运行结果。

网页看到每次训练的情况(我刚跑起来,结果还不太好,网页大致就是这样)。

8.2 train.py

我还更改了train.py中输出结果的自动生成的文件夹名称,在上面加上了时间日期。

参考博文:

https://blog.csdn.net/weixin_44180836/article/details/118408106

NVlabs/stylegan2-ada-pytorch: StyleGAN2-ADA - Official PyTorch implementation (github.com)

https://github.com/pytorch/pytorch/issues/34704

https://blog.csdn.net/qq_43826289/article/details/131578788

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值