Ender
Ender

Reputation: 202

How to define a SageMaker estimator object using a pre-trained model and then deploy it?

I have a pre-trained model, and its artifacts are saved in the S3 bucket. I tried to figure out how to define an estimator looking at the document: https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html

However, when I tried to deploy the estimator, my code below raised the error "ValueError: Estimator is not associated with a training job." I am not familiar with SageMaker, so I could not find a way to handle it. Here is my code:

my_model_uri = Path_to_model_artifacts   # 's3://..../model.tar.gz'

my_estimator = sagemaker.estimator.Estimator(
container, 
role,   
train_instance_count=1,                                    
train_instance_type='ml.m4.xlarge',                                    
sagemaker_session=session,
model_uri=my_model_uri)

my_predictor = my_estimator.deploy(initial_instance_count = 1, instance_type = 'ml.m4.xlarge')

Here is the error output:

ValueError                                Traceback (most recent call last)
<ipython-input-24-151bc6602c5a> in <module>
     43 
     44 #model_uri = model_uri(SM_MODEL_DIR)
---> 45 my_predictor = my_estimator.deploy(initial_instance_count = 1, instance_type = 'ml.m4.xlarge')
     46 
     47 #path_to_model_artifacts = os.environ[SM_MODEL_DIR]

~/anaconda3/envs/python3/lib/python3.6/site-packages/sagemaker/estimator.py in deploy(self, initial_instance_count, instance_type, accelerator_type, endpoint_name, use_compiled_model, update_endpoint, wait, model_name, kms_key, data_capture_config, tags, **kwargs)
    693                 endpoint and obtain inferences.
    694         """
--> 695         self._ensure_latest_training_job()
    696         endpoint_name = endpoint_name or self.latest_training_job.name
    697         model_name = model_name or self.latest_training_job.name

~/anaconda3/envs/python3/lib/python3.6/site-packages/sagemaker/estimator.py in _ensure_latest_training_job(self, error_message)
    982         """
    983         if self.latest_training_job is None:
--> 984             raise ValueError(error_message)
    985 
    986 

ValueError: Estimator is not associated with a training job

I will appreciate if you can specify the missing parts in my code.

Upvotes: 5

Views: 3508

Answers (2)

Ender
Ender

Reputation: 202

I found a straightforward way to create an Estimator object associated with an existing training job. That is using the attach() method of the class "sagemaker.estimator.Estimator": https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html

Here is the code I wrote to attach a previous training job to the Estimator object and to deploy it. I think it worked because I trained the model inside AWS SageMaker.

my_estimator = sagemaker.estimator.Estimator.attach(TrainingJobName)
my_predictor = my_estimator.deploy(initial_instance_count = 1, instance_type = 'ml.m4.xlarge')

Upvotes: 15

Yoav Zimmerman
Yoav Zimmerman

Reputation: 608

If you're bringing a model trained outside of AWS SageMaker, you're typically going to want to use one of the official SageMaker inference Docker images to deploy your model.

What is the format of your artifact data in model.tar.gz? What framework are you using?

https://sagemaker.readthedocs.io/en/stable/overview.html#using-models-trained-outside-of-amazon-sagemaker is a good documentation point to start at.

Upvotes: 1

Related Questions