Skip to content

Error with Prediction from TensorFlow Object Detection ONNX #9

Description

@ansarisam

I am getting an error when trying to predict from ONNX model (TensorFlow based Object Detection). When I call the api-serving Rest API, I get the following error
b'{"error":"Shape [640, 640, 3], requires 1228800 elements but the buffer has 9830400 elements."}'
Here is the code that is creating the input and calling the Rest API.

import numpy as np
import requests
import onnx_ml_pb2
from ai_serving_pb2 import RecordSpec, Record, PredictRequest, Value
from PIL import Image

port = 6222
base_url = 'http://localhost:' + str(port)
model_name = "objectmodel"
prediction_url = base_url + '/v1/models/' + model_name + '/versions/1'
print("prediction_url=", prediction_url)
image_path = "/content/sampleimage.jpg"

input_tensors = []
input_arrays = []
output_tensors = []
output_arrays = []
output_digits = []

# load image
image = Image.open(image_path)
image = image.resize((640, 640), Image.ANTIALIAS)
image = np.array(image).astype('float')


# create tensor
my_tensor = onnx_ml_pb2.TensorProto()
my_tensor.dims.extend(image.shape)
my_tensor.data_type = 1
my_tensor.raw_data = image.tobytes()

input_tensors.append(my_tensor)

# predict
headers = {'Content-Type': 'application/x-protobuf'}

# Create an instance of RecordSpec using records that contains only the first tensor.
request_message_records = PredictRequest(X=RecordSpec(
    records=[Record(fields={'input_tensor': Value(tensor_value=input_tensors[0])})]
))

# Make prediction for the records request message.
prediction_response_records = requests.post(prediction_url, headers=headers,
                                            data=request_message_records.SerializeToString())
print(prediction_response_records.content)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions