Code Monkey home page Code Monkey logo

pytorch-summary's Introduction

Use the new and updated torchinfo.

Keras style model.summary() in PyTorch

PyPI version

Keras has a neat API to view the visualization of the model which is very helpful while debugging your network. Here is a barebone code to try and mimic the same in PyTorch. The aim is to provide information complementary to, what is not provided by print(your_model) in PyTorch.

Usage

  • pip install torchsummary or
  • git clone https://github.com/sksq96/pytorch-summary
from torchsummary import summary
summary(your_model, input_size=(channels, H, W))
  • Note that the input_size is required to make a forward pass through the network.

Examples

CNN for MNIST

import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # PyTorch v0.4.0
model = Net().to(device)

summary(model, (1, 28, 28))
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1           [-1, 10, 24, 24]             260
            Conv2d-2             [-1, 20, 8, 8]           5,020
         Dropout2d-3             [-1, 20, 8, 8]               0
            Linear-4                   [-1, 50]          16,050
            Linear-5                   [-1, 10]             510
================================================================
Total params: 21,840
Trainable params: 21,840
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.06
Params size (MB): 0.08
Estimated Total Size (MB): 0.15
----------------------------------------------------------------

VGG16

import torch
from torchvision import models
from torchsummary import summary

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
vgg = models.vgg16().to(device)

summary(vgg, (3, 224, 224))
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1         [-1, 64, 224, 224]           1,792
              ReLU-2         [-1, 64, 224, 224]               0
            Conv2d-3         [-1, 64, 224, 224]          36,928
              ReLU-4         [-1, 64, 224, 224]               0
         MaxPool2d-5         [-1, 64, 112, 112]               0
            Conv2d-6        [-1, 128, 112, 112]          73,856
              ReLU-7        [-1, 128, 112, 112]               0
            Conv2d-8        [-1, 128, 112, 112]         147,584
              ReLU-9        [-1, 128, 112, 112]               0
        MaxPool2d-10          [-1, 128, 56, 56]               0
           Conv2d-11          [-1, 256, 56, 56]         295,168
             ReLU-12          [-1, 256, 56, 56]               0
           Conv2d-13          [-1, 256, 56, 56]         590,080
             ReLU-14          [-1, 256, 56, 56]               0
           Conv2d-15          [-1, 256, 56, 56]         590,080
             ReLU-16          [-1, 256, 56, 56]               0
        MaxPool2d-17          [-1, 256, 28, 28]               0
           Conv2d-18          [-1, 512, 28, 28]       1,180,160
             ReLU-19          [-1, 512, 28, 28]               0
           Conv2d-20          [-1, 512, 28, 28]       2,359,808
             ReLU-21          [-1, 512, 28, 28]               0
           Conv2d-22          [-1, 512, 28, 28]       2,359,808
             ReLU-23          [-1, 512, 28, 28]               0
        MaxPool2d-24          [-1, 512, 14, 14]               0
           Conv2d-25          [-1, 512, 14, 14]       2,359,808
             ReLU-26          [-1, 512, 14, 14]               0
           Conv2d-27          [-1, 512, 14, 14]       2,359,808
             ReLU-28          [-1, 512, 14, 14]               0
           Conv2d-29          [-1, 512, 14, 14]       2,359,808
             ReLU-30          [-1, 512, 14, 14]               0
        MaxPool2d-31            [-1, 512, 7, 7]               0
           Linear-32                 [-1, 4096]     102,764,544
             ReLU-33                 [-1, 4096]               0
          Dropout-34                 [-1, 4096]               0
           Linear-35                 [-1, 4096]      16,781,312
             ReLU-36                 [-1, 4096]               0
          Dropout-37                 [-1, 4096]               0
           Linear-38                 [-1, 1000]       4,097,000
================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 218.59
Params size (MB): 527.79
Estimated Total Size (MB): 746.96
----------------------------------------------------------------

Multiple Inputs

import torch
import torch.nn as nn
from torchsummary import summary

class SimpleConv(nn.Module):
    def __init__(self):
        super(SimpleConv, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
        )

    def forward(self, x, y):
        x1 = self.features(x)
        x2 = self.features(y)
        return x1, x2
    
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = SimpleConv().to(device)

summary(model, [(1, 16, 16), (1, 28, 28)])
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1            [-1, 1, 16, 16]              10
              ReLU-2            [-1, 1, 16, 16]               0
            Conv2d-3            [-1, 1, 28, 28]              10
              ReLU-4            [-1, 1, 28, 28]               0
================================================================
Total params: 20
Trainable params: 20
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.77
Forward/backward pass size (MB): 0.02
Params size (MB): 0.00
Estimated Total Size (MB): 0.78
----------------------------------------------------------------

References

License

pytorch-summary is MIT-licensed.

pytorch-summary's People

Contributors

cgkim412 avatar coderx7 avatar flyhighest avatar greenmonn avatar lednahc avatar naireen avatar peakashu avatar rmchurch avatar sksq96 avatar system123 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytorch-summary's Issues

multiple inputs

For a network that requires multiple inputs,
model(x) should change to model(*x)

Block not unfolded

Trying to get summary from resnet18, for some reason BasicBlock-* are have 0 params.

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1         [-1, 64, 112, 112]           9,408
       BatchNorm2d-2         [-1, 64, 112, 112]             128
              ReLU-3         [-1, 64, 112, 112]               0
         MaxPool2d-4           [-1, 64, 56, 56]               0
            Conv2d-5           [-1, 64, 56, 56]          36,864
       BatchNorm2d-6           [-1, 64, 56, 56]             128
              ReLU-7           [-1, 64, 56, 56]               0
            Conv2d-8           [-1, 64, 56, 56]          36,864
       BatchNorm2d-9           [-1, 64, 56, 56]             128
             ReLU-10           [-1, 64, 56, 56]               0
       BasicBlock-11           [-1, 64, 56, 56]               0
           Conv2d-12           [-1, 64, 56, 56]          36,864
      BatchNorm2d-13           [-1, 64, 56, 56]             128
             ReLU-14           [-1, 64, 56, 56]               0
           Conv2d-15           [-1, 64, 56, 56]          36,864
      BatchNorm2d-16           [-1, 64, 56, 56]             128
             ReLU-17           [-1, 64, 56, 56]               0
       BasicBlock-18           [-1, 64, 56, 56]               0
           Conv2d-19          [-1, 128, 28, 28]          73,728
      BatchNorm2d-20          [-1, 128, 28, 28]             256
             ReLU-21          [-1, 128, 28, 28]               0
           Conv2d-22          [-1, 128, 28, 28]         147,456
      BatchNorm2d-23          [-1, 128, 28, 28]             256
           Conv2d-24          [-1, 128, 28, 28]           8,192
      BatchNorm2d-25          [-1, 128, 28, 28]             256
             ReLU-26          [-1, 128, 28, 28]               0
       BasicBlock-27          [-1, 128, 28, 28]               0
           Conv2d-28          [-1, 128, 28, 28]         147,456
      BatchNorm2d-29          [-1, 128, 28, 28]             256
             ReLU-30          [-1, 128, 28, 28]               0
           Conv2d-31          [-1, 128, 28, 28]         147,456
      BatchNorm2d-32          [-1, 128, 28, 28]             256
             ReLU-33          [-1, 128, 28, 28]               0
       BasicBlock-34          [-1, 128, 28, 28]               0
           Conv2d-35          [-1, 256, 14, 14]         294,912
      BatchNorm2d-36          [-1, 256, 14, 14]             512
             ReLU-37          [-1, 256, 14, 14]               0
           Conv2d-38          [-1, 256, 14, 14]         589,824
      BatchNorm2d-39          [-1, 256, 14, 14]             512
           Conv2d-40          [-1, 256, 14, 14]          32,768
      BatchNorm2d-41          [-1, 256, 14, 14]             512
             ReLU-42          [-1, 256, 14, 14]               0
       BasicBlock-43          [-1, 256, 14, 14]               0
           Conv2d-44          [-1, 256, 14, 14]         589,824
      BatchNorm2d-45          [-1, 256, 14, 14]             512
             ReLU-46          [-1, 256, 14, 14]               0
           Conv2d-47          [-1, 256, 14, 14]         589,824
      BatchNorm2d-48          [-1, 256, 14, 14]             512
             ReLU-49          [-1, 256, 14, 14]               0
       BasicBlock-50          [-1, 256, 14, 14]               0
           Conv2d-51            [-1, 512, 7, 7]       1,179,648
      BatchNorm2d-52            [-1, 512, 7, 7]           1,024
             ReLU-53            [-1, 512, 7, 7]               0
           Conv2d-54            [-1, 512, 7, 7]       2,359,296
      BatchNorm2d-55            [-1, 512, 7, 7]           1,024
           Conv2d-56            [-1, 512, 7, 7]         131,072
      BatchNorm2d-57            [-1, 512, 7, 7]           1,024
             ReLU-58            [-1, 512, 7, 7]               0
       BasicBlock-59            [-1, 512, 7, 7]               0
           Conv2d-60            [-1, 512, 7, 7]       2,359,296
      BatchNorm2d-61            [-1, 512, 7, 7]           1,024
             ReLU-62            [-1, 512, 7, 7]               0
           Conv2d-63            [-1, 512, 7, 7]       2,359,296
      BatchNorm2d-64            [-1, 512, 7, 7]           1,024
             ReLU-65            [-1, 512, 7, 7]               0
       BasicBlock-66            [-1, 512, 7, 7]               0
        AvgPool2d-67            [-1, 512, 1, 1]               0
           Linear-68                 [-1, 1000]         513,000
================================================================
Total params: 11,689,512
Trainable params: 11,689,512
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 62.79
Params size (MB): 44.59
Estimated Total Size (MB): 107.96
----------------------------------------------------------------

TypeError: apply() missing 1 required positional argument: 'fn'

I tried this code

from torchsummary import summary
summary(WideSeg, input_size=(1,640,512))

and I got this error message

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-6b714975e255> in <module>()
      1 from torchsummary import summary
----> 2 summary(WideSeg, input_size=(1,640,512))

~\Anaconda3\lib\site-packages\torchsummary\torchsummary.py in summary(model, input_size)
     51         hooks = []
     52         # register hook
---> 53         model.apply(register_hook)
     54         # make a forward pass
     55         # print(x.shape)

TypeError: apply() missing 1 required positional argument: 'fn'

Extra dimension in input size

Hello,

Why is an extra dimension of length 2 added to the input size for the variable to be forward passed?

x = Variable(torch.rand(2,*input_size)).type(dtype)

When attempting to run summary on my model, I get the following error as a result:

RuntimeError: Expected 4-dimensional weight for 4-dimensional input [2, 1, 513, 20000], but got weight of size [20, 513, 1] instead

Thanks

Weights of custom layers not counted

I have created my own custom layer or module and found its parameters are not counted properly in the summary.
Looking at the code, I found this could be the reason:

            if hasattr(module, "weight") and hasattr(module.weight, "size"):
                params += torch.prod(torch.LongTensor(list(module.weight.size())))
                summary[m_key]["trainable"] = module.weight.requires_grad
            if hasattr(module, "bias") and hasattr(module.bias, "size"):
                params += torch.prod(torch.LongTensor(list(module.bias.size())))

So basically it is looking for parameters named weight or bias. I think it will be great if the code is updated to handle generic trainable parameter names.

summary() cannot be called after loading model

Hello,

I just want to share a problem that I've spent 5 hours debugging.

the summary() function cannot be called after loading model state, because it changes some weights (I didnt inspect how, or which - I was too frustrated). Maybe the easiest fix is to set model.eval() before forward pass and then set it back to whatever value it was.

So for those who are getting worse results after loading model than before saving - If you need to use it, use it before loading weights to model.

python2 import error in SyntaxError

using pip install
error when from torchsummary import summary

>>> from torchsummary import summary
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/qiaoran/python/python2/lib/python2.7/site-packages/torchsummary/__init__.py", line 1, in <module>
    from .torchsummary import summary
  File "/home/qiaoran/python/python2/lib/python2.7/site-packages/torchsummary/torchsummary.py", line 9
    def summary(model, *input_size, batch_size=-1, device="cuda"):
                                             ^
SyntaxError: invalid syntax

When running DenseNet and MobileNet I got the following errors

MobileNet:
Traceback (most recent call last):
File "MobileNet.py", line 365, in
summary(model, (3,32,32))
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torchsummary/torchsummary.py", line 56, in summary
model(x)
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in call
result = self.forward(*input, **kwargs)
File "MobileNet.py", line 114, in forward
x = self.model(x)
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in call
result = self.forward(*input, **kwargs)
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torch/nn/modules/container.py", line 67, in forward
input = module(input)
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in call
result = self.forward(*input, **kwargs)
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torch/nn/modules/container.py", line 67, in forward
input = module(input)
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 359, in call
hook_result = hook(self, input, result)
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torchsummary/torchsummary.py", line 28, in hook
params += th.prod(th.LongTensor(list(module.bias.size())))
AttributeError: 'NoneType' object has no attribute 'size'

DenseNet:
Traceback (most recent call last):
File "DenseNet.py", line 237, in
summary(model, (3,32,32))
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torchsummary/torchsummary.py", line 56, in summary
model(x)
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in call
result = self.forward(*input, **kwargs)
File "DenseNet.py", line 226, in forward
features = self.features(x)
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in call
result = self.forward(*input, **kwargs)
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torch/nn/modules/container.py", line 67, in forward
input = module(input)
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 359, in call
hook_result = hook(self, input, result)
File "/home/sriharsha/miniconda3/lib/python3.6/site-packages/torchsummary/torchsummary.py", line 28, in hook
params += th.prod(th.LongTensor(list(module.bias.size())))
AttributeError: 'bool' object has no attribute 'size'

I want format like this

Type                 Name                                          Param               Output
----------------------------------------------------------------------------------------------
Data                 data                                             --     (1, 3, 512, 512)
Convolution          conv1                                 (64, 3, 7, 7)    (1, 64, 256, 256)
Convolution          conv1                                         (64,)    (1, 64, 256, 256)
Pooling              pool1                                            --    (1, 64, 128, 128)
Convolution          conv2a_branch2a                      (64, 64, 3, 3)    (1, 64, 128, 128)
Convolution          conv2a_branch2a                               (64,)    (1, 64, 128, 128)
Convolution          conv2a_branch2b                      (64, 64, 3, 3)    (1, 64, 128, 128)
Convolution          conv2a_branch2b                               (64,)    (1, 64, 128, 128)

What Should I Do?

Got error when using with RNN model

I gt error when applying to RNN (BiLSTM) model.
The detail:
File "/home/lucas/HW_FixField/git_source/rnd_fixed_form_hw_ocr/main_source/models/model_rcnn.py", line 15, in forward
recurrent, _ = self.rnn(input)
File "/home/lucas/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 479, in call
hook_result = hook(self, input, result)
File "/home/lucas/HW_FixField/git_source/rnd_fixed_form_hw_ocr/main_source/utils/sumaryModel.py", line 23, in hook
[-1] + list(o.size())[1:] for o in output
File "/home/lucas/HW_FixField/git_source/rnd_fixed_form_hw_ocr/main_source/utils/sumaryModel.py", line 23, in
[-1] + list(o.size())[1:] for o in output
AttributeError: 'tuple' object has no attribute 'size'

The source code of RNN model:
self.rnn = nn.Sequential(
BidirectionalLSTM(512, nh, nh),
BidirectionalLSTM(nh, nh, nclass))

Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

Hi,

I'm getting the following error while trying to print model summary. The model, however, is training without an issue (when not printing the summary)

...
    print(summary(model, (3, 30, 16)))
...
Traceback (most recent call last):
  File "slideCNN-train.py", line 57, in <module>
    print(summary(model, (3, 30, 16)))
  File "/home/yeshwanth/.local/lib/python2.7/site-packages/torchsummary/torchsummary.py", line 56, in summary
    model(x)
  File "/usr/lib64/python2.7/site-packages/torch/nn/modules/module.py", line 491, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/lib64/python2.7/site-packages/torch/nn/modules/container.py", line 91, in forward
    input = module(input)
  File "/usr/lib64/python2.7/site-packages/torch/nn/modules/module.py", line 491, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/lib64/python2.7/site-packages/torch/nn/modules/conv.py", line 301, in forward
    self.padding, self.dilation, self.groups)
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

Unable to return parameters for Basic Block of DRN Network

Hi, In DRN [https://github.com/fyu/drn] there is basic block which consists of two series of conv and batchnorm layers for which number of parameters is returned is zero. Can you please look into this issue? Can you also add parameter indicating the Memory size required for Deployment(forward pass only)


       Layer (type)               Output Shape         Param 
================================================================
            Conv2d-1        [-1, 16, 512, 1024]           2,352
       BatchNorm2d-2        [-1, 16, 512, 1024]              32
              ReLU-3        [-1, 16, 512, 1024]               0
            Conv2d-4        [-1, 16, 512, 1024]           2,304
       BatchNorm2d-5        [-1, 16, 512, 1024]              32
              ReLU-6        [-1, 16, 512, 1024]               0
            Conv2d-7         [-1, 32, 256, 512]           4,608
       BatchNorm2d-8         [-1, 32, 256, 512]              64
              ReLU-9         [-1, 32, 256, 512]               0
           Conv2d-10         [-1, 64, 128, 256]          18,432
      BatchNorm2d-11         [-1, 64, 128, 256]             128
             ReLU-12         [-1, 64, 128, 256]               0
           Conv2d-13         [-1, 64, 128, 256]          36,864
      BatchNorm2d-14         [-1, 64, 128, 256]             128
           Conv2d-15         [-1, 64, 128, 256]           2,048
      BatchNorm2d-16         [-1, 64, 128, 256]             128
             ReLU-17         [-1, 64, 128, 256]               0
       BasicBlock-18         [-1, 64, 128, 256]               0
           Conv2d-19         [-1, 64, 128, 256]          36,864
      BatchNorm2d-20         [-1, 64, 128, 256]             128
             ReLU-21         [-1, 64, 128, 256]               0
           Conv2d-22         [-1, 64, 128, 256]          36,864
      BatchNorm2d-23         [-1, 64, 128, 256]             128
             ReLU-24         [-1, 64, 128, 256]               0
       BasicBlock-25         [-1, 64, 128, 256]               0
           Conv2d-26         [-1, 64, 128, 256]          36,864
      BatchNorm2d-27         [-1, 64, 128, 256]             128
             ReLU-28         [-1, 64, 128, 256]               0
           Conv2d-29         [-1, 64, 128, 256]          36,864
      BatchNorm2d-30         [-1, 64, 128, 256]             128
             ReLU-31         [-1, 64, 128, 256]               0
       BasicBlock-32         [-1, 64, 128, 256]               0
           Conv2d-33         [-1, 128, 64, 128]          73,728
      BatchNorm2d-34         [-1, 128, 64, 128]             256
             ReLU-35         [-1, 128, 64, 128]               0
           Conv2d-36         [-1, 128, 64, 128]         147,456
      BatchNorm2d-37         [-1, 128, 64, 128]             256
           Conv2d-38         [-1, 128, 64, 128]           8,192
      BatchNorm2d-39         [-1, 128, 64, 128]             256
             ReLU-40         [-1, 128, 64, 128]               0
       BasicBlock-41         [-1, 128, 64, 128]               0
           Conv2d-42         [-1, 128, 64, 128]         147,456
      BatchNorm2d-43         [-1, 128, 64, 128]             256
             ReLU-44         [-1, 128, 64, 128]               0
           Conv2d-45         [-1, 128, 64, 128]         147,456
      BatchNorm2d-46         [-1, 128, 64, 128]             256
             ReLU-47         [-1, 128, 64, 128]               0
       BasicBlock-48         [-1, 128, 64, 128]               0
           Conv2d-49         [-1, 128, 64, 128]         147,456
      BatchNorm2d-50         [-1, 128, 64, 128]             256
             ReLU-51         [-1, 128, 64, 128]               0
           Conv2d-52         [-1, 128, 64, 128]         147,456
      BatchNorm2d-53         [-1, 128, 64, 128]             256
             ReLU-54         [-1, 128, 64, 128]               0
       BasicBlock-55         [-1, 128, 64, 128]               0
           Conv2d-56         [-1, 128, 64, 128]         147,456
      BatchNorm2d-57         [-1, 128, 64, 128]             256
             ReLU-58         [-1, 128, 64, 128]               0
           Conv2d-59         [-1, 128, 64, 128]         147,456
      BatchNorm2d-60         [-1, 128, 64, 128]             256
             ReLU-61         [-1, 128, 64, 128]               0
       BasicBlock-62         [-1, 128, 64, 128]               0
           Conv2d-63         [-1, 256, 64, 128]         294,912
      BatchNorm2d-64         [-1, 256, 64, 128]             512
             ReLU-65         [-1, 256, 64, 128]               0
           Conv2d-66         [-1, 256, 64, 128]         589,824
      BatchNorm2d-67         [-1, 256, 64, 128]             512
           Conv2d-68         [-1, 256, 64, 128]          32,768
      BatchNorm2d-69         [-1, 256, 64, 128]             512
             ReLU-70         [-1, 256, 64, 128]               0
       BasicBlock-71         [-1, 256, 64, 128]               0
           Conv2d-72         [-1, 256, 64, 128]         589,824
      BatchNorm2d-73         [-1, 256, 64, 128]             512
             ReLU-74         [-1, 256, 64, 128]               0
           Conv2d-75         [-1, 256, 64, 128]         589,824
      BatchNorm2d-76         [-1, 256, 64, 128]             512
             ReLU-77         [-1, 256, 64, 128]               0
       BasicBlock-78         [-1, 256, 64, 128]               0
           Conv2d-79         [-1, 256, 64, 128]         589,824
      BatchNorm2d-80         [-1, 256, 64, 128]             512
             ReLU-81         [-1, 256, 64, 128]               0
           Conv2d-82         [-1, 256, 64, 128]         589,824
      BatchNorm2d-83         [-1, 256, 64, 128]             512
             ReLU-84         [-1, 256, 64, 128]               0
       BasicBlock-85         [-1, 256, 64, 128]               0
           Conv2d-86         [-1, 256, 64, 128]         589,824
      BatchNorm2d-87         [-1, 256, 64, 128]             512
             ReLU-88         [-1, 256, 64, 128]               0
           Conv2d-89         [-1, 256, 64, 128]         589,824
      BatchNorm2d-90         [-1, 256, 64, 128]             512
             ReLU-91         [-1, 256, 64, 128]               0
       BasicBlock-92         [-1, 256, 64, 128]               0
           Conv2d-93         [-1, 256, 64, 128]         589,824
      BatchNorm2d-94         [-1, 256, 64, 128]             512
             ReLU-95         [-1, 256, 64, 128]               0
           Conv2d-96         [-1, 256, 64, 128]         589,824
      BatchNorm2d-97         [-1, 256, 64, 128]             512
             ReLU-98         [-1, 256, 64, 128]               0
       BasicBlock-99         [-1, 256, 64, 128]               0
          Conv2d-100         [-1, 256, 64, 128]         589,824
     BatchNorm2d-101         [-1, 256, 64, 128]             512
            ReLU-102         [-1, 256, 64, 128]               0
          Conv2d-103         [-1, 256, 64, 128]         589,824
     BatchNorm2d-104         [-1, 256, 64, 128]             512
            ReLU-105         [-1, 256, 64, 128]               0
      BasicBlock-106         [-1, 256, 64, 128]               0
          Conv2d-107         [-1, 512, 64, 128]       1,179,648
     BatchNorm2d-108         [-1, 512, 64, 128]           1,024
            ReLU-109         [-1, 512, 64, 128]               0
          Conv2d-110         [-1, 512, 64, 128]       2,359,296
     BatchNorm2d-111         [-1, 512, 64, 128]           1,024
          Conv2d-112         [-1, 512, 64, 128]         131,072
     BatchNorm2d-113         [-1, 512, 64, 128]           1,024
            ReLU-114         [-1, 512, 64, 128]               0
      BasicBlock-115         [-1, 512, 64, 128]               0
          Conv2d-116         [-1, 512, 64, 128]       2,359,296
     BatchNorm2d-117         [-1, 512, 64, 128]           1,024
            ReLU-118         [-1, 512, 64, 128]               0
          Conv2d-119         [-1, 512, 64, 128]       2,359,296
     BatchNorm2d-120         [-1, 512, 64, 128]           1,024
            ReLU-121         [-1, 512, 64, 128]               0
      BasicBlock-122         [-1, 512, 64, 128]               0
          Conv2d-123         [-1, 512, 64, 128]       2,359,296
     BatchNorm2d-124         [-1, 512, 64, 128]           1,024
            ReLU-125         [-1, 512, 64, 128]               0
          Conv2d-126         [-1, 512, 64, 128]       2,359,296
     BatchNorm2d-127         [-1, 512, 64, 128]           1,024
            ReLU-128         [-1, 512, 64, 128]               0
      BasicBlock-129         [-1, 512, 64, 128]               0
          Conv2d-130         [-1, 512, 64, 128]       2,359,296
     BatchNorm2d-131         [-1, 512, 64, 128]           1,024
            ReLU-132         [-1, 512, 64, 128]               0
          Conv2d-133         [-1, 512, 64, 128]       2,359,296
     BatchNorm2d-134         [-1, 512, 64, 128]           1,024
            ReLU-135         [-1, 512, 64, 128]               0
          Conv2d-136          [-1, 19, 64, 128]           9,747
 ConvTranspose2d-137        [-1, 19, 512, 1024]           4,864
      LogSoftmax-138        [-1, 19, 512, 1024]               0
================================================================

Syntax error during importing

I got a syntax error just by importing

from torchsummary import summary

 File "main.py", line 21, in <module>
    from torchsummary import summary
  File "/home/ivan/vox/torchsummary/__init__.py", line 1, in <module>
    from .torchsummary import summary
  File "/home/ivan/vox/torchsummary/torchsummary.py", line 9
    def summary(model, *input_size, batch_size=-1, device="cuda"):
                                             ^
SyntaxError: invalid syntax

Could you suggest where to look for the catch? Thanks

whats wrong with it

File "main.py", line 301, in
summary(model.to(hyperparams['device']), input.size()[1:], device=hyperparams['device'])
File "/home/anaconda3/lib/python3.6/site-packages/torchsummary/torchsummary.py", line 44, in summary
device = device.lower()

AttributeError: 'torch.device' object has no attribute 'lower'

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

Traceback (most recent call last):
File "examples.py", line 566, in
ppo_pixel_atari(game)
File "examples.py", line 391, in ppo_pixel_atari
run_steps(PPOAgent(config))
File "/home/simon/Desktop/DeepRL.old/deep_rl/agent/PPO_agent.py", line 25, in init
torchsummary.summary(self.network,(4, 84, 84))
File "/home/simon/anaconda3/lib/python3.6/site-packages/torchsummary/torchsummary.py", line 72, in summary
model(*x)
File "/home/simon/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in call
result = self.forward(*input, **kwargs)
File "/home/simon/Desktop/DeepRL.old/deep_rl/network/network_heads.py", line 173, in forward
phi = self.network.phi_body(obs)
File "/home/simon/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in call
result = self.forward(*input, **kwargs)
File "/home/simon/Desktop/DeepRL.old/deep_rl/network/network_bodies.py", line 19, in forward
y = F.relu(self.conv1(x))
File "/home/simon/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in call
result = self.forward(*input, **kwargs)
File "/home/simon/anaconda3/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 320, in forward
self.padding, self.dilation, self.groups)
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

summary error when nn.LSTM(batch_first=True)

File "C:\Users\simon\Desktop\DeepRL.old\deep_rl\agent\PPO_agent.py", line 25, in init
torchsummary.summary(self.network,(100, 2, 11))
......
File "C:\Users\simon\Desktop\DeepRL.old\deep_rl\network\network_bodies.py", line 149, in forward
y, (h_n,c_n) = self.lstm1(x)
File "C:\Anaconda\lib\site-packages\torch\nn\modules\module.py", line 491, in call
hook_result = hook(self, input, result)
File "C:\Anaconda\lib\site-packages\torchsummary\torchsummary.py", line 23, in hook
[-1] + list(o.size())[1:] for o in output
File "C:\Anaconda\lib\site-packages\torchsummary\torchsummary.py", line 23, in
[-1] + list(o.size())[1:] for o in output
AttributeError: 'tuple' object has no attribute 'size'

where:
self.lstm1 = nn_init(nn.LSTM(input_size=self.trace_length, hidden_size=self.n_lstm_units,batch_first=True))

summary() is work for the correct network.

Here is my network.

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import init
from torch.nn import modules
torch.backends.cudnn.version()

def init_weights(net, init_type='normal', gain=0.02):
    def init_func(m):
        classname = m.__class__.__name__
        if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
            if init_type == 'normal':
                init.normal_(m.weight.data, 0.0, gain)
            elif init_type == 'xavier':
                init.xavier_normal_(m.weight.data, gain=gain)
            elif init_type == 'kaiming':
                init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
            elif init_type == 'orthogonal':
                init.orthogonal_(m.weight.data, gain=gain)
            else:
                raise NotImplementedError(
                    'initialization method [%s] is not implemented' % init_type)
            if hasattr(m, 'bias') and m.bias is not None:
                init.constant_(m.bias.data, 0.0)
        elif classname.find('BatchNorm2d') != -1:
            init.normal_(m.weight.data, 1.0, gain)
            init.constant_(m.bias.data, 0.0)

    print('initialize network with %s' % init_type)
    net.apply(init_func)


class conv_block(nn.Module):
    def __init__(self, ch_in, ch_out):
        super(conv_block, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(ch_in, ch_out, kernel_size=3,
                      stride=1, padding=1, bias=True),
            nn.BatchNorm2d(ch_out),
            nn.ReLU(inplace=True),
            nn.Conv2d(ch_out, ch_out, kernel_size=3,
                      stride=1, padding=1, bias=True),
            nn.BatchNorm2d(ch_out),
            nn.ReLU(inplace=True)
        )

    def forward(self, x):
        x = self.conv(x)
        return x


class up_conv(nn.Module):
    def __init__(self, ch_in, ch_out):
        super(up_conv, self).__init__()
        self.up = nn.Sequential(
            nn.Upsample(scale_factor=2),
            nn.Conv2d(ch_in, ch_out, kernel_size=3,
                      stride=1, padding=1, bias=True),
            nn.BatchNorm2d(ch_out),
            nn.ReLU(inplace=True)
        )

    def forward(self, x):
        x = self.up(x)
        return x


class Attention_block(nn.Module):
    def __init__(self, F_g, F_l, F_int):
        super(Attention_block, self).__init__()
        self.W_g = nn.Sequential(
            nn.Conv2d(F_g, F_int, kernel_size=1,
                      stride=1, padding=0, bias=True),
            nn.BatchNorm2d(F_int)
        )

        self.W_x = nn.Sequential(
            nn.Conv2d(F_l, F_int, kernel_size=1,
                      stride=1, padding=0, bias=True),
            nn.BatchNorm2d(F_int)
        )

        self.psi = nn.Sequential(
            nn.Conv2d(F_int, 1, kernel_size=1, stride=1, padding=0, bias=True),
            nn.BatchNorm2d(1),
            nn.Sigmoid()
        )

        self.relu = nn.ReLU(inplace=True)

    def forward(self, g, x):
        g1 = self.W_g(g)
        x1 = self.W_x(x)
        psi = self.relu(g1+x1)
        psi = self.psi(psi)

        return x*psi


class AttU_Net(nn.Module):
    def __init__(self, img_ch=3, output_ch=3):
        super(AttU_Net, self).__init__()

        self.Maxpool = nn.MaxPool2d(kernel_size=2, stride=2)

        self.Conv1 = conv_block(ch_in=img_ch, ch_out=64)
        self.Conv2 = conv_block(ch_in=64, ch_out=128)
        self.Conv3 = conv_block(ch_in=128, ch_out=256)
        self.Conv4 = conv_block(ch_in=256, ch_out=512)
        self.Conv5 = conv_block(ch_in=512, ch_out=1024)

        self.Up5 = up_conv(ch_in=1024, ch_out=512)
        self.Att5 = Attention_block(F_g=512, F_l=512, F_int=256)
        self.Up_conv5 = conv_block(ch_in=1024, ch_out=512)

        self.Up4 = up_conv(ch_in=512, ch_out=256)
        self.Att4 = Attention_block(F_g=256, F_l=256, F_int=128)
        self.Up_conv4 = conv_block(ch_in=512, ch_out=256)

        self.Up3 = up_conv(ch_in=256, ch_out=128)
        self.Att3 = Attention_block(F_g=128, F_l=128, F_int=64)
        self.Up_conv3 = conv_block(ch_in=256, ch_out=128)

        self.Up2 = up_conv(ch_in=128, ch_out=64)
        self.Att2 = Attention_block(F_g=64, F_l=64, F_int=32)
        self.Up_conv2 = conv_block(ch_in=128, ch_out=64)

        self.Conv_1x1 = nn.Conv2d(
            64, output_ch, kernel_size=1, stride=1, padding=0)

    def forward(self, x):
        # encoding path
        x1 = self.Conv1(x)

        x2 = self.Maxpool(x1)
        x2 = self.Conv2(x2)

        x3 = self.Maxpool(x2)
        x3 = self.Conv3(x3)

        x4 = self.Maxpool(x3)
        x4 = self.Conv4(x4)

        x5 = self.Maxpool(x4)
        x5 = self.Conv5(x5)

        # decoding + concat path
        d5 = self.Up5(x5)
        x4 = self.Att5(g=d5, x=x4)
        d5 = torch.cat((x4, d5), dim=1)
        d5 = self.Up_conv5(d5)

        d4 = self.Up4(d5)
        x3 = self.Att4(g=d4, x=x3)
        d4 = torch.cat((x3, d4), dim=1)
        d4 = self.Up_conv4(d4)

        d3 = self.Up3(d4)
        x2 = self.Att3(g=d3, x=x2)
        d3 = torch.cat((x2, d3), dim=1)
        d3 = self.Up_conv3(d3)

        d2 = self.Up2(d3)
        x1 = self.Att2(g=d2, x=x1)
        d2 = torch.cat((x1, d2), dim=1)
        d2 = self.Up_conv2(d2)

        d1 = self.Conv_1x1(d2)

        return d1

if __name__ == '__main__':
    from torchsummary import summary
    g = R2AttU_Net()
    t = torch.ones((3, 160, 160))
    print(summary(g, tuple([3, 160, 160])))

And when i run the code to check the model shape with summary,the bug is show ,but code is can train.how to solve it, would you help me?

Traceback (most recent call last):
  File "E:\work\project\Model\torch_unet3.py", line 446, in <module>
    print(summary(g, tuple([3, 160, 160])))
  File "D:\Software\Anaconda3\envs\torch\lib\site-packages\torchsummary\torchsummary.py", line 72, in summary
    model(*x)
  File "D:\Software\Anaconda3\envs\torch\lib\site-packages\torch\nn\modules\module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "E:\work\project\Model\torch_unet3.py", line 418, in forward
    x4 = self.Att5(g=d5, x=x4)
  File "D:\Software\Anaconda3\envs\torch\lib\site-packages\torch\nn\modules\module.py", line 491, in __call__
    hook_result = hook(self, input, result)
  File "D:\Software\Anaconda3\envs\torch\lib\site-packages\torchsummary\torchsummary.py", line 19, in hook
    summary[m_key]["input_shape"] = list(input[0].size())
IndexError: tuple index out of range
[Finished in 3.3s with exit code 1]

Summary and Size Estimation for models larger than available memory

I'm dealing with complex 3D models, which sometimes lead to problems of lack of memory. So, I would like to calculate the amount of memory required and see the model summary even for larger models to then decrease size accordingly. Can anyone kindly help me with this problem? It will help me with my models a lot.

I want to see summary and size estimation for models which are larger than the available memory in my PC.

Kindly help.

Thanks.

model.cuda() needs to be called for summary

The code or the documentation should have a check/notice about having to call model.cuda()

summary(D, (3, 32, 32))

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

But this works ok:

summary(D.cuda(), (3, 32, 32))

Add support for print_fn

Keras summary has a print_fn argument, so you can use it with logging, or writing into a file:

model.summary(print_fn=logging.info)

It would be nice to do the same instead of using print.

ERROR for embedding in pytorch.

File "/home/lkk/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 1454, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got torch.cuda.FloatTensor instead (while checking arguments for embedding)

I use the embedding in my code.
# Embedding embeddings = self.embedding(encoded_captions)

inputsize dimension runtime error

the inputsize is (channels,H,W),3-D dimension,if the input is 4 dimension ,for instance:
summary(model, (10,1,28,28))
then error comes:
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [10, 1, 5, 5], but got 5-dimensional input of size [2, 10, 1, 28, 28] instead
how to solve this problem?
@sksq96
thank you very much !

TypeError: summary() got an unexpected keyword argument 'input_size'

Trying to print summary of resnet18:

import torch

print('torch.__version__', torch.__version__)

m = torch.load('imagenet_resnet18.pth')

from torchsummary import summary
summary(m, input_size=(3, 224, 224))

I get error:
TypeError: summary() got an unexpected keyword argument 'input_size'

GPU memory problem with GN(Group Normalization)

Hi! Thanks for your great work.
I built a U-net model and used torch-summary to check the GPU memory. When I used BN(Batch Normalization), it cost about 1.8G. But when I used GN(Group Normalization), it cost about 2.2G. When the ipunt is bigger, the difference of the GPU memory will also become bigger. But the problem is, pytorch-summary gave the same costs as below:

BN:(using: torch.nn.BatchNorm2d)
image

GN:(using: torch.nn.GroupNorm)
image

That is the only difference between my code. I can`t figure out why.

graphviz output

It would be great to have an option to generate graphviz output.

This would generate pretty output like pytorchviz, but with more information

Not working with Double Precision Networks

When trying to use the summary function on a network after calling net.double(), an error is returned:

RuntimeError: Expected object of type torch.FloatTensor but found type torch.DoubleTensor for argument #2 'mat2'

The calling code is:

net = FCNet(obs_space=checkpoint['env'].observation_space.shape[0], action_space=checkpoint['env'].action_space.n,
          shape=netConfig['shape'], activation=netConfig['activation'], dropout_rate=netConfig['dropout'],
          bias=netConfig['bias'])

if TENSOR_TYPE == torch.FloatTensor:
    net = net.float()
elif TENSOR_TYPE == torch.DoubleTensor:
    net = net.double()
netArch = buildNetArch(net)
summary(net, input_size=(1, checkpoint['env'].observation_space.shape[0]))

I've gotten this error outside of torchsummary in the past when trying to pass in tensors of the wrong type. It seems like torchsummary passes a FloatTensor through the network to get the metrics, but this doesn't work on a network specifically made to use DoubleTensors. Is this correct?

If so, I could maybe try and fix it myself.

[Question] size mismatch when converting to sequential

Hi, could you please help me understand what's going wrong here? I'm able to do:

import pretrainedmodels
model_name = 'alexnet'

model = pretrainedmodels.__dict__[model_name](pretrained=None)
summary(model, input_size=(3,224,224), batch_size=1, device='cpu')

But if I do:

model = pretrainedmodels.__dict__[model_name](pretrained=None)
model = nn.Sequential(*children(model)
summary(model, input_size=(3,224,224), batch_size=1, device='cpu')

I get a size mismatch. Any idea what I'm doing wrong?

Summary with embeddings

class TextCNN(nn.Module):
    def __init__(self, nb_words, embed_dim, embedding_matrix, max_seq_len, num_filters, num_classes):
        super(TextCNN, self).__init__()
        self.num_filters=num_filters
        self.embed = nn.Embedding(nb_words, embed_dim)
        self.dropout = nn.Dropout(0.3)
        self.conv = nn.Conv1d(embed_dim, num_filters, kernel_size=2, stride=1)
        self.fc1 = nn.Linear(num_filters, 32)
        self.fc2 = nn.Linear(32, num_classes)
        self.logsigmoid = nn.Sigmoid()

    def forward(self, x):
        x = self.embed(x)
        x = x.permute(0, 2, 1)
        x = self.dropout(x)
        x = self.conv(x).permute(0, 2, 1).max(1)[0]
        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout(x)
        x = self.fc2(x)
        return self.logsigmoid(x)
model = TextCNN(1000, 100, [], 10, 20, 3)
data = torch.from_numpy(np.array([[1,4,5], [7,7,9]]))
output = model(data)
print(output)
print(model)
print(data[0].shape)
print(summary(model, (1, 10)))

Gives error:

RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got CPUFloatTensor instead (while checking arguments for embedding)

Result is not expected, output size shrinked

Hi, I uses a nn.Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1)) and the output shape shrinks.

import torch.nn as nn

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        list = [nn.Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1)),
                nn.Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)),
                nn.Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1)),
                nn.Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)),
                nn.Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1)),
                nn.Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1)), ⬅
                nn.Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1)),
                nn.Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1))]
        self.f = nn.Sequential(*list)

    def forward(self, x):
        x = self.f(x)
        return x

net = Net()
summary(net, (1024, 19, 19))
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1          [-1, 256, 19, 19]          262400
            Conv2d-2          [-1, 512, 10, 10]         1180160
            Conv2d-3          [-1, 128, 10, 10]           65664
            Conv2d-4            [-1, 256, 5, 5]          295168
            Conv2d-5            [-1, 128, 5, 5]           32896 
            Conv2d-6            [-1, 256, 3, 3]          295168 ⬅
            Conv2d-7            [-1, 128, 3, 3]           32896
            Conv2d-8            [-1, 256, 1, 1]          295168
================================================================
Total params: 2459520
Trainable params: 2459520
Non-trainable params: 0
----------------------------------------------------------------

How to pass input_size for 1d input?

Hi - thanks for the library. I'm finding it very useful so far.

The one issue I'm having is that I'm unsure how to pass input_size for a 1d input. So if, for example, I want to run summary() on a simple feed-forward network with 512 input features, how would this be done? So far I've tried input_size=(512), input_size=(1, 512), input_size=(1, 1, 512), input_size=(512, 1) and input_size=(512, 1, 1) all of which result in errors.

Am I missing something simple here? Or is the 1d use-case just not supported at this point?

Summary break with sequential()

This model does not work with summary()

class Discriminator(nn.Module):
    def __init__(self, in_channels=3):
        super(Discriminator, self).__init__()

        def discriminator_block(in_filters, out_filters, normalize=True):
            """Returns downsampling layers of each discriminator block"""
            layers = [nn.Conv2d(in_filters, out_filters, 4, stride=2, padding=1)]
            if normalize:
                layers.append(nn.InstanceNorm2d(out_filters))
            layers.append(nn.LeakyReLU(0.2, inplace=True))
            return layers

        self.model = nn.Sequential(
            *discriminator_block(in_channels, 64, normalize=False),
            *discriminator_block(64, 128),
            *discriminator_block(128, 256),
            *discriminator_block(256, 512),
            nn.ZeroPad2d((1, 0, 1, 0)),
            nn.Conv2d(512, 1, 4, padding=1)
        )

    def forward(self, img):
        return self.model(img)

Traceback (most recent call last):
  File ".\test.py", line 118, in <module>
    summary(model, (3, 28, 28))
  File "C:\Users\bikas\Anaconda3\lib\site-packages\torchsummary\torchsummary.py", line 57, in summary
    model(x)
  File "C:\Users\bikas\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 491, in __call__
    result = self.forward(*input, **kwargs)
  File ".\test.py", line 93, in forward
    return self.model(img)
  File "C:\Users\bikas\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 491, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\bikas\Anaconda3\lib\site-packages\torch\nn\modules\container.py", line 91, in forward
    input = module(input)
  File "C:\Users\bikas\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 493, in __call__
    hook_result = hook(self, input, result)
  File "C:\Users\bikas\Anaconda3\lib\site-packages\torchsummary\torchsummary.py", line 26, in hook
    params += torch.prod(torch.LongTensor(list(module.weight.size())))
AttributeError: 'NoneType' object has no attribute 'size'

Non-trainable parameters seems to have not work


    Layer (type)               Output Shape         Param #

================================================================

            Conv2d-1          [1, 16, 240, 240]             160
       BatchNorm2d-2          [1, 16, 240, 240]               0
              ReLU-3          [1, 16, 240, 240]               0
            Conv2d-4          [1, 16, 240, 240]           2,320
       BatchNorm2d-5          [1, 16, 240, 240]               0
         MaxPool2d-6          [1, 16, 120, 120]               0
         Dropout2d-7          [1, 16, 120, 120]               0
          DownConv-8  [[-1, 16, 120, 120], [-1, 16, 240, 240]]               0
            Conv2d-9          [1, 32, 120, 120]           4,640
      BatchNorm2d-10          [1, 32, 120, 120]               0
             ReLU-11          [1, 32, 120, 120]               0
           Conv2d-12          [1, 32, 120, 120]           9,248
      BatchNorm2d-13          [1, 32, 120, 120]               0
        MaxPool2d-14            [1, 32, 60, 60]               0
        Dropout2d-15            [1, 32, 60, 60]               0
         DownConv-16  [[-1, 32, 60, 60], [-1, 32, 120, 120]]               0
           Conv2d-17            [1, 64, 60, 60]          18,496
      BatchNorm2d-18            [1, 64, 60, 60]               0
             ReLU-19            [1, 64, 60, 60]               0
           Conv2d-20            [1, 64, 60, 60]          36,928
      BatchNorm2d-21            [1, 64, 60, 60]               0
        MaxPool2d-22            [1, 64, 30, 30]               0
        Dropout2d-23            [1, 64, 30, 30]               0
         DownConv-24  [[-1, 64, 30, 30], [-1, 64, 60, 60]]               0
           Conv2d-25           [1, 128, 30, 30]          73,856
      BatchNorm2d-26           [1, 128, 30, 30]               0
             ReLU-27           [1, 128, 30, 30]               0
           Conv2d-28           [1, 128, 30, 30]         147,584
      BatchNorm2d-29           [1, 128, 30, 30]               0
        MaxPool2d-30           [1, 128, 15, 15]               0
        Dropout2d-31           [1, 128, 15, 15]               0
         DownConv-32  [[-1, 128, 15, 15], [-1, 128, 30, 30]]               0
           Conv2d-33           [1, 256, 15, 15]         295,168
      BatchNorm2d-34           [1, 256, 15, 15]               0
             ReLU-35           [1, 256, 15, 15]               0
           Conv2d-36           [1, 256, 15, 15]         590,080
      BatchNorm2d-37           [1, 256, 15, 15]               0
         DownConv-38  [[-1, 256, 15, 15], [-1, 256, 15, 15]]               0
  ConvTranspose2d-39           [1, 128, 30, 30]         131,200
        Dropout2d-40           [1, 256, 30, 30]               0
           Conv2d-41           [1, 128, 30, 30]         295,040
      BatchNorm2d-42           [1, 128, 30, 30]               0
           Conv2d-43           [1, 128, 30, 30]         147,584
      BatchNorm2d-44           [1, 128, 30, 30]               0
           UpConv-45           [1, 128, 30, 30]               0
  ConvTranspose2d-46            [1, 64, 60, 60]          32,832
        Dropout2d-47           [1, 128, 60, 60]               0
           Conv2d-48            [1, 64, 60, 60]          73,792
      BatchNorm2d-49            [1, 64, 60, 60]               0
           Conv2d-50            [1, 64, 60, 60]          36,928
      BatchNorm2d-51            [1, 64, 60, 60]               0
           UpConv-52            [1, 64, 60, 60]               0
  ConvTranspose2d-53          [1, 32, 120, 120]           8,224
        Dropout2d-54          [1, 64, 120, 120]               0
           Conv2d-55          [1, 32, 120, 120]          18,464
      BatchNorm2d-56          [1, 32, 120, 120]               0
           Conv2d-57          [1, 32, 120, 120]           9,248
      BatchNorm2d-58          [1, 32, 120, 120]               0
           UpConv-59          [1, 32, 120, 120]               0
  ConvTranspose2d-60          [1, 16, 240, 240]           2,064
        Dropout2d-61          [1, 32, 240, 240]               0
           Conv2d-62          [1, 16, 240, 240]           4,624
      BatchNorm2d-63          [1, 16, 240, 240]               0
           Conv2d-64          [1, 16, 240, 240]           2,320
      BatchNorm2d-65          [1, 16, 240, 240]               0
           UpConv-66          [1, 16, 240, 240]               0
           Conv2d-67           [1, 1, 240, 240]              17


================================================================
Total params: 1,940,817
Trainable params: 1,940,817
Non-trainable params: 0

----------------------------------------------------------------

Input size (MB): 0.22
Forward/backward pass size (MB): 2177055.62
Params size (MB): 7.40
Estimated Total Size (MB): 2177063.24

----------------------------------------------------------------

The BatchNorm2d I set affine is False, while there will some Non-trainable params in the Network,
while I do the same things in Keras, and I can't count the correct parameters number in training and estimated the batch size in the work,
What happed with it?
Thanks you,
Chris

Summary assumes Cuda

Summary assumes that the model is on the GPU if cuda is available, this is not always the case and should rather be left up to the user to specify if it should use the GPU or CPU.

Invalid syntax error caused by parameter *input_size in Python 2.7 .

Hello,
Was really pleased to find this repo to help visualize my neural networks. No errors encountered in python 3.6. However, I get the following error in python 2.7 .

Failing environment:
Ubuntu 16.04
python 2.7 (anaconda)
$ pip install torchsummary

Running the demo code provided by this repo gives:

Traceback:

Traceback (most recent call last):
  File "/home/dl/code/dljy-densepose/print_resnet.py", line 13, in <module>
    from torchsummary import summary
  File "/home/dl/anaconda3/envs/torch_py27/lib/python2.7/site-packages/torchsummary/__init__.py", line 1, in <module>
    from .torchsummary import summary
  File "/home/dl/anaconda3/envs/torch_py27/lib/python2.7/site-packages/torchsummary/torchsummary.py", line 9
    def summary(model, *input_size, batch_size=-1, device="cuda"):
                                             ^
SyntaxError: invalid syntax

A remedy:

# Remove astericks from *input_size in function signature:
def summary(model, input_size, batch_size=-1, device="cuda"):

     ... omitted code ...

     #model(*x)   # change this also.
     x = torch.rand(input_size)
     model(x)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.