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 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 Platform (GCP) account
  • NVIDIA RTX 6000 Pro GPU
  • Cloud Run Jobs
  • Gemma 4 model
  • Python 3.9+
  • TensorFlow 2.5+

Step 1: Create a GCP Project and Enable Cloud Run Jobs

To get started, create a new GCP project and enable Cloud Run Jobs. You can do this by following these steps:

gcloud projects create my-project
gcloud services enable run.googleapis.com
gcloud services enable aiplatform.googleapis.com
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Required Libraries

Next, install the required libraries using pip:

pip install tensorflow google-cloud-aiplatform google-cloud-run
Enter fullscreen mode Exit fullscreen mode

Step 3: Upload the Gemma 4 Model to AI Platform

Upload the pre-trained Gemma 4 model to AI Platform using the following code:

from google.cloud import aiplatform

aiplatform.init(project='my-project', location='us-central1')

model = aiplatform.Model.upload(
    display_name='gemma-4',
    artifact_uri='gs://my-bucket/gemma-4.tar.gz',
    serving_container_image_uri='gcr.io/my-project/gemma-4:latest'
)
Enter fullscreen mode Exit fullscreen mode

Step 4: Create a Cloud Run Job

Create a Cloud Run Job using the following code:

from google.cloud import run

job = run.Job(
    name='gemma-4-job',
    location='us-central1',
    container='gcr.io/my-project/gemma-4:latest',
    service_account='my-service-account',
    machine_type='n1-standard-8',
    accelerator_count=1,
    accelerator_type='NVIDIA_TESLA_V100'
)
Enter fullscreen mode Exit fullscreen mode

Step 5: Fine-Tune the Gemma 4 Model

Fine-tune the Gemma 4 model using the following code:

from tensorflow import keras
from tensorflow.keras import layers

model = keras.models.load_model('gs://my-bucket/gemma-4.tar.gz')

# Define the fine-tuning dataset
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
validation_dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val))

# Define the fine-tuning model
fine_tuning_model = keras.Model(inputs=model.inputs, outputs=layers.Dense(10, activation='softmax')(model.output))

# Compile the fine-tuning model
fine_tuning_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train the fine-tuning model
fine_tuning_model.fit(train_dataset, epochs=10, validation_data=validation_dataset)
Enter fullscreen mode Exit fullscreen mode

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

Deploy the fine-tuned model to Cloud Run using the following code:

from google.cloud import run

job = run.Job(
    name='gemma-4-job',
    location='us-central1',
    container='gcr.io/my-project/gemma-4:latest',
    service_account='my-service-account',
    machine_type='n1-standard-8',
    accelerator_count=1,
    accelerator_type='NVIDIA_TESLA_V100'
)

job.deploy()
Enter fullscreen mode Exit fullscreen mode

Step 7: Test the Fine-Tuned Model

Test the fine-tuned model using the following code:

from google.cloud import aiplatform

model = aiplatform.Model.get('my-model')

# Define the test dataset
test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test))

# Make predictions using the fine-tuned model
predictions = model.predict(test_dataset)
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article, we've explored how to fine-tune a pre-trained Gemma 4 model for pet breed classification using Cloud Run Jobs and NVIDIA RTX 6000 Pro GPUs. We've covered the steps to create a GCP project, enable Cloud Run Jobs, upload the Gemma 4 model to AI Platform, create a Cloud Run Job, fine-tune the model, deploy the fine-tuned model to Cloud Run, and test the fine-tuned model.

Example Use Cases

  • Pet breed classification
  • Image classification
  • Object detection
  • Segmentation

Future Work

  • Explore other pre-trained models for pet breed classification
  • Investigate the use of transfer learning for pet breed classification
  • Develop a web application for pet breed classification using the fine-tuned model

Code Repository

The code for this article can be found in the following repository:

https://github.com/my-repo/gemma-4-fine-tuning

Acknowledgments

This article was made possible by the following resources:

  • Google Cloud Platform
  • NVIDIA RTX 6000 Pro GPU
  • Gemma 4 model
  • TensorFlow 2.5+
  • Python 3.9+

License

This article is licensed under the MIT License.

Disclaimer

This article is for educational purposes only and should not be used for commercial purposes without permission from the authors.


Factual

Top comments (0)