Repository navigation
Expand file tree
/
Copy pathPrediction.py
More file actions
134 lines (102 loc) · 3.44 KB
/
Copy pathPrediction.py
File metadata and controls
134 lines (102 loc) · 3.44 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
import matplotlib.pyplot as plt
import torch
from torch import optim
from torch.autograd import Variable
from torch import nn
import numpy as np
import time
from PIL import Image
import os, random
from Model import Model
from Data import load
import json
from args import pargs
import torchvision
model=Model()
optimizer = optim.Adam(model.classifier.parameters(), lr=pargs().learning_rate)
with open(pargs().category_names, 'r') as f:
cat_to_name = json.load(f)
device = pargs().gpu
# torch.device("cuda" if torch.cuda.is_available() else "cpu")
def load_checkpoint(filepath):
checkpoint = torch.load(filepath)
learning_rate=checkpoint['learning_rate']
model.epochs = checkpoint['epochs']
model.load_state_dict(checkpoint['state_dict'])
model.class_to_idx = checkpoint['class_to_idx']
optimizer.load_state_dict(checkpoint['optimizer'])
return model
model = load_checkpoint(pargs().save_dir)
def process_image(image):
''' Scales, crops, and normalizes a PIL image for a PyTorch model,
returns an Numpy array
'''
# TODO: Process a PIL image for use in a PyTorch model
im = Image.open(image)
im = im.resize((256,256))
value = 0.5*(256-224)
im = im.crop((value,value,256-value,256-value))
im = np.array(im)/255
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
im = (im - mean) / std
return im.transpose(2,0,1)
def predict(image_path,model= model, topk=pargs().top_k):
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
model.to(device)
# turn off dropout
model.eval()
# The image
image = process_image(image_path)
# tranfer to tensor
image = torch.from_numpy(np.array([image])).float()
# The image becomes the input
image = Variable(image)
image = image.to(device)
output = model.forward(image)
probabilities = torch.exp(output).data
# getting the topk (=5) probabilites and indexes
# 0 -> probabilities
# 1 -> index
prob = torch.topk(probabilities, topk)[0].tolist()[0] # probabilities
index = torch.topk(probabilities, topk)[1].tolist()[0] # index
ind = []
for i in range(len(model.class_to_idx.items())):
ind.append(list(model.class_to_idx.items())[i][0])
# transfer index to label
label = []
for i in range(topk):
label.append(ind[index[i]])
return prob, label
# TODO: Implement the code to predict the class from an image file
def Run():
img = random.choice(os.listdir(pargs().input))
img_path = pargs().input + img
prob, classes = predict(img_path)
print(prob)
print(classes)
print([cat_to_name[x] for x in classes])
max_index = np.argmax(prob)
max_probability = prob[max_index]
label = classes[max_index]
fig = plt.figure(figsize=(6,6))
ax1 = plt.subplot2grid((15,9), (0,0), colspan=9, rowspan=9)
ax2 = plt.subplot2grid((15,9), (9,2), colspan=5, rowspan=5)
image = Image.open(img_path)
ax1.axis('off')
ax1.set_title(cat_to_name[label])
ax1.imshow(image)
labels = []
for cl in classes:
labels.append(cat_to_name[cl])
y_pos = np.arange(5)
ax2.set_yticks(y_pos)
ax2.set_yticklabels(labels)
ax2.set_xlabel('Probability')
ax2.invert_yaxis()
ax2.barh(y_pos, prob, xerr=0, align='center', color='blue')
plt.show()
return None
if __name__ == '__main__':
Run()