从 Vertex AI 端点进行推理

问题描述 投票:0回答:1

我已经训练了 YOLOv8 模型并将其部署到 Vertex AI,具有以下签名

! saved_model_cli show --dir "runs/detect/train72/weights/best_saved_model" --tag_set serve --signature_def serving_default

The given SavedModel SignatureDef contains the following input(s):
  inputs['images'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 3, 960, 960)
      name: serving_default_images:0
The given SavedModel SignatureDef contains the following output(s):
  outputs['output0'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 6, 18900)
      name: StatefulPartitionedCall:0
Method name is: tensorflow/serving/predict

现在,我想知道如何从存储在我的存储桶 input_bucket/image.png 中的图像对其进行推断。

对于初学者,我尝试运行此代码

im_test = cv2.imread(im_test_path)
image = cv2.cvtColor(im_test, cv2.COLOR_BGR2RGB)

    # Resize the image to match the model's expected input size
image = cv2.resize(image, (960, 960))
#image = image / 255.0

    # Change data type to float32
image = image.astype(np.float32)
image = np.expand_dims(image, axis=0)  # Add the batch dimension

# Transpose the image to have the channel dimension as specified in the model
image = np.transpose(image, (0, 3, 1, 2))  # From NHWC to NCHW
print(image.shape)
encoded_image = numpy_array_to_base64(image)


instance = {"images": encoded_image}
instances = [instance]
client_options = {"api_endpoint": api_endpoint}
    # Initialize client that will be used to create and send requests.
    # This client only needs to be created once, and can be reused for multiple requests.
client = aiplatform.gapic.PredictionServiceClient(client_options=client_options)
parameters = predict.params.ImageObjectDetectionPredictionParams(
        confidence_threshold=0.5,
        max_predictions=5,
    ).to_value()
endpoint = client.endpoint_path(
        project=project, location=location, endpoint=endpoint_id
    )
response = client.predict(
        endpoint=endpoint, instances=instances, parameters=parameters
    )

但是它给了我这个错误

FailedPrecondition: 400 The request size (14745946 bytes) exceeds 1.500MB limit.

我原来的代码正确吗?我唯一的问题是图像尺寸吗?

python google-cloud-platform yolo google-cloud-vertex-ai
1个回答
0
投票

这是 Vertex AI Endpoints 的图像大小限制,您可以查看此处此处

您可以在发送到模型端点之前尝试调整图像大小。

© www.soinside.com 2019 - 2024. All rights reserved.