-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
191 lines (124 loc) · 4.72 KB
/
predict.py
File metadata and controls
191 lines (124 loc) · 4.72 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import json
import time
import copy
import checkpoint as loader
import argparse
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from torch.autograd import Variable
from torch import nn,optim
import torch
import torchvision
import torch.nn.functional as F
from torch import nn
from PIL import Image
from torchvision import datasets,transforms,models
from collections import OrderedDict
import torch.optim as optim
from torch.optim import lr_scheduler
def imshow(image, ax=None, title=None):
if ax is None:
fig, ax = plt.subplots()
# PyTorch tensors assume the color channel is the first dimension
# but matplotlib assumes is the third dimension
image = image.transpose((1, 2, 0))
# Undo preprocessing
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
image = std * image + mean
# Image needs to be clipped between 0 and 1 or it looks like noise when displayed
image = np.clip(image, 0, 1)
ax.imshow(image)
return ax
def process_image(image_path):
''' Scales, crops, and normalizes a PIL image for a PyTorch model,
returns an Numpy array
'''
#image_pil=Image.open(image_path)
loader = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor()])
image_pl = Image.open(image_path)
imagepl_ft = loader(image_pl).float()
np_image=np.array(imagepl_ft)
#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.transpose(np_image, (1, 2, 0)) - mean)/std
np_image = np.transpose(np_image, (2, 0, 1))
return np_image
def predict(image_path, model_name, topk=10, categories='', device='cuda'):
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
if(not torch.cuda.is_available() and device=='cuda'):
device='cpu'
# TODO: Implement the code to predict the class from an image file
with open('cat_to_name.json', 'r') as f:
label_mapper = json.load(f)
gpu=(device=='cuda')
model=loader.load_checkpoint(model_name,gpu=gpu)
model.to('cpu')
img=process_image(image_path)
img=torch.from_numpy(img).type(torch.FloatTensor)
inpt=img.unsqueeze(0)
model_result=model.forward(inpt)
expResult=torch.exp(model_result)
firstTopX,SecondTopX=expResult.topk(topk)
probs = torch.nn.functional.softmax(firstTopX.data, dim=1).numpy()[0]
#classes = SecondTopX.data.numpy()[0]
#probs = firstTopX.detach().numpy().tolist()[0]
classes = SecondTopX.detach().numpy().tolist()[0]
# Convert indices to classes
idx_to_class = {val: key for key, val in
model.class_to_idx.items()}
#labels = [label_mapper[str(lab)] for lab in SecondTopX]
labels = [idx_to_class[y] for y in classes]
flowers=[categories[idx_to_class[i]] for i in classes]
return probs,flowers
def show_prediction(image_path,probabilities,labels, categories):
plt.figure(figsize=(6,10))
ax=plt.subplot(2,1,1)
flower_index=image_path.split('/')[2]
name=categories[flower_index]
img=process_image(image_path)
imshow(img,ax)
plt.subplot(2,1,2)
sns.barplot(x=probabilities,y=labels,color=sns.color_palette()[0])
plt.show()
if __name__=="__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--input', type=str)
parser.add_argument('--gpu', action='store_true')
parser.add_argument('--epochs', type=int)
parser.add_argument('--checkpoint', type=str)
parser.add_argument('--category_names', type=str)
parser.add_argument('--top_k', type=int)
args, _ = parser.parse_known_args()
if (args.input):
input_name=args.input
else:
input_name='flowers/test/28/image_05230.jpg'
if(args.checkpoint):
checkpoint=args.checkpoint
else:
checkpoint='ic-model.pth'
if(args.category_names):
category_names=args.category_names
else:
category_names='cat_to_name.json'
if(args.gpu):
device='cuda'
else:
device='cpu'
# show_prediction(image_path=input_name,model=checkpoint,category_names=category_names)
with open(category_names, 'r') as f:
categories = json.load(f)
# run the prediction
probabilities,labels=predict(input_name,checkpoint,topk=5,categories=categories,device=device)
# show prediction
print('predict results')
print(probabilities)
print(labels)
show_prediction(image_path=input_name,probabilities=probabilities,labels= labels, categories= categories)