Reputation: 99
I have trained a semantic segmentation model using the sagemaker and the out has been saved to a s3 bucket. I want to load this model from the s3 to predict some images in sagemaker.
I know how to predict if I leave the notebook instance running after the training as its just an easy deploy but doesn't really help if I want to use an older model.
I have looked at these sources and been able to come up with something myself but it doesn't work hence me being here:
https://course.fast.ai/deployment_amzn_sagemaker.html#deploy-to-sagemaker https://aws.amazon.com/getting-started/tutorials/build-train-deploy-machine-learning-model-sagemaker/
https://sagemaker.readthedocs.io/en/stable/pipeline.html
My code is this:
from sagemaker.pipeline import PipelineModel
from sagemaker.model import Model
s3_model_bucket = 'bucket'
s3_model_key_prefix = 'prefix'
data = 's3://{}/{}/{}'.format(s3_model_bucket, s3_model_key_prefix, 'model.tar.gz')
models = ss_model.create_model() # ss_model is my sagemaker.estimator
model = PipelineModel(name=data, role=role, models= [models])
ss_predictor = model.deploy(initial_instance_count=1, instance_type='ml.c4.xlarge')
Upvotes: 7
Views: 11887
Reputation: 386
input_features_data is a dataframe
import sagemaker
from sagemaker.predictor import csv_serializer, json_deserializer
predictor = sagemaker.predictor.RealTimePredictor(
endpoint= PREDICTOR_ENDPOINT_NAME,
sagemaker_session=sagemaker.Session(),
serializer=csv_serializer,
deserializer=json_deserializer,
content_type='text/csv',
)
test_batch_size = 5
num_batches = -(-len(input_features_data) // test_batch_size)
count=0
predicted_values = []
for i in range(num_batches):
predicted_values += [predictor.predict(x) for x in
input_features_data[i * test_batch_size:(i + 1) * test_batch_size]]
return np.asarray(predicted_values)
Upvotes: 0
Reputation: 4037
You can actually instantiate a Python SDK model
object from existing artifacts, and deploy it to an endpoint. This allows you to deploy a model from trained artifacts, without having to retrain in the notebook. For example, for the semantic segmentation model:
trainedmodel = sagemaker.model.Model(
model_data='s3://...model path here../model.tar.gz',
image='685385470294.dkr.ecr.eu-west-1.amazonaws.com/semantic-segmentation:latest', # example path for the semantic segmentation in eu-west-1
role=role) # your role here; could be different name
trainedmodel.deploy(initial_instance_count=1, instance_type='ml.c4.xlarge')
And similarly, you can instantiate a predictor object on a deployed endpoint from any authenticated client supporting the SDK, with the following command:
predictor = sagemaker.predictor.RealTimePredictor(
endpoint='endpoint name here',
content_type='image/jpeg',
accept='image/png')
More on those abstractions:
Model
: https://sagemaker.readthedocs.io/en/stable/model.htmlPredictor
:
https://sagemaker.readthedocs.io/en/stable/predictors.htmlUpvotes: 15