Spatial Relationship Metrics
This module aims to implement the Spatial relationship metric described in section 3.2 of T2I-CompBench: A Comprehensive Benchmark for Open-world Compositional Text-to-image Generation.
Using an object-detection model for spatial relationship evaluation as proposed in T2I-CompBench |
Weave gives us a holistic view of the evaluations to drill into individual ouputs and scores. |
Example
Step 1: Generate evaluation dataset
Generate an evaluation dataset using the MSCOCO object vocabulary and publish it as a Weave Dataset. You can follow this notebook to learn about the porocess.
Step 2: Evaluate
import wandb
import weave
from hemm.eval_pipelines import BaseDiffusionModel, EvaluationPipeline
from hemm.metrics.image_quality import LPIPSMetric, PSNRMetric, SSIMMetric
# Initialize Weave and WandB
wandb.init(project="image-quality-leaderboard", job_type="evaluation")
weave.init(project_name="image-quality-leaderboard")
# Initialize the diffusion model to be evaluated as a `weave.Model` using `BaseWeaveModel`
model = BaseDiffusionModel(diffusion_model_name_or_path="CompVis/stable-diffusion-v1-4")
# Add the model to the evaluation pipeline
evaluation_pipeline = EvaluationPipeline(model=model)
# Define the judge model for 2d spatial relationship metric
judge = DETRSpatialRelationShipJudge(
model_address=detr_model_address, revision=detr_revision
)
# Add 2d spatial relationship Metric to the evaluation pipeline
metric = SpatialRelationshipMetric2D(judge=judge, name="2d_spatial_relationship_score")
evaluation_pipeline.add_metric(metric)
# Evaluate!
evaluation_pipeline(dataset="t2i_compbench_spatial_prompts:v0")
Metrics
SpatialRelationshipMetric2D
Bases: BaseMetric
Spatial relationship metric for image generation as proposed in Section 4.2 from the paper T2I-CompBench: A Comprehensive Benchmark for Open-world Compositional Text-to-image Generation.
Sample usage
import wandb
import weave
from hemm.eval_pipelines import BaseDiffusionModel, EvaluationPipeline
from hemm.metrics.image_quality import LPIPSMetric, PSNRMetric, SSIMMetric
# Initialize Weave and WandB
wandb.init(project="image-quality-leaderboard", job_type="evaluation")
weave.init(project_name="image-quality-leaderboard")
# Initialize the diffusion model to be evaluated as a `weave.Model` using `BaseWeaveModel`
model = BaseDiffusionModel(diffusion_model_name_or_path="CompVis/stable-diffusion-v1-4")
# Add the model to the evaluation pipeline
evaluation_pipeline = EvaluationPipeline(model=model)
# Define the judge model for 2d spatial relationship metric
judge = DETRSpatialRelationShipJudge(
model_address=detr_model_address, revision=detr_revision
)
# Add 2d spatial relationship Metric to the evaluation pipeline
metric = SpatialRelationshipMetric2D(judge=judge, name="2d_spatial_relationship_score")
evaluation_pipeline.add_metric(metric)
# Evaluate!
evaluation_pipeline(dataset="t2i_compbench_spatial_prompts:v0")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
judge |
Union[Model, DETRSpatialRelationShipJudge]
|
The judge model to predict the bounding boxes from the generated image. |
required |
iou_threshold |
Optional[float]
|
The IoU threshold for the spatial relationship. |
0.1
|
distance_threshold |
Optional[float]
|
The distance threshold for the spatial relationship. |
150
|
name |
Optional[str]
|
The name of the metric. |
'spatial_relationship_score'
|
Source code in hemm/metrics/spatial_relationship/spatial_relationship_2d.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
compose_judgement(prompt, image, entity_1, entity_2, relationship, boxes)
Compose the judgement based on the response and the predicted bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt |
str
|
The prompt using which the image was generated. |
required |
image |
Image
|
The input image. |
required |
entity_1 |
str
|
First entity. |
required |
entity_2 |
str
|
Second entity. |
required |
relationship |
str
|
Relationship between the entities. |
required |
boxes |
List[BoundingBox]
|
The predicted bounding boxes. |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: The comprehensive spatial relationship judgement. |
Source code in hemm/metrics/spatial_relationship/spatial_relationship_2d.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
evaluate(prompt, entity_1, entity_2, relationship, model_output)
Calculate the spatial relationship score for the given prompt and model output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt |
str
|
The prompt for the model. |
required |
entity_1 |
str
|
The first entity in the spatial relationship. |
required |
entity_2 |
str
|
The second entity in the spatial relationship. |
required |
relationship |
str
|
The spatial relationship between the two entities. |
required |
model_output |
Dict[str, Any]
|
The output from the model. |
required |
Returns:
Type | Description |
---|---|
Dict[str, Union[bool, float, int]]
|
Dict[str, Union[bool, float, int]]: The comprehensive spatial relationship judgement. |
Source code in hemm/metrics/spatial_relationship/spatial_relationship_2d.py
Judges
DETRSpatialRelationShipJudge
Bases: Model
DETR spatial relationship judge model for 2D images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_address |
str
|
The address of the model to use. |
'facebook/detr-resnet-50'
|
revision |
str
|
The revision of the model to use. |
'no_timm'
|
name |
str
|
The name of the judge model |
'detr_spatial_relationship_judge'
|
Source code in hemm/metrics/spatial_relationship/judges/detr.py
predict(image)
Predict the bounding boxes from the input image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
Image
|
The input image. |
required |
Returns:
Type | Description |
---|---|
List[BoundingBox]
|
List[BoundingBox]: The predicted bounding boxes. |
Source code in hemm/metrics/spatial_relationship/judges/detr.py
RTDETRSpatialRelationShipJudge
Bases: Model
RT-DETR spatial relationship judge model for 2D images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_address |
str
|
The address of the model to use. |
'facebook/detr-resnet-50'
|
revision |
str
|
The revision of the model to use. |
required |
name |
str
|
The name of the judge model |
'detr_spatial_relationship_judge'
|
Source code in hemm/metrics/spatial_relationship/judges/rt_detr.py
predict(image)
Predict the bounding boxes from the input image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
Image
|
The input image. |
required |
Returns:
Type | Description |
---|---|
List[BoundingBox]
|
List[BoundingBox]: The predicted bounding boxes. |