forked from udacity/aipnd-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_checkpoint.py
32 lines (23 loc) · 1.01 KB
/
load_checkpoint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# PROGRAMMER: Joshua Cayanan
# DATE CREATED: June 29, 2020
# REVISED DATE:
# PURPOSE: Load parameters of a trained network
#Import python modules
import torch
from torchvision import models
#Import pre-trained models
vgg16 = models.vgg16(pretrained = True)
densenet121 = models.densenet121(pretrained = True)
#Dictionary of models and the number of neurons in the last convolutional layer
models = {'vgg16': vgg16, 'densenet121': densenet121}
def load_checkpoint(filepath):
checkpoint = torch.load(filepath)
model = models[checkpoint['arch']]
#Make sure to freeze densenet feature layer parameters, otherwise will throw cuda runtime error (2): out of memory
for param in model.parameters():
param.requires_grad = False
model.classifier = checkpoint['classifier_layers']
model.load_state_dict(checkpoint['state_dict'])
epochs = checkpoint['epochs']
learning_rate = checkpoint['learnrate']
return model, epochs, learning_rate