File size: 6,788 Bytes
cb2eba1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
import math
import torch
from typing import Callable, List
import pandas as pd
import json
import os
from tqdm import tqdm
from transformers import AutoTokenizer, AutoModel
from utils import *
from PIL import Image
import shutil
from glob import glob
import numpy as np
from transformers import InstructBlipProcessor, InstructBlipForConditionalGeneration, AddedToken
from datasets import load_dataset

model = InstructBlipForConditionalGeneration.from_pretrained("UBC-NLP/Peacock")
processor = InstructBlipProcessor.from_pretrained("UBC-NLP/Peacock")
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

def handle_images1(row: pd.Series) -> List[str]:
    return [row["image"].convert("RGB")]


def handle_images2(row: pd.Series) -> List[str]:
    return [
        row.get(f"image_{i}", None).convert("RGB")
        for i in range(9)
        if row.get(f"image_{i}", None) is not None
    ]


def save_images(images: List[str], with_resize: bool = True):
    for i, image in enumerate(images):
        if image is None:
            continue

        if with_resize:
            img = image
            width, height = img.size
            req_dim = 420
            new_width = req_dim if width > height else int((req_dim / height) * width)
            new_height = int((req_dim / width) * height) if width > height else req_dim
            img = img.resize((420, 420))
            img = img.convert("RGB")
            img.save(f"temp/image{i}.png")


def generate_qwen(prompt: str, images: List[str]) -> str:
    images = images[:1]
    save_images(images)
    inputs = processor(images=Image.open("temp/image0.png").convert("RGB"), text=prompt, return_tensors="pt").to(device)
    outputs = model.generate(
            **inputs,
            do_sample=False,
            num_beams=1,
            max_length=256,
            min_length=2,
            top_p=0.9,
            temperature=1,
            length_penalty=1.0,
            repetition_penalty=1.5,
    )
    generated_text = processor.batch_decode(outputs, skip_special_tokens=True)[0].strip()
    return generated_text


answer_field = "answer"


def process_row(row: pd.Series, fn: Callable, fn_images: Callable) -> dict:
    i, row = row
    d = {}
    try:
        d["index"] = i
        images = fn_images(row)
        d["pred_answer"] = generate_qwen(fn(row), images)
        d["answer"] = str(row[answer_field])
        d["question"] = fn(row)
        print(f"Question: {fn(row)}\nPredicted: {d['pred_answer']}")
        return d
    except Exception as e:
        print(f"Error processing row: {e}")
        return None


name_to_processor = {
    "mmmu": mmmu_doc_to_text,
    "mme": mme_doc_to_text,
    "gqa": gqa_doc_to_text,
    "realworldqa": realworldqa_doc_to_text,
    "vqav2": vqav2_doc_to_text,
    "vizwiz": vizwiz_doc_to_text,
    "pope": pope_doc_to_text,
    "countbench": countbench_doc_to_text,
    "medicalMMMU": medicalMMMU_doc_to_text,
    "medicalMMMUPro": medicalMMMUPro_doc_to_text,
    "diagramsMMMU": diagramsMMMU_doc_to_text,
    "mmbench": mmbench_doc_to_text,
    "seed": seed_doc_to_text,
    "medicalmmt": medicalmmt_doc_to_text,
    "hallucinationmmt": hallucinationmmt_doc_to_text,
    "vqammt": vqammt_doc_to_text,
    "mutliimagemmt": mutliimagemmt_doc_to_text,
    "isidocvqa": isidocvqa_doc_to_text,
    "patddocvqa": patddocvqa_doc_to_text,
    "celebvqa": celebvqa_doc_to_text,
    "countriesvqa": countriesvqa_doc_to_text,
    "foodvqa": foodvqa_doc_to_text,
    "objectcoco": objectcoco_doc_to_text,
    "blink": blink_doc_to_text,
    "examsv": examsv_doc_to_text,
    "chartqa": chartqa_doc_to_text,
    "mtvqa": mtvqa_doc_to_text,
    "mathvista": mathvista_doc_to_text,
    "infographicsvqa": infographicsvqa_doc_to_text,
    "agrovqa": agrovqa_doc_to_text,
    "diagramsvqa": diagramsvqa_doc_to_text,
    "tablesvqa": tablesvqa_doc_to_text,
    "iconqa": iconqa_doc_to_text,
    "scienceqa": scienceqa_doc_to_text,
    "ocrisi": ocrisi_doc_to_text,
    "evarest": evarest_doc_to_text,
    "historicalbooks": historicalbooks_doc_to_text,
    "khatt": khatt_doc_to_text,
    "patsocr": patsocr_doc_to_text,
    "arabicocr": arabicocr_doc_to_text,
    "culturevideovqa": culturevideovqa_doc_to_text,
    "videomme": videomme_doc_to_text,
    "geochat": geochat_doc_to_text,
    "muribench": muribench_doc_to_text,
}
name_to_handle_type = {
    # "mmmu": handle_images2,
    # "mme": handle_images1,
    # "gqa": handle_images1,
    # "realworldqa": handle_images1,
    # "vqav2": handle_images1,
    # "vizwiz": handle_images1,
    # "pope": handle_images1,
    # "countbench": handle_images1,
    # "medicalMMMU": handle_images2,
    # "medicalMMMUPro": handle_images2,
    # "diagramsMMMU": handle_images2,
    # "mmbench": handle_images1,
    # "seed": handle_images2,
    "vqammt": handle_images1,
    "isidocvqa": handle_images1,
    "patddocvqa": handle_images1,
    "celebvqa": handle_images1,
    "countriesvqa": handle_images1,
    "foodvqa": handle_images1,
    "objectcoco": handle_images1,
    "blink": handle_images2,
    "examsv": handle_images1,
    "chartqa": handle_images1,
    "mtvqa": handle_images1,
    "mathvista": handle_images1,
    "infographicsvqa": handle_images1,
    "agrovqa": handle_images1,
    "diagramsvqa": handle_images1,
    "tablesvqa": handle_images1,
    "scienceqa": handle_images1,
    "geochat": handle_images1,
    "ocrisi": handle_images1,
    "evarest": handle_images1,
    "historicalbooks": handle_images1,
    "khatt": handle_images1,
    "patsocr": handle_images1,
    "hallucinationmmt": handle_images1,
    "medicalmmt": handle_images1,
    "arabicocr": handle_images1,
    # "iconqa": handle_images2,
    # "culturevideovqa": handle_images2,
    # "muribench": handle_images2,
    # "videomme": handle_images2,
    # "mutliimagemmt": handle_images2,
}
names = list(name_to_handle_type.keys())
os.makedirs("results", exist_ok=True)
os.makedirs("temp", exist_ok=True)

for name in tqdm(names):
    try:
        ds = load_dataset(f"ahmedheakl/arabicp_{name}", split="train", num_proc=4)
    except:
        continue
    # if os.path.exists(f"results/peacock_{name}.json"):
    #     with open(f"results/peacock_{name}.json", "r", encoding="utf-8") as f:
    #         dd = json.load(f)
    #     if len(dd) >= (len(ds) // 2): continue  

    df = pd.DataFrame(ds)
    print(f"Evaluating {name} dataset")
    fn = name_to_processor[name]
    fn_images = name_to_handle_type[name]
    results = []
    for i in tqdm(range(len(df))):
        results.append(process_row((i, df.iloc[i]), fn, fn_images))
    report = [r for r in results if r is not None]
    with open(f"results/peacock_{name}.json", "w", encoding="utf-8") as f:
        json.dump(report, f, ensure_ascii=False, indent=2)

shutil.rmtree("temp")