Notebooks
G
Google Gemini
[PaliGemma 1]Finetune With Image Captioning

[PaliGemma 1]Finetune With Image Captioning

recurrentgemmagemmapaligemmagemma-cookbookcodegemma
Copyright 2024 Google LLC.
[1]

Generating Scientific Figure Captions with Fine-Tuned PaliGemma

This notebook explores the use of fine-tuned PaliGemma models for generating captions for scientific figures. PaliGemma-FT is research-oriented models that are fine-tuned on specific research datasets. We'll focus on the PaliGemma-3b-ft-scicap-448 model, specifically trained on the SciCap dataset for this task.

The notebook will demonstrate how to utilize this pre-trained model for caption generation, highlighting its potential advantages due to:

  • Higher resolution input (448x448): Capturing finer details in scientific figures.
  • SciCap dataset fine-tuning: Focused training on scientific figure captions.

Setup

Select the Colab runtime

To complete this tutorial, you'll need to have a Colab runtime with sufficient resources to run the Gemma model. In this case, you should use a L4 GPU or an A100 GPU, as a T4 will be insufficient:

  1. In the upper-right of the Colab window, select ▾ (Additional connection options).
  2. Select Change runtime type.
  3. Under Hardware accelerator, select L4 GPU.

Install Accelerate and restart kernel

Before you start, you will need to install the appropriate packages and setup your environment.

[2]
Collecting accelerate
  Downloading accelerate-0.31.0-py3-none-any.whl (309 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 309.4/309.4 kB 4.7 MB/s eta 0:00:00
Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from accelerate) (1.25.2)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from accelerate) (24.1)
Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from accelerate) (5.9.5)
Requirement already satisfied: pyyaml in /usr/local/lib/python3.10/dist-packages (from accelerate) (6.0.1)
Requirement already satisfied: torch>=1.10.0 in /usr/local/lib/python3.10/dist-packages (from accelerate) (2.3.0+cu121)
Requirement already satisfied: huggingface-hub in /usr/local/lib/python3.10/dist-packages (from accelerate) (0.23.3)
Requirement already satisfied: safetensors>=0.3.1 in /usr/local/lib/python3.10/dist-packages (from accelerate) (0.4.3)
Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.14.0)
Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (4.12.2)
Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (1.12.1)
Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.3)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (3.1.4)
Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (2023.6.0)
Collecting nvidia-cuda-nvrtc-cu12==12.1.105 (from torch>=1.10.0->accelerate)
  Using cached nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (23.7 MB)
Collecting nvidia-cuda-runtime-cu12==12.1.105 (from torch>=1.10.0->accelerate)
  Using cached nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (823 kB)
Collecting nvidia-cuda-cupti-cu12==12.1.105 (from torch>=1.10.0->accelerate)
  Using cached nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (14.1 MB)
Collecting nvidia-cudnn-cu12==8.9.2.26 (from torch>=1.10.0->accelerate)
  Using cached nvidia_cudnn_cu12-8.9.2.26-py3-none-manylinux1_x86_64.whl (731.7 MB)
Collecting nvidia-cublas-cu12==12.1.3.1 (from torch>=1.10.0->accelerate)
  Using cached nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl (410.6 MB)
Collecting nvidia-cufft-cu12==11.0.2.54 (from torch>=1.10.0->accelerate)
  Using cached nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl (121.6 MB)
Collecting nvidia-curand-cu12==10.3.2.106 (from torch>=1.10.0->accelerate)
  Using cached nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl (56.5 MB)
Collecting nvidia-cusolver-cu12==11.4.5.107 (from torch>=1.10.0->accelerate)
  Using cached nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl (124.2 MB)
Collecting nvidia-cusparse-cu12==12.1.0.106 (from torch>=1.10.0->accelerate)
  Using cached nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl (196.0 MB)
Collecting nvidia-nccl-cu12==2.20.5 (from torch>=1.10.0->accelerate)
  Using cached nvidia_nccl_cu12-2.20.5-py3-none-manylinux2014_x86_64.whl (176.2 MB)
Collecting nvidia-nvtx-cu12==12.1.105 (from torch>=1.10.0->accelerate)
  Using cached nvidia_nvtx_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (99 kB)
Requirement already satisfied: triton==2.3.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate) (2.3.0)
Collecting nvidia-nvjitlink-cu12 (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.10.0->accelerate)
  Downloading nvidia_nvjitlink_cu12-12.5.40-py3-none-manylinux2014_x86_64.whl (21.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 21.3/21.3 MB 47.3 MB/s eta 0:00:00
Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub->accelerate) (2.31.0)
Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub->accelerate) (4.66.4)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch>=1.10.0->accelerate) (2.1.5)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub->accelerate) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub->accelerate) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub->accelerate) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub->accelerate) (2024.6.2)
Requirement already satisfied: mpmath<1.4.0,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy->torch>=1.10.0->accelerate) (1.3.0)
Installing collected packages: nvidia-nvtx-cu12, nvidia-nvjitlink-cu12, nvidia-nccl-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, nvidia-cusparse-cu12, nvidia-cudnn-cu12, nvidia-cusolver-cu12, accelerate
Successfully installed accelerate-0.31.0 nvidia-cublas-cu12-12.1.3.1 nvidia-cuda-cupti-cu12-12.1.105 nvidia-cuda-nvrtc-cu12-12.1.105 nvidia-cuda-runtime-cu12-12.1.105 nvidia-cudnn-cu12-8.9.2.26 nvidia-cufft-cu12-11.0.2.54 nvidia-curand-cu12-10.3.2.106 nvidia-cusolver-cu12-11.4.5.107 nvidia-cusparse-cu12-12.1.0.106 nvidia-nccl-cu12-2.20.5 nvidia-nvjitlink-cu12-12.5.40 nvidia-nvtx-cu12-12.1.105

Restart the kernel after installing packages:

[3]
{'status': 'ok', 'restart': True}

How to Access PaliGemma:

Before using PaliGemma for the first time, you'll need to request access to the model through Hugging Face. This ensures you've accepted the model's terms-of-use.

As we'll be downloading the PaliGemma model weights from the Hugging Face Hub, you'll need a Hugging Face token to verify your acceptance.

If you don't already have a Hugging Face account, you can register for one at Hugging Face. Once you have an account, follow these steps:

  1. Go to the Hugging Face Gemma Model Card (as PaliGemma is a Gemma variant) and select Request Access.
  2. Complete the consent form and accept the terms and conditions.
  3. Go to Hugging Face Hub account settings and create a new access token.
  4. Copy your token.
  5. In Colab, select Secrets (🔑) and add your Hugging Face Access Token. Store your token under desired name 'HF_TOKEN' and your token under the name value.
[1]

Import Libraries

[2]

Configuration

In your environment set cuda to lower RAM requirements

[3]

Image: Comparison of Common Loss Functions

We will use the figure comparing common loss functions as an example to generate captions for scientific figures of this nature. Source: Wikipedia.

We'll demonstrate how these models perform on the same scientific figure using various prompts:

  • caption en: Generate a general caption in English.
  • caption in detail: Provide a more detailed description of the figure.
  • caption the figure: Create a concise caption summarizing the figure's content.
[10]
Output

Loads a PaliGemma model and its corresponding tokenizer.

[11]

Generates Captions

[12]

Inference with the Base Model (PaliGemma-3b-pt-224)

Before we use the fine-tuned models, let's see how the PaliGemma base model responds to the prepared prompts.

Since this model hasn't been fine-tuned specifically for captioning scientific figures, you can expect its responses to be less accurate.

Here's a quick description of the base model:

  • Transformers PaliGemma 3B weights.
  • Pre-trained with 224x224 input images and 128 token input/output text sequences.
  • Trained on a mixture of datasets.
  • Available in float32, bfloat16, and float16 formats for fine-tuning.
[13]
config.json:   0%|          | 0.00/1.03k [00:00<?, ?B/s]
model.safetensors.index.json:   0%|          | 0.00/62.6k [00:00<?, ?B/s]
Downloading shards:   0%|          | 0/2 [00:00<?, ?it/s]
model-00001-of-00002.safetensors:   0%|          | 0.00/4.99G [00:00<?, ?B/s]
model-00002-of-00002.safetensors:   0%|          | 0.00/862M [00:00<?, ?B/s]
`config.hidden_act` is ignored, you should use `config.hidden_activation` instead.
Gemma's activation function will be set to `gelu_pytorch_tanh`. Please, use
`config.hidden_activation` if you want to override this behaviour.
See https://github.com/huggingface/transformers/pull/29402 for more details.
Loading checkpoint shards:   0%|          | 0/2 [00:00<?, ?it/s]
generation_config.json:   0%|          | 0.00/137 [00:00<?, ?B/s]
preprocessor_config.json:   0%|          | 0.00/699 [00:00<?, ?B/s]
tokenizer_config.json:   0%|          | 0.00/40.0k [00:00<?, ?B/s]
tokenizer.model:   0%|          | 0.00/4.26M [00:00<?, ?B/s]
tokenizer.json:   0%|          | 0.00/17.5M [00:00<?, ?B/s]
added_tokens.json:   0%|          | 0.00/24.0 [00:00<?, ?B/s]
special_tokens_map.json:   0%|          | 0.00/607 [00:00<?, ?B/s]
Question: caption en

Answer: a plot of a comparison of methods .

Question: caption in detail

Answer: graf von figure in the surrounding text

Question: caption the figure

Answer: figure est un diagramme de la figure

Output

Inference with the Fine-Tuned Model (PaliGemma-3b-ft-scicap-224)

We previously explored the base model's performance. Now, let's see how the fine-tuned model responds to the same prompts.

This model (PaliGemma-3b-ft-scicap-224) has been specifically fine-tuned on scientific figures using the SciCap dataset. As a result, you can expect more accurate and relevant captions compared to the base model (PaliGemma-3b-pt-224).

Here's a quick overview of the fine-tuned model:

  • Transformers PaliGemma 3B weights: This is the starting point for the fine-tuned model.
  • Fine-tuned with 224x224 input images: The model is fine-tuned to process images of this size.
  • SciCap dataset: This dataset specifically focuses on scientific figures and their captions.

SciCap: Generating Captions for Scientific Figures:

You can find more details about the SciCap dataset in the following paper.

Paper Summary: This paper presents the SciCap dataset, a large collection of scientific figures and their corresponding captions. The authors discuss the challenges of generating captions for scientific figures and the potential benefits of such models.

[14]
config.json:   0%|          | 0.00/1.03k [00:00<?, ?B/s]
model.safetensors.index.json:   0%|          | 0.00/62.6k [00:00<?, ?B/s]
Downloading shards:   0%|          | 0/2 [00:00<?, ?it/s]
model-00001-of-00002.safetensors:   0%|          | 0.00/4.99G [00:00<?, ?B/s]
model-00002-of-00002.safetensors:   0%|          | 0.00/862M [00:00<?, ?B/s]
Loading checkpoint shards:   0%|          | 0/2 [00:00<?, ?it/s]
generation_config.json:   0%|          | 0.00/137 [00:00<?, ?B/s]
preprocessor_config.json:   0%|          | 0.00/699 [00:00<?, ?B/s]
tokenizer_config.json:   0%|          | 0.00/40.0k [00:00<?, ?B/s]
tokenizer.model:   0%|          | 0.00/4.26M [00:00<?, ?B/s]
tokenizer.json:   0%|          | 0.00/17.5M [00:00<?, ?B/s]
added_tokens.json:   0%|          | 0.00/24.0 [00:00<?, ?B/s]
special_tokens_map.json:   0%|          | 0.00/607 [00:00<?, ?B/s]
Question: caption en

Answer: different values of loss function based on error .

Question: caption in detail

Answer: comparison of four different loss functions with similar nonlinearities , in terms of error .

Question: caption the figure

Answer: shows the comparison of loss functions . the red line shows the relu loss , the black line shows the hinge loss , the blue line shows the quadratic loss and the green line shows the savage loss .

Output

Inference with the Fine-Tuned Model (PaliGemma-3b-ft-scicap-448)

Building on the success of the previous model (PaliGemma-3b-ft-scicap-224), let's explore the capabilities of PaliGemma-3b-ft-scicap-448. This model offers potentially a better captions due to two key improvements:

  • Higher Resolution Input (448x448): The model is fine-tuned to process images with a higher resolution, potentially capturing finer details in scientific figures.
  • Leveraging SciCap Dataset: Similar to the previous model, it's fine-tuned on the SciCap dataset, specifically designed for scientific figure captioning.

While the model is not fine-tunable, you can still benefit from its pre-trained capabilities for research purposes. The models are available in float32, bfloat16, and float16 formats.

Fine-Tune Configuration:

  • For more details on the fine-tuning configuration, you can refer to the big_vision.
[15]
config.json:   0%|          | 0.00/1.09k [00:00<?, ?B/s]
model.safetensors.index.json:   0%|          | 0.00/62.6k [00:00<?, ?B/s]
Downloading shards:   0%|          | 0/2 [00:00<?, ?it/s]
model-00001-of-00002.safetensors:   0%|          | 0.00/4.99G [00:00<?, ?B/s]
model-00002-of-00002.safetensors:   0%|          | 0.00/862M [00:00<?, ?B/s]
Loading checkpoint shards:   0%|          | 0/2 [00:00<?, ?it/s]
generation_config.json:   0%|          | 0.00/137 [00:00<?, ?B/s]
preprocessor_config.json:   0%|          | 0.00/700 [00:00<?, ?B/s]
tokenizer_config.json:   0%|          | 0.00/40.0k [00:00<?, ?B/s]
tokenizer.model:   0%|          | 0.00/4.26M [00:00<?, ?B/s]
tokenizer.json:   0%|          | 0.00/17.5M [00:00<?, ?B/s]
added_tokens.json:   0%|          | 0.00/24.0 [00:00<?, ?B/s]
special_tokens_map.json:   0%|          | 0.00/607 [00:00<?, ?B/s]
Question: caption en

Answer: different loss functions . mae , huber , logistic loss , squared error ( sme ) .

Question: caption in detail

Answer: shows the difference between standard mae , huber , logistic loss functions and smae . the horizontal axis shows the error and the vertical shows the difference .

Question: caption the figure

Answer: shows the different loss function . the loss function of mae clearly outperforms the other loss functions .

Output

Reference

  • PaliGemma-FT: research-oriented models that are fine-tuned on specific research datasets.
  • SciCap: generating captions for scientific figures.
  • Loss_function: figure source from Wikipedia.