sksq96 / pytorch-summary

Model summary in PyTorch similar to `model.summary()` in Keras
MIT License
4.02k stars 413 forks source link

RuntimeError: register_forward_hook is not supported on ScriptModules #103

Open Kocha opened 4 years ago

Kocha commented 4 years ago

Try executed this code, but print RuntimeError message. Is there any solution?

import torch
import torchvision.models as models

from torchsummary import summary

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

model = models.detection.fasterrcnn_resnet50_fpn().to(device)

summary(model, (3, 416, 416))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/torchsummary/torchsummary.py", line 68, in summary
    model.apply(register_hook)
  File "/usr/local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 287, in apply
    module.apply(fn)
  File "/usr/local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 287, in apply
    module.apply(fn)
  File "/usr/local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 287, in apply
    module.apply(fn)
  File "/usr/local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 288, in apply
    fn(self)
  File "/usr/local/lib/python3.7/site-packages/torchsummary/torchsummary.py", line 42, in register_hook
    hooks.append(module.register_forward_hook(hook))
  File "/usr/local/lib/python3.7/site-packages/torch/jit/__init__.py", line 1689, in fail
    raise RuntimeError(name + " is not supported on ScriptModules")
RuntimeError: register_forward_hook is not supported on ScriptModules
Naireen commented 4 years ago

This is because ScriptModules (what fasterrcnn_resnet50_fpn is represented as here) doesn't currently support forward hooks, and torchsummary currently assumes that the model passed in inherits from nn.Module, so then the method register_forward_hook is supported. Hope that helps!

Kocha commented 4 years ago

@Naireen Thank you for Reply.

I tried debugging but didn't know where the problem was. Do you know which layer of fasterrcnn_resnet50_fpn is the problem?