BLIP2Seq2SeqLM classkeras_hub.models.BLIP2Seq2SeqLM(backbone, preprocessor=None, **kwargs)
An end-to-end multimodal BLIP-2 model for seq2seq language modeling.
This is the encoder-decoder (Flan-T5) BLIP-2 task. A seq2seq language model is conditioned on an input "context" — here the encoder text prompt plus a Q-Former visual soft-prompt distilled from the image — and the decoder autoregressively predicts the output text (e.g. a caption or VQA answer).
The forward pass runs the frozen vision encoder and the Q-Former once to
obtain visual query features, projects and prepends them to the T5 encoder
sequence, and decodes the answer. Because the underlying T5 stack does not
support a key/value cache, generation recomputes the decoder at each step;
the vision encoder and Q-Former are only run once per generate() call.
This model has a generate() method, which generates text based on the
image and an optional encoder/decoder prompt. The generation strategy used
is controlled by an additional sampler argument on compile(). By
default, "greedy" sampling will be used.
This model can optionally be configured with a preprocessor layer, in
which case it will automatically apply preprocessing to raw inputs during
fit(), predict(), evaluate() and generate(). This is done by default
when creating the model with from_preset().
Arguments
keras_hub.models.BLIP2Backbone instance whose
language_model is a keras_hub.models.BLIP2FlanT5.keras_hub.models.BLIP2Seq2SeqLMPreprocessor or
None. If None, this model will not apply preprocessing, and
inputs should be preprocessed before calling the model. Defaults
to None.from_preset methodBLIP2Seq2SeqLM.from_preset(preset, load_weights=True, **kwargs)
Instantiate a keras_hub.models.Task 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 Task subclass, you can run cls.presets.keys() to list all
built-in presets available on the class.
This constructor can be called in one of two ways. Either from a task
specific base class like keras_hub.models.CausalLM.from_preset(), or
from a model class like
keras_hub.models.BertTextClassifier.from_preset().
If calling from the a base class, the subclass of the returning object
will be inferred from the config in the preset directory.
Arguments
True, saved weights will be loaded into
the model architecture. If False, all weights will be
randomly initialized.Examples
# Load a Gemma generative task.
causal_lm = keras_hub.models.CausalLM.from_preset(
"gemma_2b_en",
)
# Load a Bert classification task.
model = keras_hub.models.TextClassifier.from_preset(
"bert_base_en",
num_classes=2,
)
| 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. |
generate methodBLIP2Seq2SeqLM.generate(
inputs, max_length=None, stop_token_ids="auto", strip_prompt=False
)
Generate text given prompt inputs.
This method generates text based on given inputs. The sampling method
used for generation can be set via the compile() method.
If inputs are a tf.data.Dataset, outputs will be generated
"batch-by-batch" and concatenated. Otherwise, all inputs will be handled
as a single batch.
If a preprocessor is attached to the model, inputs will be
preprocessed inside the generate() function and should match the
structure expected by the preprocessor layer (usually raw strings).
If a preprocessor is not attached, inputs should match the structure
expected by the backbone. See the example usage above for a
demonstration of each.
Arguments
tf.data.Dataset. If a
preprocessor is attached to the model, inputs should match
the structure expected by the preprocessor layer. If a
preprocessor is not attached, inputs should match the
structure expected the backbone model.sequence_length of the
preprocessor. If preprocessor is None, inputs should be
should be padded to the desired maximum length and this argument
will be ignored.None, "auto", or tuple of token ids.
Defaults to "auto" which uses the
preprocessor.tokenizer.end_token_id. Not specifying a
processor will produce an error. None stops generation after
generating max_length tokens. You may also specify a list of
token id's the model should stop on. Note that sequences of
tokens will each be interpreted as a stop token, multi-token
stop sequences are not supported.backbone propertykeras_hub.models.BLIP2Seq2SeqLM.backbone
A keras_hub.models.Backbone model with the core architecture.
preprocessor propertykeras_hub.models.BLIP2Seq2SeqLM.preprocessor
A keras_hub.models.Preprocessor layer used to preprocess input.