tensorflow / serving

A flexible, high-performance serving system for machine learning models

Home Page:https://www.tensorflow.org/serving

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I send a image to REST API?

onlyone2019 opened this issue · comments

commented

I deployed mnist using TFserving, following this tutorial. Then, I tried to test it through the rest api, but it didn't work. Mostly I didn't know how to send an image in python.

I have tried the following code.

data = {
        "signature_name" : "predict_images",
        "inputs":{
            "images":{
                "b64" : "iVBORw0KGgoAAAANSUhEUgAAAOEAAADgCAYAAAD17wHfAAANa0lEQVR4Xu3dr29USxyG8VpUExQOhcGRIAkGU1OFh/+gCZI/AEdSWdMEj0A2QW+CQyFRyAYFloS9eTf5hsm7c2a295w9c3484hPgnrvdbbvPzrYzc87J5eXl9uLiYvvmzRsAI3r9+vV2s9lsTxTgyckJgAZubm62JyrSDwAYx+fPn4kQaGkX4ePHj/cOABgHIyHQGBECjREh0BgRAo0RIdAYEQKNESHQGBECjREh0BgRAo0RIdAYEQKNESHQGBECjREh0BgRAo0RIdAYEQKNESHQGBECjREh0BgRAo0RIdAYEQKNESHQGBECjREh0BgRAo0RIdAYEQKNESHQGBECjREh0BgRAo0RIdAYEQKNESHQGBECjREh0NguwsePH+8dADAORkKgMSIEGiNCoDEiBBojQqAxIgQaI0KgMSIEGiNCoDEiBBojQqCxRUT49OnT7YsXL7KeP3++1ed3fX29vbq6ynr37l3V27dvs3Ts/Px8++TJk05am/vw4cNOp6ene58T1mMRESq2ly9fdrq8vNxuNpvdJ5vz8ePH7YcPHzpFwGnI6X97/fr19tmzZ7vgcxTio0ePOt2/f3/vc8J6LD5CjVKK0MP7PxHmpBHmECFqiHCACF+9erUXX4oIUUKEA0TISIg+iHCACEsjIRGihgiJEI0tIkI92c/OzrIUqKYRbm5uOik0j8sp5BwdU+yaavC4iAyHWESEGsm+ffuW9fXr1+3t7e32z58/29+/fx/Fr1+/tj9//uyk+//x40cnTZ/4C8Na6AmodxNd9AKpF1PNtfr8a/Dnw9wsIkJ9M/2JnVIkf//+bab2AqA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OQPNxz58/39FVbdM/dUwrWkqT8bUVMTVEOG9EOICIUOG5WoSHnEG7hgjnjQgHQITogwgHQITogwgHQITogwgHcNcIFV76JxGuGxEOQAF4fEHHFGFEF9J/E+G6EeEAtGdP+wJzXr16tfsiKxbFluNROV0EVPOMOYo4Nu4qRKeRmAinjQgHoNh8FUzQ11ZX0/Ww7iJ22OfomO5Hp8pQdBFjGiUn9502IhxALcLNZrMX1l3cJcIcIpw2IhwAEaKPXYT6ucEP4HBTijD3syERThsj4QCIEH0Q4QCIEH0Q4QCOHWFpG5SOpRHmMEUxbUQ4gL4RKiQf4dKRTh9DG4M155ijn+k12ik2FycYxnQR4QD6RqjQ/MKgQRcP1ZI4v08sBxEO4NgR6uzaGun8FPqBn/nmjQgHQITogwgHQITogwgHQITogwgHQITogwgHUIpQarsoFKFiy9E0BREuGxEOQJt3dQ3CHAWkr6+uIdildGJg0SoYhVbijwnzQYRAY0QINEaEQGNECDRGhEBjRAg0RoRAY4rwP1Ov0CXSgGK4AAAAAElFTkSuQmCC"
            }
        }
}
url = "http://localhost:8901/v1/models/mnist:predict"
ret = requests.post(url, json=data)

ERROR: Object is not of expected type: float

How can I solve this problem?

@onlyone2019,

Instead of encoding your prediction images to base64, Can you try passing the images as it is as shown in this tutorial. Thank you!

commented

@singhniraj08 Thank you! Please forgive my late reply.

I have read this tutorial carefully and rewritten my code like this:

import json
import requests
from keras.datasets import mnist

if __name__ == '__main__':
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    test_image = x_test / 255.0
    test_image = test_image.reshape(test_image.shape[0] , 28 , 28 , 1)
    jsondata = {
        "signature_name": "predict_images",
        "instances": test_image[0].tolist()
    }
    data = json.dumps(jsondata)
    heads = {
        "content-type": "application/json"
    }
    url = "http://localhost:8901/v1/models/mnist:predict"
    ret = requests.post(url, data = data , headers = heads)
    print(ret)

But it reminds me this is a Bad Request:

{
    "error": "Matrix size-incompatible: In[0]: [28,28,1], In[1]: [784,10]\\n\\t [[{{node MatMul}}]]"
}

Actually, I've encountered this in my previous attempts.

commented

Here is the MetaGraphDefs and SignatureDefs of my SavedModel.

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['predict_images']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['images'] tensor_info:
        dtype: DT_FLOAT
        shape: unknown_rank
        name: x:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: unknown_rank
        name: y:0
  Method name is: tensorflow/serving/predict

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: unknown_rank
        name: tf_example:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['classes'] tensor_info:
        dtype: DT_STRING
        shape: unknown_rank
        name: hash_table_Lookup/LookupTableFindV2:0
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: unknown_rank
        name: TopKV2:0
  Method name is: tensorflow/serving/classify

@onlyone2019,

The error you are facing is not related to model-server. Instead, this error looks like the model input shape and the test data shape you are sending to model server are incompatible.

Can you make sure if you TF model(with loading it on TF serving) can make predictions on the same test data. Also, I came across a similar issue, where properly reshaping your data during model building layers resolved the issue. Hope this helps.

Thank you!

commented

Thank you! @singhniraj08 I got it!

The dimension of the parameter I have to send is 1*784.

This is my test (passed):

import json
import requests
from keras.datasets import mnist
import numpy

if __name__ == '__main__':
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    test_image = x_test[0] / 255.0
    test_image = numpy.asarray(numpy.resize(test_image, (1, 784)))
    jsondata = {
        "signature_name": "predict_images",
        "instances": test_image.tolist()
    }
    data = json.dumps(jsondata)
    heads = {
        "content-type": "application/json"
    }
    url = "http://localhost:8501/v1/models/mnist:predict"
    ret = requests.post(url, data = data , headers = heads)
    response = numpy.array(json.loads(ret.text)['predictions'])
    prediction = numpy.argmax(response)
    print("the prediction is " + str(prediction))
    print("the answer is " + str(y_test[0]))

Sorry for wasting your time.

Finally, I suggest adding a similar example to this tutorial, which would be very helpful for beginners like me.

Thanks again! Best wishes for you!

@onlyone2019,

Thank you for your suggestions. We will work on this to make our documentations more robust for the community.
Meanwhile, Requesting you to close this issue since your issue is resolved. Thank you!