forked from udacity/aipnd-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
136 lines (94 loc) · 3.48 KB
/
Copy pathpredict.py
File metadata and controls
136 lines (94 loc) · 3.48 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import numpy as np
import torchvision
import torch
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
import torchvision.models as models
import torch.nn as nn
from PIL import Image
import argparse
import json
def load_checkpoint(path):
'''
Load Model Checkpoint
'''
checkpoint = torch.load(path)
model = checkpoint['model']
class_to_idx = checkpoint['class_to_idx']
model.eval()
return model,class_to_idx
def process_image(image):
''' Scales, crops, and normalizes a PIL image for a PyTorch model,
returns an Numpy array
'''
test_transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean = [0.485, 0.456, 0.406],
std = [0.229, 0.224, 0.225])
])
im = Image.open(image)
im = test_transform(im)
return im
def predict(image_path, model,class_idx,topk=5):
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
image = process_image(image_path)
image = torch.unsqueeze(image,0).to(device)
result = model(image)
result = nn.functional.softmax(result,dim=1)
prob = torch.topk(result, topk)[0][0].tolist()
indices = torch.topk(result, topk)[1][0].tolist()
for i in range(len(indices)):
for key, val in class_idx.items():
if indices[i] == val:
indices[i] = key
return prob,indices
def main(args):
model,class_idx = load_checkpoint(args.checkpoint)
max_val,max_idx = predict(args.image_Path, model,class_idx,args.topk)
with open(args.category_names, 'r') as f:
cat_to_name = json.load(f)
name = []
for i in range(len(max_idx)):
for key, val in cat_to_name.items():
if max_idx[i] == key:
name.append(val)
break
for i in range(len(max_val)):
print("Probability: {prob}, Class_Name:{catname}".format(prob=max_val[i],catname=name[i]))
if __name__=='__main__':
parser = argparse.ArgumentParser(description="Deep Learning on Amazon Sagemaker")
parser.add_argument("image_Path",
type=str,
default="flower/test/10/image_07090.jpg",
metavar="img_path",
help="Image Path on which Inference to be performed"
)
parser.add_argument("checkpoint",
type=str,
default="./model.pt",
metavar="Checkpoint",
help="Model Checkpoint Path",
)
parser.add_argument("--topk",
type=int,
default=3,
metavar="topk",
help="top k predictions",
)
parser.add_argument("--category_names",
type=str,
default="cat_to_name.json",
metavar="cat_to_name",
help="Name of classes",
)
parser.add_argument("--gpu",
type=bool,
default=True,
help="Training on GPU")
args = parser.parse_args()
if args.gpu:
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
main(args)