'BatchNorm2d' object has no attribute 'track_running_stats'

William_Lee: <p>Sorry, I am using a new computer and I just installed the newest version of Pytorch.<br>
My code runs perfectly on my old computer.<br>
But now BatchNorm2d is giving me this error when the model is switched to evaluation mode.</p>
<p>‘BatchNorm2d’ object has no attribute ‘track_running_stats’</p>
<p>Does anyone know how to solve this?<br>
I saw the source code change warning, but I am not sure about how to solve it.<br>
My model is based on pre-trained ResNet101</p>

ptrblck: <p>Could you print your <code>torch.__version__</code>?<br>
Also could you post a small code snippet reproducing the error?</p>

William_Lee: <p>Hi, I solved the problem by installing pytorch 0.3.1<br>
Here is my code:</p>
<p>“<span class="hashtag">#My</span> pretrained model” is ResNet101 + 1 Conv2d.  It was trained on my old computer with pytorch 0.3.1.</p>
<pre><code class="lang-auto">net.load_state_dict(torch.load("#My pretrained model"))
net.cuda()
val_set = Voc2012Segmentation('val', transform=None,
                              target_transform=None)
test_loader = torch.utils.data.DataLoader(val_set, batch_size=4,
                                           shuffle=False, num_workers=0)

dataiter = iter(test_loader)
net.eval()
for img_num in range(20):

    image, target = dataiter.next()
    image = Variable(image.cuda())
    target = Variable(target.cuda())
    heatmap = net(image)
</code></pre>

Wanger-SJTU: <p>problem got solved? I met the same problem. If solved, could you give some hint?</p>

William_Lee: <p>As I said in the previous post, I solved the problem by installing a previous version of pytorch. 0.3.1.<br>
Hope it helps <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

Leif_Ulstrup: <p>Encountered same problem. Upgraded to V0.4 and rebuilt model. It required some minor re-working of the code. The model now works without error using torch.<strong>version</strong> 0.4.0</p>

C0rine: <p>Can you elaborate on the minor changes you had to make? Also, by rebuilding the model, do you mean retraining? <a class="mention" href="/u/leif_ulstrup">@Leif_Ulstrup</a></p>

Leif_Ulstrup: <p><a class="mention" href="/u/c0rine">@C0rine</a>, apologies for the slow response. The only issue I had was a previously working function that computed  accuracy and sensitivity values had to be updated. The V0.4.0 version of PyTorch required me to explicitly cast the tensor to a float to compute the value. It may have been ‘operator error’ on my part not following a guideline but it worked in V0.3. After figuring that out, I was able to ‘retrain’ the model and use it in a separate production prediction microservice. I want to do more study on the differences in the model predictions between the two versions. I noticed some differences in prediction computations but have not had a chance to do a rigorous analysis. Good luck.</p>

C0rine: <p>Thanks for the elaboration!</p>
<p>I eventually also decided to just roll back to v0.3.1 to bypass the issue as I did not have the resources to retrain the model.</p>

Marat: <p>I have the same issue <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=5" title=":frowning:" class="emoji" alt=":frowning:"> It is seems that it will be solved only in next version of pytorch</p>

tommey_chang: <p>You can try to load the parameters instead of the model.</p>

Ayush_rai: <p>I am also facing the same problem and would want to roll back to previous version i.e 0.3.1. How can I do that using pip. Can you please share you method of downgrading the pytorch version?</p>

William_Lee: <p>Dear Ayush,<br>
This might help.</p>
<p><a href="https://discuss.pytorch.org/t/could-i-downgrade-pytorch-or-should-i-do-something-more-after-upgrading/1617">Could I downgrade pytorch? or should I do something more after upgrading?</a></p>
<p>I used conda but I think the commands should be similar</p>

synchro: <p>Is there any update to this? Still having the same issue.<br>
I didn’t get it. What is it related to?</p>

Kornel_Dylski: <aside class="quote no-group" data-username="synchro" data-post="14" data-topic="17525">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/synchro/48/764_2.png" class="avatar"> synchro:</div>
<blockquote>
<p>What is it related to?</p>
</blockquote>
</aside>
<p>Its related to new version of pytorch.<br>
Try version 0.3 instead of 0.4</p>

heroinlin: <p>Your model train by pytorch0.3.x, but run in pytorch &gt; 0.4.0.<br>
Change the parameter of BatchNorm2d  by yourself.<br>
For example, define the function</p>
<pre><code class="lang-auto">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
</code></pre>
<p>and<br>
use it when you load model</p>
<pre><code class="lang-auto">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()
</code></pre>
<p>I have ran 0.3.1 model in pytorch0.4.1. and pytorch1.0.0.</p>

111182: <p>Thank you for your excellent work. Your approach is excellent！</p>

remissio: <p>had the same error. pretrained model from 0.4 doesnt work on later versions. downgrading pytorch fixed it.</p>