Adjustment of VGG model

Hi all,

I know I can adjust the layers in vgg model,but could I adjust the forward function such that It can receive list type input?

Thanks.

Your question is vague. Please explain more to clarify it
here is a webpage that I think can help you.

Thanks for your reply.

To clarify my question, first I open two terminal, and I define a simple class named BasicNN in the first terminal like below.

class simple(nn.Module):
def init(self):
super(simple, self).init()
self.net = nn.Linear(28 * 28, 2)
def forward(self, x):
x = np.array(x)
x = torch.from_numpy(x)
x = Variable(x)
x = x.view(1,1,28,28)
batch_size = x.size(0)
x = x.view(batch_size, -1)
output = self.net(x.float())
return out

model = simple()
torch.save(model,‘model.pkl’)

Then, I open a new terminal(or in another .py file), I want to load the model.

simple_model = torch.load(‘model.pkl’)

I get AttributeError : No module named simple.

But If I save and load a pretrained model like torchvision.models.vgg16, there is no such error. So I want to know whether I can change the forward function after I run model = torchvision.models.vgg16(pretrained=False)

Excuse me for my late reply.
I ran your code in my computer. But there is no any problem and all things work fine.

Are you running

simple_model = torch.load(‘model.pkl’)

in a new terminal? In the new terminal, I don’t define the class. Could you take a screenshot for me?

At first, I must say that I ran your code in an IDE because there is no difference; however, I checked both of them.
Then you are using the following code in a new terminal:

   import torch
   model = torch.load('model.pkl')

Running the above code returns the error. But I could see that if you include the definition of your class (simple), there will not be error. I mean write your code as the following:

class simple(nn.Module):
def init(self):
    super(simple, self).init()
    self.net = nn.Linear(28 * 28, 2)

def forward(self, x):
    x = np.array(x)
    x = torch.from_numpy(x)
    x = Variable(x)
    x = x.view(1,1,28,28)
    batch_size = x.size(0)
    x = x.view(batch_size, -1)
    output = self.net(x.float())
    return output
model = torch.load('model.pkl')

The result in an IDE and Terminal are the same.
Please check it.

I hope my help could be helpful. :slight_smile:

Ok, does that mean I have to define the class again if I want to load it?

Based on my experience, It is not needed to define the class again because of the fact that in transfer learning we load the model without defining the class again. But here it’s different and vague.