hoffstadt / DearPyGui

Dear PyGui: A fast and powerful Graphical User Interface Toolkit for Python with minimal dependencies
https://dearpygui.readthedocs.io/en/latest/
MIT License
13.22k stars 688 forks source link

Very high GPU and RAM use when displaying image series #1796

Open keck-in-space opened 2 years ago

keck-in-space commented 2 years ago

Version of Dear PyGui

Version: 1.6.2 Operating System: Windows 10

My Issue/Question

I have noticed that when I display a texture the GPU usage in Task Manager is very high. I am displaying two textures (two PNG images of about 4MB each, though this also occurs with jpgs), and my GPU usage is about 90% (using a NVIDIA T1200 Laptop GPU).

The weirdest part is that if I am actively loading in new images, the GPU usage drops down to about 40%, but as soon as I stop loading new images in, the GPU goes to 90%.

Also, the frame-rate tanks to close to 20 FPS after the images have been loaded.

To Reproduce

Steps to reproduce the behavior:

  1. Create a DPG plot
  2. Add a texture to the texture registry
  3. Add the texture as an image series
  4. Show the image series
  5. Note the high GPU usage

Expected behavior

DPG GPU usage would be high during loading and initial display, but lower after the image has been loaded.

Screenshots/Video

image

You can see before clicking load images, after, and then after the application is closed in this small graph.

image

Standalone, minimal, complete and verifiable example

Here's the image I used. I renamed it to nasa.jpg.

# Here's some code anyone can copy and paste to reproduce your issue
import dearpygui.dearpygui as dpg

dpg.create_context()
dpg.create_viewport(title='Custom Title', width=1600, height=1000)

with dpg.window(label="View One", tag="view_one", min_size=(int(dpg.get_viewport_width()), dpg.get_viewport_height())):
    dpg.add_plot(
        tag=f"curr_image_plot_1",
        height=-1,
        width=-1,
        crosshairs=True,
        query=True,
        equal_aspects=True,
    )
    dpg.add_plot_axis(label="Width",
                      axis=1,
                      tag=f"curr_image_plot_x_axis_1",
                      no_gridlines=False,
                      lock_min=False,
                      parent=f"curr_image_plot_1")
    dpg.add_plot_axis(label="Height",
                      axis=2,
                      tag=f"curr_image_plot_y_axis_1",
                      no_gridlines=False,
                      lock_min=False,
                      parent=f"curr_image_plot_1")

def load_images():
    width, height, _, data = dpg.load_image("nasa.jpg")
    with dpg.texture_registry():
        dpg.add_raw_texture(width=width, height=height, default_value=data, tag=f"test_texture")
    dpg.add_image_series(f"test_texture", [0, 0], [width, height], parent=f"curr_image_plot_y_axis_1")
    dpg.fit_axis_data(f"curr_image_plot_x_axis_1")
    dpg.fit_axis_data(f"curr_image_plot_y_axis_1")
    dpg.show_metrics()
    dpg.hide_item("win_controls")

with dpg.window(label="Controls", autosize=True, tag="win_controls"):
    dpg.add_button(label="Load Images", callback=load_images)

dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
dpg.destroy_context()
keck-in-space commented 2 years ago

Another issue I noticed with this is that the RAM usage is very high at over 2GB to display this image. I do not know where that RAM requirement could be coming from.

image

perhaps related to #1109

krister-ts commented 2 years ago

I had an issue opening my images at all so I used pillow and numpy to load them:

def add_static_image(filename, width, height, *args, **kwargs):
    with importlib.resources.path(images, filename) as img_file:
        try:
            width, height, _, data = dpg.load_image(img_file)
        except SystemError:
            warn(
                "DearPyGUI load_image failed. Trying PIL Image.open...",
                stacklevel=2,
            )
            image = Image.open(img_file).resize((width, height))
            # width, height = image.size
            data = np.frombuffer(image.tobytes(), dtype=np.uint8) / 255.0
        dpg.add_static_texture(
            width,
            height,
            data,
            *args,
            # format=dpg.mvFormat_Float_rgba,
            **kwargs,
        )

Try this instead of dpg.load_image:

image = Image.open(img_file).resize((width, height))
width, height = image.size
data = np.frombuffer(image.tobytes(), dtype=np.uint8) / 255.0
hoffstadt commented 2 years ago

@keck-in-space I saw you work at NASA. I’m now working at NASA (JSC) as a contractor. Are you on the DPG discord channel?

hoffstadt commented 2 years ago

@krister-ts and this fixed the ram issue?

krister-ts commented 2 years ago

@krister-ts and this fixed the ram issue?

Never actually had dpg work for my images...

EDIT: I mean the load_image function from DearPyGui to be more clear

keck-in-space commented 2 years ago

@hoffstadt Awesome! Welcome to NASA! I am not on the Discord channel... I don't use Discord for privacy reasons since it is closed source owned by Tencent. Just my opinion lol.