使用 tensorflow 服务预测图像 - "error":“重塑的输入是一个具有 10000 个值的张量

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)

打印 (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}}]]"

我正在尝试对图像中的边界框进行分类。 问题从这部分开始 instances.tolist() 如果我删除 .tolist(),我将得到

TypeError: Object of type EagerTensor is not JSON serializable

如果我保留它,它会破坏图像尺寸。 我该如何解决这个问题?

找到问题了!

我不得不重新训练模型,并使用以下代码保存它:

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 )