Attempt for a x2 image upscaler, using CNN

Very nice! Two things I find interesting:

  • I’m surprised it can do so well with only luminance. When testing patch-matching based scaling, I found that increasing ~saturation of the input improves matches significantly: mix_channels (3,-1,-1;-1,3,-1;-1,-1,3)
  • PSNR was the main measurement used to tweak the solver upscale, so that’s likely why it does quite well on that. It’s a bit of a cheat in a way, because the upscaled image is error corrected vs the original at the end. I wonder if you could use the same trick…
    # error correction: ensure downscaled ~= original
    k[small,target]
    +ri[target] [small],2 sub. [small]
    ri. [target] sub[target,-1] rm[small]
    k[target]
2 Likes

Hi
Not for nothing but I did an update of GMIC the other day and immediately afterwards the CNN scaller stopped working.
This is the error I see when I try to use it:

*** Error in ./fx_scale2x_cnn/scale2x_cnn/ *** Command ‘input’: [instance(1,1,100,1,00000000030ff8b0,non-shared)] gmic::[gmic_math_parser] gmic<
float32>::fill(): When substituting function ‘nn_add_forward()’: Operator ‘+’: Right-hand operand has invalid type ‘vector262144’ (should be ‘scalar’ or ‘vector
65536’), in expression ‘( pB3 = UP_b + C3_2’.

Which to me is a bit too confusing to figure out at this point.

Am I the only one that gets this?

Oh and I also get another error when I try it in the GUI version, the error there is related to the preview, but I assume it all comes down to the error I posted above.

There is an error with version 3.4.4-pre 20241120 but not with version 3.4.3
To avoid errors, set the cursor ‘Sharpness’ to 0

Thank you for replying, and indeed with sharpness at 0 it works.

But just for clarity I am using 3.4.3 but I guess that definition update I pulled using the GUI’s version update button must have added some stuff meant for 3.4.4 final then?
So there is hope, and it’s not something on my system then, so I can just wait and all should be well with a future update I’m guessing.

Well, my fault.
Sharpness=0 is actually useless, as is corresponds to the classical bilinear filter.

I’m currently reimplementing/cleaning a lot of stuffs in my neural network library, and it breaks almost everything, but I forgot to disable the filter update mechanism :frowning:

Trying to fix this ASAP, sorry for the inconvenience.

No worries, it’s not the end of the world and I for me am just happy I don’t have to look for some mess-up on my system.
And yeah sharpness 0 defeats things anyway, I use the filter because of the (relatively clean) sharpness, and I expect anybody that uses it does so for the same reason.
In fact I prefer a setting of between 1.5 and 2.00 over the default 1.25.

It should be fixed right now for G’MIC 3.4.3.
Could you please try to update your filters and tell me if that works OK now ?
Thanks!

Yes indeed, all is working well again with the CNN scaler
Thanks for the rapid response and fix.
It’s quite unique to see things being fixed this quick in the modern world :slight_smile:

There are a few other filters that crashed the GUI version I noticed while I was looking to see if there was a filter for something I was musing on, and that too seem to be fixed and much better now.

1 Like

Minor typo with Defaut.

#@cli scale2x_cnn : _sharpness>=0
#@cli : Double XY-size of selected images, using a convolutional neural network.
#@cli : Defau*t value: 'sharpness=1.25'.

Not just the edges (unnaturally sharpened). The more an area is non-edge, the more it is unnaturally smoothed (the gradient or texture is uncanny).

That is why I was curious how the CNN upscaler would perform under heavy noise, where edges, textures and gradients are not as well-defined.

Unnaturalness is small quibble on an overall great effect. I would say that although GCD Solver sometimes has a better PSNR, CNN is perceptually better in all cases. Of course, PSNR is just one of many ways to measure quality.

Talking of noise and upscaling, I noticed that the CLI version has no help at all for gcd_upscale_noise (at the moment) and a parse_cli print does not find existence of it, yet it is still there to be used.
I say at the moment because since I have a script that uses its 2 arguments it must have been there at some point, or maybe it was in a forum post?
Anyway, if you are looking at various upscalers you can give it a try.

I’m digging up this post, because I’ve implemented this trick in command scale2x_cnn, so that the error between the /2 downscaled version of the x2 upscaled result image and the original image becomes minimal.

It seems to work very well, thanks for the suggestion!

2 Likes

Trying to test this out with the client. Can you tell me how it’s used?
Also, it there a chance for gpu acceleration?

gmic i test.jpg scale2x_cnn o out.jpg
[gmic]./ Start G’MIC interpreter (v.3.5.0).
[gmic]./ Input file ‘test.jpg’ at position 0 (1 image 1748x1056x1x3).
[gmic] *** Error in ./scale2x_cnn/ *** Command ‘check’: Invalid argument ‘o>=0’: Unrecognized item ‘o’ in expression ‘o>=0’.
[gmic] Command ‘check’ has the following description:

check (+):
condition

Evaluate specified condition and display an error message if evaluated to false.

No, there isn’t. I wish we can integrate other languages with G’MIC easier, but as far as I know, there is no socket support, and there is no clipboard support. So, you’d have to save to disk, and I prefer to keep it all in memory. If any one has a suggestion on the G’MIC end of things, that’d be great. That would allow for GPU acceleration by sending the job to a language that supports GPU.

$ gmic h scale2x_cnn

  scale2x_cnn:
      _sharpness>=0

    Double XY-size of selected images, using a convolutional neural network.

    Default value: 'sharpness=1.25'.

    Example:
      [#1] image.jpg rescale2d 128 +scale2x_cnn ,

So command scale2x_cnn requires an argument:

$ gmic test.jpg -scale2x_cnn 1.25 -o out.jpg