Lyken17 / pytorch-OpCounter

Count the MACs / FLOPs of your PyTorch model.
MIT License
4.88k stars 527 forks source link

Is there something wrong with nn.AdaptiveAvgPool? #97

Open ricardomlyk opened 4 years ago

ricardomlyk commented 4 years ago

Hi, i encountered an error when using THOP to count models with nn.AdaptiveAvgPool. Here is the code: ` import torch from torch import nn from thop import profile from thop import clever_format

class toy_net(nn.Module):

def __init__(self, input_channel, output_channel=512):
    super(toy_net, self).__init__()
    self.ConvNet = nn.Sequential(
        nn.Conv2d(input_channel, 64, 3, 1, 1), nn.ReLU(True),
        nn.AdaptiveAvgPool2d((None, 1)))

def forward(self, input):
    return self.ConvNet(input)

model = toy_net(3) input_rand = torch.randn(1, 3, 224, 224) flops, params = profile(model, inputs=(input_rand, )) flops, params = clever_format([flops, params], '%.3f') print(flops, params) `

The result is: ` Traceback (most recent call last): File "D:/gitlab/playground/test_thop.py", line 20, in flops, params = profile(model, inputs=(input_rand, )) File "C:\Users.conda\envs\pytorch\lib\site-packages\thop\profile.py", line 188, in profile model(inputs) File "C:\Users.conda\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 550, in call result = self.forward(input, *kwargs) File "D:/gitlab/playground/test_thop.py", line 15, in forward return self.ConvNet(input) File "C:\Users.conda\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 550, in call result = self.forward(input, *kwargs) File "C:\Users.conda\envs\pytorch\lib\site-packages\torch\nn\modules\container.py", line 100, in forward input = module(input) File "C:\Users.conda\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 552, in call hook_result = hook(self, input, result) File "C:\Users.conda\envs\pytorch\lib\site-packages\thop\vision\basic_hooks.py", line 92, in count_adap_avgpool kernel = torch.DoubleTensor([(x[0].shape[2:])]) // torch.DoubleTensor(list((m.output_size,))).squeeze() TypeError: must be real number, not NoneType

Process finished with exit code 1 `

And when i change nn.AdaptiveAvgPool2d((None, 1))) to nn.AdaptiveAvgPool2d(1)) It works normally.

Looking forward to your reply.

Lyken17 commented 4 years ago

Uh, didn't consider the None type when wrote the code. Will fix it.

qq1243196045 commented 2 years ago

I also meet this trouble,how do you solve it?

Lyken17 commented 2 years ago

@qq1243196045 could you attach a code to reproduce?