在 pytorch 中使用 print() 或 summary() 查看 Pytorch 模型中的层和权重尺寸时出错

Getting error when using print() or summary() in pytorch to see the layers and weight dimensions in a Pytorch model

在现有模型上使用打印时, 它不打印模型。相反,它显示: <function resnext101_32x8d at 0x00000178CC26BA68>

>>> import torch
>>> import torchvision.models as models 
>>> m1 = models.resnext101_32x8d
>>> print(m1)
<function resnext101_32x8d at 0x00000178CC26BA68>
>>>

使用summary时出现如下错误:

AttributeError: 'function' object has no attribute 'apply'

>>> import torch
>>> import torchvision.models as models 
>>> from torchvision import summary
>>> m1 = models.resnext101_32x8d
>>>
>>> summary(m1, (3, 224, 224))
 Traceback(most recent call last):
   File "<stdin>", line 1, in <module>
   File torchsummary.py, line 68, in summary
      model.apply(register_hook)
AttributeError: 'function' object has no attribute 'apply'

如何解决这些与 printsummary 相关的问题?还有其他方法可以轻松查看所有 pytorch 层和模型拓扑吗?

models.resnext101_32x8d是class构造函数,需要调用构造函数,最后加括号即可。

m1 = models.resnext101_32x8d()
print(m1)