-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
57 lines (36 loc) · 2.11 KB
/
predict.py
File metadata and controls
57 lines (36 loc) · 2.11 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
import argparse
import helper_predict as hp
import json
def main(image_path, checkpoint, top_k, cat_to_name_json, on_gpu):
if (not hp.file_exist(image_path)) or (not hp.file_exist(checkpoint)):
print("Either the image or model check point does not exist")
with open(cat_to_name_json, 'r') as f:
cat_to_name = json.load(f)
result = hp.load_checkpoint(checkpoint)
model = result[1]
class_to_idx = result[3]
prediction = hp.predict(image_path, model, top_k, on_gpu)
names_probs = hp.get_names_probs(prediction, cat_to_name, class_to_idx)
print("Names: {}".format(names_probs[0]))
print("Probabilities: {}".format(names_probs[1]))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='A simple app predict an image using a trained neural net')
parser.add_argument('image_path', action='store',
help="Specifies the path to the image file to predict it's class")
parser.add_argument('checkpoint', action='store',
help="Specifies the path to the trained model's checkpoint")
parser.add_argument('--top_k', action='store', type=int, default=5, dest='top_k',
help="Specifies the number of classes to return")
parser.add_argument('--category_names', action='store', default='cat_to_name.json', dest='cat_to_name_json',
help="Specifies the json file containing the category name and mapping for all categories")
parser.add_argument('--gpu', action="store_false", default=True, dest='on_gpu')
# Parse commandline arguments
args = parser.parse_args()
print("#################....Parameters....##################")
print("save_dir.........................{}".format(args.image_path))
print("arch.............................{}".format(args.checkpoint))
print("learning_rate....................{}".format(args.top_k))
print("hidden_units.....................{}".format(args.cat_to_name_json))
print("gpu..............................{}".format(args.on_gpu))
print("#################....Parameters....##################")
main(args.image_path, args.checkpoint, args.top_k, args.cat_to_name_json, args.on_gpu)