PeterL1n / BackgroundMattingV2

Real-Time High-Resolution Background Matting
MIT License
6.81k stars 950 forks source link

Error loading model in libTorch #64

Closed brinoausrino closed 3 years ago

brinoausrino commented 3 years ago

I receive this error when trying to load the model:

error loading the model : ■   open file failed, file path: Exception raised from FileAdapter at ..\..\caffe2\serialize\file_adapter.cc:11 (most recent call first): 00007FFE9633A7B200007FFE9633A750 c10.dll!c10::Error::Error [<unknown file> @ <unknown line number>] 00007FFE69BA5A3D00007FFE69BA56D0 torch_cpu.dll!caffe2::serialize::FileAdapter::FileAdapter [<unknown file> @ <unknown line number>] 00007FFE6AAB408C00007FFE6AAB4050 torch_cpu.dll!torch::jit::load [<unknown file> @ <unknown line number>]

My Configuration:

Win 10 libTorch 1.7.1 cuda 11.0 Visual Studio 2017

The example compiles correct and creates the cuda device correctly. I'm trying to load the torchscript_resnet50_fp16.pth model.

Any suggestions or ideas on how to solve this?

PeterL1n commented 3 years ago

Do other variants work? Such as the fp32 ones.

brinoausrino commented 3 years ago

The error occurs with every model. The torch and cuda version I'm using are correct?

PeterL1n commented 3 years ago

The Torch and CUDA version seems correct to me. However, are you sure you are using LibTorch with CUDA compiled instead of the CPU version of LibTorch?

brinoausrino commented 3 years ago

I'm using the cuda version.

I only had to change auto device = torch::Device("cuda"); for auto device = torch::Device(c10::DeviceType::CUDA,0);. Otherwise it did not find the cuda card. Also torch::cuda::cudnn_is_available() showed true.

zjhwtonywang commented 3 years ago

I use script module with cuda it can be work fine,but use CPU device then crashed My Configuration: Win 10 libTorch 1.7.1 cuda 10.2 Visual Studio 2019

PeterL1n commented 3 years ago

There is a couple of things you can do. Since you have the source code here, you can export TorchScript yourself. You can also play around with the Python API instead of C++ to see if it works there. If this is a TorchScript issue, you will have to report to Pytorch.

zjhwtonywang commented 3 years ago

here my code:

import torch import cv2 import numpy as np from PIL import Image

device = torch.device('cuda') precision = torch.float16

model = torch.jit.load('mobilenetv2.pth') model.backbone_scale = 0.25 model.refine_mode = 'sampling' model.refine_sample_pixels = 80_000 model = model.to(device)

w = 1920 h = 1080

video_source = cv2.VideoCapture("test.mp4") old_bgr = Image.open("test.png").convert('RGB') bgrarry = np.array(old_bgr) bgr = torch.tensor(bgrarry.transpose((2, 0, 1)) / 255, dtype=torch.float16, device=device).view(1, 3, h, w)

new_bk_img = Image.open("shangyu.jpg").resize((w, h), Image.ANTIALIAS) new_bk = np.array(new_bk_img.convert('RGB')) new_back_tensor = torch.tensor(new_bk.transpose((2, 0, 1)) / 255, dtype=torch.float16, device=device).view(1, 3, h, w)

def cv2_frame_to_tensor(frame): frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) narray = cv2.resize(frame, (w, h)) new_bk = np.array(narray) return torch.tensor(new_bk.transpose((2, 0, 1)) / 255, dtype=torch.float16, device=device).view(1, 3, h, w)

while True: r, f = video_source.read() if not r: break src = cv2_frame_to_tensor(f) src = src.to(device, non_blocking=True) bgr = bgr.to(device, non_blocking=True)

pha, fgr = model(src, bgr)[:2]

com = fgr * pha + new_back_tensor * (1 - pha)
frames = com.mul(255).byte()
frames = frames.permute(0, 2, 3, 1).cpu().numpy()
for i in range(frames.shape[0]):
    frame = frames[i]
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    cv2.imshow("demo:", frame)
if cv2.waitKey(40) & 0xFF == ord('q'):
    break

when I change the code device = torch.device('cuda') to device = torch.device("cpu") Then error occurs: pha, fgr = model(src, bgr)[:2] if input.dim() == 4 and mode == 'bilinear': assert align_corners is not None return torch._C._nn.upsample_bilinear2d(input, output_size, align_corners, scale_factors)


    if input.dim() == 5 and mode == 'trilinear':
        assert align_corners is not None
RuntimeError: "upsample_bilinear2d_channels_last" not implemented for 'Half'

Any suggestions or ideas on how to solve this ,thanks 
PeterL1n commented 3 years ago

Change precision to float32. CPU upsampling operator does not support float16. That's what the error message was telling you.

zjhwtonywang commented 3 years ago

ok, thank you

zhanghongyong123456 commented 3 years ago

将精度更改为float32。CPU上采样运算符不支持float16。这就是错误消息告诉您的内容。

Change precision to float32. CPU upsampling operator does not support float16. That's what the error message was telling you.

Change precision to float32. CPU upsampling operator does not support float16. That's what the error message was telling you.

I'm going to run export_torchscript.py and I'm not sure what the error is, 001

PeterL1n commented 3 years ago

@zhanghongyong123456 Just pushed a new version to fix the bug. Let me know if it works now.