I am converting pytorch code for AlignedReID++ to C++ code using torchscript. During the process I encountered the following error:
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
Further, the error comes from the torchvision Resnet model and not my code which confused me as to where the error was
I am running the code with pytorch==1.2. I have tried using Optional[T] tags but it did not work as expected still
I want to know if there's anything wrong on my end or must I create more files for scripting to happen.
Any help or suggestions would be welcome
The first model
traced_model = torch.jit.script(model)
And the second model
myexactor = FeatureExtractor(traced_model, exact_list)
...
scripted_trace = torch.jit.script(myexactor, img1)
The code is taken from github : https://github.com/michuanhaohao/AlignedReID
and I made changes in the file : https://github.com/michuanhaohao/AlignedReID/blob/master/Alignedreid_demo.py