executing: 'python3 inference/inference_stylegan2.py' results in error
-
Downloaded BasicSR-1.4.2 release
-
Downloaded pretrained models and placed them in 'experiments/pretrained_models' subfolder:
experiments/pretrained_models/BasicVSR/EDVR_REDS_pretrained_for_IconVSR-f62a2f1e.pth experiments/pretrained_models/BasicVSR/BasicVSR_Vimeo90K_BIx4-2a29695a.pth experiments/pretrained_models/BasicVSR/IconVSR_Vimeo90K_BIx4-35fec07c.pth experiments/pretrained_models/BasicVSR/BasicVSR_Vimeo90K_BDx4-e9bf46eb.pth experiments/pretrained_models/BasicVSR/EDVR_Vimeo90K_pretrained_for_IconVSR-ee48ee92.pth experiments/pretrained_models/BasicVSR/BasicVSR_REDS4-543c8261.pth experiments/pretrained_models/BasicVSR/IconVSR_Vimeo90K_BDx4-cfcb7e00.pth experiments/pretrained_models/BasicVSR/IconVSR_REDS-aaa5367f.pth experiments/pretrained_models/EDVR/EDVR_L_deblur_REDS_official-ca46bd8c.pth experiments/pretrained_models/EDVR/EDVR_L_deblurcomp_REDS_official-0e988e5c.pth experiments/pretrained_models/EDVR/EDVR_M_x4_SR_REDS_official-32075921.pth experiments/pretrained_models/EDVR/EDVR_L_x4_SR_Vimeo90K_official-162b54e4.pth experiments/pretrained_models/EDVR/EDVR_L_x4_SR_REDS_official-9f5f5039.pth experiments/pretrained_models/EDVR/EDVR_M_woTSA_x4_SR_REDS_official-1edf645c.pth experiments/pretrained_models/EDVR/EDVR_L_x4_SRblur_REDS_official-983d7b8e.pth experiments/pretrained_models/TOF/tof_x4_vimeo90k_official-32c9e01f.pth experiments/pretrained_models/DFDNet/DFDNet_official-d1fa5650.pth experiments/pretrained_models/DFDNet/DFDNet_dict_512-f79685f0-001.pth experiments/pretrained_models/DUF/DUF_x3_16L_official-34ce53ec.pth experiments/pretrained_models/DUF/DUF_x4_28L_official-cbada450.pth experiments/pretrained_models/DUF/DUF_x4_52L_official-483d2c78.pth experiments/pretrained_models/DUF/DUF_x2_16L_official-39537cb9.pth experiments/pretrained_models/DUF/DUF_x4_16L_official-bf8f0cfa.pth experiments/pretrained_models/dlib/shape_predictor_5_face_landmarks-c4b1e980.dat experiments/pretrained_models/dlib/mmod_human_face_detector-4cb19393.dat experiments/pretrained_models/dlib/shape_predictor_68_face_landmarks-fbdc2cb8.dat experiments/pretrained_models/flownet/spynet_sintel_final-3d2a1287.pth experiments/pretrained_models/EDSR/EDSR_Lx2_f256b32_DIV2K_official-be38e77d.pth experiments/pretrained_models/EDSR/EDSR_Lx3_f256b32_DIV2K_official-3660f70d.pth experiments/pretrained_models/EDSR/EDSR_Mx4_f64b16_DIV2K_official-0c287733.pth experiments/pretrained_models/EDSR/EDSR_Lx4_f256b32_DIV2K_official-76ee1c8f.pth experiments/pretrained_models/EDSR/EDSR_Mx3_f64b16_DIV2K_official-6908f88a.pth experiments/pretrained_models/EDSR/EDSR_Mx2_f64b16_DIV2K_official-3ba7b086.pth experiments/pretrained_models/README.md experiments/pretrained_models/StyleGAN/stylegan2_horse_config_f_256_discriminator_official-be6c4c33.pth experiments/pretrained_models/StyleGAN/stylegan2_cat_config_f_256_discriminator_official-2c97fd08.pth experiments/pretrained_models/StyleGAN/stylegan2_car_config_f_512_discriminator_official-5008e3d1.pth experiments/pretrained_models/StyleGAN/stylegan2_horse_config_f_256_official-26d57fee.pth experiments/pretrained_models/StyleGAN/stylegan2_cat_config_f_256_official-0a9173ad.pth experiments/pretrained_models/StyleGAN/stylegan2_ffhq_config_f_1024_official-3ab41b38.pth experiments/pretrained_models/StyleGAN/stylegan2_church_config_f_256_official-44ba63bf.pth experiments/pretrained_models/StyleGAN/stylegan2_church_config_f_256_discriminator_official-20cd675b.pth experiments/pretrained_models/StyleGAN/stylegan2_ffhq_config_f_1024_discriminator_official-a386354a.pth experiments/pretrained_models/StyleGAN/stylegan2_car_config_f_512_official-e8fcab4f.pth experiments/pretrained_models/ESRGAN/ESRGAN_PSNR_SRx4_DF2K_official-150ff491.pth experiments/pretrained_models/ESRGAN/ESRGAN_SRx4_DF2KOST_official-ff704c30.pth -
executed: 'python3 inference/inference_stylegan2.py'
-
got the following error:
/opt/venv/lib/python3.11/site-packages/torchvision/transforms/functional_tensor.py:5: UserWarning: The torchvision.transforms.functional_tensor module is deprecated in 0.15 and will be **removed in 0.17**. Please don't rely on it. You probably just need to use APIs in torchvision.transforms.functional or in torchvision.transforms.v2.functional. warnings.warn( Traceback (most recent call last): File "/opt/BasicSR-1.4.2/inference/inference_stylegan2.py", line 70, in <module> generate(args, g_ema, device, mean_latent, args.randomize_noise) File "/opt/BasicSR-1.4.2/inference/inference_stylegan2.py", line 18, in generate sample, _ = g_ema([sample_z], ^^^^^^^^^^^^^^^^^ File "/opt/venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/venv/lib/python3.11/site-packages/basicsr/archs/stylegan2_arch.py", line 539, in forward styles = [self.style_mlp(s) for s in styles] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/venv/lib/python3.11/site-packages/basicsr/archs/stylegan2_arch.py", line 539, in <listcomp> styles = [self.style_mlp(s) for s in styles] ^^^^^^^^^^^^^^^^^ File "/opt/venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/venv/lib/python3.11/site-packages/torch/nn/modules/container.py", line 217, in forward input = module(input) ^^^^^^^^^^^^^ File "/opt/venv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/venv/lib/python3.11/site-packages/basicsr/archs/stylegan2_arch.py", line 172, in forward out = fused_leaky_relu(out, bias) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/venv/lib/python3.11/site-packages/basicsr/ops/fused_act/fused_act.py", line 95, in fused_leaky_relu return FusedLeakyReLUFunction.apply(input, bias, negative_slope, scale) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/venv/lib/python3.11/site-packages/torch/autograd/function.py", line 506, in apply return super().apply(*args, **kwargs) # type: ignore[misc] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/venv/lib/python3.11/site-packages/basicsr/ops/fused_act/fused_act.py", line 65, in forward out = fused_act_ext.fused_bias_act(input, bias, empty, 3, 0, negative_slope, scale) ^^^^^^^^^^^^^ NameError: name 'fused_act_ext' is not defined
add BASICSR_JIT=True and use python
if you still have this problem, the fix is to either install the extension, which didnt work for me on windows, or use this code workaround with the same functionality: fused_act.py
# modify from https://github.com/rosinality/stylegan2-pytorch/blob/master/op/fused_act.py # noqa:E501
import os
import torch
from torch import nn
from torch.autograd import Function
BASICSR_JIT = os.getenv('BASICSR_JIT')
if BASICSR_JIT == 'True':
from torch.utils.cpp_extension import load
module_path = os.path.dirname(__file__)
fused_act_ext = load(
'fused',
sources=[
os.path.join(module_path, 'src', 'fused_bias_act.cpp'),
os.path.join(module_path, 'src', 'fused_bias_act_kernel.cu'),
],
)
else:
try:
from . import fused_act_ext
except ImportError:
pass
# avoid annoying print output
# print(f'Cannot import deform_conv_ext. Error: {error}. You may need to: \n '
# '1. compile with BASICSR_EXT=True. or\n '
# '2. set BASICSR_JIT=True during running')
class FusedLeakyReLUFunctionBackward(Function):
@staticmethod
def forward(ctx, grad_output, out, negative_slope, scale):
ctx.save_for_backward(out)
ctx.negative_slope = negative_slope
ctx.scale = scale
empty = grad_output.new_empty(0)
grad_input = fused_act_ext.fused_bias_act(grad_output, empty, out, 3, 1, negative_slope, scale)
dim = [0]
if grad_input.ndim > 2:
dim += list(range(2, grad_input.ndim))
grad_bias = grad_input.sum(dim).detach()
return grad_input, grad_bias
@staticmethod
def backward(ctx, gradgrad_input, gradgrad_bias):
out, = ctx.saved_tensors
gradgrad_out = fused_act_ext.fused_bias_act(gradgrad_input, gradgrad_bias, out, 3, 1, ctx.negative_slope,
ctx.scale)
return gradgrad_out, None, None, None
def FusedLeakyReLUFunction(input, bias, negative_slope=0.2, scale=2**0.5):
"""
Fused bias addition and activation (LeakyReLU) in one function.
Mimics the fused operation in CUDA but using PyTorch.
Args:
input (Tensor): The input tensor.
bias (Tensor): The bias term, typically a learned parameter.
negative_slope (float): The negative slope for LeakyReLU.
scale (float): A scaling factor applied to the output after activation.
Returns:
Tensor: The result after bias addition and LeakyReLU activation.
"""
# Bias addition
out = input + bias
# Apply Leaky ReLU activation
out = torch.nn.functional.leaky_relu(out, negative_slope=negative_slope)
# Apply scaling factor (if needed)
out = out * scale
return out
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)) # Shape (channel,)
self.negative_slope = negative_slope
self.scale = scale
# Using standard PyTorch LeakyReLU
self.leaky_relu = nn.LeakyReLU(negative_slope=negative_slope)
def forward(self, input):
# Reshape the bias to match input size
bias = self.bias.view(1, -1, 1, 1) # Shape: (1, channel, 1, 1)
# Apply the bias and LeakyReLU activation
return self.leaky_relu(input + bias)
def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2**0.5):
return FusedLeakyReLUFunction(input, bias, negative_slope, scale)