PeterL1n / BackgroundMattingV2

Real-Time High-Resolution Background Matting
MIT License
6.85k stars 952 forks source link

Run "inference_webcam.py" at 100% CPU, how to use CUDA? #75

Closed Hanleon closed 3 years ago

Hanleon commented 3 years ago

Hi: I tried to run "inference_webcam.py", but the CPU always at 100%(the CPU is 9700K). Then I tried to change resolution "1280x720" or "640x480", also not work. And at this part res = res.mul(255).byte().cpu().permute(0, 2, 3, 1).numpy()[0], it's run by CPU. So, is it problem? And how can I fix it to run CUDA? Thx~~~

Hanleon commented 3 years ago

I checked the code again, and found return ToTensor()(Image.fromarray(frame)).unsqueeze_(0).cuda() this part is the key. It's not used CUDA to run. Then I checked CUDA and CUDNN on my environment. Python 3.8.8 (default, Feb 24 2021, 15:54:32) [MSC v.1928 64 bit (AMD64)] :: Anaconda, Inc. on win32 Type "help", "copyright", "credits" or "license" for more information. import torch print(torch.cuda.is_available()) True from torch.backends import cudnn print(cudnn.is_available()) True It's available. Now I have no idea, please help me, Thx~

Hanleon commented 3 years ago

Soved it. Just change return ToTensor()(Image.fromarray(frame)).unsqueeze_(0).cuda() to return torch.as_tensor(frame, device=torch.device("cuda")).type(torch.cuda.FloatTensor).unsqueeze(0).permute(0, 3, 1, 2)/255.