Hi all, I’m recently learning to convert a model into jit model since I want to load a model without source class. And I use forward hook to extract middle layer outputs as features and then try to return the modified features, the code looks like follows:
from typing import Tuple, List, Callable
class MyClass(nn.Module):
features: List[torch.Tensor]
def __init__(self):
super(MyClass, self).__init__()
self.features = []
self.model = torchvision.models.resnet50()
self.model.layer3[-1].register_forward_hook(self.extract_feature())
self.model.layer3[-1].register_forward_hook(self.extract_feature())
def extract_feature(self) -> Callable:
def hook(module, input: Tuple[torch.Tensor], output):
self.features.append(output)
return hook_t
def __some_operation(self):
self.result = torch.cat((self.features[0], self.features[1]), 1)
def forward(self, x):
self.features = []
_ = self.model(x)
self.__some_operation()
return self.result
my_model = MyClass()
my_script = jit.torch.scipt(my_model)
my_script.save("my_jit_model.pth")
but I got this error:
attribute lookup is not defined on python value of type 'MyClass':
self.features.append(output)
~~~~~~~~~~~~~ <--- HERE
Sorry I’m not familiar with jit and just getting started, I would appreciate it if anyone could help me figure out the mistakes I make, thanks!