Search code examples
pythontensorflowtensorflow-serving

predicting images using tensorflow serving - "error": "Input to reshape is a tensor with 10000 values


# server URL
url = 'http://localhost:8501/v1/models/img_classifier:predict'


def make_prediction(instances):
    data = json.dumps({"signature_name": "serving_default", "instances": instances.tolist()})
    headers = {"content-type": "application/json"}
    json_response = requests.post(url, data=data, headers=headers)
    print(json_response.text)
    predictions = json.loads(json_response.text)['predictions']
    return predictions

reshaped_array = tf.expand_dims(temp_image, 0)

prediction = make_prediction(reshaped_array)

I get an error when printing the (json_response.text),

"error": "Input to reshape is a tensor with 10000 values, but the requested shape requires a multiple of 784\n\t [[{{node sequential_2/flatten_2/Reshape}}]]"

I am trying to classify a bounding box in an image. The issue starts at this part instances.tolist() If I remove the .tolist(), I will get

TypeError: Object of type EagerTensor is not JSON serializable

and if I keep it, then it ruins the image dimensions. How can I fix this issue?


Solution

  • Found the issue!

    I had to retrain the model, and save it using the following code:

    MODEL_DIR = 'imageClassifier2021'
    version = 1
    export_path = os.path.join(MODEL_DIR, str(version))
    tf.keras.models.save_model(
        model,
        export_path,
        overwrite=True,
        include_optimizer=True,
        save_format=None,
        signatures=None,
        options=Non
    

    e )