-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
68 lines (50 loc) · 1.78 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
import numpy as np
import argparse
import json
from PIL import Image
from time import time
import torch
import functions as func
def main():
start_time = time()
in_arg = get_input_args()
device = set_device(in_arg.gpu)
prob, classes = func.predict(in_arg.image_dir, in_arg.checkpoint, device, in_arg.top_k)
names = []
try:
with open(in_arg.category_names, 'r') as f:
cat_to_name = json.load(f)
for clas in classes:
names.append(cat_to_name[str(clas)])
except:
pass
if len(names) != 0:
for i in range(len(classes)):
print("Classes: ", classes[i], names[i], " Probabilty: ", prob[i])
elif not names:
if len(names) == 0:
for i in range(len(classes)):
print("Classes: ", classes[i], " Probabilty: ", prob[i])
def get_input_args():
"""Get command line arguments. """
# Creates parser
parser = argparse.ArgumentParser()
parser.add_argument('image_dir', type=str,
help='path to the image')
parser.add_argument('checkpoint', type=str,
help='path to the checkpoint')
parser.add_argument('--top_k', type=int, default=1,
help='Top K')
parser.add_argument('--category_names', type=str, default='',
help='file patch to category name file')
parser.add_argument('--gpu', type=str, default='cpu',
help='set device for training.')
return parser.parse_args()
def set_device(gpu):
"""Set device."""
if gpu.lower() == 'cpu':
device = 'cpu'
else:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
return device
main()