BLIP2Seq2SeqLMPreprocessor classkeras_hub.models.BLIP2Seq2SeqLMPreprocessor(
tokenizer,
image_converter=None,
encoder_sequence_length=512,
decoder_sequence_length=512,
**kwargs
)
Multimodal seq2seq preprocessor for the BLIP-2 Flan-T5 variant.
This preprocessing layer is meant for use with
keras_hub.models.BLIP2Seq2SeqLM, the encoder-decoder (Flan-T5) BLIP-2
task. Following the keras-hub seq2seq convention it takes a dictionary with
"encoder_text" and (optionally) "decoder_text" keys, and additionally
accepts an "images" key when an image_converter is configured.
The encoder text is fed to the T5 encoder (alongside the Q-Former visual
soft-prompt produced by the backbone), and the decoder text is the
teacher-forced target during training, or the decoder prompt during
generation. It returns outputs in the standard (x, y, sample_weight)
format, where y is the decoder sequence shifted one step left.
For use with generation, the layer also exposes generate_preprocess()
and generate_postprocess(), which are called implicitly by
keras_hub.models.BLIP2Seq2SeqLM.generate().
Arguments
keras_hub.models.BLIP2FlanT5Tokenizer instance.keras_hub.models.BLIP2ImageConverter instance, or
None. If None, the preprocessor operates in text-only mode.512.512.Examples
preprocessor = keras_hub.models.BLIP2Seq2SeqLMPreprocessor.from_preset(
"blip2_flan_t5_xl"
)
# Image + text input.
preprocessor({
"images": np.random.randint(0, 256, (2, 224, 224, 3)),
"encoder_text": ["a photo of a cat", "a photo of a dog"],
"decoder_text": ["a cat sitting", "a running dog"],
})
# Prepare an image and prompt for generation.
preprocessor.generate_preprocess({
"images": np.random.randint(0, 256, (224, 224, 3)),
"encoder_text": "a photo of",
})
References
from_preset methodBLIP2Seq2SeqLMPreprocessor.from_preset(
preset, config_file="preprocessor.json", **kwargs
)
Instantiate a keras_hub.models.Preprocessor from a model preset.
A preset is a directory of configs, weights and other file assets used
to save and load a pre-trained model. The preset can be passed as
one of:
'bert_base_en''kaggle://user/bert/keras/bert_base_en''hf://user/bert_base_en''./bert_base_en'For any Preprocessor subclass, you can run cls.presets.keys() to
list all built-in presets available on the class.
As there are usually multiple preprocessing classes for a given model,
this method should be called on a specific subclass like
keras_hub.models.BertTextClassifierPreprocessor.from_preset().
Arguments
Examples
# Load a preprocessor for Gemma generation.
preprocessor = keras_hub.models.CausalLMPreprocessor.from_preset(
"gemma_2b_en",
)
# Load a preprocessor for Bert classification.
preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
"bert_base_en",
)
| Preset | Parameters | Description |
|---|---|---|
| blip2_opt_2.7b | 3.74B | BLIP-2 model using OPT-2.7B as the frozen language model. |
| blip2_flan_t5_xl | 3.94B | BLIP-2 model using Flan-T5-XL (~3B) as the frozen language model. |
| blip2_opt_6.7b | 7.75B | BLIP-2 model using OPT-6.7B as the frozen language model. |
| blip2_flan_t5_xxl | 12.23B | BLIP-2 model using Flan-T5-XXL (~11B) as the frozen language model. |
image_converter propertykeras_hub.models.BLIP2Seq2SeqLMPreprocessor.image_converter
The image converter used to preprocess image data.
tokenizer propertykeras_hub.models.BLIP2Seq2SeqLMPreprocessor.tokenizer
The tokenizer used to tokenize strings.