[PaliGemma 2]Finetune With Keras
Copyright 2024 Google LLC.
Fine-tune PaliGemma 2 with Keras
This notebook shows how to fine-tune PaliGemma 2 on a vision-language task with Keras. Fine-tuning is a process that can improve your model's performance on specific tasks or help the model adhere to specific output requirements when instructions aren't sufficient and you have a set of examples that demonstrate the outputs you want. Gemma-based models like PaliGemma require fine-tuning to produce expected results.
Before you begin
Before going through this notebook, you should be familiar with Python code, as well as how large language models (LLMs) are trained. You don't need to be familiar with Keras, but basic knowledge about Keras (or similar technologies) is helpful when reading through the example code.e.
Install KerasHub
Collecting einops Downloading einops-0.8.0-py3-none-any.whl.metadata (12 kB) Downloading einops-0.8.0-py3-none-any.whl (43 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.2/43.2 kB 1.9 MB/s eta 0:00:00 Installing collected packages: einops Successfully installed einops-0.8.0
Download the training and validation data
/tmp/ipykernel_23/4203520384.py:15: DeprecationWarning: Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display from IPython.core.display import display, HTML
View training examples
In this notebook, the training data contains 90 images that are paired with long descriptions of what's depicted in the image.
Note: Normal training data sets that are meant to be used for practical use cases should contain more images, but this notebook limits the number of data points so that you can train the model in a reasonable amount of time for an example.
The code below prints a random selection of images with their descriptions from the training data set so that you can see what the images and descriptions your model is trained on looks like. Each image is displayed in as a 128x128 pixel JPEG, with the description printed next to the image to the right.
Training examples
Load Model
normalizer.cc(51) LOG(INFO) precompiled_charsmap is empty. use identity normalization.
Inference before fine tuning
Inference Result
LoRA Fine-tuning
Fine-tune the model
Epoch 1/4 90/90 ━━━━━━━━━━━━━━━━━━━━ 117s 877ms/step - loss: 1.6715 - sparse_categorical_accuracy: 0.5523 Epoch 2/4 90/90 ━━━━━━━━━━━━━━━━━━━━ 80s 567ms/step - loss: 0.9483 - sparse_categorical_accuracy: 0.6871 Epoch 3/4 90/90 ━━━━━━━━━━━━━━━━━━━━ 52s 555ms/step - loss: 0.7245 - sparse_categorical_accuracy: 0.7587 Epoch 4/4 90/90 ━━━━━━━━━━━━━━━━━━━━ 52s 555ms/step - loss: 0.5710 - sparse_categorical_accuracy: 0.8049
Output
The validation data for this notebook consists of just 10 images. In normal code, you would likely have many more data points for validation, but for this notebook, run the following code to generate descriptions for all 10 images. After tuning the model, these descriptions should be very similar in form and content coverage to the descriptions included with the training data that you looked at earlier in this notebook.
Run the below code to generate descriptions for the validation data set.
Inference Result
Run in Google Colab
View source on GitHub