import os
import json
import requests
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
(_, _), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_test = x_test[..., np.newaxis]/255.0
# picking up a random image from the test set for prediction
IMG_NUM = np.random.randint(0, len(x_test))
json_data = json.dumps({"signature_name": "serving_default", "instances": x_test[[IMG_NUM]].tolist()})
headers = {"content-type": "application/json"}
json_response = requests.post('http://localhost:8501/v1/models/mnist_digit:predict', data=json_data, headers=headers)
predictions = json.loads(json_response.text)['predictions']
predictions = np.array(predictions).argmax()
print('The predicted result is ',predictions)
print('True Label: ',y_test[IMG_NUM])
print('Predicted Label: ',predictions)
plt.imshow(x_test[IMG_NUM].reshape(28,28), cmap = 'gray');
# picking up a random image from the test set for prediction
IMG_NUM = np.random.randint(0, len(x_test), 5)
json_data = json.dumps({"signature_name": "serving_default", "instances": x_test[[IMG_NUM]].tolist()})
for idx in IMG_NUM:
label = y_test[idx]
print('The label is ', label)
headers = {"content-type": "application/json"}
json_response = requests.post('http://localhost:8501/v1/models/mnist_digit:predict', data=json_data, headers=headers)
predictions = json.loads(json_response.text)['predictions']
for pred in predictions:
prediction = np.array(pred).argmax()
print('The predicted result is ', prediction)