-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpredict.py
More file actions
84 lines (57 loc) · 1.79 KB
/
predict.py
File metadata and controls
84 lines (57 loc) · 1.79 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
import sys
import time
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds
from PIL import Image
import argparse
import json
batch_size = 32
image_size = 224
class_names = {}
def process_image(image):
image = tf.cast(image, tf.float32)
image= tf.image.resize(image, (image_size, image_size))
image /= 255
image = image.numpy()
return image
def predict(image_path, model, top_k=5):
image = Image.open(image_path)
image = np.asarray(image)
image = np.expand_dims(image, axis=0)
image = process_image(image)
prob_list = model.predict(image)
classes = []
probs = []
rank = prob_list[0].argsort()[::-1]
for i in range(top_k):
index = rank[i] + 1
cls = class_names[str(index)]
probs.append(prob_list[0][index])
classes.append(cls)
return probs, classes
if __name__ == '__main__':
print('predict.py, running')
parser = argparse.ArgumentParser()
parser.add_argument('arg1')
parser.add_argument('arg2')
parser.add_argument('--top_k')
parser.add_argument('--category_names')
args = parser.parse_args()
print(args)
print('arg1:', args.arg1)
print('arg2:', args.arg2)
print('top_k:', args.top_k)
print('category_names:', args.category_names)
image_path = args.arg1
model = tf.keras.models.load_model(args.arg2 ,custom_objects={'KerasLayer':hub.KerasLayer} )
top_k = args.top_k
if top_k is None:
top_k = 5
with open(args.category_names, 'r') as f:
class_names = json.load(f)
probs, classes = predict(image_path, model, top_k)
print(probs)
print(classes)