-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Large Images Load Very Slowly from Examples or Outputs #2635
Comments
Thanks @JohannesAck will take a look next week! |
Same problem. It is very slow when the image resolution is high. |
Hi @JohannesAck the gist you provided no longer is a valid URL. Would you mind providing another example we can use to reproduce please? |
Hi @abidlabs , Here's also the code (after installing gradio in a previous cell), in case my gist gets lost again:
As you will see, the function finishes execution after after one or two seconds, but it takes another 30-40 seconds for the image to display. |
Thanks @JohannesAck for providing the code example. I can confirm that I can reproduce the problem (though the total inference time on my machine is about 20 seconds). I looked into this and the reason that this is happening is because we convert the PIL image to a byte format here in order to serialize it into base64 format so that it can be read by a browser, and this process is very slow. There are faster ways to convert PIL images to byte formats, but they rely on 3rd party libraries, such as However, if you would like, you could monkey patch your local installation of import numpy as np
import cv2
from cv2 import imencode
import base64
def encode_pil_to_base64_new(pil_image):
print("using new encoding method")
image_arr = np.asarray(pil_image)[:,:,::-1]
_, byte_data = imencode('.png', image_arr)
base64_data = base64.b64encode(byte_data)
base64_string_opencv = base64_data.decode("utf-8")
return "data:image/png;base64," + base64_string_opencv And then redefine: import gradio as gr
gr.processing_utils.encode_pil_to_base64 = encode_pil_to_base64_new If you then launch your gradio interface, it should use the faster processing method. On my machine, this brought down the total processing time from ~19 seconds to ~5 seconds. Finally, I'll mention that we will consider removing serialization for images altogether when we release Gradio 4.0 (just created an issue to track this: #3158), which will hopefully make this point moot. I'll go ahead and close this issue since there isn't anything else we can do here for now. |
Thanks for looking into this and providing the monkey patch. |
The
Hmm probably the base64 encoders take much longer to transmit through your network than when you run things locally. Agreed that having a way to transmit Images as files instead of base64 would be the solution, which we'll tackle as part of #3158
Not sure in your specific case, but in general, the smaller the sizes of the images, the smaller the base64 encoding, which means they should load faster.
Fast reverse proxy, https://github.com/fatedier/frp |
Is it possible to use some sort of 'double-buffering' to have two images and hide the visibility of the one that's currently getting redrawn? I didn't see a function that would tell me when the pic is ready though |
@abidlabs Is there a way to use JPEG encoding instead of base64? Jpeg is smaller than base64. Where I should place this function?
|
Hi @betterze if you upgrade to Gradio 4.x, we don't use base64 anymore. Images are uploaded, which generally should be much faster. |
Describe the bug
Hi, I'm trying to build a demo for a high-resolution image editing approach using gradio currently.
Overall it works well, however loading large images from the examples or outputs is very slow for some reason.
For example, loading a 4896 x 3264 takes 30 seconds when loading it from
gr.Examples
and almost a minute from the output of a function.When using the "upload" function of
gr.Image
instead, it is almost instantaneous.Is there an existing issue for this?
Reproduction
I wrote a small colab that reproduces the issue but for downloads and examples
https://gist.github.com/JohannesAck/802af4af6a89d6a273b6732cb3ce7629
Screenshot
Logs
System Info
Severity
serious, but I can work around it
The text was updated successfully, but these errors were encountered: