Code breaks in PyTorch 1.0

Hello, I started a project a long time ago using Pytorch 0.4.1. Now I am working on it again on PyTorch 1.0.
The code works on PyTorch 0.4.1 but not on 1.0. It breaks when I try to execute a forward() pass on the pretrained VGG network I had saved back on 0.4.1. It yields the following error message:

Traceback (most recent call last):
  File "demo.py", line 186, in <module>
    frame = process_frame(frame)
  File "demo.py", line 151, in process_frame
    age, gender = forward(input)
  File "demo.py", line 113, in forward
    age = age_model(input.to(device))
  File "/home/diego/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/diego/.local/lib/python3.6/site-packages/torchvision/models/vgg.py", line 43, in forward
    x = self.avgpool(x)
  File "/home/diego/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 535, in __getattr__
    type(self).__name__, name))
AttributeError: 'VGG' object has no attribute 'avgpool'

The line for loading the model: torch.load("model/age_model.pth").to(device) .
I execute the forward pass like so:

    with torch.no_grad():
        age_model.eval() # we will only run inference on this model
        age = age_model(input.to(device))

It’s worth noticing that the exact same code works on 0.4.1.

Thanks In advance for any help! :slight_smile:

P.S: The code also throws some warnings:

/home/diego/.local/lib/python3.6/site-packages/torch/serialization.py:435: SourceChangeWarning: source code of class 'torchvision.models.vgg.VGG' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/home/diego/.local/lib/python3.6/site-packages/torch/serialization.py:435: SourceChangeWarning: source code of class 'torch.nn.modules.container.Sequential' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/home/diego/.local/lib/python3.6/site-packages/torch/serialization.py:435: SourceChangeWarning: source code of class 'torch.nn.modules.conv.Conv2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/home/diego/.local/lib/python3.6/site-packages/torch/serialization.py:435: SourceChangeWarning: source code of class 'torch.nn.modules.activation.ReLU' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/home/diego/.local/lib/python3.6/site-packages/torch/serialization.py:435: SourceChangeWarning: source code of class 'torch.nn.modules.pooling.MaxPool2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/home/diego/.local/lib/python3.6/site-packages/torch/serialization.py:435: SourceChangeWarning: source code of class 'torch.nn.modules.linear.Linear' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/home/diego/.local/lib/python3.6/site-packages/torch/serialization.py:435: SourceChangeWarning: source code of class 'torch.nn.modules.dropout.Dropout' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)

This seems to be due to this commit, which I guess was not present with the torchvision version shipped with 0.4.1? (I don’t really understand the versioning in this case)

Since nn.AdaptiveAvgPool2d is not a learnable layer, I think you could just add it to your model before running inference, as follows:

age_model.avgpool = nn.AdaptiveAvgPool2d((7, 7))

I think you can get rid of the warnings by saving the model from pytorch 1.0 and then loading it back…?

1 Like