TylerYep / torchinfo

View model summaries in PyTorch!
MIT License
2.56k stars 119 forks source link

different with torchsummary #82

Closed ltm920716 closed 3 years ago

ltm920716 commented 3 years ago

hello, I use craft from https://github.com/clovaai/CRAFT-pytorch

the torchsummary output is: image

the torchinfo output is: image

there is a big different, please help

TylerYep commented 3 years ago

torchinfo should be more correct than torchsummary; torchsummary is not currently maintained. Feel free to reopen if you have any additional concerns.

ltm920716 commented 3 years ago

hi, @TylerYep this is a demo

################################# `import torch import torch.nn as nn import torch.nn.functional as F from torchinfo import summary as sm

device = torch.device('cpu') num_classes = 10

class LeNet(nn.Module): def init(self, in_channels, num_classes): super(LeNet, self).init() self.conv1 = nn.Conv2d(in_channels, 20, kernel_size=5, stride=1) # 20x24x24 self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2) # 20x12x12 self.conv2 = nn.Conv2d(20, 50, kernel_size=5, stride=1) # 50x8x8 self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2) # 50x4x4 self.fc1 = nn.Linear(50 4 4, 500) # 500 self.fc2 = nn.Linear(500, num_classes) # 10

def forward(self, input):
    out = self.conv1(input)
    out = self.pool1(out)
    out = self.conv2(out)
    out = self.pool2(out)
    out = out.reshape(out.size(0), -1) 
    out = F.relu(self.fc1(out))
    out = self.fc2(out)
    return out

model = LeNet(1, num_classes).to(device)

sm(model, input_size=(1, 1, 28, 28))` ################################

here is the output: image

and here is the ground-thurh:

So what the Forward/backward pass size (MB): 0.12 means? If I want to know the forward inference memory, could torchinfo show?

Thanks!

TylerYep commented 3 years ago

Note that memory calculation should be slightly more for a tensor, because torch.Tensor stores other information too, not just the 20x24x24 shape. We use sys.getsizeof(tensor) to get the most accurate size in MB, which works for all types of layers.

0.12 and 0.072 are close enough to explain the difference, but if you see an error in the calculation, feel free to open a PR!

ltm920716 commented 3 years ago

@TylerYep Great,thanks!

Yuxinyi-Qiyu commented 1 year ago

hey, sorry to bother you. I want to know how the "Params size" calculate. the params are 431080, so the Params_mem:431080 * (32/8) / 1024 / 1024 = 1.64 MB, why the "Params size" is 1.72 instead of 1.64?

mert-kurttutan commented 1 year ago

hey, sorry to bother you. I want to know how the "Params size" calculate. the params are 431080, so the Params_mem:431080 * (32/8) / 1024 / 1024 = 1.64 MB, why the "Params size" is 1.72 instead of 1.64?

Hi @Yuxinyi-Qiyu What you are calculating is MiB, not MB MB is calculated in terms of power of 10: 1MB= 10*6 B. So, the calculation done in source code: 431080 (32/8) / 1000 / 1000 = 1.72

Yuxinyi-Qiyu commented 1 year ago

hey, sorry to bother you. I want to know how the "Params size" calculate. the params are 431080, so the Params_mem:431080 * (32/8) / 1024 / 1024 = 1.64 MB, why the "Params size" is 1.72 instead of 1.64?

Hi @Yuxinyi-Qiyu What you are calculating is MiB, not MB MB is calculated in terms of power of 10: 1MB= 10*6 B. So, the calculation done in source code: 431080 (32/8) / 1000 / 1000 = 1.72

I got it! thanks!!! 🥰