TransparentLC / realesrgan-gui

实用、美观的 Real-ESRGAN 图形界面,同时支持 Windows、Ubuntu 和 macOS 平台。现在也支持 Real-CUGAN 了!(Cross-platform GUI for image upscaler Real-ESRGAN with additional features. Now with Real-CUGAN support!)
http://akarin.dev/realesrgan-gui/
GNU Affero General Public License v3.0
767 stars 44 forks source link

It is recommended to add support for realcugan super-resolution models #76

Closed hyrulelinks closed 5 months ago

hyrulelinks commented 5 months ago

https://github.com/nihui/realcugan-ncnn-vulkan

After comparative testing, the sharpness of real-esrgan is higher than that of real-cugan after enlargement, but the details are very badly smeared, especially for images with text, if the original image is not clear enough, real-esrgan may distort the text content. Another difference is that the real-esrgan enlarged image is "brighter" than the original image, that is, the color becomes lighter, which is particularly noticeable when compared to the original, while the color of the real-cugan enlarged image is the same as the original image.

In general, Real-Esrgan is more aggressive than Real-Cugan, but the smearing is also more severe, Real-Cugan is relatively more beneficial to the magnification effect of low-definition original images, and Real-Escan is more suitable for high-definition image magnification

This is original

截屏2024-04-29 10 30 17

Below are esrgan and cugan(The shadow above cugan is the border shadow effect of the MacOS Preview App window, because the windows overlap for easy screenshot and are pressed on top of the lower image. It is not caused by cugan zooming in.)

截屏2024-04-29 10 25 20

If directly put the real-cugan model file into the models folder of the project, it will report an error, because the -n parameter of real-cugan corresponds to the denoise level, and the -n parameter of real-esrgan corresponds to models

real-cugan models comparison models-pro (for illustrations: better performance than models-se) models-se (for illustrations: many variations of scaling and denoising) model-nose (for illustrations: thin edge looks)

A more effective approach is to first use cugan to enlarge the image and reduce the size of the image to a suitable size, and then enlarge it again with esrgan, so that the output image retains enough details while ensuring sufficient sharpness.

This will increase the application size by about 70MB, but the zoom effect and wider image type zoom support are worth this degree of volume increase.

I hope the author can add real-cugan support, thanks

@TransparentLC

TransparentLC commented 5 months ago

CONTRIBUTING.md

... Therefore I have no plans to support other upscalers.

Close as not planned ... Only joking.


https://github.com/nihui/realcugan-ncnn-vulkan/blob/395302c5c70f1bff604c974e92e0a87e45c9f9ee/src/main.cpp#L733

Real-CUGAN uses the upscale ratio and denoise level other than the model name to determine which model file to use. I have written a couple of lines to adapt this.

I have no plans to add a new artifact with Real-CUGAN to the action workflow for now, but a guide could be added to the readme.

To use Real-CUGAN:

  1. Place Real-CUGAN models (models-{nose,pro,se}) in the model dir.
  2. Set upscaler and modeldir in the config.ini.

Feel free to try it out.

hyrulelinks commented 5 months ago
  • Place Real-CUGAN models (models-{nose,pro,se}) in the model dir.
  • Set upscaler and modeldir in the config.ini.

I set it up in the config.ini as follows

[Config]
upscaler = realcugan-ncnn-vulkan
modeldir = models-se

Then run the source code project, a window pops up with a message saying "Real-ESRGAN-ncnn-vulkan" not found.

Do I need to rename realcugan-ncnn-vulkan to Real-ESRGAN-ncnn-vulkan?

@TransparentLC

TransparentLC commented 5 months ago

No need to rename. If you rename it, the GUI will not spawn the process in the Real-CUGAN way.

upscaler is the executable's path. Don't forget to add the .exe extension if you are using Windows.

modeldir = models

models
├─models-nose
├─models-pro
├─models-se
└─...
hyrulelinks commented 5 months ago

The current settings are as follows

[Config]
upscaler = realcugan-ncnn-vulkan
modeldir =

The directory tree of executables and models is as follows

❯ tree . -L 2
.
├── models
│ ├── models-nose
│ ├── models-pro
│ ├── models-se
├── realcugan-ncnn-vulkan
├── realesrgan-ncnn-vulkan
├── upscayl-bin
...

Now can choose the model for cugan.

Screenshot 2024-04-30 10:43:58

But after clicking start, the error message output is as follows:

Using executable: realcugan-ncnn-vulkan
Traceback (most recent call last):
File "/Users/hyrulelinks/Downloads/realesrgan-gui/task.py", line 348, in taskRunner
queue.popleft().run()
File "/Users/hyrulelinks/Downloads/realesrgan-gui/task.py", line 114, in run
with subprocess.Popen(
^^^^^^^^^^^^^^^^^
File "/Users/hyrulelinks/.pyenv/versions/3.12.2/lib/python3.12/subprocess.py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Users/hyrulelinks/.pyenv/versions/3.12.2/lib/python3.12/subprocess.py", line 1953, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'realcugan-ncnn-vulkan'

Tried to edit define.py but not works

for executableName in (
    'upscayl-bin',
    'realesrgan-ncnn-vulkan',
    'realcugan-ncnn-vulkan',

Where should I place realcugan-ncnn-vulkan to be found? Is it also possible to add an option to select esrgan or cugan in the settings interface? If the executable program selected for esrgan or cugan does not exist or is not found, it will prompt to download the executable program and model to the correct project path?

thanks! @TransparentLC

TransparentLC commented 5 months ago

I forget to test the relative path case ... Now it should works.

Switching can be a bit complicated to do and is not planned.

hyrulelinks commented 5 months ago

I forget to test the relative path case ... Now it should works.

Switching can be a bit complicated to do and is not planned.

Now it can be run, thank you! Then this is some of my own thoughts and ideas, just for discussion and learning. I rolled back the project commit version to the last version before compatible with cugan, and then I want to make the following modifications:

  1. Rename the model files of cugan (bin and param) to something like cugan-pro-x3-denoise, cugan-se-x4-no-denoise, in a similar format, then directly place them in the models folder together with esrgan's model files.

  2. Place realcugan-ncnn-vulkan and upscayl-bin (or realesrgan-ncnn-vulkan) in the project root directory.

  3. By modifying task.py (or maybe more files to be modfied if required), when the selected model file name contains 'cugan', define.RE_PATH points to realcugan-ncnn-vulkan; otherwise all point to upscayl-bin (or realesrgan-ncnn-vulkan).

In this way, I think there is no need to modify the interface of the application program so that executable file switching can be achieved. I don't know if this can be implemented or not. If it can be done, how specifically?

There is an issue that I don’t understand: RE_PATH obtains the path of an executable file; when realcugan-ncnn-vulkan, upscayl-bin or realesrgan-ncnn-vulkan all exist at once, how does one distinguish which executable file path is needed? My idea is to differentiate using RESR_PATH and RECU_PATH or similiar, but it seems like there are many places that need modification which may lead to more problems.

I tried below but not works

if 'cugan' in self.config.model:
    executableNames = [
        'realcugan-ncnn-vulkan',
    ]
else:
    executableNames = [
        'upscayl-bin',
        'realesrgan-ncnn-vulkan',
    ]

for executableName in executableNames:
    RE_PATH = os.path.join(APP_PATH, executableName + ('.exe' if os.name == 'nt' else ''))
    if os.path.exists(RE_PATH):
        break
for i in range(len(files) - 1):
    inputPath, outputPath = files[i:(i + 2)]
    alphaOverridePath = None
    if 'cugan' in self.config.model:
        model, modelFilename = self.config.model.split('#', 1)
        denoiseLevel = {
            'conservative': -1,
            'no-denoise': 0,
            **{f'denoise{i}x': i for i in range(1, 4)},
        }[modelFilename.split('-', 1)[1]]
        cmd = (
            define.RE_PATH,
            '-v',
            '-i', inputPath,
            '-o', outputPath,
            '-s', str(self.config.modelFactor),
            '-t', str(self.config.tileSize),
            '-m', os.path.join(self.config.modelDir, model),
            '-n', str(denoiseLevel),
            '-g', 'auto' if self.config.gpuID < 0 else str(self.config.gpuID),
            '-c', '1', # accurate sync
            *(('-x', ) if self.config.useTTA else ()),
        )
    else:
        cmd = (
            define.RE_PATH,
            '-v',
            '-i', inputPath,
            '-o', outputPath,
            '-s', str(self.config.modelFactor),
            '-t', str(self.config.tileSize),
            '-n', self.config.model,
            '-g', 'auto' if self.config.gpuID < 0 else str(self.config.gpuID),
            *(('-x', ) if self.config.useTTA else ()),
        )

If you have time please provide some help on this matter,thank you! @TransparentLC

TransparentLC commented 5 months ago

Rename the model files of cugan ...

You can't. They hardcoded the filename format. Unless you want to change these lines and recompile it yourself.

sprintf(parampath, "%s/up%dx-denoise%dx.param", model.c_str(), scale, noise);
sprintf(modelpath, "%s/up%dx-denoise%dx.bin", model.c_str(), scale, noise);

How does one distinguish which executable file path is needed?

I have never considered that. upscayl-bin is an exception because it is an actively maintained fork and a drop-in replacement of realesrgan-ncnn-vulkan so I think it is necessary to add support and it will be used in priority.

My idea is to differentiate using RESR_PATH and RECU_PATH or similiar, but it seems like there are many places that need modification which may lead to more problems.

This is the reason. Is anyone really needs to use different upscalers at the same time? If you find Real-ESRGAN or Real-CUGAN is better, set it up in the config.ini, then you don't need to care about it for a long time.

hyrulelinks commented 5 months ago

You can't. They hardcoded the filename format. Unless you want to change these lines and recompile it yourself.

Quick and very valuable response.

It is obvious that modifying the source code of the cugan project has far exceeded my abilities. Continuing to modify the judgment conditions for selecting executable files also does not match the time and effort invested with the benefits.

Thank you for your help.

hyrulelinks commented 5 months ago

There is another question I would like to ask, when running the source code, it prompts:

realesrgan-gui/main.py:669: DeprecationWarning: 'locale.getdefaultlocale' is deprecated and slated for removal in Python 3.15. Use setlocale(), getencoding() and getlocale() instead.
  'AppLanguage': locale.getdefaultlocale()[0],

Does this need to be fixed? Python 3.15 is mentioned in the prompt, it should be a long time later, is it necessary to fix it now?

@TransparentLC

TransparentLC commented 5 months ago

I have no idea. Maybe wait until 3.14 comes out?

locale.getdefaultlocale()
# ('zh_CN', 'cp936')
locale.getencoding()
# 'cp936'
locale.getlocale()
# ('Chinese (Simplified)_China', '936')

I need the BCP 47/RFC 1766 language tag like zh_CN. But these alternatives don't offer this.

https://github.com/python/cpython/issues/90817#issuecomment-1345455713

2-www commented 3 months ago

how? getlocale should return the tag

>>> locale.getlocale()
('uk_UA', 'UTF-8')
TransparentLC commented 3 months ago

@2-www The language tag is returned on Linux, but not on Windows.

Benson1997 commented 1 month ago

早安,透明老大🤞

老问题了,congfig.ini按下面两种形式都设置过了。但是打开 realesrgan-gui.exe(20240602的最新版) 时,还是报错了。 (直接使用命令行是可以运行的,说明文件都是完整的。)

upscaler = D:\App\myTools\realcugan\realcugan-ncnn-vulkan.exe

upscaler = realcugan-ncnn-vulkan.exe

图片

请问应该如何设置,如果可以的话,ini的设置可以截下图吗?😘

期待回复,感谢!🤞

TransparentLC commented 1 month ago

@Benson1997 因为你还没有设置模型路径。

[Config]
upscaler = /path/to/realcugan-ncnn-vulkan/realcugan-ncnn-vulkan.exe
modeldir = /path/to/realcugan-ncnn-vulkan/models
...
Benson1997 commented 1 month ago

@Benson1997 因为你还没有设置模型路径。

[Config]
upscaler = /path/to/realcugan-ncnn-vulkan/realcugan-ncnn-vulkan.exe
modeldir = /path/to/realcugan-ncnn-vulkan/models
...

原来以为会默认到models下,所以没加上models的路径 但是现在加上了,还是不行呢,不应该啊 😂

图片

TransparentLC commented 1 month ago

@Benson1997 这个 models 里面应该是 Real-CUGAN 的三个模型文件夹(models-nose、models-pro、models-se),用 Real-CUGAN 的话是不能使用/不会识别 Real-ESRGAN 的模型的。

Benson1997 commented 1 month ago

@Benson1997 这个 models 里面应该是 Real-CUGAN 的三个模型文件夹(models-nose、models-pro、models-se),用 Real-CUGAN 的话是不能使用/不会识别 Real-ESRGAN 的模型的。

可以了,非常感谢🤞