-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpredict.py
157 lines (129 loc) · 5.33 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
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
#importing necessary libraries
import matplotlib.pyplot as plt
import torch
import numpy as np
from torch import nn
from torch import optim
from torchvision import datasets, models, transforms
import torch.nn.functional as F
import torch.utils.data
import pandas as pd
from collections import OrderedDict
from PIL import Image
import argparse
import json
# define Mandatory and Optional Arguments for the script
parser = argparse.ArgumentParser (description = "Parser of prediction script")
parser.add_argument ('image_dir', help = 'Provide path to image. Mandatory argument', type = str)
parser.add_argument ('load_dir', help = 'Provide path to checkpoint. Mandatory argument', type = str)
parser.add_argument ('--top_k', help = 'Top K most likely classes. Optional', type = int)
parser.add_argument ('--category_names', help = 'Mapping of categories to real names. JSON file name to be provided. Optional', type = str)
parser.add_argument ('--GPU', help = "Option to use GPU. Optional", type = str)
# a function that loads a checkpoint and rebuilds the model
def loading_model (file_path):
checkpoint = torch.load (file_path) #loading checkpoint from a file
if checkpoint ['arch'] == 'alexnet':
model = models.alexnet (pretrained = True)
else: #vgg13 as only 2 options available
model = models.vgg13 (pretrained = True)
model.classifier = checkpoint ['classifier']
model.load_state_dict (checkpoint ['state_dict'])
model.class_to_idx = checkpoint ['mapping']
for param in model.parameters():
param.requires_grad = False #turning off tuning of the model
return model
# function to process a PIL image for use in a PyTorch model
def process_image(image):
''' Scales, crops, and normalizes a PIL image for a PyTorch model,
returns an Numpy array
'''
im = Image.open (image) #loading image
width, height = im.size #original size
# smallest part: width or height should be kept not more than 256
if width > height:
height = 256
im.thumbnail ((50000, height), Image.ANTIALIAS)
else:
width = 256
im.thumbnail ((width,50000), Image.ANTIALIAS)
width, height = im.size #new size of im
#crop 224x224 in the center
reduce = 224
left = (width - reduce)/2
top = (height - reduce)/2
right = left + 224
bottom = top + 224
im = im.crop ((left, top, right, bottom))
#preparing numpy array
np_image = np.array (im)/255 #to make values from 0 to 1
np_image -= np.array ([0.485, 0.456, 0.406])
np_image /= np.array ([0.229, 0.224, 0.225])
#PyTorch expects the color channel to be the first dimension but it's the third dimension in the PIL image and Numpy array.
#The color channel needs to be first and retain the order of the other two dimensions.
np_image= np_image.transpose ((2,0,1))
return np_image
#defining prediction function
def predict(image_path, model, topkl, device):
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
# Implement the code to predict the class from an image file
image = process_image (image_path) #loading image and processing it using above defined function
#we cannot pass image to model.forward 'as is' as it is expecting tensor, not numpy array
#converting to tensor
if device == 'cuda':
im = torch.from_numpy (image).type (torch.cuda.FloatTensor)
else:
im = torch.from_numpy (image).type (torch.FloatTensor)
im = im.unsqueeze (dim = 0) #used to make size of torch as expected. as forward method is working with batches,
#doing that we will have batch size = 1
#enabling GPU/CPU
model.to (device)
im.to (device)
with torch.no_grad ():
output = model.forward (im)
output_prob = torch.exp (output) #converting into a probability
probs, indeces = output_prob.topk (topkl)
probs = probs.cpu ()
indeces = indeces.cpu ()
probs = probs.numpy () #converting both to numpy array
indeces = indeces.numpy ()
probs = probs.tolist () [0] #converting both to list
indeces = indeces.tolist () [0]
mapping = {val: key for key, val in
model.class_to_idx.items()
}
classes = [mapping [item] for item in indeces]
classes = np.array (classes) #converting to Numpy array
return probs, classes
#setting values data loading
args = parser.parse_args ()
file_path = args.image_dir
#defining device: either cuda or cpu
if args.GPU == 'GPU':
device = 'cuda'
else:
device = 'cpu'
#loading JSON file if provided, else load default file name
if args.category_names:
with open(args.category_names, 'r') as f:
cat_to_name = json.load(f)
else:
with open('cat_to_name.json', 'r') as f:
cat_to_name = json.load(f)
pass
#loading model from checkpoint provided
model = loading_model (args.load_dir)
#defining number of classes to be predicted. Default = 1
if args.top_k:
nm_cl = args.top_k
else:
nm_cl = 1
#calculating probabilities and classes
probs, classes = predict (file_path, model, nm_cl, device)
#preparing class_names using mapping with cat_to_name
class_names = [cat_to_name [item] for item in classes]
for l in range (nm_cl):
print("Number: {}/{}.. ".format(l+1, nm_cl),
"Class name: {}.. ".format(class_names [l]),
"Probability: {:.3f}..% ".format(probs [l]*100),
)