Fine-Tuning Gemma 4 with Cloud Run Jobs: Unlocking Serverless GPU Power with NVIDIA RTX 6000 Pro for Pet Breed Classification
Introduction
In this article, we'll explore how to fine-tune a pre-trained Gemma 4 model for pet breed classification using Cloud Run Jobs and the NVIDIA RTX 6000 Pro GPU. This tutorial is designed for beginners who want to leverage serverless computing and GPU acceleration for machine learning tasks.
Prerequisites
- Google Cloud Platform (GCP) account
- Cloud Run enabled
- NVIDIA RTX 6000 Pro GPU available in your GCP project
- Python 3.9+
- pip installed
Step 1: Create a GCP Project and Enable Cloud Run
- Log in to your GCP account and create a new project.
- Navigate to the Cloud Run page and click on "Enable Cloud Run" to enable the service.
- Verify that the Cloud Run API is enabled in the API Library page.
Step 2: Set Up a Cloud Run Service
- Create a new Cloud Run service by clicking on "Create service" in the Cloud Run page.
- Choose "Containerized" as the service type and select the Python 3.9 runtime.
- Set the service name and click on "Create".
Step 3: Install Required Libraries
- Create a new file named
requirements.txtand add the following dependencies:
gemma4
google-cloud-aiplatform
google-cloud-run
nvidia-ml-py
- Install the dependencies using pip:
pip install -r requirements.txt
Step 4: Clone the Gemma 4 Model
- Clone the Gemma 4 model repository using Git:
git clone https://github.com/google-research/gemma.git
- Navigate to the cloned repository and create a new directory for your project:
mkdir pet_breed_classification
cd pet_breed_classification
Step 5: Prepare the Dataset
- Download the pet breed classification dataset from Kaggle:
wget https://www.kaggle.com/competitions/pet-breed-classification/data.zip
- Extract the dataset:
unzip data.zip
Step 6: Fine-Tune the Gemma 4 Model
- Create a new file named
main.pyand add the following code:
import os
import numpy as np
import pandas as pd
from gemma4 import Gemma4
from google.cloud import aiplatform
from google.cloud import run
from nvidia_ml_py import Nvml
# Set up the NVIDIA RTX 6000 Pro GPU
nvml = Nvml()
nvml.init()
gpu_id = nvml.device_get_handle_by_index(0)
# Set up the Cloud Run service
client = run.Client()
service = client.services.get(name='pet-breed-classification-service')
# Set up the Gemma 4 model
model = Gemma4(
name='pet-breed-classification-model',
description='Pet breed classification model',
gpu_id=gpu_id
)
# Load the dataset
df = pd.read_csv('data/pet_breed_classification.csv')
# Split the dataset into training and validation sets
train_df, val_df = df.split(test_size=0.2, random_state=42)
# Fine-tune the Gemma 4 model
model.fit(
train_df,
epochs=10,
batch_size=32,
validation_data=val_df
)
# Save the fine-tuned model
model.save('pet-breed-classification-model.h5')
- Run the code using the following command:
python main.py
Step 7: Deploy the Fine-Tuned Model to Cloud Run
- Create a new file named
app.pyand add the following code:
import os
import numpy as np
from google.cloud import aiplatform
from google.cloud import run
from nvidia_ml_py import Nvml
# Set up the NVIDIA RTX 6000 Pro GPU
nvml = Nvml()
nvml.init()
gpu_id = nvml.device_get_handle_by_index(0)
# Set up the Cloud Run service
client = run.Client()
service = client.services.get(name='pet-breed-classification-service')
# Load the fine-tuned model
model = Gemma4.load('pet-breed-classification-model.h5')
# Define a prediction function
def predict(request):
# Get the input data from the request
input_data = request.get_json()
# Preprocess the input data
input_data = np.array(input_data)
# Make a prediction using the fine-tuned model
prediction = model.predict(input_data)
# Return the prediction
return {'prediction': prediction.tolist()}
# Create a Cloud Run job
job = client.jobs.create(
name='pet-breed-classification-job',
service=service,
container='pet-breed-classification-container',
command=['python', '-m', 'app']
)
# Start the Cloud Run job
job.start()
- Create a new file named
Dockerfileand add the following code:
dockerfile
FROM python:3.9-slim
# Install the required libraries
RUN pip install -r requirements.txt
# Copy the fine-tuned model
COPY pet-breed-classification-model.h5 /app/
# Define the prediction function
WORKDIR /app
COPY app.py /app/
# Expose the prediction function
EXPOSE 8080
# Run the prediction function
CMD ["python",
---
☕ **Factual**
Top comments (0)