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 will explore how to fine-tune a pre-trained Gemma 4 model for pet breed classification using Cloud Run Jobs and NVIDIA RTX 6000 Pro GPUs. Gemma 4 is a powerful AI model that can be used for various computer vision tasks, including image classification, object detection, and segmentation.

Prerequisites

  • Google Cloud account with billing enabled
  • NVIDIA RTX 6000 Pro GPU
  • Cloud Run Jobs enabled
  • Python 3.9 or later
  • pip installed

Step 1: Create a Cloud Run Job

To create a Cloud Run Job, we need to create a new Cloud Run service and configure it to use a GPU. We will use the gcloud command-line tool to create the service.

gcloud services enable run.googleapis.com
gcloud run services create pet-breed-classifier --platform managed --region us-central1 --cpu 1 --memory 2G --concurrency 80 --port 8080 --allow-unauthenticated
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure the Cloud Run Job to use a GPU

To use a GPU with Cloud Run, we need to configure the service to use a GPU instance. We will use the gcloud command-line tool to update the service configuration.

gcloud run services update pet-breed-classifier --platform managed --region us-central1 --cpu 1 --memory 2G --concurrency 80 --port 8080 --allow-unauthenticated --accelerator=type=nvidia-tesla-t4,count=1
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Docker Image for the Gemma 4 Model

To fine-tune the Gemma 4 model, we need to create a Docker image that includes the model and the necessary dependencies. We will use the docker command-line tool to create the image.

FROM tensorflow/tensorflow:2.9.1-gpu

# Install necessary dependencies
RUN pip install tensorflow-gpu==2.9.1
RUN pip install numpy
RUN pip install Pillow

# Copy the Gemma 4 model
COPY gemma4_model.tar.gz /app/

# Set the working directory
WORKDIR /app/

# Unpack the Gemma 4 model
RUN tar -xvf gemma4_model.tar.gz

# Set the environment variables
ENV TF_CPP_MIN_LOG_LEVEL=2
ENV TF_ENABLE_XLA=False
Enter fullscreen mode Exit fullscreen mode

Step 4: Build the Docker Image

To build the Docker image, we need to run the docker build command.

docker build -t pet-breed-classifier .
Enter fullscreen mode Exit fullscreen mode

Step 5: Push the Docker Image to Google Container Registry

To use the Docker image with Cloud Run, we need to push it to Google Container Registry. We will use the gcloud command-line tool to push the image.

gcloud container images push gcr.io/[PROJECT_ID]/pet-breed-classifier
Enter fullscreen mode Exit fullscreen mode

Step 6: Create a Cloud Run Job Configuration File

To create a Cloud Run Job, we need to create a configuration file that specifies the Docker image and the command to run. We will use a YAML file to define the configuration.

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: pet-breed-classifier
spec:
  template:
    spec:
      containers:
      - name: pet-breed-classifier
        image: gcr.io/[PROJECT_ID]/pet-breed-classifier
        command: ["python", "main.py"]
        args: ["--model", "gemma4_model"]
Enter fullscreen mode Exit fullscreen mode

Step 7: Create a Cloud Run Job

To create a Cloud Run Job, we need to apply the configuration file to the Cloud Run service. We will use the kubectl command-line tool to apply the configuration.

kubectl apply -f pet-breed-classifier.yaml
Enter fullscreen mode Exit fullscreen mode

Step 8: Fine-Tune the Gemma 4 Model

To fine-tune the Gemma 4 model, we need to create a Python script that uses the tf.keras API to fine-tune the model. We will use the main.py file to define the fine-tuning script.


python
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam

# Load the Gemma 4 model
model = tf.keras.models.load_model('gemma4_model')

# Define the fine-tuning data generator
train_datagen = ImageDataGenerator(rescale=1./255,
                                    shear_range=0.2,
                                    zoom_range=0.2,
                                    horizontal_flip=True)

# Define the fine-tuning optimizer and loss function
optimizer = Adam(lr=1e-5)
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

# Define the fine-tuning epochs and batch size
epochs = 10
batch_size = 32

# Fine-tune the Gemma 4 model
history = model.fit(train_datagen.flow_from_directory('data/train',
                                                      target_size=(224, 224),
                                                      batch_size=batch_size,
                                                      class_mode='sparse'),
                    epochs=epochs,
                    validation_data=train_datagen.flow_from_directory('data/validation',
                                                                      target_size=(224, 224),
                                                                      batch_size=batch_size,
                                                                      class_mode='sparse'),
                    verbose=2,
                    callbacks=[tf.keras.callbacks.ModelCheckpoint('gemma4_model_fine_tuned.h5',
                                                                 save_best_only=True,
                                                                 monitor='val_loss',


---

☕ **Appreciative**
Enter fullscreen mode Exit fullscreen mode

Top comments (0)