[解决方案记录]No module named fused(stylegan2的bug,已更新)

更新于2021.9.14:
之前的upfirdn2d_native有bug,维度有错误,参考另一个链接后修复了该bug。

基本情况

运行psp时出现的问题。其实就是stylegan2里面采用了c++编译等功能带来的bug,非常烦人。
系统:windows
平台:pycharm + jupyter notebook
GPU:GTX1660Ti

解决方案

(0)无脑但是有效:直接把fused_leakyrelu,upfirdn2d_native和FusedLeakyRelu替换为pytorch实现即可。

参考链接

def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):
    # out = UpFirDn2d.apply(
    #     input, kernel, (up, up), (down, down), (pad[0], pad[1], pad[0], pad[1])
    # )
    out = upfirdn2d_native(input, kernel, up, up, down, down, pad[0], pad[1], pad[0], pad[1])
    return out
def upfirdn2d_native(
    input, kernel, up_x, up_y, down_x, down_y, pad_x0, pad_x1, pad_y0, pad_y1
):
    input = input.permute(0, 2, 3, 1)
    _, in_h, in_w, minor = input.shape
    kernel_h, kernel_w = kernel.shape
    out = input.view(-1, in_h, 1, in_w, 1, minor)
    out = F.pad(out, [0, 0, 0, up_x - 1, 0, 0, 0, up_y - 1])
    out = out.view(-1, in_h * up_y, in_w * up_x, minor)

    out = F.pad(
        out, [0, 0, max(pad_x0, 0), max(pad_x1, 0), max(pad_y0, 0), max(pad_y1, 0)]
    )
    out = out[
        :,
        max(-pad_y0, 0) : out.shape[1] - max(-pad_y1, 0),
        max(-pad_x0, 0) : out.shape[2] - max(-pad_x1, 0),
        :,
    ]

    out = out.permute(0, 3, 1, 2)
    out = out.reshape(
        [-1, 1, in_h * up_y + pad_y0 + pad_y1, in_w * up_x + pad_x0 + pad_x1]
    )
    w = torch.flip(kernel, [0, 1]).view(1, 1, kernel_h, kernel_w)
    out = F.conv2d(out, w)
    out = out.reshape(
        -1,
        minor,
        in_h * up_y + pad_y0 + pad_y1 - kernel_h + 1,
        in_w * up_x + pad_x0 + pad_x1 - kernel_w + 1,
    )
    # out = out.permute(0, 2, 3, 1)
    return out[:, :, ::down_y, ::down_x]

import torch
from torch import nn
import torch.nn.functional as F


class FusedLeakyReLU(nn.Module):
    def __init__(self, channel, negative_slope=0.2, scale=2 ** 0.5):
        super().__init__()

        self.bias = nn.Parameter(torch.zeros(channel))
        self.negative_slope = negative_slope
        self.scale = scale

    def forward(self, input):
        return fused_leaky_relu(input, self.bias, self.negative_slope, self.scale)


def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
    return scale * F.leaky_relu(input + bias.view((1, -1) + (1,) * (len(input.shape) - 2)),
                                negative_slope=negative_slope)

FuseLeakyReLU(x)等价于 scale * F.leaky_relu(x + bias)
要注意的是,这里的FuseLeakyReLU要求x的shape是[batch, channel, …]的形式,否则要对bias那里的维度进行调整。

(1)anaconda环境的CUDA要保证在10.1及以上

否则不能编译成功,这是github上一个老哥说的,具体链接不记得了。conda环境的CUDA版本是根据这个环境下的cudatoollkit控制的,因此在该环境下用torch官网的方法更新cudatoolkit版本即可。
在这里插入图片描述

(2)cl.exe, Visual Studio要加入环境变量

建议用everything这个搜索软件定位关键文件的位置,然后加入环境变量,不要照单全抄,因为每个人电脑上软件安装位置有细微的区别,需要自己更改一些细节。

cl.exe不加入的话会报错Error checking compiler version for cl

参考教程:
VS加入环境变量
cl.exe加入环境变量

教程不一定是这两个,可以自己找其他的,我也是随便找的。

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值