I have a python code and a model that is pre-trained and has a model.pkl file with me in the same directory where the code i, now i have to run or deploy this to the aws sagemaker but not getting any solution for this as aws sagemaker supports only two commands train or serve for training and deploying respectively.
currently, I am running the program using the command "python filename.py" and it is running successfully I want the same to run on the aws sagemaker.
Any Solution??
I tried the same as deploying the model to the s3 and call at the time of deploy I don't know is it correct or wrong.
If you have a pretrained model and a file
filename.py
that you want to run on SageMaker Endpoints, you just need to package this up as a Docker image to create a model which you can then deploy to an Endpoint and make invocations.To do this, I'm just following this guide on the AWS documentation on using your own inference code.
The steps will be:
Step 1: Create the model code
Let's take this simple model as an example in Python:
Here's are requirements.txt:
Step 2: Create the Docker image
We need a Dockerfile to build our image. Here's the one I used:
Dockerfile:
We can build the image by running:
docker build -t simple-model:latest .
This will create the image and now we can test it by running it:
If it is running, you should be able to
curl
any of the endpoints:Now we need to publish it to ECR as SageMaker reads the model from ECR. I'm following this guide from AWS
Grab the image id by running
docker images
Use that here. For convenience, I'm using us-west-2. Replace that with your chosen region:
Now we should push it to ECR:
Step 3: Create Endpoint
Now we can create a model with this image. First, you need a SageMaker Execution role. This will be used to access your image and other resources. You can set that up here on this AWS doc page.
Secondly, you need to have the AWS CLI setup.
Let's get started.
Let's create the model first. This will point to your ECR image you created in the last step. Substitute the role name you created above in this command:
That'll create your model. Now we need to create an
EndpointConfig
which will tell your SageMaker Endpoint how it needs to be configured:And now finally, we can create our Endpoint using that config:
If all that works, wait until
aws sagemaker describe-endpoint --endpoint-name SimpleEndpoint
says it isInService
.Once it is, we can now call invocations against it:
Conclusion
If that all worked, you'll have your own endpoint. The next steps would be to customize that Python script to do your own inference with your own model. SageMaker also has the ability to grab your model artifacts automatically and you don't have to include them in your model container. See the documentation here.
Hopefully that helps!