TencentARC / GFPGAN

GFPGAN aims at developing Practical Algorithms for Real-world Face Restoration.
Other
35.47k stars 5.88k forks source link

Possible to use Real-ESRGAN on mac os AMD, and 16-bit images? #95

Open KeygenLLC opened 2 years ago

KeygenLLC commented 2 years ago

Faces work excellent with GFPGAN, it's pretty amazing, but without CUDA support, the backgrounds are left pretty nasty. Still amazed at how fast it runs on CPU (only a few seconds per image, even with scale of 8), granted it's using the non-color version, but I have colorizing software and do the rest manually. I've been unsuccessful trying to enable color using the Paper model and forcing CPU.

More importantly than color, as I use other software for colorization, is the background. I need to get the backgrounds restored and not just faces.

I've tried setting --bg_upsampler realesrgan flag, and it does not throw an error, but this seems to have no effect on the output image. I do get the warning though that Real-ESRGAN is slow and not used for CPU. Is it possible to enable Real-ESRGAN on macOS so that it uses AMD GPU and restores the background (I have a desktop with Pro Vega 64)? I saw the other Real-ESRGAN compiled for mac/AMD, maybe the two can be linked somehow?

If it can't use the AMD GPU, can it be forced to use the CPU? I don't care if it's slow, I just need it to work. :) I do a lot of rendering that is slow, because sometimes it's the only way. Main thing is getting it to work.

Also, is it possible to enable the use of 16-bit PNG, TIFF, or cinema DNG? Would be really cool if it could support 32-bit float TIFF or EXR.

Thank you

KeygenLLC commented 2 years ago

I managed to figure how to force the CPU to be used if you don't have a CUDA card. There may be a better way, but I am getting results. It might take a minute or a few minutes with larger images, but it's not so slow that it is unusable. Still rather fast imo.

You need to modify the code in the inference_gfpgan.py file to make sure RealESRGANer is used instead of throwing a warning.

Change this code:

    # background upsampler
    if args.bg_upsampler == 'realesrgan':
        if not torch.cuda.is_available():  # CPU
            import warnings
            warnings.warn('The unoptimized RealESRGAN is very slow on CPU. We do not use it. '
                          'If you really want to use it, please modify the corresponding codes.')
            bg_upsampler = None
        else:
            from realesrgan import RealESRGANer
            bg_upsampler = RealESRGANer(
                scale=2,
                model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
                tile=args.bg_tile,
                tile_pad=10,
                pre_pad=0,
                half=True)  # need to set False in CPU mode
    else:
        bg_upsampler = None

To this code:

    # background upsampler
    if args.bg_upsampler == 'realesrgan':
        from realesrgan import RealESRGANer
        bg_upsampler = RealESRGANer(
            scale=2,
            model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
            tile=args.bg_tile,
            tile_pad=10,
            pre_pad=0,
            half=False)  # need to set False in CPU mode
    else:
        bg_upsampler = None

When you run the script it will now show you Tiles being rendered instead of just the file names. There will be more tiles with larger images.

If anyone can shed light on how to get this working with an AMD GPU on the mac side, I would appreciate it, as well using images with higher bit depth (16-bit, 32-bit per channel).