When JIT saving “model.pt” of a complex pytorch model with many custom classes, I am encountering the error that pytorch doesn’t know the type annotation of one of those custom classes. In other words, the following code (drastically summarized from original) fails on the seventh line:
import torch
from gan import Generator
from gan.blocks import SpadeBlock
generator = Generator()
generator.load_weights("path/to/weigts")
jitted = torch.jit.script(generator)
torch.jit.save(jitted, "model.pt")
Error:
Traceback (most recent call last):
File "pth2onnx.py", line 72, in <module>
to_torch_jit(generator)
File "pth2onnx.py", line 24, in to_torch_jit
jitted = torch.jit.script(generator)
File "/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/__init__.py", line 1516, in script
return torch.jit._recursive.create_script_module(obj, torch.jit._recursive.infer_methods_to_compile)
File "/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py", line 310, in create_script_module
concrete_type = concrete_type_store.get_or_create_concrete_type(nn_module)
File "/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py", line 269, in get_or_create_concrete_type
concrete_type_builder = infer_concrete_type_builder(nn_module)
File "/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py", line 138, in infer_concrete_type_builder
sub_concrete_type = concrete_type_store.get_or_create_concrete_type(item)
File "/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py", line 269, in get_or_create_concrete_type
concrete_type_builder = infer_concrete_type_builder(nn_module)
File "/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py", line 138, in infer_concrete_type_builder
sub_concrete_type = concrete_type_store.get_or_create_concrete_type(item)
File "/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py", line 269, in get_or_create_concrete_type
concrete_type_builder = infer_concrete_type_builder(nn_module)
File "/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py", line 126, in infer_concrete_type_builder
attr_type = infer_type(name, item)
File "/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/_recursive.py", line 99, in infer_type
attr_type = torch.jit.annotations.ann_to_type(class_annotations[name], _jit_internal.fake_range())
File "/home/a.nieuwland/.conda/envs/python3.6/lib/python3.6/site-packages/torch/jit/annotations.py", line 303, in ann_to_type
raise ValueError("Unknown type annotation: '{}'".format(ann))
ValueError: Unknown type annotation: '<class 'gan.blocks.SpadeBlock'>'
The type it complains about is indeed a class we ourselves have programmed and used in the loaded Generator
. I would appreciate pointers on what could cause this or how to investigate this!
I tried the following:
- explicitly importing
SpadeBlock
in the script that calls torch.jit.script - ensured it inherits from
nn.Module
(as doesGenerator
) - ensured the gan package is installed, using
pip install --user -e <directory>
Any ideas? Thanks in advance!