sksq96 / pytorch-summary

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

Multi-input error #113

Closed TangChiaHsin closed 4 years ago

TangChiaHsin commented 4 years ago

I just try to summary my multi-input networks like README.md. The networks can forward correctly, but torchsummary report a mismatch error.

the module is listed as:

class SiameseNets(nn.Module):
    def __init__(self):
        super(SiameseNets, self).__init__()
        self.conv1 = nn.Conv2d(1, 64, 10)
        self.conv2 = nn.Conv2d(64, 128, 7)
        self.conv3 = nn.Conv2d(128, 128, 4)
        self.conv4 = nn.Conv2d(128, 256, 4)

        self.pooling = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(256, 4096)
        self.fc2 = nn.Linear(4096, 1)
        self.dropout = nn.Dropout(0.5)

    def forward(self, x1, x2):
        x1 = self.pooling(F.relu(self.conv1(x1)))
        x1 = self.pooling(F.relu(self.conv2(x1)))
        x1 = self.pooling(F.relu(self.conv3(x1)))
        x1 = self.pooling(F.relu(self.conv4(x1)))

        x2 = self.pooling(F.relu(self.conv1(x2)))
        x2 = self.pooling(F.relu(self.conv2(x2)))
        x2 = self.pooling(F.relu(self.conv3(x2)))
        x2 = self.pooling(F.relu(self.conv4(x2)))

        x1 = x1.view(-1)
        x2 = x2.view(-1)

        x1 = self.fc1(x1)
        x2 = self.fc1(x2)

        metric = torch.abs(x1 - x2)
        similarity = F.sigmoid(self.fc2(self.dropout(metric)))
        return similarity

Call function as: summary(net, [(1, 88, 88), (1, 88, 88)])

Error:

Traceback (most recent call last):
  File "/Users/***/opt/anaconda3/envs/pytorch/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-52-3e3d0d779b7b>", line 1, in <module>
    summary(net,[(1,88,88),(1,88,88)])
  File "/Users/***/opt/anaconda3/envs/pytorch/lib/python3.8/site-packages/torchsummary/torchsummary.py", line 72, in summary
    model(*x)
  File "/Users/***/opt/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/Users/***/PycharmProjects/ML/models.py", line 61, in forward
    x1 = self.fc1(x1)
  File "/Users/***/opt/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)

So, did I make any thing wrong? I will check my code again, and waiting your answer. Thx! XD

TangChiaHsin commented 4 years ago

Hello, it seems like that the listed code can not be recognize.

x1 = x1.view(-1)
x2 = x2.view(-1)

This operation is not added in summary() forward. So the mismatch error happens. It's a bug or just a feature? LOL, so how could I do to run my code successfully?

Inspired by Issue: Parameters used with functional API are not included #24

TylerYep commented 4 years ago

See #124

TangChiaHsin commented 4 years ago

See #124

thanks for your comment and code

I change torchsummary to torch-summary, but there is still something wrong. And my network structure and input size are referred in my previous comment.

Error:

Failed to run torchsummary, printing sizes of executed layers: [Conv2d: 1-1, MaxPool2d: 1-2, Conv2d: 1-3, MaxPool2d: 1-4, Conv2d: 1-5, MaxPool2d: 1-6, Conv2d: 1-7, MaxPool2d: 1-8, Conv2d: 1-9, MaxPool2d: 1-10, Conv2d: 1-11, MaxPool2d: 1-12, Conv2d: 1-13, MaxPool2d: 1-14, Conv2d: 1-15, MaxPool2d: 1-16]
Traceback (most recent call last):
  File "/Users/***/opt/anaconda3/envs/pytorch/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-da955e7f420f>", line 1, in <module>
    runfile('/Users/***/PycharmProjects/ML/models.py', wdir='/Users/***/PycharmProjects/ML')
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/***/PycharmProjects/ML/models.py", line 79, in <module>
    summary(net, [(1, 88, 88), (1, 88, 88)])
  File "/Users/***/opt/anaconda3/envs/pytorch/lib/python3.8/site-packages/torchsummary/torchsummary.py", line 74, in summary
    _ = model(*x, *args, **kwargs)
  File "/Users/***/opt/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/Users/***/PycharmProjects/ML/models.py", line 62, in forward
    x1 = self.fc1(x1)
  File "/Users/***/opt/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/Users/***/opt/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/linear.py", line 87, in forward
    return F.linear(input, self.weight, self.bias)
  File "/Users/***/opt/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/nn/functional.py", line 1372, in linear
    output = input.matmul(weight.t())
RuntimeError: size mismatch, m1: [1 x 512], m2: [256 x 4096] at /Users/distiller/project/conda/conda-bld/pytorch_1580186068235/work/aten/src/TH/generic/THTensorMath.cpp:136

The error tip shows that the size mismatch, but I have two tensors with size[256] instead of one tensor with size[1*512].

The two tensors with size[256] will be respectively inputted to a weight-shared full connected layer with size[256,4096].

I change my inputs to other sizes, the error: size mismatch happened in the same way. It seems that the calculation function computes two tensor as one.

TylerYep commented 4 years ago

This looks like a bug in your implementation of the model, not torch-summary. The line x1 = x1.view(-1) squeezes your input into a single dimension, which does not work with a Linear layer of size 256 if the batch size is not 1.

In particular, torch-summary uses a batch_size of 2 in order to calculate statistics for batchnorm layers.

To fix your model, I would recommend changing that view call to:

batch_size = x1.size(0)
x1 = x1.view(batch_size, -1)
x2 = x2.view(batch_size, -1)
TangChiaHsin commented 4 years ago

Much as gracias.

I follow your recommendation and it works successfully.

But the size of inputs seems not correct.

Input size (MB): 228.77
Forward/backward pass size (MB): 4.32
Params size (MB): 8.59
Estimated Total Size (MB): 241.68

The multi-inputs size is [(1, 88, 88), (1, 88, 88)].

TylerYep commented 4 years ago

I fixed the problem - you can run pip install --upgrade torch-summary to get the latest version.

TangChiaHsin commented 4 years ago

I fixed the problem - you can run pip install --upgrade torch-summary to get the latest version.

Thank you for your timely reply and modification. It's really impressive.