Deploy Machine Learning Models to Google Cloud, AWS, and Heroku
In the previous lesson, we explored how to serve machine learning models using Flask and FastAPI. These tools helped us create APIs that could run predictions locally. However, when it comes to real-world use, we need to deploy models to the cloud for better scalability and reliability. This lesson will guide you through deploying models to Google Cloud, AWS, and Heroku, which are platforms that can handle multiple requests efficiently.
Let me share a use-case I faced recently. I built a model that predicts house prices based on features like location, size, and amenities. While testing it locally with Flask, I realized it couldn’t handle more than 10 requests at a time. This was a problem because the app was meant for a real estate website with thousands of users. That’s when I decided to deploy it to the cloud. By using Google Cloud, I was able to scale the model to handle over 1,000 requests per second without crashing.
Now, let’s dive into the steps for deploying models to each platform.
Deploying Models to Google Cloud
Google Cloud is a powerful platform that offers tools like AI Platform and Cloud Functions for deploying machine learning models. Here’s how you can deploy your model:
- Prepare Your Model
Save your trained model in a format like TensorFlow SavedModel or scikit-learn’s joblib. For example, if you’re using TensorFlow, you can save the model using:
model.save('saved_model/')
-
Create a Google Cloud Project
Go to the Google Cloud Console and create a new project. Enable the AI Platform API for your project. -
Upload Your Model
Use the Google Cloud SDK to upload your model to a storage bucket:
gsutil cp -r saved_model/ gs://your-bucket-name/
- Deploy the Model
Use the AI Platform to create a model version:
gcloud ai-platform versions create v1 --model=your_model_name --origin=gs://your-bucket-name/saved_model/ --runtime-version=2.10
- Test the Deployment
Send a prediction request using the AI Platform API:
from google.cloud import aiplatform
aiplatform.init(project='your-project-id', location='us-central1')
endpoint = aiplatform.Endpoint('your-endpoint-id')
response = endpoint.predict(instances=[your_input_data])
print(response.predictions)
By following these steps, you can deploy your model to Google Cloud and scale it to handle high traffic.
Deploying Models to AWS
AWS is another popular platform for deploying machine learning models. It offers services like SageMaker and Lambda for this purpose.
- Save Your Model
Export your model in a format like ONNX or PMML. For example, if you’re using PyTorch, you can save the model as:
torch.save(model.state_dict(), 'model.pth')
- Create an S3 Bucket
Upload your model to an S3 bucket using the AWS Management Console or CLI:
aws s3 cp model.pth s3://your-bucket-name/
- Create a SageMaker Model
Use SageMaker to create a model and deploy it to an endpoint:
import sagemaker
sagemaker_session = sagemaker.Session()
model = sagemaker.model.Model(model_data='s3://your-bucket-name/model.pth', role='your-iam-role', image_uri='your-docker-image')
predictor = model.deploy(initial_instance_count=1, instance_type='ml.m5.large')
- Test the Deployment
Send a prediction request to the SageMaker endpoint:
response = predictor.predict(your_input_data)
print(response)
AWS SageMaker makes it easy to deploy and scale models for production use.
Deploying Models to Heroku
Heroku is a user-friendly platform for deploying web applications, including machine learning models.
- Create a Flask or FastAPI App
Wrap your model in a Flask or FastAPI app. For example:
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/predict', methods=['POST'])
def predict():
data = request.json
prediction = model.predict([data['input']])
return jsonify({'prediction': prediction.tolist()})
- Create a Procfile
Add a Procfile to your project to define how Heroku should run your app:
web: gunicorn app:app
- Deploy to Heroku
Use the Heroku CLI to deploy your app:
heroku create
git push heroku main
- Test the Deployment
Send a POST request to your Heroku app’s endpoint:
curl -X POST -H "Content-Type: application/json" -d '{"input": [1, 2, 3]}' https://your-app.herokuapp.com/predict
Heroku is a great choice for small to medium-scale deployments.
Conclusion
In this tutorial, we covered how to deploy machine learning models to Google Cloud, AWS, and Heroku. Each platform has its strengths, and the choice depends on your needs. Google Cloud and AWS are ideal for large-scale deployments, while Heroku is perfect for smaller projects.
By following the steps above, you can ensure your models are scalable and ready for real-world use. In the next lesson, we’ll dive into advanced topics like model monitoring and optimization.
Comments
There are no comments yet.