Spaces:
Runtime error
Runtime error
Ivan Shelonik
commited on
Commit
·
0938778
1
Parent(s):
696f1ca
rm: redundant files
Browse files- api_client.py +0 -70
api_client.py
DELETED
@@ -1,70 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import time
|
3 |
-
import numpy as np
|
4 |
-
import requests
|
5 |
-
import matplotlib.pyplot as plt
|
6 |
-
|
7 |
-
# Disable tensorflow warnings
|
8 |
-
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
9 |
-
|
10 |
-
from keras.datasets import mnist
|
11 |
-
from typing import List
|
12 |
-
|
13 |
-
|
14 |
-
# Set random seed for reproducibility
|
15 |
-
np.random.seed(50)
|
16 |
-
|
17 |
-
# Number of images taken from test dataset to make prediction
|
18 |
-
N_IMAGES = 9
|
19 |
-
|
20 |
-
def get_image_prediction(image: List):
|
21 |
-
"""Get Model prediction for a given image
|
22 |
-
:param
|
23 |
-
image: List
|
24 |
-
Grayscale Image
|
25 |
-
:return: Json
|
26 |
-
HTTP Response format:
|
27 |
-
{
|
28 |
-
"prediction": predicted_label,
|
29 |
-
"ml-latency-ms": latency_in_milliseconds
|
30 |
-
(Measures time only for ML operations preprocessing with predict)
|
31 |
-
}
|
32 |
-
"""
|
33 |
-
# Making prediction request API
|
34 |
-
response = requests.post(url='http://127.0.0.1:5000/predict', json={'image': image})
|
35 |
-
# Parse the response JSON
|
36 |
-
return response.json()
|
37 |
-
|
38 |
-
# Load the dataset from keras.datasets
|
39 |
-
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
40 |
-
|
41 |
-
# Select N-th (N_IMAGES) random indices from x_test
|
42 |
-
indices = np.random.choice(len(x_test), N_IMAGES, replace=False)
|
43 |
-
|
44 |
-
# Get the images and labels based on the selected indices
|
45 |
-
images, labels, predictions = x_test[indices], y_test[indices], []
|
46 |
-
|
47 |
-
# Iterate over each image, invoke prediction API and save results to predictions array
|
48 |
-
for i in range(N_IMAGES):
|
49 |
-
# Send the POST request to the Flask server
|
50 |
-
start_time = time.time()
|
51 |
-
model_response = get_image_prediction(images[i].tolist())
|
52 |
-
print('Model Response:', model_response)
|
53 |
-
print('Total Measured Time (ms):', round((time.time() - start_time) * 1000, 3))
|
54 |
-
# Save prediction data into predictions array
|
55 |
-
predictions.append(model_response['prediction'])
|
56 |
-
|
57 |
-
def plot_images_and_results_plot(images_, labels_, predictions_):
|
58 |
-
"""Plotting the images with their labels and predictions
|
59 |
-
"""
|
60 |
-
fig, axes = plt.subplots(N_IMAGES, 1, figsize=(6, 10))
|
61 |
-
|
62 |
-
for i in range(N_IMAGES):
|
63 |
-
axes[i].imshow(images_[i], cmap='gray')
|
64 |
-
axes[i].axis('off')
|
65 |
-
axes[i].set_title("Label/Prediction: {}/{}".format(labels_[i], predictions_[i]))
|
66 |
-
|
67 |
-
plt.tight_layout()
|
68 |
-
plt.show()
|
69 |
-
|
70 |
-
plot_images_and_results_plot(images, labels, predictions)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|