sparknlp.annotator.cv.internvl_for_multimodal#

Module Contents#

Classes#

InternVLForMultiModal

InternVLForMultiModal can load InternVL Vision models for visual question answering.

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

InternVLForMultiModal can load InternVL Vision models for visual question answering. The model consists of a vision encoder, a text encoder, a text decoder and a model merger. The vision encoder will encode the input image, the text encoder will encode the input text, the model merger will merge the image and text embeddings, and the text decoder will output the answer.

InternVL 2.5 is an advanced multimodal large language model (MLLM) series that builds upon InternVL 2.0, maintaining its core model architecture while introducing significant enhancements in training and testing strategies as well as data quality. Key features include: - Large context window support - Multilingual support - Multimodal capabilities handling both text and image inputs - Optimized for deployment with int4 quantization

Pretrained models can be loaded with pretrained() of the companion object: >>> visualQA = InternVLForMultiModal.pretrained() … .setInputCols(“image_assembler”) … .setOutputCol(“answer”)

The default model is “internvl2_5_1b_int4”, if no name is provided. For available pretrained models, refer to the Models Hub.

Input Annotation types

Output Annotation type

IMAGE

DOCUMENT

Parameters:
batchSizeint, optional

Batch size. Larger values allow faster processing but require more memory, by default 1.

maxSentenceLengthint, optional

Maximum sentence length to process, by default 4096.

Examples

>>> import sparknlp
>>> from sparknlp.base import *
>>> from sparknlp.annotator import *
>>> from pyspark.ml import Pipeline
>>> from pyspark.sql.functions import lit
>>> image_df = spark.read.format("image").load(path=images_path)
>>> test_df = image_df.withColumn(
...     "text",
...     lit("<|im_start|><image>\nDescribe this image in detail.<|im_end|><|im_start|>assistant\n")
... )
>>> imageAssembler = ImageAssembler() \
...     .setInputCol("image") \
...     .setOutputCol("image_assembler")
>>> visualQA = InternVLForMultiModal.pretrained() \
...     .setInputCols("image_assembler") \
...     .setOutputCol("answer")
>>> pipeline = Pipeline().setStages([
...     imageAssembler,
...     visualQA
... ])
>>> result = pipeline.fit(test_df).transform(test_df)
>>> result.select("image_assembler.origin", "answer.result").show(truncate=False)
name = 'InternVLForMultiModal'[source]#
inputAnnotatorTypes[source]#
outputAnnotatorType = 'document'[source]#
minOutputLength[source]#
maxOutputLength[source]#
doSample[source]#
temperature[source]#
topK[source]#
topP[source]#
repetitionPenalty[source]#
noRepeatNgramSize[source]#
ignoreTokenIds[source]#
beamSize[source]#
setMaxSentenceSize(value)[source]#

Sets Maximum sentence length that the annotator will process, by default 4096. Parameters ———- value : int

Maximum sentence length that the annotator will process

setIgnoreTokenIds(value)[source]#

A list of token ids which are ignored in the decoder’s output. Parameters ———- value : List[int]

The words to be filtered out

setMinOutputLength(value)[source]#

Sets minimum length of the sequence to be generated. Parameters ———- value : int

Minimum length of the sequence to be generated

setMaxOutputLength(value)[source]#

Sets maximum length of output text. Parameters ———- value : int

Maximum length of output text

setDoSample(value)[source]#

Sets whether or not to use sampling, use greedy decoding otherwise. Parameters ———- value : bool

Whether or not to use sampling; use greedy decoding otherwise

setTemperature(value)[source]#

Sets the value used to module the next token probabilities. Parameters ———- value : float

The value used to module the next token probabilities

setTopK(value)[source]#

Sets the number of highest probability vocabulary tokens to keep for top-k-filtering. Parameters ———- value : int

Number of highest probability vocabulary tokens to keep

setTopP(value)[source]#

Sets the top cumulative probability for vocabulary tokens. If set to float < 1, only the most probable tokens with probabilities that add up to topP or higher are kept for generation. Parameters ———- value : float

Cumulative probability for vocabulary tokens

setRepetitionPenalty(value)[source]#

Sets the parameter for repetition penalty. 1.0 means no penalty. Parameters ———- value : float

The repetition penalty

References#

See Ctrl: A Conditional Transformer Language Model For Controllable Generation for more details.

setNoRepeatNgramSize(value)[source]#

Sets size of n-grams that can only occur once. If set to int > 0, all ngrams of that size can only occur once. Parameters ———- value : int

N-gram size can only occur once

setBeamSize(value)[source]#

Sets the number of beam size for beam search, by default 1. Parameters ———- value : int

Number of beam size for beam search

static loadSavedModel(folder, spark_session, use_openvino=False)[source]#

Loads a locally saved model. Parameters ———- folder : str

Folder of the saved model

spark_sessionpyspark.sql.SparkSession

The current SparkSession

Returns#

InternVLForMultiModal

The restored model

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

Downloads and loads a pretrained model. Parameters ———- name : str, optional

Name of the pretrained model, by default “internvl2_5_1b_int4”

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#

InternVLForMultiModal

The restored model