Pytorch: Recover network with customized VGG model that was saved improperly
I am currently doing work with customizing the forward method for models. I was using some tutorial code that ran VGG. I did a few runs with the baseline model and it seemed to work fine. Afterwards, I replaced the forward method for the VGG using:

net.forward = types.MethodType(forward_vgg_new, net)

Unfortunately, the way that the tutorial code saves the models is:

            state = {
                    'net':net,
                    'acc':acc,
                    'epoch':epoch,
            }
...
            torch.save(state, ...)



While This worked for the original tutorial code, loading no longer works for my custom models as I get:


  AttributeError: 'VGG' object has no attribute 'forward_vgg_new'


I have since read from the documentation that it is better for me to save the model's state_dict:

            state = {
                    'net':net.state_dict(),
                    'acc':acc,
                    'epoch':epoch,
            }
...
            torch.save(state, ...)


While I will change the code for future runs, I was wondering if it was possible to salvage the models I have already trained. I naively already tried to import the VGG class and add my forward_vgg_new method to it:

setattr(VGG, 'forward_vgg_new', forward_vgg_new)



before calling torch.load, but it doesn't work.

To solve the problem, I went directly into the VGG library and temporarily added my function so that I could load the saved models and save only their state dicts. I reverted the changes to the VGG library after I recovered the saves. Not the most graceful way of fixing the problem, but it worked.
