plemeri / transparent-background

This is a background removing tool powered by InSPyReNet (ACCV 2022)
MIT License
795 stars 87 forks source link

any way to reduce the Cuda usage when using transparent_background? #39

Closed wilfrediscoming closed 10 months ago

wilfrediscoming commented 1 year ago

I sometimes get CUDA out of memory when processing larger images. Is there any way to reduce the Cuda usage when using transparent_background?

or I should simply reduce the image size before using remove()

plemeri commented 1 year ago

Hi, @wilfrediscoming, can you tell me your machine's specifications? GPU memory size any everything.

gef12 commented 1 year ago

use option --jit e case you get a VRAM error, use these already compiled models

add in config.yaml

base-cuda-pt: url: "https://drive.google.com/file/d/1-3zbdYKuXlHlh-cj1UJfem8mcRKBlP1d/view?usp=drive_link" md5: NULL ckpt_name: "ckpt_base_cuda.pt" http_proxy: NULL base_size: [1024, 1024]

fast-cuda-pt: url: "https://drive.google.com/file/d/1OlruRBHEFjRUoM5PwdnWnnAAuLXG35fU/view?usp=drive_link" md5: NULL ckpt_name: "ckpt_fast_cuda.pt" http_proxy: NULL base_size: [384, 384]

base-nightly-cuda-pt: url: "https://drive.google.com/file/d/1-UkSRe8RpT1iblo6orbklEzX5BnBOY15/view?usp=drive_link" md5: NULL ckpt_name: "ckpt_base_nightly_cuda.pt" http_proxy: NULL base_size: [1024, 1024]

wilfrediscoming commented 1 year ago

Hi, @wilfrediscoming, can you tell me your machine's specifications? GPU memory size any everything.

I am using a T4 GPU.. but I am using other stable diffusion model too, so the GPU usage is tight

and I found that when I upload an (large) image for remove background, it will pops cuda out of memory

so I am wondering is there any way to avoid this

wilfrediscoming commented 1 year ago

use option --jit e case you get a VRAM error, use these already compiled models

add in config.yaml

base-cuda-pt: url: "https://drive.google.com/file/d/1-3zbdYKuXlHlh-cj1UJfem8mcRKBlP1d/view?usp=drive_link" md5: NULL ckpt_name: "ckpt_base_cuda.pt" http_proxy: NULL base_size: [1024, 1024]

fast-cuda-pt: url: "https://drive.google.com/file/d/1OlruRBHEFjRUoM5PwdnWnnAAuLXG35fU/view?usp=drive_link" md5: NULL ckpt_name: "ckpt_fast_cuda.pt" http_proxy: NULL base_size: [384, 384]

base-nightly-cuda-pt: url: "https://drive.google.com/file/d/1-UkSRe8RpT1iblo6orbklEzX5BnBOY15/view?usp=drive_link" md5: NULL ckpt_name: "ckpt_base_nightly_cuda.pt" http_proxy: NULL base_size: [1024, 1024]

this jit way doesn't work

`Downloading... From: https://drive.google.com/uc?id=13oBl5MTVcWER3YU4fSxW3ATlVfueFQPY To: /root/.transparent-background/ckpt_base.pth 100%|██████████| 368M/368M [00:05<00:00, 61.9MB/s] /usr/local/lib/python3.10/dist-packages/torch/functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:3526.) return _VF.meshgrid(tensors, *kwargs) # type: ignore[attr-defined] /usr/local/lib/python3.10/dist-packages/transparent_background/backbones/SwinTransformer.py:428: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! if W % self.patch_size[1] != 0: /usr/local/lib/python3.10/dist-packages/transparent_background/backbones/SwinTransformer.py:430: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! if H % self.patch_size[0] != 0: /usr/local/lib/python3.10/dist-packages/transparent_background/backbones/SwinTransformer.py:366: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! Hp = int(np.ceil(H / self.window_size)) self.window_size /usr/local/lib/python3.10/dist-packages/transparent_background/backbones/SwinTransformer.py:367: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! Wp = int(np.ceil(W / self.window_size)) self.window_size /usr/local/lib/python3.10/dist-packages/transparent_background/backbones/SwinTransformer.py:203: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! assert L == H W, "input feature has wrong size" /usr/local/lib/python3.10/dist-packages/transparent_background/backbones/SwinTransformer.py:62: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! B = int(windows.shape[0] / (H W / window_size / window_size)) /usr/local/lib/python3.10/dist-packages/transparent_background/backbones/SwinTransformer.py:241: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! if pad_r > 0 or pad_b > 0: /usr/local/lib/python3.10/dist-packages/transparent_background/backbones/SwinTransformer.py:274: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! assert L == H W, "input feature has wrong size" /usr/local/lib/python3.10/dist-packages/transparent_background/backbones/SwinTransformer.py:279: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! pad_input = (H % 2 == 1) or (W % 2 == 1) /usr/local/lib/python3.10/dist-packages/transparent_background/backbones/SwinTransformer.py:280: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! if pad_input:

ValueError Traceback (most recent call last) /usr/local/lib/python3.10/dist-packages/transparent_background/Remover.py in init(self, mode, jit, device, ckpt, fast) 95 try: ---> 96 traced_model = torch.jit.load( 97 os.path.join(ckpt_dir, ckpt_name), map_location=self.device

26 frames ValueError: The provided filename /root/.transparent-background/ckpt_base_cuda:0.pt does not exist

During handling of the above exception, another exception occurred:

OutOfMemoryError Traceback (most recent call last) /usr/local/lib/python3.10/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias) 454 weight, bias, self.stride, 455 _pair(0), self.dilation, self.groups) --> 456 return F.conv2d(input, weight, bias, self.stride, 457 self.padding, self.dilation, self.groups) 458

OutOfMemoryError: CUDA out of memory. Tried to allocate 64.00 MiB. GPU 0 has a total capacty of 14.75 GiB of which 18.81 MiB is free. Process 2097 has 14.73 GiB memory in use. Of the allocated memory 13.29 GiB is allocated by PyTorch, and 190.99 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF`

gef12 commented 1 year ago

generated name is wrong ckpt_base_cuda:0.pt --> ckpt_base_cuda.pt

plemeri commented 10 months ago

Closing due to inactivity.