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

基本情况

https://github.com/rosinality/stylegan2-pytorch/issues/81

运行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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
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]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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加入环境变量

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

 

ModuleNotFoundError: No module named 'EAdam'是一个Python错误,它表示找不到名为'EAdam'的模块。这可能是由于以下几个原因之一导致的: 1. 模块未安装:如果'EAdam'是一个第三方模块,您需要使用pip或其他包管理工具将其安装到您的Python环境中。您可以尝试使用命令`pip install EAdam`来安装它。 2. 模块名称错误:请确保您在代码中正确地引用了模块名称。检查拼写错误或大小写错误。 3. 模块路径问题:如果模块不在默认的模块搜索路径中,您需要确保将模块所在的目录添加到sys.path中。您可以在代码中使用以下方式添加路径: ```python import sys sys.path.append("/path/to/module_directory") ``` 请注意,根据提供的引用内容,没有找到与'EAdam'相关的具体信息。因此,我无法提供更具体的解决方案。如果您可以提供更多背景信息或代码片段,我将能够给出更准确的答案。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ModuleNotFoundError: No module named ‘cuda‘、‘tensorrt](https://blog.csdn.net/qq_37700257/article/details/130187061)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [ModuleNotFoundError: No module namedfused_layer_norm_cuda](https://blog.csdn.net/Zhangye1011/article/details/125962036)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Python模块导入出现ModuleNotFoundError: No module named ‘***’](https://download.csdn.net/download/weixin_38658085/14885867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值