-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
43 lines (32 loc) · 1.22 KB
/
utils.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
33
34
35
36
37
38
39
40
41
42
43
import argparse
import torch
from torchvision import transforms, datasets
import copy
import os
import json
def save_checkpoint(filepath, model, optimizer, args, classifier):
checkpoint = {'arch': args.arch,
'model': model,
'learning_rate': args.learning_rate,
'hidden_units': args.hidden_units,
'classifier' : classifier,
'epochs': args.epochs,
'optimizer': optimizer.state_dict(),
'state_dict': model.state_dict(),
'class_to_idx': model.class_to_idx}
torch.save(checkpoint, filepath)
def load_checkpoint(filepath):
checkpoint = torch.load(filepath)
print(checkpoint.keys())
model = checkpoint['model']
model.classifier = checkpoint['classifier']
learning_rate = checkpoint['learning_rate']
epochs = checkpoint['epochs']
optimizer = checkpoint['optimizer']
model.load_state_dict(checkpoint['state_dict'])
model.class_to_idx = checkpoint['class_to_idx']
return model
def load_cat_names(filename):
with open(filename) as f:
category_names = json.load(f)
return category_names