PeterL1n / RobustVideoMatting

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

how to solve: torch.jit.trace() convert .pth to .pt or .torchscript error! #192

Open sankexin opened 1 year ago

sankexin commented 1 year ago

`import argparse import torch from torch.utils.mobile_optimizer import optimize_for_mobile from model import MattingNetwork import torch.utils.data.distributed

rec = (torch.zeros([1, 1, 1, 1, 1]).to("cuda"),) * 4 src = torch.randn(1, 1, 3, 1080, 1920).to("cuda") downsample_ratio = torch.tensor([0.25]).to("cuda")

model = MattingNetwork('mobilenetv3', 'deep_guided_filter').eval().to("cuda", torch.float32) checkpoint = 'rvm_mobilenetv3.pth' model.load_state_dict(torch.load(checkpoint, map_location=f"cuda"), strict=False) model.eval()

traced_script_module = torch.jit.trace(model, (src, *rec, downsample_ratio)) optimized_traced_model = optimize_for_mobile(traced_script_module) optimized_traced_model._save_for_lite_interpreter("rvm_mobilenetv3_fp32.pt")`

error: File "to_pt2.py", line 24, in traced_script_module = torch.jit.trace(model, (src, *rec, downsample_ratio) TypeError: upsample_bilinear2d() received an invalid combination of arguments - got (Tensor, NoneType, bool, list), but expected one of:

zhanghongyong123456 commented 1 year ago

`import argparse import torch from torch.utils.mobile_optimizer import optimize_for_mobile from model import MattingNetwork import torch.utils.data.distributed

rec = (torch.zeros([1, 1, 1, 1, 1]).to("cuda"),) * 4 src = torch.randn(1, 1, 3, 1080, 1920).to("cuda") downsample_ratio = torch.tensor([0.25]).to("cuda")

model = MattingNetwork('mobilenetv3', 'deep_guided_filter').eval().to("cuda", torch.float32) checkpoint = 'rvm_mobilenetv3.pth' model.load_state_dict(torch.load(checkpoint, map_location=f"cuda"), strict=False) model.eval()

traced_script_module = torch.jit.trace(model, (src, *rec, downsample_ratio)) optimized_traced_model = optimize_for_mobile(traced_script_module) optimized_traced_model._save_for_lite_interpreter("rvm_mobilenetv3_fp32.pt")`

error: File "to_pt2.py", line 24, in traced_script_module = torch.jit.trace(model, (src, *rec, downsample_ratio) TypeError: upsample_bilinear2d() received an invalid combination of arguments - got (Tensor, NoneType, bool, list), but expected one of:

  • (Tensor input, tuple of ints output_size, bool align_corners, tuple of floats scale_factors) didn't match because some of the arguments have invalid types: (Tensor, NoneType, bool, list)
  • (Tensor input, tuple of ints output_size, bool align_corners, float scales_h, float scales_w, *, Tensor out)

Where can I get this conversion script,Can you share that? Thank you

sankexin commented 1 year ago

import argparse import torch from torch.utils.mobile_optimizer import optimize_for_mobile from model import MattingNetwork import torch.utils.data.distributed rec = (torch.zeros([1, 1, 1, 1, 1]).to("cuda"),) * 4 src = torch.randn(1, 1, 3, 1080, 1920).to("cuda") downsample_ratio = torch.tensor([0.25]).to("cuda") model = MattingNetwork('mobilenetv3', 'deep_guided_filter').eval().to("cuda", torch.float32) checkpoint = 'rvm_mobilenetv3.pth' model.load_state_dict(torch.load(checkpoint, map_location=f"cuda"), strict=False) model.eval() traced_script_module = torch.jit.trace(model, (src, *rec, downsample_ratio)) optimized_traced_model = optimize_for_mobile(traced_script_module) optimized_traced_model._save_for_lite_interpreter("rvm_mobilenetv3_fp32.pt") error: File "to_pt2.py", line 24, in traced_script_module = torch.jit.trace(model, (src, *rec, downsample_ratio) TypeError: upsample_bilinear2d() received an invalid combination of arguments - got (Tensor, NoneType, bool, list), but expected one of:

  • (Tensor input, tuple of ints output_size, bool align_corners, tuple of floats scale_factors) didn't match because some of the arguments have invalid types: (Tensor, NoneType, bool, list)
  • (Tensor input, tuple of ints output_size, bool align_corners, float scales_h, float scales_w, *, Tensor out)

Where can I get this conversion script,Can you share that? Thank you

I wrote it myself.

zhanghongyong123456 commented 1 year ago

import argparse import torch from torch.utils.mobile_optimizer import optimize_for_mobile from model import MattingNetwork import torch.utils.data.distributed rec = (torch.zeros([1, 1, 1, 1, 1]).to("cuda"),) * 4 src = torch.randn(1, 1, 3, 1080, 1920).to("cuda") downsample_ratio = torch.tensor([0.25]).to("cuda") model = MattingNetwork('mobilenetv3', 'deep_guided_filter').eval().to("cuda", torch.float32) checkpoint = 'rvm_mobilenetv3.pth' model.load_state_dict(torch.load(checkpoint, map_location=f"cuda"), strict=False) model.eval() traced_script_module = torch.jit.trace(model, (src, *rec, downsample_ratio)) optimized_traced_model = optimize_for_mobile(traced_script_module) optimized_traced_model._save_for_lite_interpreter("rvm_mobilenetv3_fp32.pt") error: File "to_pt2.py", line 24, in traced_script_module = torch.jit.trace(model, (src, *rec, downsample_ratio) TypeError: upsample_bilinear2d() received an invalid combination of arguments - got (Tensor, NoneType, bool, list), but expected one of:

  • (Tensor input, tuple of ints output_size, bool align_corners, tuple of floats scale_factors) didn't match because some of the arguments have invalid types: (Tensor, NoneType, bool, list)
  • (Tensor input, tuple of ints output_size, bool align_corners, float scales_h, float scales_w, *, Tensor out)

Where can I get this conversion script,Can you share that? Thank you

I wrote it myself. Can you share that? I'm not too familiar with that. Thank you very much