As AI continues to bridge the gap between vision and language, Google’s PaliGemma-2 stands out as a powerful model for understanding and generating text from images. This vision-language model (VLM) is designed for a wide range of applications, from image captioning and optical character recognition (OCR) to visual question answering and object detection. It is built upon the strengths of Gemma 2 and SigLIP vision models. It processes both images and text inputs, making it an essential tool for researchers and developers working on multimodal AI applications. If you’re looking to enhance accessibility solutions, automate content generation, or build intelligent assistants, PaliGemma-2 offers state-of-the-art performance with fine-tuned precision across multiple tasks.
In this guide, we’ll walk you through the step-by-step installation process, helping you set up PaliGemma-2 on Jupyter Notebook so you can seamlessly test and integrate it into your AI-driven workflows.
Prerequisites
The minimum system requirements for this use case are:
- GPUs: RTX 4090 or RTX A6000 (for smooth execution).
- Disk Space: 100 GB
- RAM: At least 16 GB.
- Jupyter Notebook installed.
Note: The prerequisites for this are highly variable across use cases. A high-end configuration could be used for a large-scale deployment.
Step-by-step process to install & run Google’s PaliGemma-2 mix
For the purpose of this tutorial, we’ll use a GPU-powered Virtual Machine by NodeShift since it provides high compute Virtual Machines at a very affordable cost on a scale that meets GDPR, SOC2, and ISO27001 requirements. Also, it offers an intuitive and user-friendly interface, making it easier for beginners to get started with Cloud deployments. However, feel free to use any cloud provider of your choice and follow the same steps for the rest of the tutorial.
Step 1: Setting up a NodeShift Account
Visit app.nodeshift.com and create an account by filling in basic details, or continue signing up with your Google/GitHub account.
If you already have an account, login straight to your dashboard.
Step 2: Create a GPU Node
After accessing your account, you should see a dashboard (see image), now:
- Navigate to the menu on the left side.
- Click on the GPU Nodes option.
- Click on Start to start creating your very first GPU node.
These GPU nodes are GPU-powered virtual machines by NodeShift. These nodes are highly customizable and let you control different environmental configurations for GPUs ranging from H100s to A100s, CPUs, RAM, and storage, according to your needs.
Step 3: Selecting configuration for GPU (model, region, storage)
- For this tutorial, we’ll be using the RTX 4090 GPU; however, you can choose any GPU of your choice based on your needs.
- Similarly, we’ll opt for 200GB storage by sliding the bar. You can also select the region where you want your GPU to reside from the available ones.
Step 4: Choose GPU Configuration and Authentication method
- After selecting your required configuration options, you’ll see the available VMs in your region and according to (or very close to) your configuration. In our case, we’ll choose a 1x RTX 4090 GPU node with 12 vCPUs/96GB RAM/200 GB SSD.
2. Next, you’ll need to select an authentication method. Two methods are available: Password and SSH Key. We recommend using SSH keys, as they are a more secure option. To create one, head over to our official documentation.
Step 5: Choose an Image
The final step would be to choose an image for the VM, which in our case is Jupyter Notebook, where we’ll deploy and run the inference of our model.
That’s it! You are now ready to deploy the node. Finalize the configuration summary, and if it looks good, click Create to deploy the node.
Step 6: Connect to active Compute Node using SSH
- As soon as you create the node, it will be deployed in a few seconds or a minute. Once deployed, you will see a status Running in green, meaning that our Compute node is ready to use!
- Once your GPU shows this status, navigate to the three dots on the right and click on Connect with SSH. This will open a new tab with a Jupyter Notebook session in which we can run our model.
Step 7: Setting up Python Notebook
Start by creating a .ipynb notebook by clicking on Python 3 (ipykernel).
Next, If you want to check the GPU details, run the following command in the Jupyter Notebook cell:
!nvidia-smi
Output:
Step 8: Download model files with dependencies.
Since PaliGemma-2 is a Gated model, ensure you’ve been granted access to the model before moving to installation.
- Install Python dependencies to run the model.
!pip install torch torchvision torchaudio einops timm pillow
!pip install git+https://github.com/huggingface/transformers
!pip install git+https://github.com/huggingface/accelerate
!pip install git+https://github.com/huggingface/diffusers
!pip install huggingface_hub
!pip install sentencepiece bitsandbytes protobuf decord numpy
Output:
2. Login to Hugging Face CLI.
(replace <HF_TOKEN>
with your access token from the Hugging Face dashboard)
!huggingface-cli login --token <HF_TOKEN> --add-to-git-credential
Output:
3. Download model files.
from PIL import Image
from transformers import (
PaliGemmaProcessor,
PaliGemmaForConditionalGeneration,
)
from transformers.image_utils import load_image
import torch
model_id = "google/paligemma2-10b-mix-448"
model = PaliGemmaForConditionalGeneration.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto").eval()
processor = PaliGemmaProcessor.from_pretrained(model_id)
Output:
Step 9: Run and test the model
- Test the description capabilities of the model with the following prompt.
In the given code snippet, the value of the prompt is “describe en
” which is a pre-built prompt template to tell the model to describe the image in the English language.
image = Image.open('./test_image.jpg').convert('RGB')
prompt = "describe en"
model_inputs = processor(text=prompt, images=image, return_tensors="pt").to(torch.bfloat16).to(model.device)
input_len = model_inputs["input_ids"].shape[-1]
with torch.inference_mode():
generation = model.generate(**model_inputs, max_new_tokens=100, do_sample=False)
generation = generation[0][input_len:]
decoded = processor.decode(generation, skip_special_tokens=True)
print(decoded)
Here’s the test image we have used in the above code:
Below is the output generated by the PaliGemma model for this image:
Output:
2. In the second test, we’ll ask it to find and detect any text or numeric in the same image using the “ocr
” prompt template. To do this, just replace the prompt value with “ocr en
“.
Output:
The model detects the number 097 from the image, which can be seen as a vehicle number in the image:
Conclusion
With Google’s PaliGemma-2, developers can build advanced vision-language AI applications, from image captioning and OCR to object detection and multilingual question answering. By leveraging fine-tuned models and diverse pre-trained datasets, PaliGemma-2 delivers high accuracy across various tasks, making it a valuable tool for AI-driven automation and research. However, running such a large-scale model efficiently requires robust infrastructure, and this is where NodeShift’s cloud platform plays a crucial role. With optimized compute resources and seamless deployment capabilities, NodeShift ensures that PaliGemma-2 operates smoothly in production environments, allowing developers to scale their applications effortlessly.