TylerYep / torchinfo

View model summaries in PyTorch!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot assign str/repr from model.summary to a variable without 'workaround' in Colab

Antsthebul opened this issue · comments

Not sure if this should be a feature request since a solution is known...

Describe the bug

According to Py-pi/torch-summary:

from torchsummary import summary
model_stats = summary(your_model, (3, 28, 28), verbose=0)
summary_str = str(model_stats)

And while also keeping in mind what was mentioned on GitHub

Note: if you are using a Jupyter Notebook or Google Colab, summary(model, ...) must be the returned value of the cell. If it is not, you should wrap the summary in a print(), e.g. print(summary(model, ...)). See tests/jupyter_test.ipynb for examples.

I was unable to assign the str of summary to a value and using going colab the print statement is not required for the cell to display the results

To Reproduce
Steps to reproduce the behavior:

  1. Create a model in google colab and install necessary libraries
  2. Try to create a variable using str wrapper around summary, varibale will not be returned

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

screenshot 1

Colab Cell repr (also value not assigned)
image

screenshot 2

Colab cell (w/str)
image

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser: Chrome
  • Version [e.g. 22]

Additional context

screenshot 3

Solution is to use contextlib Note: print statement used to evaluate newlines.

image

Hmm, I am unable to reproduce the issue. Which Python-version do you use? Which torchinfo-version? And could you provide the code for your model?

In screenshot 2, are you certain that you've executed cell 25 before cell 26? In other words, have you been able to replicate the issue?

Sorry if this sounds harsh, I'm just a bit confused for two reasons:

  1. summary returns a ModelStatistics-object, which has no __str__-method. This means that str(summary(...)) falls back on the ModelStatistics.__repr__-method, and that never returns None or "None".
  2. I've tried to replicate the behavior shown above: with simple modules, ones that simply return the input (and so have no parameters), modules without a forward-method (raises exception), and even things other than a torch.nn.Module (also raises exception).

Hmm, I am unable to reproduce the issue. Which Python-version do you use? Which torchinfo-version? And could you provide the code for your model?

In screenshot 2, are you certain that you've executed cell 25 before cell 26? In other words, have you been able to replicate the issue?

Sorry if this sounds harsh, I'm just a bit confused for two reasons:

  1. summary returns a ModelStatistics-object, which has no __str__-method. This means that str(summary(...)) falls back on the ModelStatistics.__repr__-method, and that never returns None or "None".
  2. I've tried to replicate the behavior shown above: with simple modules, ones that simply return the input (and so have no parameters), modules without a forward-method (raises exception), and even things other than a torch.nn.Module (also raises exception).

Ah sorry. This is in google colab. So python 3.8, in chrome browser. I meant to add that in the title, but I did mention it in step 1

@snimu so sorry. I didnt mean to waste your time, but I appreciate your response. It appears that although the import is the same from torchsummary [..] there are 2 different packages. I've been using the pytorch-summary pkg, and not torch-summary 🤦‍♂️

@Antsthebul no problem :) I'm glad that I could help.

@Antsthebul Please use torchinfo instead of torch-summary, thanks!