-
Notifications
You must be signed in to change notification settings - Fork 0
/
udacity_code.txt
59 lines (45 loc) · 2.16 KB
/
udacity_code.txt
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Load your model to this variable
import torch
from torch import nn
import torch.nn.functional as F
from torchvision import models
from collections import OrderedDict
model = models.densenet121(pretrained=True)
for param in model.parameters():
param.requires_grad = False
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(1024,512)),
('relu', nn.ReLU()),
('dropout1', nn.Dropout(0.1)),
('fc2', nn.Linear(512,256)),
('dropout2', nn.Dropout(0.1)),
('relu', nn.ReLU()),
('fc3', nn.Linear(256,102)),
('output', nn.LogSoftmax(dim=1))
]))
model.classifier = classifier
def load_checkpoint(checkpoint_path):
checkpoint = torch.load(checkpoint_path, map_location=lambda storage, loc: storage)
model = models.densenet121(pretrained=True)
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(1024,512)),
('relu', nn.ReLU()),
('dropout1', nn.Dropout(0.5)),
('fc2', nn.Linear(512,256)),
('dropout2', nn.Dropout(0.1)),
('relu', nn.ReLU()),
('fc3', nn.Linear(256,102)),
('output', nn.LogSoftmax(dim=1))
]))
model.classifier = classifier
for param in model.parameters():
param.requires_grad = False
model.load_state_dict(checkpoint['state_dict'], strict = False)
return model
model = load_checkpoint('/home/workspace/model_checkpoint.pth')
# If you used something other than 224x224 cropped images, set the correct size here
image_size = 224
# Values you used for normalizing the images. Default here are for
# pretrained models from torchvision.
norm_mean = [0.485, 0.456, 0.406]
norm_std = [0.229, 0.224, 0.225]