本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
從部署的服務 (Boto3) 請求推論
您可以使用 for Python (Boto3) 用戶端和 提交推論請求 SageMaker SDK,invoke_endpoint()
InService
。下列程式碼範例會示範如何傳送映像進行推論:
- PyTorch and MXNet
-
import boto3 import json endpoint =
'insert name of your endpoint here'
runtime = boto3.Session().client('sagemaker-runtime') # Read image into memory with open(image, 'rb') as f: payload = f.read() # Send image via InvokeEndpoint API response = runtime.invoke_endpoint(EndpointName=endpoint, ContentType='application/x-image', Body=payload) # Unpack response result = json.loads(response['Body'].read().decode()) - TensorFlow
-
使用 TensorFlow 提交
application/json
內容類型的輸入。from PIL import Image import numpy as np import json import boto3 client = boto3.client('sagemaker-runtime') input_file = 'path/to/image' image = Image.open(input_file) batch_size = 1 image = np.asarray(image.resize((224, 224))) image = image / 128 - 1 image = np.concatenate([image[np.newaxis, :, :]] * batch_size) body = json.dumps({"instances": image.tolist()}) ioc_predictor_endpoint_name = 'insert name of your endpoint here' content_type = 'application/json' ioc_response = client.invoke_endpoint( EndpointName=ioc_predictor_endpoint_name, Body=body, ContentType=content_type )
- XGBoost
-
對於XGBoost應用程式,您應該改為提交CSV文字:
import boto3 import json endpoint =
'insert your endpoint name here'
runtime = boto3.Session().client('sagemaker-runtime') csv_text = '1,-1.0,1.0,1.5,2.6' # Send CSV text via InvokeEndpoint API response = runtime.invoke_endpoint(EndpointName=endpoint, ContentType='text/csv', Body=csv_text) # Unpack response result = json.loads(response['Body'].read().decode())
請注意, BYOM 允許自訂內容類型。如需詳細資訊,請參閱runtime_InvokeEndpoint
。
從部署的服務請求推論 (Amazon SageMaker SDK)
從部署的服務請求推論 (AWS CLI)