1

We train our sklearn model locally and then upload it as *.tar.gz file to S3 in order to deploy it via Sagemaker. There we use Sagemaker's own SKLearnModel docker image and aim to deploy our model to a Sagemaker endpoint. Our approach is similar to what is described in this thread or over here.

This is basically, what the code in our Sagemaker jupyter notebook looks like:

from sagemaker.sklearn.model import SKLearnModel

sagemaker_model = SKLearnModel(model_data='s3://ourModelS3Bucket/ourModelTarball.tar.gz',
                                 role='arn:aws:iam::someNumber:role/OurPredefinedRole',
                                 entry_point='our_entry_point.py')

predictor = sagemaker_model.deploy(initial_instance_count=1,
                                   instance_type='ml.t2.large') #be careful which instance you choose!

What we want to know is, how do we have to write the entry point code (here called 'our_entry_point.py') for our model, so that Sagemaker can deploy it successfully?

Does it have to contain a predict() function, is it executed like a script from top to bottom or do we have to define a if __name__ == "__main__": block? This is especially relevant, since we probably want to add some additional feature generation/selection here, before we execute the prediction itself.

Any help would be very welcome, either a direct answer or a link to a documentation/tutorial which might provide the answer. So far, the Sagemaker docs and some of the Github examples were quite helpful...

Syrill
  • 27
  • 1
  • 8
  • PS: We are aware that we can/could use [AWS Sagemaker's inference pipeline](https://stackoverflow.com/questions/49581156/how-can-i-preprocess-input-data-before-making-predictions-in-sagemaker) for preprocessing. – Syrill Nov 20 '19 at 12:08
  • Id recommend taking a look at: * https://github.com/awslabs/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/scikit_learn_randomforest/Sklearn_on_SageMaker_end2end.ipynb – sanjams Dec 12 '19 at 21:25

1 Answers1

0

Id recommend taking a look at:

But to answer your questions directly: A main block is always a good idea to have for portability of your code. SageMaker does support but does not require specifying such a block in your entry_point script (see example above). SageMaker instead will import the functions it needs from that file and not execute the main block.

In terms of the functions you need to define, the docs above state:

Before a model can be served, it must be loaded. The SageMaker Scikit-learn model server loads your model by invoking a model_fn function that you must provide in your script. The model_fn should have the following signature:

and

The SageMaker Scikit-learn model server provides default implementations of these functions. You can provide your own implementations for these functions in your hosting script. If you omit any definition then the SageMaker Scikit-learn model server will use its default implementation for that function.

So it looks like you have to define model_fn but everything else is optional (this can also be seen in the example)

sanjams
  • 190
  • 1
  • 11