PeterL1n / RobustVideoMatting

Robust Video Matting in PyTorch, TensorFlow, TensorFlow.js, ONNX, CoreML!
https://peterl1n.github.io/RobustVideoMatting/
GNU General Public License v3.0
8.61k stars 1.14k forks source link

ONNX #96

Closed rocketboi98 closed 3 years ago

rocketboi98 commented 3 years ago

I can`t understand how to fix that error with downsample ratio

from PIL import Image
import numpy as np
import torchvision.transforms as transforms
import onnxruntime as ort
import cv2
import torch
import time

torch_transforms = transforms.Compose(
    [
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
    ]
)

sess = ort.InferenceSession('rvm_mobilenetv3_fp16.onnx')
rec = [ np.zeros([1, 1, 1, 1], dtype=np.float16) ] * 4
downsample_ratio = np.array([0.25], dtype=np.float32)

# initialize the video stream and allow the camera sensor to
vs = cv2.VideoCapture(0)
vs.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
vs.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
time.sleep(2.0)

_, frame_np = vs.read()

frame_np = cv2.cvtColor(frame_np, cv2.COLOR_BGR2RGB)
frame_np = cv2.resize(frame_np, (910, 512), cv2.INTER_AREA)
frame_np = frame_np[:, 120:792, :]
frame_np = cv2.flip(frame_np, 1)

frame_PIL = Image.fromarray(frame_np)
frame_tensor = torch_transforms(frame_PIL)
frame_tensor = frame_tensor[None, :, :, :]
frame_tensor = np.array(frame_tensor)

#Get mask
fgr, pha, *rec = sess.run([], {
    'src': frame_tensor, 
    'r1i': rec[0], 
    'r2i': rec[1], 
    'r3i': rec[2], 
    'r4i': rec[3], 
    'downsample_ratio': downsample_ratio
})

Traceback (most recent call last): File "test.py", line 46, in 'downsample_ratio': downsample_ratio File "C:\Users\ivan\AppData\Local\Programs\Python\Python36\lib\site-packages\onnxruntime\capi\onnxruntime_inference_collection.py", line 188, in run return self._sess.run(output_names, input_feed, run_options) onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Unexpected input data type. Actual: (tensor(float)) , expected: (tensor(float16)) [ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-uzca7qz1\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

When I change type np.float32 to np.float16 for downsample ratio I get new error: Unexpected input data type. Actual: (tensor(float16)) , expected: (tensor(float)) (for downsample ratio)

rocketboi98 commented 3 years ago

i fixed that. It was a problem with type of frame_tensor. frame_tensor = np.array(frame_tensor, dtype=np.float16)