amazon-sagemaker-examples icon indicating copy to clipboard operation
amazon-sagemaker-examples copied to clipboard

[Example Request] How to deploy the best tuned model using sagemaker pipelines?

Open jayanthante opened this issue 2 years ago • 0 comments

I have trained an XGBoost model, fine-tuned it, evaluated it and registered it using aws sagemaker pipeline. Now I want to deploy the model which performs the best. 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.

This is what I have done so far:


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

jayanthante avatar Jul 01 '22 12:07 jayanthante