PeterL1n / BackgroundMattingV2

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

problem running the webcam inference script #17

Closed jinzishuai closed 3 years ago

jinzishuai commented 3 years ago

As a first step to test this model, I got the conda environment set up and made sure pytorch=1.7 is installed properly with the GPU version etc.

But when I ran the script, I got the following error

(bgm2) C:\ZeroBox\src\BackgroundMattingV2> python inference_webcam.py --model-type mattingrefine --model-backbone resnet50 --model-checkpoint TorchScript/torchscript_resnet50_fp32.pth --resolution 1280 720
C:\Users\jinzi\miniconda3\envs\bgm2\lib\site-packages\torch\serialization.py:589: UserWarning: 'torch.load' received a zip file that looks like a TorchScript archive dispatching to 'torch.jit.load' (call 'torch.jit.load' directly to silence this warning)
  warnings.warn("'torch.load' received a zip file that looks like a TorchScript archive"
Traceback (most recent call last):
  File "inference_webcam.py", line 138, in <module>
    model.load_state_dict(torch.load(args.model_checkpoint), strict=False)
  File "C:\Users\jinzi\miniconda3\envs\bgm2\lib\site-packages\torch\nn\modules\module.py", line 1025, in load_state_dict
    state_dict = state_dict.copy()
  File "C:\Users\jinzi\miniconda3\envs\bgm2\lib\site-packages\torch\jit\_script.py", line 558, in __getattr__
    return super(RecursiveScriptModule, self).__getattr__(attr)
  File "C:\Users\jinzi\miniconda3\envs\bgm2\lib\site-packages\torch\jit\_script.py", line 288, in __getattr__
    return super(ScriptModule, self).__getattr__(attr)
  File "C:\Users\jinzi\miniconda3\envs\bgm2\lib\site-packages\torch\nn\modules\module.py", line 778, in __getattr__
    raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
torch.nn.modules.module.ModuleAttributeError: 'RecursiveScriptModule' object has no attribute 'copy'

As a reference, here is some of my envrionment information

(bgm2) C:\ZeroBox\src\BackgroundMattingV2>conda list torch*
# packages in environment at C:\Users\jinzi\miniconda3\envs\bgm2:
#
# Name                    Version                   Build  Channel
pytorch                   1.7.0           py3.8_cuda102_cudnn7_0    pytorch
torchaudio                0.7.0                      py38    pytorch
torchvision               0.8.1                py38_cu102    pytorch

(bgm2) C:\ZeroBox\src\BackgroundMattingV2>python --version
Python 3.8.5

Do you know what is wrong here? Thanks a lot.

jinzishuai commented 3 years ago

turned out I used the TorchScript model file. It works after switching to use the PyTorch model file.