Hello @ptrblck,
Thanks for your quick reply!
I have used the solution as you suggested but I am still facing the same error. My codes as follow:
Build Classifier
model = models.vgg19(pretrained=True)
for param in model.parameters():
param.requires_grad = False
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(25088, 4096)),
('relu1', nn.ReLU()),
('dropout1', nn.Dropout()),
('fc2', nn.Linear(4096, 102)),
('output', nn.LogSoftmax(dim=1))
]))
model.classifier = classifier
model.to(device)
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr=0.001)
Saving and Loading Model
torch.save(model.state_dict(), 'check_point.pth')
model.load_state_dict(torch.load('check_point.pth'))
However, I still receive the same RunTime Error when I tried to load the check_point.
On a side note, there is a tutorial from PyTorch on saving and loading model (https://pytorch.org/tutorials/beginner/saving_loading_models.html#what-is-a-state-dict). The example seems to be for building a new CNN rather than using an existing pre-trained model?
Below is the code that is illustrated:
# Define model
class TheModelClass(nn.Module):
def __init__(self):
super(TheModelClass, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
# Initialize model
model = TheModelClass()
My Questions:
- What am I missing in my code that is causing the RunTime Error?
- If I am using a pre-trained model, do I still need to define TheModelClass?
- Is the following line of code necessary if I am using a pre-trained model?
the_model = TheModelClass(*args, **kwargs)
Thanks!