Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 890
Ender Avatar asked Jan 25 '23 20:01

Ender


1 Answers

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')
like image 133
Ender Avatar answered May 16 '23 07:05

Ender