Datasets:
Modalities:
Image
Formats:
imagefolder
Languages:
English
Size:
< 1K
Tags:
safety
vision-language-models
counterfactual
interpretability
mechanistic-interpretability
red-teaming
License:
File size: 8,659 Bytes
30e4105 | 1 2 3 4 5 6 7 8 9 10 11 12 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 | #!/usr/bin/env python3
"""Check that the SSU-Bench files and metadata are complete and consistent.
Run from the repository root: python scripts/validate_dataset.py
Requires Pillow and NumPy. Exits with status 1 if any check fails.
"""
import csv
import json
import os
import sys
import numpy as np
from PIL import Image
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
problems = []
N_TEXT, N_IMAGE = 107, 109 # 57 + 50 text pairs, 60 + 49 image pairs (ids 61-85 and 107 unused)
STATUSES = {"reviewed", "pending_review"}
def check(cond, msg):
if not cond:
problems.append(msg)
def validate_text():
d = os.path.join(ROOT, "text_counterfactuals")
meta = json.load(open(os.path.join(d, "prompts.json"), encoding="utf-8"))
rows = list(csv.DictReader(open(os.path.join(d, "pairs.csv"), encoding="utf-8")))
check(len(meta) == N_TEXT, f"text prompts.json has {len(meta)} entries, expected {N_TEXT}")
check(len(rows) == len(meta), "text pairs.csv and prompts.json differ in length")
ids = [int(e["pair_id"]) for e in meta]
check(ids == sorted(set(ids)), "text pair ids are not unique and sorted")
formats = {}
for e, r in zip(meta, rows):
pid = int(e["pair_id"])
check(int(r["pair_id"]) == pid, f"text pair {pid}: csv/json order mismatch")
path = os.path.join(d, "images", e["image_name"])
check(os.path.isfile(path), f"text pair {pid}: image missing ({e['image_name']})")
if os.path.isfile(path):
try:
with Image.open(path) as im:
im.load()
formats[im.format] = formats.get(im.format, 0) + 1
except Exception as err: # noqa: BLE001
check(False, f"text pair {pid}: image cannot be opened ({err})")
s, u, w = e["safe_prompt"], e["unsafe_prompt"], e["marked_word"]
check(s != u, f"text pair {pid}: prompts identical")
check(w and w in u.split() or w in u, f"text pair {pid}: critical word '{w}' not in unsafe prompt")
check(r["safe_prompt"] == s and r["unsafe_prompt"] == u and r["critical_word"] == w,
f"text pair {pid}: pairs.csv disagrees with prompts.json")
check(e.get("status") in STATUSES and r.get("status") == e.get("status"),
f"text pair {pid}: status missing, unknown or inconsistent")
ds, du = s.split(), u.split()
n_diff = sum(1 for a, b in zip(ds, du) if a != b) + abs(len(ds) - len(du))
if n_diff != 1:
print(f"note: text pair {pid} differs in more than one word ({s!r} vs {u!r})")
extra = sorted(set(os.listdir(os.path.join(d, "images"))) - {e["image_name"] for e in meta})
check(not extra, f"text images without metadata: {extra}")
v2 = {int(r["pair_id"]) for r in csv.DictReader(open(os.path.join(d, "v2_metadata.csv"), encoding="utf-8"))}
pending = {int(e["pair_id"]) for e in meta if e["status"] == "pending_review"}
check(v2 == pending, "text v2_metadata.csv does not list exactly the pending_review pairs")
n_status = {st: sum(1 for e in meta if e["status"] == st) for st in sorted(STATUSES)}
print(f"text subset: {len(meta)} pairs {n_status}, image formats {formats}")
def validate_image():
d = os.path.join(ROOT, "image_counterfactuals")
meta = json.load(open(os.path.join(d, "prompts.json"), encoding="utf-8"))
rows = list(csv.DictReader(open(os.path.join(d, "pairs.csv"), encoding="utf-8")))
check(len(meta) == N_IMAGE, f"image prompts.json has {len(meta)} entries, expected {N_IMAGE}")
ids = [int(e["image_number"]) for e in meta]
check(ids == sorted(set(ids)), "image pair ids are not unique and sorted")
check(len(rows) == len(meta), "image pairs.csv and prompts.json differ in length")
sizes = {}
coverage = []
for e, r in zip(meta, rows):
n = int(e["image_number"])
check(int(r["pair_id"]) == n, f"image pair {n}: csv/json order mismatch")
check(r["prompt"] == e["prompt"] and r["critical_word"] == e["word"],
f"image pair {n}: pairs.csv disagrees with prompts.json")
check(e["word"] in e["prompt"], f"image pair {n}: critical word '{e['word']}' not in prompt")
check(e.get("status") in STATUSES and r.get("status") == e.get("status"),
f"image pair {n}: status missing, unknown or inconsistent")
paths = {k: os.path.join(d, r[c]) for k, c in (("safe", "safe_image"), ("unsafe", "unsafe_image"), ("mask", "mask"))}
ims = {}
for k, p in paths.items():
check(os.path.isfile(p), f"image pair {n}: {k} file missing ({os.path.relpath(p, d)})")
if os.path.isfile(p):
try:
im = Image.open(p)
im.load()
ims[k] = im
except Exception as err: # noqa: BLE001
check(False, f"image pair {n}: {k} cannot be opened ({err})")
if len(ims) == 3:
check(ims["safe"].size == ims["unsafe"].size == ims["mask"].size,
f"image pair {n}: sizes differ {[im.size for im in ims.values()]}")
m = np.asarray(ims["mask"].convert("L"))
vals = set(np.unique(m).tolist())
check(vals <= {0, 255}, f"image pair {n}: mask is not binary 0/255 ({sorted(vals)[:6]})")
check(255 in vals, f"image pair {n}: mask is empty")
coverage.append(float((m == 255).mean()))
sizes[ims["safe"].size] = sizes.get(ims["safe"].size, 0) + 1
if e["status"] == "pending_review":
# v2 pairs are built by masked editing: outside the mask the two images are identical
a = np.asarray(ims["safe"].convert("RGB"), dtype=np.int16)
b = np.asarray(ims["unsafe"].convert("RGB"), dtype=np.int16)
outside = int(np.any(a != b, axis=2)[m != 255].sum())
check(outside == 0, f"image pair {n}: {outside} pixels differ outside the mask")
n_img = len([f for f in os.listdir(os.path.join(d, "images")) if f.endswith(".png")])
n_mask = len([f for f in os.listdir(os.path.join(d, "masks")) if f.endswith(".png")])
check(n_img == 2 * N_IMAGE and n_mask == N_IMAGE,
f"expected {2 * N_IMAGE} images and {N_IMAGE} masks, found {n_img} and {n_mask}")
v2 = {int(r["pair_id"]) for r in csv.DictReader(open(os.path.join(d, "v2_metadata.csv"), encoding="utf-8"))}
pending = {int(e["image_number"]) for e in meta if e["status"] == "pending_review"}
check(v2 == pending, "image v2_metadata.csv does not list exactly the pending_review pairs")
for r in csv.DictReader(open(os.path.join(d, "v2_metadata.csv"), encoding="utf-8")):
check(r["perception_answer_safe"] != r["perception_answer_unsafe"],
f"image pair {r['pair_id']}: perception answers do not differ")
n_status = {st: sum(1 for e in meta if e["status"] == st) for st in sorted(STATUSES)}
if coverage:
print(f"image subset: {len(meta)} pairs {n_status}, {len(sizes)} distinct sizes (most common {max(sizes, key=sizes.get)}), "
f"mask coverage {100*min(coverage):.1f}-{100*max(coverage):.1f}% (median {100*float(np.median(coverage)):.1f}%)")
def validate_eligibility():
p = os.path.join(ROOT, "eligibility", "eligible_pairs.json")
e = json.load(open(p, encoding="utf-8"))
text_ids = {int(x["pair_id"]) for x in json.load(open(os.path.join(ROOT, "text_counterfactuals", "prompts.json"), encoding="utf-8"))}
image_ids = {int(x["image_number"]) for x in json.load(open(os.path.join(ROOT, "image_counterfactuals", "prompts.json"), encoding="utf-8"))}
for model, ids in e["text_counterfactuals"]["eligible"].items():
check(set(ids) <= text_ids, f"eligibility: text ids for {model} not in dataset")
bad = set(ids) & set(e["text_counterfactuals"]["tokenizer_check_failed"][model])
check(not bad, f"eligibility: {model} text pairs both eligible and tokenizer-failed: {sorted(bad)}")
for model, ids in e["image_counterfactuals"]["eligible"].items():
check(set(ids) <= image_ids, f"eligibility: image ids for {model} not in dataset")
counts = {m: len(v) for m, v in e["text_counterfactuals"]["eligible"].items()}
counts_i = {m: len(v) for m, v in e["image_counterfactuals"]["eligible"].items()}
print(f"eligible pairs: text {counts}, image {counts_i}")
if __name__ == "__main__":
validate_text()
validate_image()
validate_eligibility()
if problems:
print("\nPROBLEMS:")
for p in problems:
print(" -", p)
sys.exit(1)
print("all checks passed")
|