sparknlp.annotator.cv.blip_for_question_answering#

Module Contents#

Classes#

BLIPForQuestionAnswering

BLIPForQuestionAnswering can load BLIP models for visual question answering.

class BLIPForQuestionAnswering(classname='com.johnsnowlabs.nlp.annotators.cv.BLIPForQuestionAnswering', java_model=None)[source]#

BLIPForQuestionAnswering can load BLIP models for visual question answering. The model consists of a vision encoder, a text encoder as well as a text decoder. The vision encoder will encode the input image, the text encoder will encode the input question together with the encoding of the image, and the text decoder will output the answer to the question.

Pretrained models can be loaded with pretrained() of the companion object:

>>> visualQAClassifier = BLIPForQuestionAnswering.pretrained() \
...     .setInputCols(["image_assembler"]) \
...     .setOutputCol("answer")

The default model is "blip_vqa_base", if no name is provided.

For available pretrained models please see the Models Hub.

To see which models are compatible and how to import them see Import Transformers into Spark NLP 🚀.

Input Annotation types

Output Annotation type

IMAGE

DOCUMENT

Parameters:
batchSize

Batch size. Large values allows faster processing but requires more memory, by default 2

configProtoBytes

ConfigProto from tensorflow, serialized into byte array.

maxSentenceLength

Max sentence length to process, by default 50

Examples

>>> import sparknlp
>>> from sparknlp.base import *
>>> from sparknlp.annotator import *
>>> from pyspark.ml import Pipeline
>>> image_df = SparkSessionForTest.spark.read.format("image").load(path=images_path)
>>> test_df = image_df.withColumn("text", lit("What's this picture about?"))
>>> imageAssembler = ImageAssembler() \
...     .setInputCol("image") \
...     .setOutputCol("image_assembler")
>>> visualQAClassifier = BLIPForQuestionAnswering.pretrained() \
...     .setInputCols("image_assembler") \
...     .setOutputCol("answer") \
...     .setSize(384)
>>> pipeline = Pipeline().setStages([
...     imageAssembler,
...     visualQAClassifier
... ])
>>> result = pipeline.fit(test_df).transform(test_df)
>>> result.select("image_assembler.origin", "answer.result").show(false)
+--------------------------------------+------+
|origin                                |result|
+--------------------------------------+------+
|[file:///content/images/cat_image.jpg]|[cats]|
+--------------------------------------+------+
setMaxSentenceSize(value)[source]#

Sets Maximum sentence length that the annotator will process, by default 50.

Parameters:
valueint

Maximum sentence length that the annotator will process

static loadSavedModel(folder, spark_session)[source]#

Loads a locally saved model.

Parameters:
folderstr

Folder of the saved model

spark_sessionpyspark.sql.SparkSession

The current SparkSession

Returns:
CLIPForZeroShotClassification

The restored model

static pretrained(name='blip_vqa_base', lang='en', remote_loc=None)[source]#

Downloads and loads a pretrained model.

Parameters:
namestr, optional

Name of the pretrained model, by default “blip_vqa_tf”

langstr, optional

Language of the pretrained model, by default “en”

remote_locstr, optional

Optional remote address of the resource, by default None. Will use Spark NLPs repositories otherwise.

Returns:
CLIPForZeroShotClassification

The restored model