InvokeAI icon indicating copy to clipboard operation
InvokeAI copied to clipboard

[bug]: upscaling not work

Open steto123 opened this issue 2 years ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

OS

Windows

GPU

cuda

VRAM

12GB

What happened?

When i cjhoose upscaloe image there is a error. In the console i can find: local variable 'upsampler' referenced before assignment

Whats the problerm?

Screenshots

No response

Additional context

No response

Contact Details

[email protected]

steto123 avatar Dec 05 '22 17:12 steto123

I had this problem too. Quick fix is to edit file -> .venv\Lib\site-packages\ldm\invoke\restoration\realesrgan.py and change model_path = os.path.join(Globals.root,'models/realesrgan/realesr-general-x4v3.pth') to model_path = os.path.abspath('../../../../models/realesrgan/realesr-general-x4v3.pth') I know it shouldn't be like this, but this is a quick fix. Original code gives me '.\\models/realesrgan/realesr-general-x4v3.pth' <- File not found, what leads to "local variable 'upsampler' referenced before assignment" on my Windows machine.

pdsmike avatar Dec 27 '22 21:12 pdsmike

I have this problem too

Kiaana avatar Feb 09 '23 08:02 Kiaana

I had this problem but I just have figured it out. Download this file then put it into models/realesgran/ folder. It was an empty folder in my case and now upscaling is working without problem. Doesn't need to change any file.

925-Studio avatar Feb 10 '23 09:02 925-Studio

Still exists in 2.3.4.post1 and the model already exists in the folder. Even hardcoding the absolute path in realesrgan.py doesn't fix the issue so I'm assuming it's to do with the way the pipeline is being called.

Traceback (most recent call last):
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/invokeai/backend/invoke_ai_web_server.py", line 826, in handle_run_postprocessing
    image = self.esrgan.process(
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/ldm/invoke/restoration/realesrgan.py", line 73, in process
    output, _ = upsampler.enhance(
UnboundLocalError: local variable 'upsampler' referenced before assignment

Upgraded to main branch on GitHub with pip install --upgrade git+ and issue still exists on 3.0.0+a0.

Traceback (most recent call last):
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/invokeai/backend/web/invoke_ai_web_server.py", line 1313, in generate_images
    self.generate.prompt2image(
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/invokeai/backend/generate.py", line 505, in prompt2image
    results = generator.generate(
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/invokeai/backend/generator/base.py", line 392, in generate
    image_callback(
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/invokeai/backend/web/invoke_ai_web_server.py", line 1167, in image_done
    image = self.esrgan.process(
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/invokeai/backend/restoration/realesrgan.py", line 94, in process
    output, _ = upsampler.enhance(
UnboundLocalError: local variable 'upsampler' referenced before assignment

Tracing this further it looks like it's because of this block.

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    warnings.filterwarnings("ignore", category=UserWarning)

    try:
        upsampler = self.load_esrgan_bg_upsampler(denoise_str)
    except Exception:
        import sys
        import traceback

        print(">> Error loading Real-ESRGAN:", file=sys.stderr)
        print(traceback.format_exc(), file=sys.stderr)

We attempt to initialise the upsampler object but if that fails we don't have it available for subsequent calls. Looking at the logs for this initialisation attempt:

>> Error loading Real-ESRGAN:███████████████████| 50/50 [01:18<00:00,  1.58s/it]
Traceback (most recent call last):
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/invokeai/backend/restoration/realesrgan.py", line 72, in process
    upsampler = self.load_esrgan_bg_upsampler(denoise_str)
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/invokeai/backend/restoration/realesrgan.py", line 46, in load_esrgan_bg_upsampler
    bg_upsampler = RealESRGANer(
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/realesrgan/utils.py", line 57, in __init__
    loadnet = self.dni(model_path[0], model_path[1], dni_weight)
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/realesrgan/utils.py", line 83, in dni
    net_b = torch.load(net_b, map_location=torch.device(loc))
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/torch/serialization.py", line 771, in load
    with _open_file_like(f, 'rb') as opened_file:
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/torch/serialization.py", line 270, in _open_file_like
    return _open_file(name_or_buffer, mode)
  File "/Users/darth/.pyenv/versions/3.10.9/lib/python3.10/site-packages/torch/serialization.py", line 251, in __init__
    super(_open_file, self).__init__(open(name, mode))
FileNotFoundError: [Errno 2] No such file or directory: '/Users/darth/invokeai/models/realesrgan/realesr-general-wdn-x4v3.pth'

If I look at that last line it looks as though we're attempting to load a file realesr-general-wdn-x4v3.pth whereas mine locally is as per above realesr-general-x4v3.pth. Looking at the releases there are actually two files for ESRGAN. Downloading the second (missing) one and putting into the same folder fixes the issue. Not sure when this regression was caused but clearly at some stage we've only used one of them. I was upgrading from 3.4.0 originally when this started to occur.

TLDR

  • Go to ESRGAN;
  • Download both realesr-general-wdn-x4v3.pth and realesr-general-x4v3.pth; and
  • Put them into $INVOKE_ROOT/models/realesrgan/

darth-veitcher avatar Apr 16 '23 07:04 darth-veitcher