Uminosachi / sd-webui-inpaint-anything

Inpaint Anything extension performs stable diffusion inpainting on a browser UI using masks from Segment Anything.
Apache License 2.0
1.11k stars 101 forks source link

runnning out of vram #102

Open nlienard opened 1 year ago

nlienard commented 1 year ago

A1111 v1.6 latest inpaint anything version: [4f0d8eba] Model: sam_vit_h_4b8939.pth nvidia driver version: 537.34 Windows 11

When just clicking on "Run Segment Anything" after adding a picture, my vram is fully eaten and it is using shared ram

image

As you can see, i got 12GB VRAM and 32GB shared RAM (i got 64GB Ram) and i don't understand why does it require 46GB VRAM (that i don't have) to do the segmentation.

After 2mn (very long), i got finally the mask

image

VRAM usage decreased at normal:

image

Any guidance would be appreciated.

Thanks

nlienard commented 1 year ago

I downgraded to nvidia 531.79 version and it stops using my ram. now got OOM error.

2023-09-27 10:58:11,061 - Inpaint Anything - ERROR - CUDA out of memory. Tried to allocate 8.72 GiB (GPU 0; 12.00 GiB total capacity; 3.31 GiB already allocated; 6.51 GiB free; 3.40 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation.  See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

I notice it works now on small resolution image up to 1641x2048 but now it takes 1mn.

So at the end, i 've to choose between being able to segment big resolution but very slow OR segment middle resolution image with better speed.

Sj-Si commented 1 year ago

On a 1920x1080 image, the sam_vit_h_4b8939 model used about 16GB of memory total on my computer. This model is the biggest of the three sam_vit models if I remember correctly so it will use more memory than the others. On top of that, it sounds like you're running segmentation on much larger images.

Keep in mind that increasing the resolution of the image will exponentially increase the required memory. For me, an image over 2500x2500 will eat every last bit of my 10GB of VRAM and 64GB of RAM. Then I just have to wait while my computer slowly crashes.

I'm not really sure if there is a nice solution to this. Do you really need to use the full size images for segmentation? If all you're doing is creating masks, then it may be worth while to scale the images down (while maintaining aspect ratio), run segmentation and create your mask, then scale the mask back up before running inpainting or whatever. But even then I imagine you're going to hit even harder limits to your image size when running the inpainting models.