danielgatis / rembg

Rembg is a tool to remove images background
MIT License
17.09k stars 1.88k forks source link

GPU not used,CPU running full #682

Open sasachen opened 1 month ago

sasachen commented 1 month ago

wen running u2netp GPU not used,CPU running full

import torch from PIL import Image

from config.conf import PROJECT_ROOT from server.rembg.rembg import new_session, remove

rembg_module = Rembg().load_image_net()

image = url_to_PIL_array(image_url)

new_img = rembg_module.process(image)

class Rembg: model = { "U2NET": "u2net", "U2NETP": "u2netp", "U2NET_HUMAN_SEG": "u2net_human_seg", "U2NET_CLOTH_SEG": "u2net_cloth_seg", "SILUETA": "silueta", "ISNET-GENERAL-USE": "isnet-general-use", "ISNET-ANIME": "isnet-anime", "SAM": "sam", }

def __init__(self):
    self.net = None

def load_image_net(self, model="u2netp"):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    cache = PROJECT_ROOT + "/package/rembg/"
    net = new_session(model, cache=cache, device=device)
    self.net = net
    return self

def resize_image(self, image):
    image = image.convert('RGB')
    model_input_size = (1024, 1024)
    image = image.resize(model_input_size, Image.BILINEAR)
    return image

def process(self, image):
    new_im = remove(
        image,
        alpha_matting=True,
        session=self.net,
        only_mask=False,
    )
    return new_im
    # return [new_orig_image, new_im]

GPU

image
Goki-0324 commented 3 weeks ago

I have same issues. Don't know how to fix it.

xy3xy3 commented 2 weeks ago

I have same issues. Don't know how to fix it.

me too.

xy3xy3 commented 2 weeks ago
from rembg import remove
import cv2
import onnxruntime as ort

print(ort.get_available_providers())
exit()

python 3.12 pip install onnxruntime-gpu pip install "rembg[gpu,cli]" # for library + cli pip install opencv-python

GPU is not avaliable.However,cuda is ok.

PS > python use_rembg.py      
['AzureExecutionProvider', 'CPUExecutionProvider']
PS> nvidia-smi
Sat Nov  9 22:02:30 2024       
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 560.94                 Driver Version: 560.94         CUDA Version: 12.6     |
|-----------------------------------------+------------------------+----------------------+
| GPU  Name                  Driver-Model | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA GeForce RTX 3060 ...  WDDM  |   00000000:01:00.0 Off |                  N/A |
| N/A   56C    P8             10W /  115W |     226MiB /   6144MiB |      0%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI        PID   Type   Process name                              GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|    0   N/A  N/A      5540    C+G   D:\Program Files\Tencent\QQNT\QQ.exe        N/A      |
|    0   N/A  N/A     16676    C+G   D:\Program Files\Tencent\QQNT\QQ.exe        N/A      |
+-----------------------------------------------------------------------------------------+
PS D:\Proj\sqc\数字人\绿幕背景换图> nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Thu_Sep_12_02:55:00_Pacific_Daylight_Time_2024
Cuda compilation tools, release 12.6, V12.6.77
Build cuda_12.6.r12.6/compiler.34841621_0
PS > 
sasachen commented 1 week ago

I have same issues. Don't know how to fix it.

me too.

try thanks https://huggingface.co/briaai/RMBG-1.4 https://huggingface.co/briaai/RMBG-2.0