loading and deploying a previously trained sagemaker xgboost model

0

I am trying to write an inference pipeline where I load a previously trained sagemaker xgboost model stored in s3 as a tar.gz file (following sagemaker tutorial) and deploy it as an endpoint for prediction. Here is my code:

trainedmodel = sagemaker.model.Model(    
    model_data='data-path-to-my-model-in-s3/model.tar.gz',
    image=container,  
    role=role)  

xgb_predictor = trainedmodel.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge')

The code runs fine but after that when I try to call predict() on xgb_predictor I get an error saying 'NoneType' object has no attribute 'predict'. I followed the example here to train the xgboost model:

https://aws.amazon.com/blogs/machine-learning/simplify-machine-learning-with-xgboost-and-amazon-sagemaker/

Why am I getting this error? What's the correct way to load a previously trained model? Help would be appreciated.

hadi86
asked 3 years ago498 views
3 Answers
0

thanks for using SageMaker! you're on the right path - you'll need to pass in an argument for "predictor_cls" when creating your Model instance in order for a predictor object to be returned after calling deploy(), e.g.

from sagemaker.model import Model
from sagemaker.predictor import RealTimePredictor, csv_serializer, csv_deserializer

class Predictor(RealTimePredictor):
    def __init__(self, endpoint_name, sagemaker_session=None):
        super(Predictor, self).__init__(
            endpoint_name, sagemaker_session, csv_serializer, csv_deserializer
        )

trainedmodel = Model(..., predictor_cls=Predictor)
xgb_predictor = trainedmodel.deploy(...)

xgb_predictor.predict(...)

API reference:

hope that helps!

answered 3 years ago
0

Thank you. This solution worked!

hadi86
answered 3 years ago
0

Any special reason for using csv serializer/deserializer? In my case I reload a model to analyze videos (frames in numpy array actually). What serializer/deserializer should I use?
Actually, any doc regarding how to properly use the argument predictor_cls would be highly appreicated.

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions