DEV Community

Orbit Websites
Orbit Websites

Posted on

Fine-Tuning Gemma 4 with Cloud Run Jobs: Unlocking Serverless GPU Power with NVIDIA RTX 6000 Pro for Pet Breed Classification

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

  1. Log in to your GCP account and create a new project.
  2. Navigate to the Cloud Run page and click on "Enable Cloud Run" to enable the service.
  3. Verify that the Cloud Run API is enabled in the API Library page.

Step 2: Set Up a Cloud Run Service

  1. Create a new Cloud Run service by clicking on "Create service" in the Cloud Run page.
  2. Choose "Containerized" as the service type and select the Python 3.9 runtime.
  3. Set the service name and click on "Create".

Step 3: Install Required Libraries

  1. Create a new file named requirements.txt and add the following dependencies:
gemma4
google-cloud-aiplatform
google-cloud-run
nvidia-ml-py
Enter fullscreen mode Exit fullscreen mode
  1. Install the dependencies using pip:
pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Step 4: Clone the Gemma 4 Model

  1. Clone the Gemma 4 model repository using Git:
git clone https://github.com/google-research/gemma.git
Enter fullscreen mode Exit fullscreen mode
  1. Navigate to the cloned repository and create a new directory for your project:
mkdir pet_breed_classification
cd pet_breed_classification
Enter fullscreen mode Exit fullscreen mode

Step 5: Prepare the Dataset

  1. Download the pet breed classification dataset from Kaggle:
wget https://www.kaggle.com/competitions/pet-breed-classification/data.zip
Enter fullscreen mode Exit fullscreen mode
  1. Extract the dataset:
unzip data.zip
Enter fullscreen mode Exit fullscreen mode

Step 6: Fine-Tune the Gemma 4 Model

  1. Create a new file named main.py and 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')
Enter fullscreen mode Exit fullscreen mode
  1. Run the code using the following command:
python main.py
Enter fullscreen mode Exit fullscreen mode

Step 7: Deploy the Fine-Tuned Model to Cloud Run

  1. Create a new file named app.py and 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()
Enter fullscreen mode Exit fullscreen mode
  1. Create a new file named Dockerfile and 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**
Enter fullscreen mode Exit fullscreen mode

Top comments (0)