Skip to content

Instantly share code, notes, and snippets.

@himanshurawlani
Created October 19, 2018 22:35
Show Gist options
  • Save himanshurawlani/63124b14c5f9f5e795f57db0c087b7c1 to your computer and use it in GitHub Desktop.
Save himanshurawlani/63124b14c5f9f5e795f57db0c087b7c1 to your computer and use it in GitHub Desktop.
Script to make a POST request to TensorFlow Serving server with input image given as argument
import argparse
import json
import numpy as np
import requests
from keras.applications import inception_v3
from keras.preprocessing import image
# Argument parser for giving input image_path from command line
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path of the image")
args = vars(ap.parse_args())
image_path = args['image']
# Preprocessing our input image
img = image.img_to_array(image.load_img(image_path, target_size=(224, 224))) / 255.
# this line is added because of a bug in tf_serving(1.10.0-dev)
img = img.astype('float16')
payload = {
"instances": [{'input_image': img.tolist()}]
}
# sending post request to TensorFlow Serving server
r = requests.post('http://localhost:9000/v1/models/ImageClassifier:predict', json=payload)
pred = json.loads(r.content.decode('utf-8'))
# Decoding the response
# decode_predictions(preds, top=5) by default gives top 5 results
# You can pass "top=10" to get top 10 predicitons
print(json.dumps(inception_v3.decode_predictions(np.array(pred['predictions']))[0]))
@tkdgma0724
Copy link

Hello I used this code in Ubuntu 18.04 but it didn't run with the following error. Do you know how to solve it?

Using TensorFlow backend.
Traceback (most recent call last):
File "serving_sample_request.py", line 33, in
print(json.dumps(inception_v3.decode_predictions(np.array(pred['predictions']))[0]))
KeyError: 'predictions'

Copy link

ghost commented Jul 22, 2021

I am having the same issue as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment