pytorch - LibTorch,将 deeplabv3_resnet101 转换为 c++

标签 pytorch libtorch

我正在尝试使用 PyTorch website 中的示例代码转换 python 模型以在 PyTorch c++ api (LibTorch) 中使用。

Converting to Torch Script via Tracing
To convert a PyTorch model to Torch Script via tracing, you must pass an instance of your model along with an example input to the torch.jit.trace function. This will produce a torch.jit.ScriptModule object with the trace of your model evaluation embedded in the module’s forward method:

import torch
import torchvision

# An instance of your model.
model = torchvision.models.resnet18()

# An example input you would normally provide to your model's forward() method.
example = torch.rand(1, 3, 224, 224)

# Use torch.jit.trace to generate a torch.jit.ScriptModule via tracing.
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("model.pt")

这个示例工作正常,并按预期保存了文件。 当我切换到这个模型时:

model = models.segmentation.deeplabv3_resnet101(pretrained=True)

它给了我以下错误:

File "convert.py", line 14, in <module>
    traced_script_module = torch.jit.trace(model, example)
  File "C:\Python37\lib\site-packages\torch\jit\__init__.py", line 636, in trace
          raise ValueError('Expected more than 1 value per channel when training, got input size {}'.format(size))
ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 256, 1, 1])

我认为这是因为 example 格式错误,但是我怎样才能得到正确的格式呢?

根据下面的评论,我的新代码是:

import torch
import torchvision
from torchvision import models


model = models.segmentation.deeplabv3_resnet101(pretrained=True)
model.eval()


# An example input you would normally provide to your model's forward() method.
example = torch.rand(1, 3, 224, 224)

# Use torch.jit.trace to generate a torch.jit.ScriptModule via tracing.
traced_script_module = torch.jit.trace(model, example)

traced_script_module.save("model.pt")

我现在收到错误:

File "convert.py", line 15, in <module>
    traced_script_module = torch.jit.trace(model, example)
  File "C:\Python37\lib\site-packages\torch\jit\__init__.py", line 636, in trace
    var_lookup_fn, _force_outplace)
RuntimeError: Only tensors and (possibly nested) tuples of tensors are supported as inputs or outputs of traced functions (toIValue at C:\a\w\1\s\windows\pytorch\torch/csrc/jit/pybind_utils.h:91)
(no backtrace available)

最佳答案

(来自 pytorch 论坛)

trace 仅支持以张量或张量元组作为输出的模块。 根据deeplabv3的实现,它的输出是OrderedDict。这是一个问题。 为了解决这个问题,制作一个包装模块

class wrapper(torch.nn.Module):
    def __init__(self, model):
        super(wrapper, self).__init__()
        self.model = model

    def forward(self, input):
        results = []
        output = self.model(input)
        for k, v in output.items():
            results.append(v)
        return tuple(results)

model = wrapper(deeplap_model)
#trace...

我的模型已保存。

关于pytorch - LibTorch,将 deeplabv3_resnet101 转换为 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56902458/

相关文章:

python - 给定索引 ndarray 和标志 ndarray 是否有任何 numpy/torch 样式来设置值?

c++ - 使用 Rcpp 在 Ubuntu Xenial 上抛出 std::runtime_error 时出现段错误

cmake - 链接静态库 pytorch 在构建过程中找不到其内部函数

c++ - libtorch:如何从tensorRT fp16半类型指针创建张量?

python - PyInstaller 可执行文件无法获取 TorchScript 的源代码

pytorch - pytorch中的多标签分类

python - PyTorch 警告关于在前向包含多个 autograd 节点时使用非完整后向钩子(Hook)

python - 当想要查找得分最高的 `start` 标记时,torch.argmax() 中出现 TypeError

c++ - libtorch 中 numpy.spacing(1) 的等价物是什么?

opencv - 无法使用 cmake 与 OpenCV 和 LibTorch 链接项目