|

|  How to Integrate Keras with Google Cloud Platform

How to Integrate Keras with Google Cloud Platform

January 24, 2025

Learn to integrate Keras with Google Cloud Platform using this step-by-step guide. Enhance your AI projects with scalable cloud services.

How to Connect Keras to Google Cloud Platform: a Simple Guide

 

Set Up Google Cloud Platform (GCP)

 

  • Create a Google Cloud account if you don’t have one. Go to the [Google Cloud Console](https://console.cloud.google.com/) and sign in.
  •  

  • After signing in, create a new project or select an existing project for your Keras application.
  •  

  • Enable the necessary APIs for your project. Go to the "APIs & Services" section in the console and enable "Compute Engine", "Cloud Storage", and other services you will be using with Keras.

 

Install Google Cloud SDK

 

  • Download and install the Google Cloud SDK from the [official website](https://cloud.google.com/sdk/docs/quickstarts). This SDK allows you to manage GCP services from the command line.
  •  

  • After installation, initialize the SDK using the command:

 

gcloud init

 

Set Up Virtual Environment and Install Keras

 

  • Create a virtual environment to manage your Python dependencies. In your project directory, run:

 

python -m venv myenv
source myenv/bin/activate  # On Windows use: myenv\Scripts\activate

 

  • Install Keras and other necessary libraries:

 

pip install keras tensorflow google-cloud-storage

 

Configure Authentication for Google Cloud

 

  • Set up application default credentials to allow access to GCP. Generate a service account key file and download the JSON credentials file.
  •  

  • Set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the path of your JSON credentials file:

 

export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/credentials.json"

 

Use Google Cloud Storage with Keras

 

  • To save and load Keras models from Google Cloud Storage, you can use the `google-cloud-storage` package. Below is an example of saving a Keras model:

 

from google.cloud import storage
from keras.models import Sequential

# Define your model
model = Sequential()
# Add layers to your model
# ...

# Save the model to a file
model.save('model.h5')

# Upload to Google Cloud Storage
client = storage.Client()
bucket = client.get_bucket('your-bucket-name')
blob = bucket.blob('path/in/bucket/model.h5')
blob.upload_from_filename('model.h5')

 

  • To load a model from Google Cloud Storage:

 

from keras.models import load_model
from google.cloud import storage

client = storage.Client()
bucket = client.get_bucket('your-bucket-name')
blob = bucket.blob('path/in/bucket/model.h5')
blob.download_to_filename('model.h5')

model = load_model('model.h5')

 

Deploy Keras Model on Google Cloud

 

  • Google Cloud offers several ways to deploy machine learning models, including Google Cloud AI Platform. For a basic deployment, you might use Google App Engine or Cloud Run.
  •  

  • Package your application and deploy using your chosen platform’s deployment guidelines.

 

# Example deployment for App Engine
gcloud app deploy

 

  • Ensure your service account has the necessary roles and permissions to access other GCP resources like storage and compute services.

 

Monitor and Manage the Deployment

 

  • Use Google Cloud Console to monitor the performance of your deployed model.
  •  

  • Set up alerts and logging for any anomalies or performance metrics you need to track.

 

Omi Necklace

The #1 Open Source AI necklace: Experiment with how you capture and manage conversations.

Build and test with your own Omi.

How to Use Keras with Google Cloud Platform: Usecases

 

Use Case: Training a Neural Network with Keras on Google Cloud Platform

 

  • Objective: Utilize Keras for creating a deep learning model and leverage Google Cloud Platform (GCP) for scalable training and deployment.

 

Setting Up the Environment

 

  • Create a Google Cloud account and set up a new project.
  • Enable the necessary APIs, particularly the AI Platform, Compute Engine, and Cloud Storage.
  • Install the Google Cloud SDK on your local machine to interact with GCP resources.
  • Use a virtual environment to manage dependencies:

 

python3 -m venv myenv
source myenv/bin/activate
pip install tensorflow keras google-cloud-storage

 

Develop a Keras Model

 

  • Implement your neural network model using Keras. For example, a simple sequential model could look like this:

 

from keras.models import Sequential
from keras.layers import Dense, Activation

model = Sequential([
    Dense(64, input_shape=(input_dim,)),
    Activation('relu'),
    Dense(10),
    Activation('softmax'),
])

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

 

  • Prepare your dataset for training. Ensure the data is preprocessed and split into appropriate training and validation sets.

 

Leverage Google Cloud Storage

 

  • Upload your dataset to a Google Cloud Storage bucket to easily access and use it during training on GCP.
  • Use the gsutil command to transfer files:

 

gsutil cp /path/to/local/data.csv gs://your-bucket-name/data.csv

 

Train the Model on AI Platform

 

  • Create a training job on Google Cloud AI Platform to utilize GCP's infrastructure for scalable model training.
  • Package your code as a Python module and define a setup.py file for installation on the cloud.
  • Submit a training job:

 

gcloud ai-platform jobs submit training job_name \
    --scale-tier BASIC \
    --package-path /your/package/path \
    --module-name your_package.module_name \
    --region us-central1 \
    -- \
    --data-dir gs://your-bucket-name/data.csv \
    --num-epochs 10

 

Deploy the Model

 

  • Once the model is trained, deploy it using Google Cloud AI Platform for serving predictions through a REST API.
  • Export your trained model as a SavedModel and upload it to Cloud Storage:

 

model.save('model.h5')
gsutil cp model.h5 gs://your-bucket-name/models/model.h5

 

  • Create a model resource and a version of the trained model for serving:

 

gcloud ai-platform models create your_model_name --regions us-central1
gcloud ai-platform versions create v1 \
    --model your_model_name \
    --origin gs://your-bucket-name/models/model.h5 \
    --runtime-version 2.4 \
    --framework "KERAS" \
    --python-version 3.7

 

Conclusion

 

  • By integrating Keras with Google Cloud Platform, you can effectively train and deploy deep learning models at scale. The cloud infrastructure not only accelerates the training process but also provides a robust environment for deploying and managing AI solutions.

 

 

Use Case: Deploying a Keras Image Classification Model Using Google Cloud Platform

 

  • Objective: Build an image classification model using Keras and deploy it on Google Cloud Platform (GCP) to handle requests at scale.

 

Prepare the Environment

 

  • Create a Google Cloud account and initiate a new project.
  • Enable APIs such as Cloud Storage, AI Platform, and Compute Engine for your project.
  • Install Google Cloud SDK for managing GCP resources, and configure it to access your project.
  • Set up a virtual environment and install necessary libraries:

 

python3 -m venv myenv
source myenv/bin/activate
pip install tensorflow keras google-cloud-storage

 

Build the Keras Model

 

  • Create an image classification model using Keras. Here's an example using a convolutional neural network:

 

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

model = Sequential([
    Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),
    MaxPooling2D(pool_size=(2, 2)),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

 

  • Preprocess and organize your image dataset into train and test directories.

 

Utilize Google Cloud Storage

 

  • Upload your image data to a Google Cloud Storage bucket for accessibility during training and inference.
  • Transfer files using the gsutil command:

 

gsutil cp -r /local/path/to/images gs://your-bucket-name/images

 

Train the Model on AI Platform

 

  • Train your model on AI Platform to take advantage of GCP’s powerful infrastructure.
  • Convert your training script into a Python module, and create a setup.py file to define dependencies.
  • Submit the training job:

 

gcloud ai-platform jobs submit training job_name \
    --scale-tier BASIC_TPU \
    --package-path /path/to/your/code \
    --module-name your_code.module_name \
    --region us-central1 \
    -- \
    --data-dir gs://your-bucket-name/images \
    --num-epochs 20

 

Model Deployment on AI Platform

 

  • Deploy your trained model to Google Cloud AI Platform for scalable serving.
  • Export the model using the SavedModel format and upload it to your Cloud Storage bucket:

 

model.save('save_dir/model.h5')
gsutil cp save_dir/model.h5 gs://your-bucket-name/models/model.h5

 

  • Register the model and create a version to serve it through a managed endpoint:

 

gcloud ai-platform models create image_classifier --regions us-central1
gcloud ai-platform versions create v1 \
    --model image_classifier \
    --origin gs://your-bucket-name/models/model.h5 \
    --runtime-version 2.5 \
    --framework "KERAS" \
    --python-version 3.8

 

Conclusion

 

  • By integrating Keras with Google Cloud Platform, you can efficiently build, train, and deploy image classification models. GCP provides resources that enable model scalability and accessibility for users worldwide through its robust infrastructure.

 

Omi App

Fully Open-Source AI wearable app: build and use reminders, meeting summaries, task suggestions and more. All in one simple app.

Github →

OMI NECKLACE + OMI APP
First & only open-source AI wearable platform

a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded