Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'torch.Size' object has no attribute 'numel' #45

Closed
SuckChen opened this issue Sep 17, 2019 · 4 comments
Closed

AttributeError: 'torch.Size' object has no attribute 'numel' #45

SuckChen opened this issue Sep 17, 2019 · 4 comments

Comments

@SuckChen
Copy link

Traceback (most recent call last):
File "/home/gpu/chen/IMIXNet-PyTorch/code/get_Netpara.py", line 21, in
print("Torch:", thop.profile(torch.nn.Conv2d(1, 128, (3, 3)), inputs=(torch.zeros((1, 1, 128, 128)),), verbose=False)[0])
File "/home/gpu/.conda/envs/pytorch4/lib/python3.6/site-packages/thop/profile.py", line 92, in profile
model(*inputs)
File "/home/gpu/.conda/envs/pytorch4/lib/python3.6/site-packages/torch/nn/modules/module.py", line 479, in call
hook_result = hook(self, input, result)
File "/home/gpu/.conda/envs/pytorch4/lib/python3.6/site-packages/thop/count_hooks.py", line 20, in count_convNd
kernel_ops = m.weight.size()[2:].numel() # Kw x Kh
AttributeError: 'torch.Size' object has no attribute 'numel'

what‘s the problem here ?
the version of thop is wrong?

@Lyken17
Copy link
Owner

Lyken17 commented Sep 17, 2019

Upgrade your PyTorch and thop to lastest version.

@Lyken17 Lyken17 closed this as completed Sep 17, 2019
@sureJ0824
Copy link

can you tell me which version for pytorch you use , and I use pytorch 0.4.0 and upgrade the thop, but meet the same problem. thank you a lot

@Lyken17
Copy link
Owner

Lyken17 commented Oct 15, 2019

PyTorch version should be newer than 1.0

@mrfarazi
Copy link

mrfarazi commented Dec 3, 2019

@sureJ0824, if you want to use pytorch 0.4.0 (which I needed to do as I was calculating flops for a legacy project), you can use dummy variable to get its number of elements.

For example, in count_hooks.py you can make the following change:
# kernel_ops = m.weight.size()[2:].numel()
kernel_ops = torch.zeros(m.weight.size()[2:]).numel()

This essentially creates a same sized tensor of zeros, which then can be passed to numel(). This worked for me in pytorch=0.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants