Skip to content

Instantly share code, notes, and snippets.

@himanshurawlani
Last active March 21, 2019 16:15
Show Gist options
  • Save himanshurawlani/b3d2634bd30a3bdb8f674f951bb0b768 to your computer and use it in GitHub Desktop.
Save himanshurawlani/b3d2634bd30a3bdb8f674f951bb0b768 to your computer and use it in GitHub Desktop.
Making a TensorFlow Serving POST request to the REST client API
import json, requests
from tensorflow.keras.preprocessing.image import img_to_array, load_img
import numpy as np
image_path = 'sunflower.jpg'
# Loading and pre-processing our input image
img = image.img_to_array(image.load_img(image_path, target_size=(128, 128))) / 255.
img = np.expand_dims(img, axis=0)
payload = {"instances": img.tolist()}
# sending post request to TensorFlow Serving server
json_response = requests.post('http://localhost:9000/v1/models/FlowerClassifier:predict', json=payload)
pred = json.loads(json_response.content.decode('utf-8'))
# Decoding the response using decode_predictions() helper function
# You can pass "k=5" to get top 5 predicitons
get_top_k_predictions(pred, k=3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment