Inference With Keras
Copyright 2024 Google LLC.
Generate PaliGemma output with Keras
PaliGemma models have multimodal capabilities, allowing you to generate output using both text and image input data. You can use image data with these models to provide additional context for your requests, or use the model to analyze the content of images. This tutorial shows you how to use PaliGemma with Keras to can analyze images and answer questions about them.
What's in this notebook
This notebook uses PaliGemma with Keras and shows you how to:
- Install Keras and the required dependencies
- Download
PaliGemmaCausalLM, a pre-trained PaliGemma variant for causal visual language modeling, and use it to create a model - Test the model's ability to infer information about supplied images
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 is helpful when reading through the example code.
Setup
The following sections explain the preliminary steps for getting a notebook to use a PaliGemma model, including model access, getting an API key, and configuring the notebook runtime.
Get access to PaliGemma
Before using PaliGemma for the first time, you must request access to the model through Kaggle by completing the following steps:
- Log in to Kaggle, or create a new Kaggle account if you don't already have one.
- Go to the PaliGemma model card and click Request Access.
- Complete the consent form and accept the terms and conditions.
Configure your API key
To use PaliGemma, you must provide your Kaggle username and a Kaggle API key.
To generate a Kaggle API key, open your Settings page in Kaggle and click Create New Token. This triggers the download of a kaggle.json file containing your API credentials.
Then, in Colab, select Secrets (π) in the left pane and add your Kaggle username and Kaggle API key. Store your username under the name KAGGLE_USERNAME and your API key under the name KAGGLE_KEY.
Select the runtime
To complete this tutorial, you'll need to have a Colab runtime with sufficient resources to run the PaliGemma model. In this case, you can use a T4 GPU:
- In the upper-right of the Colab window, click the βΎ (Additional connection options) dropdown menu.
- Select Change runtime type.
- Under Hardware accelerator, select T4 GPU.
Set environment variables
Set the environment variables for KAGGLE_USERNAME, KAGGLE_KEY, and KERAS_BACKEND.
Install Keras
Run the below cell to install Keras.
ββββββββββββββββββββββββββββββββββββββββ 792.1/792.1 kB 9.2 MB/s eta 0:00:00
Import dependencies and configure Keras
Install the dependencies needed for this notebook and configure Keras' backend. You'll also set Keras to use bfloat16 so that the framework uses less memory.
Load the model
Now that you've set everything up, you can download the pre-trained model and create some utility methods to help your model generate its responses.
In this step, you download a model using PaliGemmaCausalLM from Keras Hub. This class helps you manage and run the causal visual language model structure of PaliGemma. A causal visual language model predicts the next token based on previous tokens. Keras Hub provides implementations of many popular model architectures.
Create the model using the from_preset method and print its summary. This process will take about a minute to complete.
Create utility methods
To help you generate responses from your model, create two utility methods:
crop_and_resize: Helper method forread_img. This method crops and resizes the image to the passed in size so that the final image is resized without skewing the proportions of the image.read_img: Helper method forread_img_from_url. This method is what actually opens the image, resizes it so that it fits in the model's constraints, and puts it into an array that can be interpreted by the model.read_img_from_url: Takes in an image via a valid URL. You need this method to pass the image to the model.
You'll use read_img_from_url in the next step of this notebook.
Generate output
After loading the model and creating utility methods, you can prompt the model with image and text data to generate a responses. PaliGemma models are trained with specific prompt syntax for specific tasks, such as answer, caption, and detect. For more information about PaliGemma prompt task syntax, see PaliGemma prompt and system instructions.
Prepare an image for use in a generation prompt by using the following code to load a test image into an object:
Answer in a specific language
The following example code shows how to prompt the PaliGemma model for information about an object appearing in a provided image. This example uses the answer {lang} syntax and shows additional questions in other languages:
Note: Prompts that use PaliGemma prompt command syntax must end with an "\n" character.
Use detect prompt
The following example code uses the detect prompt syntax to locate an object in the provided image. The code uses the previously defined parse_bbox_and_labels() and display_boxes() functions to interpret the model output and display the generated bounding boxes.
Use segment prompt
The following example code uses the segment prompt syntax to locate the area of an image occupied by an object. It uses the Google big_vision library to interpret the model output and generate a mask for the segmented object.
Before getting started, install the big_vision library and its dependencies, as shown in this code example:
For this segmentation example, load and prepare a different image that includes a cat.
Here is a function to help parse the segment output from PaliGemma
Query PaliGemma to segment the cat in the image
Visualize the generated mask from PaliGemma
Batch prompts
You can provide more than one prompt command within a single prompt as a batch of instructions. The following example demonstrates how to structure your prompt text to provide multiple instructions.
Important: Each prompt command must end with an "\n" character, as shown.
Run in Google Colab
View source on GitHub