How to create a model on model.tar.gz in SageMaker?

Viewed 1338

I want to create a model on my model artefacts (s3:/bucket/output/model.tar.gz) for the purpose of beach transform and deployment? My model is a simple random forest which I trained it using Python SDK and train script. In my train script, I just have the model_fn function and main function.

now I want to create a model for batch transform job using :

from sagemaker.image_uris import retrieve
image = retrieve(region= sagemaker.Session().boto_session.region_name, framework='sklearn', version='0.23-1' )
from sagemaker.model import Model
estimator =  model.deploy(initial_instance_count = 1 , instance_type = 'ml.p2.xlarge')

and I am getting this error

Error hosting endpoint sagemaker-scikit-learn-2021-05-14-19-43-21-320: Failed. Reason:  The primary container for production variant AllTraffic did not pass the ping health check. Please check CloudWatch logs for this endpoint...

Also, I tried to get a transform job and my job kept running forever with this error

transformer = model.transformer(instance_count=1, instance_type="ml.m5.xlarge")
transformer.transform('address to s3 input')

Error :

Traceback (most recent call last):
File "/miniconda3/lib/python3.7/site-packages/gunicorn/workers/base_async.py", line 55, in handle
self.handle_request(listener_name, req, client, addr)
File "/miniconda3/lib/python3.7/site-packages/gunicorn/workers/ggevent.py", line 143, in handle_request
super().handle_request(listener_name, req, sock, addr)
File "/miniconda3/lib/python3.7/site-packages/gunicorn/workers/base_async.py", line 106, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/miniconda3/lib/python3.7/site-packages/sagemaker_sklearn_container/serving.py", line 128, in main
serving_env.module_dir)
File "/miniconda3/lib/python3.7/site-packages/sagemaker_sklearn_container/serving.py", line 105, in import_module
user_module = importlib.import_module(module_name)
File "/miniconda3/lib/python3.7/importlib/__init__.py", line 118, in import_module
if name.startswith('.'):

AttributeError: 'NoneType' object has no attribute 'startswith'
  • Should I use Sagemaker.model.SKLearnModel? In that case, what's the difference between them?
    If I want to use SKLearnModel then I need to have inference.py, how would it look like? Any sample would be appreciated,
  • why don't I need it if immediately after training I deploy and create a transform job, is that because the model_fn is in my train script?
  • Is not having input_fn and output_fn and predict_fn in my train script a source of the problem?
1 Answers

If you already have the model artifacts in S3 stored as model.tar.gz.

You should -

  1. Create an SKLearnModel
  2. Package your inference.py file (this should have model_fn, predict_fn, input_fn) into file named source.tar.gz
  3. Specify the environment variable SAGEMAKER_SUBMIT_DIRECTORY as the path to source.tar.gz in the SKLearnModel object.
  4. Deploy the model to an endpoint
  5. Create a batch transform job.

Example in the docs

Related