File size: 3,333 Bytes
79e014d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# https://raw.githubusercontent.com/YanNeu/RePOPE/refs/heads/main/annotations/coco_repope_random.json
# https://raw.githubusercontent.com/YanNeu/RePOPE/refs/heads/main/annotations/coco_repope_adversarial.json
# https://raw.githubusercontent.com/YanNeu/RePOPE/refs/heads/main/annotations/coco_repope_popular.json
## example in json:
# coco_repope_random.json
# {"question_id": 1, "image": "COCO_val2014_000000310196.jpg", "text": "Is there a snowboard in the image?", "label": "yes"}
# coco_repope_adversarial.json
# {"question_id": 1, "image": "COCO_val2014_000000210789.jpg", "text": "Is there a truck in the image?", "label": "yes"}



from datasets import load_dataset

ds = load_dataset("lmms-lab/POPE", "default")['test']
# sample entry:
# {'id': '0', 'question_id': '1', 'question': 'Is there a snowboard in the image?',
#  'answer': 'yes', 'image_source': 'COCO_val2014_000000310196',
#  'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=640x427 at 0x1552C16AB2B0>,
#  'category': 'adversarial'}

# ---------------------------------------------------------------------------
# helper: pull annotations from remote JSONs and attach to examples
# ---------------------------------------------------------------------------
import requests

annotation_urls_jsonl = [
    "https://raw.githubusercontent.com/YanNeu/RePOPE/refs/heads/main/annotations/coco_repope_random.json",
    "https://raw.githubusercontent.com/YanNeu/RePOPE/refs/heads/main/annotations/coco_repope_adversarial.json",
    "https://raw.githubusercontent.com/YanNeu/RePOPE/refs/heads/main/annotations/coco_repope_popular.json",
]  # JSON filenames encode the category we want to propagate

# load and index by category, subdividing by question_id
# build a lookup index from category and question_id to annotation
import os
repope_index = {}
for url in annotation_urls_jsonl:
    basename = os.path.splitext(os.path.basename(url))[0]  # e.g. coco_repope_random
    category = basename.split("_")[-1]
    r = requests.get(url)
    r.raise_for_status()
    for line in r.text.splitlines():
        if not line.strip():
            continue
        ann = requests.compat.json.loads(line)
        ann["source_category"] = category
        qid = str(ann.get("question_id"))
        repope_index.setdefault(category, {})[qid] = ann

# iterate once, updating answers and gathering statistics
count = changed = removed = 0
new_examples = []
for idx, example in enumerate(ds):
    qid = str(example.get("question_id"))
    ann = repope_index.get(example.get("category"), {}).get(qid)
    if not ann:
        print(f"⚠️ No annotation for idx={idx} qid={qid} category={example.get('category')}")
        removed += 1
        continue
    example['pope_old_answer'] = example['answer']
    if example['answer'] != ann['label']:
        example['answer'] = ann['label']
        changed += 1
    new_examples.append(example)
    count += 1

print(f"✅ Matched {count} examples.")
print(f"🔄 Changed {changed} answers.")
print(f"❌ Removed {removed} with no annotation.")
print("inspection complete")
# ✅ Matched 8185 examples.
# 🔄 Changed 494 answers.
# ❌ Removed 815 with no annotation.
from datasets import Dataset, DatasetDict
ds = Dataset.from_list(new_examples)
dataset = DatasetDict({ "test": ds})
dataset.push_to_hub("SushantGautam/RePOPE")