Image Coherence
The Image Coherence metric assesses the coherent alignment of images with their accompanying text, evaluating how effectively the visual content complements and enhances the textual narrative. deepeval's Image Coherence metric is a self-explaining MLLM-Eval, meaning it outputs a reason for its metric score.
Required Arguments
To use the ImageCoherence, you'll have to provide the following arguments when creating a LLMTestCase:
inputactual_output
The input and actual_output are required to create an LLMTestCase (and hence required by all metrics) even though they might not be used for metric calculation. Read the How Is It Calculated section below to learn more.
Usage
from deepeval import evaluate
from deepeval.metrics import ImageCoherenceMetric
from deepeval.test_case import LLMTestCase, MLLMImage
metric = ImageCoherenceMetric(
threshold=0.7,
include_reason=True,
)
m_test_case = LLMTestCase(
input=f"Provide step-by-step instructions on how to fold a paper airplane.",
actual_output=f"""
1. Take the sheet of paper and fold it lengthwise:
{MLLMImage(url="./paper_plane_1", local=True)}
2. Unfold the paper. Fold the top left and right corners towards the center.
{MLLMImage(url="./paper_plane_2", local=True)}
...
"""
)
evaluate(test_cases=[m_test_case], metrics=[metric])There are SIX optional parameters when creating a ImageCoherence:
- [Optional]
threshold: a float representing the minimum passing threshold. Can also be set toNoneto run the metric in score-only mode. Defaulted to0.5. - [Optional]
strict_mode: a boolean which when set toTrue, enforces a binary metric score: 1 for perfection, 0 otherwise. It also overrides the current threshold and sets it to 1. Defaulted toFalse. - [Optional]
async_mode: a boolean which when set toTrue, enables concurrent execution within themeasure()method. Defaulted toTrue. - [Optional]
verbose_mode: a boolean which when set toTrue, prints the intermediate steps used to calculate said metric to the console, as outlined in the How Is It Calculated section. Defaulted toFalse. - [Optional]
max_context_size: a number representing the maximum number of characters in each context, as outlined in the How Is It Calculated section. Defaulted toNone. - [Optional]
flaky: a boolean which when set toTrue, marks the metric as flaky. Defaulted toFalse.
As a standalone
You can also run the ImageCoherenceMetric on a single test case as a standalone, one-off execution.
...
metric.measure(m_test_case)
print(metric.score, metric.reason)How Is It Calculated?
The ImageCoherence score is calculated as follows:
- Individual Image Coherence: Each image's coherence score is based on the text directly above and below the image, limited by a
max_context_sizein characters. Ifmax_context_sizeis not supplied, all available text is used. The equation can be expressed as:
- Final Score: The overall
ImageCoherencescore is the average of all individual image coherence scores for each image:
FAQs
What's the difference between Image Coherence and Image Helpfulness?
ImageCoherence measures alignment — whether an image fits and flows with the text around it — while ImageHelpfulness measures usefulness. An image can be coherent yet add little value, so track both together.What exactly is being aligned in a coherence check?
How is the score computed across multiple images?
MLLMImage is scored from the text directly above and below it, and the final score is the average (O = (ΣC_i) / n). Use max_context_size to cap the surrounding text per image.What typically causes a low Image Coherence score?
metric.reason to see which image broke the flow.