image-super-resolution icon indicating copy to clipboard operation
image-super-resolution copied to clipboard

Inference of large images

Open ontheway16 opened this issue 5 years ago • 16 comments

I think it would be a great feature to have some option for prediction of larger images, by applying sr to tiles of the main image and stitching the results, this is after enabling gpu inference of course. Thanks!

ontheway16 avatar Apr 01 '19 18:04 ontheway16

Unfortunately this does not work. https://colab.research.google.com/drive/1L8Q6_MkTqBDL3NCYi6bAdGq_lq7I_vs-

cfrancesco avatar Apr 02 '19 09:04 cfrancesco

@cfrancesco Do you mean by calculation time gains? Actually, my concern was on ability to run inference on images larger then GPU memory, I am not expecting a speed gain, but ability to use GPU accelerated functionality on very large images (40-50 megapixels).

ontheway16 avatar Apr 02 '19 10:04 ontheway16

@ontheway16 sorry, I misunderstood. It's a very valid point - I added it in the todo list. Thanks!

cfrancesco avatar Apr 02 '19 12:04 cfrancesco

seems it can change the backbone to mobilenet to reduce the parameters of network. this change can make the large image inference more easier.

whitelok avatar Apr 04 '19 06:04 whitelok

@whitelok If it will come without an accuracy drop, then might be. But speed is not ciritical, speaking for myself. it can take minutes, instead of seconds, if better accuracy obtained.

ontheway16 avatar Apr 04 '19 06:04 ontheway16

I tested similar things with CycleGAN and pix2pix (different use, but same theory). The algorithms don’t care about how images are processed before and after they are executed, but there’s the issue of consistency between the tiles and at the borders of the tiles, I did not know how to manage that, as the content of each tile changes what the model detects

victorca25 avatar Apr 04 '19 09:04 victorca25

@victorca25 do you mean seam lines occur ? I made some tests in another project, named EDSR, and there was no problem with their solution (as a feature its already available in there; --chop parameter)

ontheway16 avatar Apr 04 '19 09:04 ontheway16

@ontheway16 in my case it did, because cycleGAN’s results are not necessarily consistent between two pieces of the same image, but maybe the SR is much more consistent and this won’t happen?

victorca25 avatar Apr 04 '19 13:04 victorca25

@victorca25 In my tests joints were seamless, so you may be right, some models might be more suitable for this.

ontheway16 avatar Apr 04 '19 16:04 ontheway16

Just in case it's useful, this simple script handles the cropping of images: https://github.com/xinntao/BasicSR/blob/master/codes/scripts/extract_subimgs_single.py

victorca25 avatar Apr 10 '19 09:04 victorca25

@ontheway16 Any suggestions on which architectures lead to more seamless results? My process here definitely has artefacts along the tiling lines

https://colab.research.google.com/drive/1pwSlxnbmKYHxwA-s6mf1aMFl8saWc6Ii

Fullsize output is too big to display in Colab without crashing it! in this example my tiles were 400x300 which is smaller than necessary - but the same thing happens no matter how big they are - I just haven't figured out how to calculate the max size I can fit in memory at one time. https://i.imgur.com/8eWduaj.jpg

jemgold avatar Apr 16 '19 23:04 jemgold

The seams issue can easily be solved by padding. I worked around this exact problem for Prosr image upscaling here: https://github.com/fperazzi/proSR/issues/21

This approach should be easy to apply here as well. I'll probably go and do it myself if I find time, but in the meantime anyone can feel free to do anything they want with my code.

Shadetail avatar May 01 '19 18:05 Shadetail

@jongold, had no seam lines with edsr, although it was some other code. Made no seam tests with this one.

ontheway16 avatar May 01 '19 20:05 ontheway16

The seams issue can easily be solved by padding. I worked around this exact problem for Prosr image upscaling here: fperazzi/proSR#21

This approach should be easy to apply here as well. I'll probably go and do it myself if I find time, but in the meantime anyone can feel free to do anything they want with my code.

Cool idea @Shadetail . Atm I'm working on a model variant that should allow faster inference and could not require special treatment for large images. Regardless, in two weeks I'll be back from conferences and vacation and I'll implement an option for larger images with padding.

Obviously, feel free to create a PR. I would say it should be implemented as an option in the predict function of the ImageModel class.

Thanks for you contribution.

cfrancesco avatar May 04 '19 11:05 cfrancesco

Hi, I added a first (fairly inefficient) implementation of an option in the model predict function to process the image by patch. The usage is the following, using for instance a RRDN model

model = RRDN(arch_params={'C':4, 'D':3, 'G':32, 'G0':32, 'T':10, 'x':4})
model.model.load_weights(weights)
sr_img = model.predict(image, by_patch_of_size=30)

Note, this is not a Keras function, so you need to use the predict function from ISR ImageModel class, NOT the predict function that belongs to the Keras model, so sr_img = model.predict(image, by_patch_of_size=30) instead of sr_img = model.model.predict(image, by_patch_of_size=30)

By default by_patch_of_size is None. If it's not none, than the model will try to compute by patch. Padding can also be specified (by default is 2) and it's the padding for each of the patches. Batch size (how many patches are inferred in one go) is also a parameter (default is 10). It has no tests yet and it's in the dev branch for now, but it should work fine. Let me know if you find any problem with it.

cfrancesco avatar May 30 '19 12:05 cfrancesco

Hi, is it possible to port the patch prediction model to tensorflowjs? If yes, how would it work for inference? I ported the normal model over to js, and used it with success, although even on a gtx 1070 8gb of ram i can't go from 128x128 to 512x512. I can do it with CPU but it takes around 1 hour!!! This option would be a great place to start!

talvasconcelos avatar Aug 29 '19 12:08 talvasconcelos