-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredict.py
145 lines (114 loc) · 4.35 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
#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
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)
def loading_model (file_path):
checkpoint = torch.load (file_path) #loading checkpoint from a file
if checkpoint ['arch'] == 'vgg16':
model = models.vgg16 (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)
width, height = im.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
np_image -= np.array ([0.485, 0.456, 0.406])
np_image /= np.array ([0.229, 0.224, 0.225])
np_image= np_image.transpose ((2,0,1))
return np_image
def predict(image_path, model, topkl, device):
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
image = process_image (image_path) #loading image and processing it using above defined function
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)
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
if args.GPU == 'GPU':
device = 'cuda'
else:
device = 'cpu'
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)
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),
)