Image vectors and quality scoring
This topic describes the CLIP image embedding, image-text similarity, and image quality scoring operators of the Python DataFrame API.
Before you begin
-
Available only in Realtime Compute Engine VVR 11.8 and later.
-
Model-based operators can run on CPUs or GPUs. The device is selected automatically based on whether GPU resources are configured.
-
All operators on this page are model-based operators, except
image_quality_score.
Operator overview
|
Category |
Operator |
Description |
|
Embedding and similarity |
Generates CLIP embedding vectors from images. |
|
|
Computes the CLIP cosine similarity between images and text. |
||
|
Quality scoring |
Computes a composite quality score. |
|
|
Assesses the risk of NSFW content. |
||
|
Assesses the aesthetic quality of images. |
||
|
Assesses the risk that an image contains a watermark. |
Common runtime parameters
The function signature of each operator retains the runtime parameters that the operator actually supports. To avoid repetition, the parameter table of each operator describes only business parameters. The common runtime parameters are described as follows. All operators on this page are model-based operators, except image_quality_score.
|
Parameter |
Type |
Default |
Applicable scope |
Description |
|
|
|
|
All operators on this page |
The UDF concurrency. |
|
|
|
|
Model-based operators on this page |
The model sharing mode. |
|
|
|
|
Model-based operators on this page |
The batch size of the batch UDF. |
|
|
|
|
Model-based operators on this page |
The number or fraction of GPUs requested per model instance, for example, |
|
|
|
|
Model-based operators on this page |
The GPU type. Configure it together with |
Image vectors and similarity
image_embedding
Dependency: pip install open_clip_torch torch
Generates CLIP embedding vectors from images.
Input type: DataType.image(), a decoded image column.
Function signature:
image_embedding(
*columns,
model="ViT-B/32",
pretrained="openai",
model_sharing=None,
concurrency=None,
batch_size=None,
num_gpus=None,
gpu_type=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The CLIP model architecture. Note: Currently, only the built-in |
|
|
|
|
The pretrained weight checkpoint. |
The vector dimension depends on the selected model. For example, ViT-B/32 returns 512-dimensional vectors. Null input returns None.
Return type: DataType.list(DataType.float32()).
from pyflink.dataframe import col
from pyflink.multimodal.operators import image_embedding
result = df.with_column(
"image_vector",
image_embedding(
col("image"),
model="ViT-B/32",
pretrained="openai"
)
)
image_text_similarity
Dependency: pip install open_clip_torch torch
Computes the CLIP cosine similarity between images and text. The text input supports a fixed text constant or a text column (per-row matching).
Input type:
-
Column 1 (image column):
DataType.image(). -
Column 2 (optional, text column): for per-row text matching, pass
DataType.string() -
When
textis set, pass only the image column. Whentext=None, pass the image column followed by the text column.
Function signature:
image_text_similarity(
*columns,
text=None,
model="ViT-B/32",
pretrained="openai",
model_sharing=None,
concurrency=None,
batch_size=None,
num_gpus=None,
gpu_type=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
Fixed text. |
|
|
|
|
The CLIP model architecture. Note: Currently, only the built-in |
|
|
|
|
The pretrained weight checkpoint. |
Return type: DataType.float64().
The return value is in the range [-1, 1]. A null image, or a null text in per-row text mode, returns None.
Fixed text example:
from pyflink.multimodal.operators import image_text_similarity
result = df.with_column(
"cat_score",
image_text_similarity(
col("image"),
text="A photo of a cat")
)
Per-row text example:
result = df.with_column(
"image_text_score",
image_text_similarity(
col("image"),
col("description")
)
)
Image quality scoring
image_quality_score
Dependency: pip install transformers torch
Computes a composite quality score that combines sharpness, contrast, and color richness.
Input type: DataType.image(), a decoded image column.
Function signature:
image_quality_score(
*columns,
concurrency=None
)
The computation weights for sharpness, contrast, and color richness are fixed at 0.5, 0.3, and 0.2, respectively.
Return type: DataType.float64(). The value range is [0, 1].
from pyflink.multimodal.operators import image_quality_score
result = df.with_column(
"quality_score",
image_quality_score(col("image"))
)
image_nsfw_score
Dependency: pip install transformers torch
Uses a HuggingFace image classifier to assess the risk of NSFW content.
Input type: DataType.image(), a decoded image column.
Function signature:
image_nsfw_score(
*columns,
hf_nsfw_model="Falconsai/nsfw_image_detection",
model_sharing=None,
concurrency=None,
batch_size=None,
num_gpus=None,
gpu_type=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The HuggingFace NSFW image classification model ID. Note: Currently, only the built-in default model is supported. |
Return type: DataType.float64().
The value range is [0, 1]. A higher value indicates a higher risk of NSFW content.
from pyflink.multimodal.operators import image_nsfw_score
result = df.with_column(
"nsfw_score",
image_nsfw_score(col("image"))
)
image_aesthetic_score
Dependency: pip install transformers torch
Uses an aesthetic predictor to assess the aesthetic quality of images.
Input type: DataType.image(), a decoded image column.
Function signature:
image_aesthetic_score(
*columns,
hf_scorer_model=(
"shunk031/"
"aesthetics-predictor-v2-sac-logos-ava1-l14-linearMSE"
),
model_sharing=None,
concurrency=None,
batch_size=None,
num_gpus=None,
gpu_type=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The HuggingFace aesthetic scoring model ID. Note: Currently, only the built-in default model is supported. |
Return type: DataType.float64().
The value range is [0, 1]. A higher value indicates a higher aesthetic score from the model.
from pyflink.multimodal.operators import image_aesthetic_score
result = df.with_column(
"aesthetic_score",
image_aesthetic_score(col("image"))
)
image_watermark_score
Dependency: pip install transformers torch
Uses an image classifier to assess the risk that an image contains a watermark.
Input type: DataType.image(), a decoded image column.
Function signature:
image_watermark_score(
*columns,
hf_watermark_model="amrul-hzz/watermark_detector",
model_sharing=None,
concurrency=None,
batch_size=None,
num_gpus=None,
gpu_type=None
)
|
Parameter |
Type |
Default |
Description |
|
|
|
|
The HuggingFace watermark detection model ID. Note: Currently, only the built-in default model is supported. |
Return type: DataType.float64(). The value range is [0, 1]. A higher value indicates a higher probability that the image contains a watermark.
from pyflink.multimodal.operators import image_watermark_score
result = df.with_column(
"watermark_score",
image_watermark_score(col("image"))
)