Loading PyTorch pre-trained model invalid syntax

I’ve trained a model using PyTorch and saved a state dict file. I have loaded the pre-trained model using the code below. I am getting an error message regarding invalid syntax. Do i need to replace **kwargs with something else? Many Thanks in advance

  File "load_model_delete.py", line 63
    model=VGG((*args, **kwargs))
SyntaxError: invalid syntax

I am following instruction available at this site: https://pytorch.org/tutorials/beginner/saving_loading_models.html#saving-loading-model-across-devices

Many Thanks

import argparse
import datetime
import glob
import os
import random
import shutil
import time
from os.path import join

import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import ToTensor
from tqdm import tqdm
import torch.optim as optim

from convnet3 import Convnet
from dataset2 import CellsDataset

from convnet3 import Convnet
from VGG import VGG
from dataset2 import CellsDataset
from torchvision import models
from Conv import Conv2d

parser = argparse.ArgumentParser('Predicting hits from pixels')
parser.add_argument('name',type=str,help='Name of experiment')
parser.add_argument('data_dir',type=str,help='Path to data directory containing images and gt.csv')
parser.add_argument('--weight_decay',type=float,default=0.0,help='Weight decay coefficient (something like 10^-5)')
parser.add_argument('--lr',type=float,default=0.0001,help='Learning rate')
args = parser.parse_args()

metadata = pd.read_csv(join(args.data_dir,'gt.csv'))
metadata.set_index('filename', inplace=True)

# create datasets:

dataset = CellsDataset(args.data_dir,transform=ToTensor(),return_filenames=True)
dataset = DataLoader(dataset,num_workers=4,pin_memory=True)
model_path = '/Users/nubstech/Documents/GitHub/CellCountingDirectCount/VGG_model_V1/checkpoints/checkpoint.pth'

class VGG(nn.Module):
    def __init__(self, pretrained=True):
        super(VGG, self).__init__()
        vgg = models.vgg16(pretrained=True)
        # if pretrained:
        features = list(vgg.features.children())
        self.features4 = nn.Sequential(*features[0:23])

        self.de_pred = nn.Sequential(Conv2d(512, 128, 1, same_padding=True, NL='relu'),
                                    Conv2d(128, 1, 1, same_padding=True, NL='relu'))

    def forward(self, x):
        x = self.features4(x)       
        x = self.de_pred(x)

        return x


#optimizer = torch.optim.Adam(model.parameters(),lr=args.lr,weight_decay=args.weight_decay)

for images, paths in tqdm(dataset):

    targets = torch.tensor([metadata['count'][os.path.split(path)[-1]] for path in paths]) # B
    targets = targets.float()

    # code to print training data to a csv file
    output = model(images) # B x 1 x 9 x 9 (analogous to a heatmap)
    preds = output.sum(dim=[1,2,3]) # predicted cell counts (vector of length B)
    paths_test = np.array([paths])
    names_preds = np.hstack(paths)
    df=pd.DataFrame({'Image_Name':names_preds, 'Target':targets.detach(), 'Prediction':preds.detach()})
    # save image name, targets, and predictions
    df.to_csv(r'model.csv', index=False, mode='a')

This should be VGG(*args, **kwargs). I’m noticing that you don’t have kwargs defined however. Args should be a list or tuple of values. Kwargs should be a dictionary.

Here’s an example of how to use the * operator:

args = (a, b, c)
kwargs = {'x': 1, 'y': 1}

# These two lines are equivalent 
Model(*args, **kwargs)
Model(a, b, c, x=1, y=1)

Many Thanks! How do decide which values to use please? Sorry, if this is really stupid question.

@ayalaa2 Is there an example i could learn from please?

No worries! There’s no stupid questions.

If you already trained a model and you want to load its weight, then you need to initialize the model object using the exact same parameters.

In your example, I can see in your def __init__(self, pretrained=True): that there’s no positional arguments (we don’t count self) and you have one keyword argument. Since your pretrained doesn’t do anything yet (you have the code commented out), using either VGG(pretrained=True) or VGG(pretrained=False) will work. Really the most important thing is that the same exact layers are being constructed and initialized.

Thanks! Sorry i am still confused about how to go about getting this to work. I have taken the line of code model=VGG((*args, **kwargs)) but now got an error message NameError: name 'models is not defined. Many Thanks for your patient.

You have this line under your init method. It doesn’t know where to get vgg16 from. If you don’t need this (since it looks like you already have VGG defined in the class?) you can just comment it out. Otherwise if you do need it, you need to have this line: import torchvision.models as models to get access to the vgg16 class.

1 Like

Thanks very mcuh!

I am getting the following error:

RuntimeError: Error(s) in loading state_dict for VGG:
	Missing key(s) in state_dict: "features.0.weight", "features.0.bias", "features.2.weight", "features.2.bias", "features.5.weight", "features.5.bias", "features.7.weight", "features.7.bias", "features.10.weight", "features.10.bias", "features.12.weight", "features.12.bias", "features.14.weight", "features.14.bias", "features.17.weight", "features.17.bias", "features.19.weight", "features.19.bias", "features.21.weight", "features.21.bias", "features.24.weight", "features.24.bias", "features.26.weight", "features.26.bias", "features.28.weight", "features.28.bias", "classifier.0.weight", "classifier.0.bias", "classifier.3.weight", "classifier.3.bias", "classifier.6.weight", "classifier.6.bias". 
	Unexpected key(s) in state_dict: "state_dict", "optimizer_state_dict", "globalStep", "train_paths", "test_paths". 

If i add strict= false to the load statement the code work but the prediction don’t looks right to me. Any idea why would this be the case please?

I have updated the code in the snippet t show the latest code that i am working on.