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

Introduction to Fine-Tuning Gemma 4 with Cloud Run Jobs

Fine-tuning pre-trained models like Gemma 4 can be a powerful way to achieve state-of-the-art results on specific tasks, such as pet breed classification. However, training these models requires significant computational resources, particularly when using GPU acceleration. In this article, we will explore how to fine-tune Gemma 4 using Cloud Run Jobs, leveraging the NVIDIA RTX 6000 Pro GPU for serverless computing.

Prerequisites

Before we begin, ensure you have the following:

  • A Google Cloud account with the Cloud Run and Cloud Storage APIs enabled
  • The gcloud command-line tool installed and authenticated
  • A basic understanding of Python and deep learning concepts
  • The transformers and torch libraries installed (pip install transformers torch)

Step 1: Prepare the Dataset

First, we need a dataset for pet breed classification. For this example, we'll use the Stanford Dogs Dataset. Download the dataset and extract it to a local directory.

# Download the Stanford Dogs Dataset
wget http://vision.stanford.edu/aditya86/ImageNetDogs/main.zip
unzip main.zip
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Cloud Storage Bucket

Create a Cloud Storage bucket to store our dataset and model checkpoints.

# Create a Cloud Storage bucket
gsutil mb gs://pet-breed-classification
Enter fullscreen mode Exit fullscreen mode

Step 3: Upload the Dataset to Cloud Storage

Upload the dataset to our Cloud Storage bucket.

# Upload the dataset to Cloud Storage
gsutil -m cp -r Images gs://pet-breed-classification/
Enter fullscreen mode Exit fullscreen mode

Step 4: Create a Cloud Run Job

Create a Cloud Run Job that will run our fine-tuning script. We'll use the gcloud command-line tool to create the job.

# Create a Cloud Run Job
gcloud run jobs create pet-breed-classification \
  --image=gcr.io/cloud-run/jobs/gpu:latest \
  --region=us-central1 \
  --platform=managed \
  --set-env-vars=BUCKET=gs://pet-breed-classification \
  --set-env-vars=MODEL=gemma4 \
  --set-env-vars=GPU_TYPE=nvidia-rtx6000-pro
Enter fullscreen mode Exit fullscreen mode

Step 5: Define the Fine-Tuning Script

Create a Python script that will fine-tune the Gemma 4 model using the Stanford Dogs Dataset. We'll use the transformers library to load the pre-trained model and the torch library for training.

# fine_tune_gemma4.py
import os
import torch
from transformers import Gemma4ForImageClassification, Gemma4FeatureExtractor
from PIL import Image
from torch.utils.data import Dataset, DataLoader
from sklearn.metrics import accuracy_score

# Define a custom dataset class for the Stanford Dogs Dataset
class StanfordDogsDataset(Dataset):
    def __init__(self, images, labels, feature_extractor):
        self.images = images
        self.labels = labels
        self.feature_extractor = feature_extractor

    def __len__(self):
        return len(self.images)

    def __getitem__(self, idx):
        image = Image.open(self.images[idx])
        inputs = self.feature_extractor(images=image, return_tensors="pt")
        label = torch.tensor(self.labels[idx])
        return inputs, label

# Load the pre-trained Gemma 4 model and feature extractor
model = Gemma4ForImageClassification.from_pretrained("gemma4")
feature_extractor = Gemma4FeatureExtractor.from_pretrained("gemma4")

# Load the dataset and create a data loader
dataset = StanfordDogsDataset(
    images=["Images/n02085620-Chihuahua/n02085620-Chihuahua-1.jpg"],
    labels=[0],
    feature_extractor=feature_extractor
)
data_loader = DataLoader(dataset, batch_size=32, shuffle=True)

# Fine-tune the model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-5)

for epoch in range(5):
    model.train()
    total_loss = 0
    for batch in data_loader:
        inputs, labels = batch
        inputs = {k: v.to(device) for k, v in inputs.items()}
        labels = labels.to(device)
        optimizer.zero_grad()
        outputs = model(**inputs)
        loss = criterion(outputs.logits, labels)
        loss.backward()
        optimizer.step()
        total_loss += loss.item()
    print(f"Epoch {epoch+1}, Loss: {total_loss / len(data_loader)}")

# Evaluate the model
model.eval()
predictions = []
labels = []
with torch.no_grad():
    for batch in data_loader:
        inputs, label = batch
        inputs = {k: v.to(device) for k, v in inputs.items()}
        outputs = model(**inputs)
        logits = outputs.logits
        predictions.extend(torch.argmax(logits, dim=1).cpu().numpy())
        labels.extend(label.cpu().numpy())
accuracy = accuracy_score(labels, predictions)
print(f"Accuracy: {accuracy:.4f}")
Enter fullscreen mode Exit fullscreen mode

Step 6: Run the Fine-Tuning Script

Run the fine-tuning script using the Cloud Run Job.

# Run the fine-tuning script
gcloud run jobs execute pet-breed-classification \
  --region=us-central1 \
  --platform=managed \
  --command="python fine_tune_gemma4.py"
Enter fullscreen mode Exit fullscreen mode

Benefits of Using Cloud Run


Professional

Top comments (0)