How to deploy the best tuned model using sagemaker pipelines?

Viewed 79

I have trained an XGBoost model, finetuned it, evaluated it and registered it using aws sagemaker pipeline. Now I want to deploy the model. However, the location of the model artefact is saved as Join object because of which deployment is not working by best_model.deploy(...). Any suggestions on how to deploy the best trained model.

tuning_step = TuningStep(name="HPTuning",
                        tuner=tuner_log,
                        inputs={
                            "train":...,
                            "validation":...
                        },
                        cache_config=cache_config)


best_model = Model(image_uri=image_uri, 
                  model_data=tuning_step.get_top_model_s3_uri(top_k=0,s3_bucket=model_bucket_key),
                  sagemaker_session=sm_session,
                  role=role,
                  predictor_cls=XGBoostPredictor)


register_step = RegisterModel(name="RegisterBestChurnModel",
                             estimator=xgb_estimator,
                             model_data=tuning_step.get_top_model_s3_uri(top_k=0, s3_bucket=model_bucket_key),
                             content_types=["text/csv"],
                             response_types=["test/csv"],
                             inference_instances=["ml.t2.medium", "ml.m5.large"],
                             transform_instances=["ml.m5.large"],
                             approval_status="Approved",
                             model_metrics=model_metrics)


The problem with best_model.deploy(...) is that tuning_step.get_top_model_s3_uri(top_k=0,s3_bucket=model_bucket_key) is a Join object. And deploy needs the s3 bucket location as a string. So that doesn't work.

I was also trying to deploy the registered model using

model_package_arn = register_step.properties.ModelPackageArn,
model = ModelPackage(role=role, 
                     model_package_arn=create_top_step.properties.ModelArn, 
                     sagemaker_session=session)
model.deploy(initial_instance_count=1, instance_type='ml.m5.xlarge')

which is giving me the error

...

/opt/conda/lib/python3.7/site-packages/sagemaker/model.py in _create_sagemaker_model(self, *args, **kwargs)
   1532             container_def["Environment"] = self.env
   1533 
-> 1534         self._ensure_base_name_if_needed(model_package_name.split("/")[-1])
   1535         self._set_model_name_if_needed()
   1536 

AttributeError: 'tuple' object has no attribute 'split'

Which I also suspect arise for the same reason.

I followed this tutorial pretty much

https://github.com/aws/amazon-sagemaker-examples/blob/main/sagemaker-pipelines/tabular/tuning-step/sagemaker-pipelines-tuning-step.ipynb

1 Answers

There are two ways to do this -

1/ Once you have registered the best model from the Tuning Job in the Model Registry, run your model deployment code through the Lambda Step. Here is an example of how to do that. Pass the output of the register step as the input of the Lambda step and call your model deployment code in the Lambda function.

2/ After your sagemaker pipeline executes and you have registered the model, use the registered model ARN from the Model Registry and call the deployment code. You can not use register_step.properties.ModelPackageArn outside the context of the Pipeline execution.

Related