2

我正在使用 torchscript 将 AlignedReID++ 的 pytorch 代码转换为 C++ 代码。在此过程中,我遇到了以下错误:

Traceback (most recent call last):
  File "selfDemo.py", line 28, in <module>
    traced_model = torch.jit.script(model)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 1162, in script
    return _convert_to_script_module(obj)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 1812, in _convert_to_script_module
    return WeakScriptModuleProxy(mod, stubs)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 1386, in init_then_register
    original_init(self, *args, **kwargs)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 1736, in __init__
    _create_methods_from_stubs(self, stubs)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 1347, in _create_methods_from_stubs
    self._c._create_methods(self, defs, rcbs, defaults)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 982, in _make_strong_submodule
    new_strong_submodule = _convert_to_script_module(module)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 1791, in _convert_to_script_module
    return _create_constant_iterable_module(mod)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 963, in _create_constant_iterable_module
    modules[key] = _create_constant_iterable_module(submodule)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 965, in _create_constant_iterable_module
    modules[key] = _convert_to_script_module(submodule)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 1812, in _convert_to_script_module
    return WeakScriptModuleProxy(mod, stubs)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 1386, in init_then_register
    original_init(self, *args, **kwargs)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 1736, in __init__
    _create_methods_from_stubs(self, stubs)
  File "/nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torch/jit/__init__.py", line 1347, in _create_methods_from_stubs
    self._c._create_methods(self, defs, rcbs, defaults)
RuntimeError: 
attribute 'downsample' of type 'NoneType' is not usable in a script method (did you forget to add it __constants__?):
at /nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torchvision/models/resnet.py:109:12
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
           ~~~~~~~~~~~~~~~ <--- HERE
            identity = self.downsample(x)
        out += identity
        out = self.relu(out)

        return out
'__module__.__torch__.torchvision.models.resnet.___torch_mangle_32.Bottleneck.forward' is being compiled since it was called from '__module__.__torch__.models.ResNet.ResNet50.forward'
at /nfs/engine/rajagopalar/anaconda3/envs/torchExp/lib/python3.6/site-packages/torchvision/models/resnet.py:114:16

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
            identity = self.downsample(x)
        out += identity
        out = self.relu(out)

        return out
               ~~~ <--- HERE

此外,错误来自torchvision Resnet模型,而不是我的代码,这让我对错误的位置感到困惑

我正在使用 pytorch==1.2 运行代码。我已经尝试使用 Optional[T] 标签,但它仍然没有按预期工作

我想知道我是否有任何问题,或者我必须创建更多文件以进行脚本编写。

欢迎任何帮助或建议

第一个模型

traced_model = torch.jit.script(model)

第二个模型

myexactor = FeatureExtractor(traced_model, exact_list)

...
scripted_trace = torch.jit.script(myexactor, img1)

代码取自github:https ://github.com/michuanhaohao/AlignedReID

我在文件中进行了更改: https ://github.com/michuanhaohao/AlignedReID/blob/master/Alignedreid_demo.py

4

0 回答 0