Bug in upfirdn2d_native and native pytorch version of it.
I'm trying to use a native pytorch version of fused_leaky_relu and upfirdn2d #66 #70
However there is a dimensionality bug in the upfirdn2d_native so I fixed it like this,
import torch.nn.functional as F
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]
For fused_leaky_relu, I used
import torch.nn.functional as F
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)
For those having a hard time compiling cuda code, this could be an easy way to do a demo
https://libraries.io/pypi/torch-dwconv
Besides, I'm not sure if this library could be used to substitute the compiled upfirdn2d
I think (original) implementation is correct as result of it matches with cuda implementation.
Regarding torch-dwconv, as it is also dependent on cuda kernel, it will not very helpful for those who have problems on compiling cuda codes.
Thanks a lot, this solution is work for me
非常感谢,这个解决方案对我来说是有效的
我做了这个修改后,仍会报错,请问你是怎么解决此问题的
修改点:

依然报错
Traceback (most recent call last):
File "convert_weight.py", line 13, in
非常感谢,这个解决方案对我来说是有效的
我做了这个修改后,仍会报错,请问你是怎么解决此问题的
修改点:
依然报错 Traceback (most recent call last): File "convert_weight.py", line 13, in from model import Generator, Discriminator File "/home/dan.wang/train_script/GAN/gan2shape/GAN2Shape/gan2shape/stylegan2/stylegan2-pytorch/model.py", line 11, in from op import FusedLeakyReLU, fused_leaky_relu, upfirdn2d, conv2d_gradfix File "/home/dan.wang/train_script/GAN/gan2shape/GAN2Shape/gan2shape/stylegan2/stylegan2-pytorch/op/init.py", line 1, in from .fused_act import FusedLeakyReLU, fused_leaky_relu File "/home/dan.wang/train_script/GAN/gan2shape/GAN2Shape/gan2shape/stylegan2/stylegan2-pytorch/op/fused_act.py", line 15, in os.path.join(module_path, "fused_bias_act_kernel.cu"), File "/home/dan.wang/anaconda3/envs/stylegan2py37/lib/python3.7/site-packages/torch/utils/cpp_extension.py", line 661, in load is_python_module) File "/home/dan.wang/anaconda3/envs/stylegan2py37/lib/python3.7/site-packages/torch/utils/cpp_extension.py", line 841, in _jit_compile return _import_module_from_library(name, build_directory, is_python_module) File "/home/dan.wang/anaconda3/envs/stylegan2py37/lib/python3.7/site-packages/torch/utils/cpp_extension.py", line 1048, in _import_module_from_library file, path, description = imp.find_module(module_name, [path]) File "/home/dan.wang/anaconda3/envs/stylegan2py37/lib/python3.7/imp.py", line 297, in find_module raise ImportError(_ERR_MSG.format(name), name=name) ImportError: No module named 'fused'
在你需要使用fused_leaky_relu的那个py文件里单独定义一个函数再去使用,不需要在原本的op文件夹里的py文件中修改源码。