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

Demo error - 'BatchNorm2d' object has no attribute 'track_running_stats' #36

Closed
tobiascz opened this issue Aug 20, 2018 · 14 comments
Closed

Comments

@tobiascz
Copy link

Hi,

I'am using torch version 0.5 and i get that error in that line

output = model(input_var)

The error message looks like that:

  File "src/demo.py", line 26, in main
    output = model(input_var)
  File "/home/narvis/miniconda3/envs/hpe/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/narvis/Dev/pytorch-pose-hg-3d/src/models/hg_3d.py", line 101, in forward
    x = self.bn1(x)
  File "/home/narvis/miniconda3/envs/hpe/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/narvis/miniconda3/envs/hpe/lib/python3.6/site-packages/torch/nn/modules/batchnorm.py", line 66, in forward
    self.training or not self.track_running_stats,
  File "/home/narvis/miniconda3/envs/hpe/lib/python3.6/site-packages/torch/nn/modules/module.py", line 518, in __getattr__
    type(self).__name__, name))
AttributeError: 'BatchNorm2d' object has no attribute 'track_running_stats'

I found that other repositories have similar issues when upgrading from torch 0.3 to 0.4:
kunglab/ddnn#2

And I checked the specific commit to find out how to solve this errorr: kunglab/ddnn@071c82f

But I couldn't find the right solution yet.

I would like to use the most recent torch version so downgrading is not really an option for me.

@tobiascz
Copy link
Author

In the mean time i started training the model myself on the dataset with python3 and pytorch 0.5. I can load the model and do the forward pass without getting the track_running_stats missing error.

@llcshappy
Copy link

"train_params = [x for x in model.parameters() if x.requires_grad] "
It works.

@jixinhe111
Copy link

In the mean time i started training the model myself on the dataset with python3 and pytorch 0.5. I can load the model and do the forward pass without getting the track_running_stats missing error.

Hi,
I'am using torch version 0.4 and i get that error, can you tell how to solve this problem,thanks.

@tobiascz
Copy link
Author

The Problem is that higher torch versions use the attribute track_running_stats. But track_runninh_stats is a new attribute and by the time this model was trained it was not available. So you try loading a model without this Attribute with a pytorch version that needs that attribute. I couldn’t find a way to add this attribute to the already trained model that is why I trained my own model with a higher pytorch version so that the model includes the attribute. Hope I could help

@zkyf
Copy link

zkyf commented Oct 12, 2018

pytorch 0.3.1 works well on my computer. My configuration is Ubuntu 16.04, cuda 9.0, pytroch 0.3.1, python 2.7.15.

@aseerkhan
Copy link

aseerkhan commented Oct 24, 2018

I am facing the same problem ...is any can help me

@zkyf
Copy link

zkyf commented Oct 24, 2018

I believe you need to manually download an older version of pytorch like 0.3.0 or 0.3.1

@xingyizhou
Copy link
Owner

Hi,
The current version should work on python3 and pytorch 0.4+

@heroinlin
Copy link

Your model train by pytorch0.3.x, but run in pytorch > 0.4.0.
Change the parameter of BatchNorm2d by yourself.
For example, define the function

def recursion_change_bn(module):
    if isinstance(module, torch.nn.BatchNorm2d):
        module.track_running_stats = 1
    else:
        for i, (name, module1) in enumerate(module._modules.items()):
            module1 = recursion_change_bn(module1)
    return module

and
use it when you load model

check_point = torch.load(check_point_file_path)
model = check_point['net']
for i, (name, module) in enumerate(model._modules.items()):
    module = recursion_change_bn(model)
model.eval()

I have ran 0.3.1 model in pytorch0.4.1 and pytorch1.0.0.
you can use this way to update you model from 0.3.x to 0.4+

@chandiniRNair
Copy link

@heroinlin : I added the above code and a new error creeped in:
File "/home/chandini/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 535, in getattr
type(self).name, name))
AttributeError: 'Upsample' object has no attribute 'name'

I couldn't find any leads on this in google search.
Any suggestions please ?

@wuzb19951013
Copy link

@chanduKichu I have met the same problem. Did you solve it?

@gauharbains
Copy link

gauharbains commented Nov 30, 2020

@chandiniRNair @wuzb19951013 I have the same error. Were you able to figure it out ?

@tianlinxu312
Copy link

A working solution is to load the state_dict of the model with argument "strict=False".

Example code as below:

ckpt = torch.load(model_path)
model = Model(model_args)
model.load_state_dict(ckpt.state_dict(), strict=False)
model.eval()

@yyunhh
Copy link

yyunhh commented Aug 21, 2021

I got the error:

AttributeError: 'Upsample' object has no attribute 'align_corners'

There is no answer to my error on google.
But I try to modify the lib\site-packages\torch\nn\modules\upsampling.py", line 141:

def forward(self, input: Tensor) -> Tensor:
        return F.interpolate(input, self.size, self.scale_factor, self.mode)#self.align_corners

It works...
Hope it helps someone encounter the same error, but please correct me if I am doing the wrong way out.

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