-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
94 lines (73 loc) · 2.91 KB
/
predict.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import argparse
import json
from PIL import Image
import numpy as np
import torch
from utils import model_utils
parser = argparse.ArgumentParser(description='Model training parameters.')
parser.add_argument('model_path', metavar='path', type=str,
help='path with model')
parser.add_argument('img_path', metavar='img', type=str,
help='path with image for predicting')
parser.add_argument('--top_k_classes', metavar='topk', type=int, nargs="?",
default=1, help='print top K classes with probabilities')
parser.add_argument('--json_dict', metavar='json', type=str, default=None,
help='JSON file that maps the class values to other category names')
parser.add_argument('--train_on_gpu', action='store_true',
default=False, help='defines if training is on gpu or not')
# Parse all args
args = parser.parse_args()
model_path = args.model_path
img_path = args.img_path
topk = args.top_k_classes
json_dict_path = args.json_dict
using_cuda = args.train_on_gpu
# Load json if possible
if json_dict_path is not None:
with open(json_dict_path, 'r') as f:
cat_to_name = json.load(f)
# Load model
if using_cuda:
checkpoint = torch.load(model_path)
else:
checkpoint = torch.load(model_path, map_location=torch.device('cpu'))
model, criterion, optimizer = model_utils.define_model(checkpoint['hidden_size'],
checkpoint['architecture'],
checkpoint['learning_rate'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
model.load_state_dict(checkpoint['model_state_dict'])
if using_cuda:
model.cuda()
# Predict image
def process_image(image):
''' Scales, crops, and normalizes a PIL image for a PyTorch model,
returns an Numpy array
'''
image = image.resize((224, 224))
np_image = np.array(image)
if np_image.dtype == np.dtype('uint8'):
np_image = np_image / 255
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
np_image = (np_image - mean) / std
np_image = np_image.transpose((2, 0, 1))
return np_image
def predict(image_path, model, topk=5):
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
image = Image.open(image_path)
image = process_image(image)
image = np.array([image])
inputs = torch.from_numpy(image).float()
if using_cuda:
inputs.cuda()
model = model.cuda()
preds = model(inputs)
probabilities = torch.nn.functional.softmax(preds, dim=1)
probabilities = probabilities[0].cpu().detach().numpy()
preds = np.argpartition(probabilities, -topk)[-topk:]
probs = probabilities[preds]
if cat_to_name is not None:
preds = [cat_to_name[str(flower)] for flower in preds + 2]
return probs, preds
print(predict(img_path, model, topk))