SageMaker Project: Image Classification Task Using SageMaker Canvas
To download the zip file of fruit images for this project, please click here
On your local machine, please place the unzipped fruit-data folder into a new folder called Fruit Classification that will exist within Data Science Infinity/AWS/SageMaker
Code to invoke deployed model:
import boto3
import json
# initialize the SageMaker runtime client
client = boto3.client("sagemaker-runtime")
# specify the path for the image file
file_name = r'xxxxxxx'
# open the file in binary mode
with open(file_name, 'rb') as f:
payload = f.read()
# specify the SageMaker endpoint name
endpoint_name = "xxxxxx"
# Invoke the endpoint
response = client.invoke_endpoint(
EndpointName=endpoint_name,
ContentType="application/x-image", # This content type may vary depending on the model
Body=payload
)
# print the full response received from the model
# (predicted_label, probability, probabilities, labels)
prediction = response['Body'].read().decode('utf-8')
print(prediction)
# print only the predicted label
prediction_label = prediction.split(',')[0]
print(prediction_label)