Pytorch LibTorch,将deeplabv3_resnet101转换为c++;

Pytorch LibTorch,将deeplabv3_resnet101转换为c++;,pytorch,libtorch,Pytorch,Libtorch,我试图使用PyTrac中的这个示例代码来转换Python模型,用于PyTrac C++ API(LIB火炬)。 此示例工作正常,并按预期保存文件。 当我切换到该型号时: model=models.segmentation.deeplabv3\u resnet101(pretrained=True) 它给了我以下错误: File "convert.py", line 14, in <module> traced_script_module = torch.jit.trace(m

我试图使用PyTrac中的这个示例代码来转换Python模型,用于PyTrac C++ API(LIB火炬)。 此示例工作正常,并按预期保存文件。 当我切换到该型号时:

model=models.segmentation.deeplabv3\u 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])
现在我得到了一个错误:

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)
文件“convert.py”,第15行,在
tracked\u script\u module=torch.jit.trace(模型,示例)
文件“C:\Python37\lib\site packages\torch\jit\\uuuu init\uuuu.py”,第636行,在跟踪中
var_查找_fn,_force_outplace)
RuntimeError:仅支持将张量和(可能嵌套的)张量元组作为跟踪函数的输入或输出(C:\a\w\1\s\windows\pytorch\torch/csc/jit/pybind\u utils.h:91处的值)
(无回溯可用)

您的问题源于BatchNorm层。如果每个通道需要多个值,则模型处于训练模式。你能调用模型看看是否有改进吗

否则,您也可以尝试在批处理中使用多个实例生成随机数据,即
example=torch.rand(5,3,224,224)

此外,您应该注意正确地规范化您的数据,但是,这不会导致此处的错误。

(来自pytorch论坛)

trace只支持输出张量或张量元组的模块。 根据deeplabv3实现,其输出是OrderedICT。这是个问题。 要解决此问题,请创建一个包装器模块

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...

我的模型已经存钱了

你确定你的示例大小有多个通道,并且在你的代码中以
example
的形式传递示例形状吗?我不确定!我如何知道要传递什么作为示例?谢谢使用相同的形状
(1,3,224,224)
对不起,我还是不明白。“deeplabv3_resnet101”有不同的形状吗?我怎样才能知道它是什么?感谢您的时间。通过检查模型或阅读有关模型的内容。不过你还是应该提供你将通过网络上传的图像的输入形状。嗨,谢谢你的回复。我添加了:
model=models.segmentation.deeplabv3_resnet101(pretrained=True)model.eval()
,并使用了
example=torch.rand(5,3,224,224)
,现在我得到了:
运行时错误:只有张量和(可能嵌套的)张量元组被支持作为跟踪函数的输入或输出(C:\a\w\1\s\windows\pytorch\torch/csc/jit/pybind_utils.h:91)(无回溯可用)
@anti您可以尝试其中一种吗?即将模型设置为评估模式或使用
torch.rand(5,3,224,224)尝试。
评估模式应该可以解决问题。两种方式的结果相同。`仅使用张量和(可能嵌套)支持将张量元组作为跟踪函数的输入或输出(C:\a\w\1\s\windows\pytorch\torch/csc/jit/pybind_utils.h:91处的值)`
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)
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...