-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinfer.py
More file actions
49 lines (34 loc) · 1.24 KB
/
infer.py
File metadata and controls
49 lines (34 loc) · 1.24 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
import argparse
import time
from pathlib import Path
import numpy as np
from sagemaker.tensorflow import TensorFlowPredictor
def main():
parser = argparse.ArgumentParser()
parser.add_argument('endpoint_name')
parser.add_argument('--data_dir', default='data/')
parser.add_argument('--mnist_index', '-i', type=int, default=0)
args = parser.parse_args()
predictor = TensorFlowPredictor(args.endpoint_name)
image = get_mnist_data(args.data_dir, index=args.mnist_index)
inputs = {'instances': image}
t = time.time()
outputs = predictor.predict(inputs)
print(f'inference time: {(time.time() - t) * 1000:.2f} ms')
prediction = np.array(outputs['predictions'][0])
pred_label = np.argmax(prediction)
pred_confidence = np.max(prediction)
print(f'prediction: {pred_label} ({pred_confidence * 100:.1f}%)')
def get_mnist_data(data_dir, index=0):
data_dir = Path(data_dir)
x = np.load(str(data_dir / 'test.npz'))['image']
y = np.load(str(data_dir / 'test.npz'))['label']
image = x[index]
image = image.reshape(1, 28, 28, 1)
image = image.astype(np.float32)
image /= 255
label = y[index]
print(f'ground truth: {label}')
return image
if __name__ == "__main__":
main()