sksq96/pytorch-summary

AttributeError: 'dict' object has no attribute 'size'

nathanpainchaud opened this issue ยท 5 comments

Currently, if we try to use the summary or summary_string functions with modules that output a dictionary (or contain submodules that output dictionary, we get the following error:

    def hook(module, input, output):
        class_name = str(module.__class__).split(".")[-1].split("'")[0]
        module_idx = len(summary)
    
        m_key = "%s-%i" % (class_name, module_idx + 1)
        summary[m_key] = OrderedDict()
        summary[m_key]["input_shape"] = list(input[0].size())
        summary[m_key]["input_shape"][0] = batch_size
        if isinstance(output, (list, tuple)):
            summary[m_key]["output_shape"] = [
                [-1] + list(o.size())[1:] for o in output
            ]
        else:
>           summary[m_key]["output_shape"] = list(output.size())
E           AttributeError: 'dict' object has no attribute 'size'

It would be useful if we could support running modules that output a dictionary.

@nathanpainchaud hi,

The feature you added is really helpful and meets my use case. But I encountered another error

Traceback (most recent call last):
  File "XXX.py", line 442, in <module>
    summary(model, input_size=(1, 28, 28))
  File "/home/liuzhian/anaconda3/envs/pt1.5/lib/python3.6/site-packages/torchsummary/torchsummary.py", line 13, in summary
    model, input_size, batch_size, device, dtypes)
  File "/home/liuzhian/anaconda3/envs/pt1.5/lib/python3.6/site-packages/torchsummary/torchsummary.py", line 88, in summary_string
    total_output += np.prod(summary[layer]["output_shape"])
  File "<__array_function__ internals>", line 6, in prod
  File "/home/liuzhian/anaconda3/envs/pt1.5/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 3000, in prod
    keepdims=keepdims, initial=initial, where=where)
  File "/home/liuzhian/anaconda3/envs/pt1.5/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 87, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
TypeError: can't multiply sequence by non-int of type 'list'

Process finished with exit code 1

It seems that the bug was triggered by the line total_output += np.prod(summary[layer]["output_shape"]). I am not very clear on how to fix that, could you provide some advice?

@LiUzHiAn I'm not really sure what version of the code you're running, but it doesn't seem to be the version from the PR I opened (or master for that matter). I think, you identified the line where the error occurs correctly, but your stack message indicates it's at line 88. However, this code can be found at line 94 on master, and has been outright replaced by lines 97-102 in my PR.

Because of that, I suspect something is up with your version of the torchsummary package installed in your environment, and I'd make sure you have installed the version from my PR. You should be able to do this easily with pip:

pip install git+https://github.com/nathanpainchaud/pytorch-summary.git@fix/layer_dict_output

@nathanpainchaud Thanks for your help. It works well.

Oof it seems like your fork hosting the fix no longer exists, any chance it got migrated somewhere? @nathanpainchaud

@niniack This repo hasn't been maintained in 5 years, so it seems I cleaned up my fork in early 2023 (can't even remember honestly, but it seems to be the case ๐Ÿ˜…).

As the readme now indicates, I'd suggest migrating to the newer (and better supported) torchinfo. That's what I did on my end a while ago already.