File size: 29,640 Bytes
b925209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
import json
import os
import sys
import time
import yaml
import spacy
import ast
from PIL import Image
from glob import glob
from tqdm import tqdm
from collections import defaultdict
import pandas as pd
from io import BytesIO
import base64
from anls import anls_score
import torch
from torch.utils.data import Dataset, DataLoader, DistributedSampler
import torchvision.transforms as T
from eval import conversation as conversation_lib
from eval.mmmu_utils import CAT_SHORT2LONG, DOMAIN_CAT2SUB_CAT, parse_multi_choice_response, parse_open_response, \
    process_single_sample, construct_prompt, mmmu_main_eval, process_single_sample_pro, construct_prompt_pro
from eval.mmmu_utils import evaluate as evaluate_mmmu
from torchvision.transforms.functional import InterpolationMode
from datasets import load_dataset, concatenate_datasets

IMAGENET_MEAN = (0.485, 0.456, 0.406)
IMAGENET_STD = (0.229, 0.224, 0.225)


def build_transform(input_size):
    MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
    transform = T.Compose([
        T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
        T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
        T.ToTensor(),
        T.Normalize(mean=MEAN, std=STD)
    ])
    return transform


def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
    best_ratio_diff = float('inf')
    best_ratio = (1, 1)
    area = width * height
    for ratio in target_ratios:
        target_aspect_ratio = ratio[0] / ratio[1]
        ratio_diff = abs(aspect_ratio - target_aspect_ratio)
        if ratio_diff < best_ratio_diff:
            best_ratio_diff = ratio_diff
            best_ratio = ratio
        elif ratio_diff == best_ratio_diff:
            if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
                best_ratio = ratio
    return best_ratio


def dynamic_preprocess(image, min_num=1, max_num=6, image_size=448, use_thumbnail=False):
    orig_width, orig_height = image.size
    aspect_ratio = orig_width / orig_height

    # calculate the existing image aspect ratio
    target_ratios = set(
        (i, j) for n in range(min_num, max_num + 1) for i in range(1, n + 1) for j in range(1, n + 1) if
        i * j <= max_num and i * j >= min_num)
    target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])

    # find the closest aspect ratio to the target
    target_aspect_ratio = find_closest_aspect_ratio(
        aspect_ratio, target_ratios, orig_width, orig_height, image_size)

    # calculate the target width and height
    target_width = image_size * target_aspect_ratio[0]
    target_height = image_size * target_aspect_ratio[1]
    blocks = target_aspect_ratio[0] * target_aspect_ratio[1]

    # resize the image
    resized_img = image.resize((target_width, target_height))
    processed_images = []
    for i in range(blocks):
        box = (
            (i % (target_width // image_size)) * image_size,
            (i // (target_width // image_size)) * image_size,
            ((i % (target_width // image_size)) + 1) * image_size,
            ((i // (target_width // image_size)) + 1) * image_size
        )
        # split the image
        split_img = resized_img.crop(box)
        processed_images.append(split_img)
    assert len(processed_images) == blocks
    if use_thumbnail and len(processed_images) != 1:
        thumbnail_img = image.resize((image_size, image_size))
        processed_images.append(thumbnail_img)
    return processed_images


def load_image(image, input_size=448, max_num=6, decoded=False):
    if not decoded:
        image = Image.open(image).convert('RGB')
    transform = build_transform(input_size=input_size)
    images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
    pixel_values = [transform(image) for image in images]
    pixel_values = torch.stack(pixel_values)
    return pixel_values


def levenshtein_distance(s1, s2):
    if len(s1) > len(s2):
        s1, s2 = s2, s1

    distances = range(len(s1) + 1)
    for i2, c2 in enumerate(s2):
        distances_ = [i2 + 1]
        for i1, c1 in enumerate(s1):
            if c1 == c2:
                distances_.append(distances[i1])
            else:
                distances_.append(1 + min((distances[i1], distances[i1 + 1], distances_[-1])))
        distances = distances_
    return distances[-1]


def get_anls_score(pred, gold_labels, threshold, llava_eval=False):
    values = []
    for answer in gold_labels:
        # preprocess both the answers - gt and prediction
        gt_answer = ' '.join(answer.strip().lower().split())
        det_answer = ' '.join(pred.strip().lower().split())

        dist = levenshtein_distance(gt_answer, det_answer)
        length = max(len(answer.upper()), len(pred.upper()))
        values.append(0.0 if length == 0 else float(dist) / float(length))

    question_result = 1 - min(values)

    if llava_eval:
        question_result = 1.0 if question_result >= threshold else 0.0
    else:
        if (question_result < threshold):
            question_result = 0

    return question_result


def isNumber(n: str):
    try:
        float(n)
        return True
    except ValueError:
        return False


class COCOEvalDataset(Dataset):
    def __init__(self, args, img_dir, subset=None):
        self.args = args
        self.img_files = sorted(glob(os.path.join(img_dir, "*")))

        if subset:
            self.img_files = self.img_files[:subset]

        self.image_ids = [int(img_file.split("_")[-1].split(".")[0]) for img_file in self.img_files]

    def __len__(self):
        return len(self.img_files)

    def __getitem__(self, idx):
        img_path = self.img_files[idx]
        img = load_image(img_path, max_num=6).to(torch.bfloat16)

        return self.image_ids[idx], img


class Flickr30KEvalDataset(Dataset):
    def __init__(self, args, img_dir, subset=None):
        self.args = args
        self.img_dir = img_dir
        self.test_samples = json.load(open(os.path.join(img_dir, "flickr30k_test.json"), encoding='utf-8'))

        if subset:
            self.test_samples = self.test_samples[:subset]

    def __len__(self):
        return len(self.test_samples)

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.test_samples[idx]["image"])
        img = load_image(img_path, max_num=6).to(torch.bfloat16)

        image_id = int(self.test_samples[idx]["image"].split("/")[-1].replace(".jpg", ""))

        return image_id, img


class VQAv2EvalDataset(Dataset):
    def __init__(self, args, img_dir, gt_path, subset=None):
        self.args = args
        self.img_dir = img_dir
        self.gt = json.load(open(gt_path, encoding='utf-8'))

        if subset:
            self.gt = self.gt[:subset]

    def __len__(self):
        return len(self.gt)

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.gt[idx]["image"])
        img = load_image(img_path, max_num=6).to(torch.bfloat16)

        question_id = self.gt[idx]["question_id"]
        question = self.gt[idx]["question"]
        answer = self.gt[idx]["answer"]

        return img, question_id, question, answer


class TextVQAEvalDataset(Dataset):
    def __init__(self, args, img_dir, gt_path, subset=None):
        self.args = args
        self.img_dir = img_dir
        self.gt = json.load(open(gt_path, encoding='utf-8'))['data']

        if subset:
            self.gt = self.gt[:subset]

    def __len__(self):
        return len(self.gt)

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.gt[idx]["image_id"] + '.jpg')
        if not os.path.exists(img_path):
            img_path = img_path.replace('.jpg', '.png')
        img = load_image(img_path, max_num=6).to(torch.bfloat16)

        question_id = self.gt[idx]["question_id"]
        question = self.gt[idx]["question"]
        answer = self.gt[idx]["answers"]

        return img, question_id, question, answer


class GQAEvalDataset(Dataset):
    def __init__(self, args, img_dir, gt_path, subset=None):
        self.args = args
        self.img_dir = img_dir
        self.gt = json.load(open(gt_path, encoding='utf-8'))
        self.gt = [{
            "question_id": int(k),
            "image": v['imageId'] + ".jpg",
            "question": v['question'],
            "answer": v['answer']
        } for k, v in self.gt.items()]

        if subset:
            self.gt = self.gt[:subset]

    def __len__(self):
        return len(self.gt)

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.gt[idx]["image"])
        img = load_image(img_path, max_num=6).to(torch.bfloat16)

        question_id = self.gt[idx]["question_id"]
        question = self.gt[idx]["question"]
        answer = self.gt[idx]["answer"]

        return img, question_id, question, [answer]


class ChartQAEvalDataset(Dataset):
    def __init__(self, args, img_dir, gt_path, subset=None):
        self.args = args
        self.img_dir = img_dir
        self.gt = json.load(open(gt_path, encoding='utf-8'))
        for i in range(len(self.gt)):
            self.gt[i]['question_id'] = i

        if subset:
            self.gt = self.gt[:subset]

    def __len__(self):
        return len(self.gt)

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.gt[idx]["imgname"])
        img = load_image(img_path, max_num=6).to(torch.bfloat16)

        question_id = self.gt[idx]["question_id"]
        question = self.gt[idx]["query"]
        answer = self.gt[idx]["label"]

        return img, question_id, question, [answer]


class OKVQAEvalDataset(Dataset):
    def __init__(self, args, img_dir, gt_path, question_path, subset=None):
        self.args = args
        self.img_dir = img_dir
        self.gt = json.load(open(gt_path, encoding='utf-8'))['annotations']
        self.questions = json.load(open(question_path, 'r'))['questions']

        if subset:
            self.gt = self.gt[:subset]

        qid2q = {q['question_id']: q['question'] for q in self.questions}

        for ann in self.gt:
            ann['answers'] = [ans['answer'] for ans in ann['answers']]
            ann['question'] = qid2q[ann['question_id']]

    def __len__(self):
        return len(self.gt)

    def __getitem__(self, idx):
        img_id = str(self.gt[idx]["image_id"])
        img_id = '0' * (12 - len(img_id)) + img_id
        img_file_name = f"COCO_val2014_{img_id}.jpg"
        img_path = os.path.join(self.img_dir, img_file_name)
        img = load_image(img_path, max_num=6).to(torch.bfloat16)

        question_id = self.gt[idx]["question_id"]
        question = self.gt[idx]["question"]
        answer = self.gt[idx]["answers"]

        return img, question_id, question, answer


class DocVQAEvalDataset(Dataset):
    def __init__(self, args, img_dir, gt_path, split='val', subset=None):
        self.args = args
        self.img_dir = img_dir
        self.gt = json.load(open(gt_path, encoding='utf-8'))['data']

        if subset:
            self.gt = self.gt[:subset]

        self.split = split

    def __len__(self):
        return len(self.gt)

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.gt[idx]['image'].split('/')[-1])
        img = load_image(img_path, max_num=6).to(torch.bfloat16)

        question_id = self.gt[idx]["questionId"]
        question = self.gt[idx]["question"]

        if self.split == 'val':
            answer = self.gt[idx]["answers"]
        else:
            answer = ['']

        return img, question_id, question, answer


class OCRBenchEvalDataset(Dataset):
    def __init__(self, args, img_dir, gt_path, subset=None):
        self.args = args
        self.img_dir = img_dir
        self.gt = json.load(open(gt_path, encoding='utf-8'))

        if subset:
            self.gt = self.gt[:subset]

    def __len__(self):
        return len(self.gt)

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.gt[idx]['image_path'])
        img = load_image(img_path, max_num=6).to(torch.bfloat16)

        dataset_name = self.gt[idx]["dataset_name"]
        question_id = f"{idx}"
        question = self.gt[idx]["question"]
        answer = self.gt[idx]["answers"]
        data_type = self.gt[idx]["type"]

        return img, question_id, question, answer, dataset_name, data_type


class AI2DiagramEvalDataset(Dataset):
    def __init__(self, args, img_dir, gt_path, subset=None):
        self.args = args
        self.img_dir = img_dir

        with open(gt_path, 'r') as json_file:
            json_list = list(json_file)
        self.gt = [json.loads(json_str) for json_str in json_list]

        if subset:
            self.gt = self.gt[:subset]

    def __len__(self):
        return len(self.gt)

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.gt[idx]['image'])
        img = load_image(img_path, max_num=6).to(torch.bfloat16)

        question_id = self.gt[idx]["question_id"]
        question = self.gt[idx]["question"]
        answer = self.gt[idx]["answer"]

        return img, question_id, question, answer


class AI2DiagramNoMaskEvalDataset(Dataset):
    def __init__(self, args, img_dir, gt_path, subset=None):
        self.args = args
        self.img_dir = img_dir

        with open(gt_path, 'r') as json_file:
            json_list = list(json_file)
        self.gt = [json.loads(json_str) for json_str in json_list]

        if subset:
            self.gt = self.gt[:subset]

    def __len__(self):
        return len(self.gt)

    def __getitem__(self, idx):
        img_file_name = self.gt[idx]['image'].replace("AI2D_TEST", "AI2D_TEST_NO_MASK_IMAGES")
        img_path = os.path.join(self.img_dir, img_file_name)
        img = load_image(img_path, max_num=6).to(torch.bfloat16)

        question_id = self.gt[idx]["question_id"]
        question = self.gt[idx]["question"]
        answer = self.gt[idx]["answer"]

        return img, question_id, question, answer


class RealworldQAEvalDataset(Dataset):
    def __init__(self, args, img_dir, gt_path, subset=None):
        self.args = args
        self.img_dir = img_dir
        self.gt = json.load(open(gt_path, encoding='utf-8'))

        if subset:
            self.gt = self.gt[:subset]

    def __len__(self):
        return len(self.gt)

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.gt[idx]['image'])
        img = load_image(img_path, max_num=6).to(torch.bfloat16)

        question_id = int(self.gt[idx]['image'].replace(".webp", ""))
        question = self.gt[idx]["question"]

        if self.gt[idx]['question_type'] == "multi-choice":
            choices = self.gt[idx]["choices"]
            start_chr = 'A'
            choices_str = ''
            index2ans = {}
            all_choices = []
            for choice in choices:
                all_choices.append(start_chr)
                index2ans[start_chr] = choice
                choices_str += f"{start_chr}. {choice}\n"
                start_chr = chr(ord(start_chr) + 1)

            question = question + '\n' + choices_str
            question = question + "Answer with the option's letter from the given choices directly."
            answer = chr(ord('A') + self.gt[idx]['correct_choice_index'])
        else:
            question = question + "\nAnswer the question using a single word or phrase."
            answer = self.gt[idx]['answer']

        return img, question_id, question, [answer]


class MathVistaEvalDataset(Dataset):
    def __init__(self, args, task_cfg, gt_path=None):
        self.args = args
        self.task_cfg = task_cfg
        self.dataset = load_dataset("AI4Math/MathVista")['testmini']

    def __len__(self):
        return len(self.dataset)

    def __getitem__(self, idx):
        img = self.dataset[idx]['decoded_image']
        img = load_image(img.convert("RGB"), max_num=6, decoded=True).to(torch.bfloat16)

        question_id = self.dataset[idx]["pid"]
        question = self.dataset[idx]["question"]
        question_type = self.dataset[idx]["question_type"]  # free_form or multi_choice
        query = self.dataset[idx]["query"]
        choices = self.dataset[idx]["choices"]
        answer = self.dataset[idx]["answer"]

        if question_type == 'multi_choice':
            start_chr = 'A'
            choices_str = ''
            index2ans = {}
            all_choices = []
            for choice in choices:
                all_choices.append(start_chr)
                index2ans[start_chr] = choice
                choices_str += f"{start_chr}. {choice}\n"
                start_chr = chr(ord(start_chr) + 1)

            question = question + '\n' + choices_str
            question = question + "Answer with the option's letter from the given choices directly."
            answer = chr(ord('A') + choices.index(answer))
        else:
            question = query.replace("Hint: ", "")
            index2ans = {}
            all_choices = []

        return img, question_id, question_type, question, answer, str(index2ans), str(all_choices)


def construct_prompt_for_fewshot(sample):
    config = {
        "task_instructions": "",
        "multi_choice_example_format": "{}\n{}Answer with the option's letter from the given choices directly.",
        "short_ans_example_format": "{}\nAnswer the question using a single word or phrase."
    }

    question = sample['question'].strip()


    options = eval(sample['options'])
    example = ""
    if sample['question_type'] == 'multiple-choice':
        start_chr = 'A'
        prediction_range = []
        index2ans = {}
        for option in options:
            prediction_range.append(start_chr)
            example += f"({start_chr}) {option}\n"
            index2ans[start_chr] = option
            start_chr = chr(ord(start_chr) + 1)
        empty_prompt_sample_structure = config['multi_choice_example_format']
        empty_prompt = empty_prompt_sample_structure.format(question, example)
        res_dict = {'type': 'multichoice'}
        res_dict['index2ans'] = index2ans
        res_dict['correct_choice'] = sample['answer']
        res_dict['all_choices'] = prediction_range
        res_dict['empty_prompt'] = empty_prompt
        if config['task_instructions']:
            res_dict['final_input_prompt'] = config['task_instructions'].strip() + '\n\n' + empty_prompt
        else:
            res_dict['final_input_prompt'] = empty_prompt

        res_dict['gt_content'] = options[ord(sample['answer'].upper()) - ord('A')]
    else:
        empty_prompt_sample_structure = config['short_ans_example_format']
        empty_prompt = empty_prompt_sample_structure.format(question)
        res_dict = {'type': 'open'}
        res_dict['empty_prompt'] = empty_prompt
        if config['task_instructions']:
            res_dict['final_input_prompt'] = config['task_instructions'].strip() + '\n\n' + empty_prompt
        else:
            res_dict['final_input_prompt'] = empty_prompt
        res_dict['gt_content'] = sample['answer']

    res_dict.update(sample)
    return res_dict


def process_image_tag(q):
    q = q.strip()

    # heuristic way of removing <image 1>
    if q == '<image 1>':
        q = 'Answer the question in the image.'
    elif ':<image 1>' in q:
        q = q.replace(':<image 1>', ' in the image. ')
        q = q.strip()
    elif ': <image 1>' in q:
        q = q.replace(': <image 1>', ' in the image. ')
        q = q.strip()
    elif '.<image 1>' in q or '. <image 1>' in q:
        q_list = q.split('<image 1>')
        q_list = [part.strip() for part in q_list if part.strip() != '']
        q = ' '.join(q_list)
    elif q.startswith('<image 1> '):
        if q[10].isupper():
            q = q.replace('<image 1>', '')
        else:
            q = q.replace('<image 1>', 'The image')
        q = q.strip()
    elif q.startswith('<image 1>'):
        q = q.replace('<image 1>', '')
    elif q.endswith('<image 1>?'):
        q = q.replace('<image 1>', 'the image')
    elif q.endswith('?<image 1>') or q.endswith('? <image 1>') or q.endswith('\n<image 1>'):
        q = q.replace('<image 1>', '')
        q = q.strip()
    elif ' <image 1> ' in q:
        q = q.replace('<image 1>', 'the image')
    elif ' <image 1>' in q:
        q = q.replace('<image 1>', 'the image')
    elif '()<image 1>' in q:
        q = q.replace('()<image 1>', '')
    elif '(<image 1>)' in q:
        q = q.replace('(<image 1>)', '')
    elif '<image 1>.' in q:
        q = q.replace("<image 1>.", ". ")
    else:
        q = q.replace("<image 1>", ". ")
        q = q.strip()

    # remove <image 2> to <image 8>
    for i in range(2, 8):
        q = q.replace(f"<image {i}>", "")

    return q


class MMMUProEvalDataset(Dataset):
    def __init__(self, args, task_cfg, subset=None):
        self.args = args
        self.task_cfg = task_cfg
        sub_dataset_list = []
        # load_dataset will throw error if split is 'dev'
        # 'dev' is part of the 'validation' and we need to manually split them

        MMMU_path = "MMMU/MMMU_Pro"

        _split = "test"

        self.dataset = load_dataset(MMMU_path, "standard", split=_split)
        if subset:
            self.dataset = self.dataset[:subset]

    def __len__(self):
        return len(self.dataset)

    def __getitem__(self, idx):
        # ===== single-image =====
        sample = self.dataset[idx]
        sample = process_single_sample_pro(sample)
        sample = construct_prompt_pro(sample, self.task_cfg)
        img = load_image(sample['image'].convert("RGB"), max_num=6, decoded=True).to(torch.bfloat16)

        # img = img.reshape(-1, 3, self.args.img_h, self.args.img_w)

        question_id = sample['id']
        question = sample['final_input_prompt']
        answer = sample['answer']

        question = process_image_tag(question)
        question = self.task_cfg['default_image_token'] + '\n' + question

        if sample['question_type'] == 'multiple-choice':
            index2ans = sample['index2ans']
            all_choices = sample['all_choices']
        else:
            index2ans = {}
            all_choices = []

        return img, question_id, sample['subfield'], sample['question_type'], question, answer, str(index2ans), str \
            (all_choices)


class MMMUEvalDataset(Dataset):
    def __init__(self, args, task_cfg, subset=None, start_idx=None):
        self.args = args
        self.task_cfg = task_cfg
        sub_dataset_list = []
        # load_dataset will throw error if split is 'dev'
        # 'dev' is part of the 'validation' and we need to manually split them

        MMMU_path = "MMMU/MMMU"

        _split = "test" if task_cfg["split"] == "test" else "validation"
        for subject in CAT_SHORT2LONG.values():
            sub_dataset = load_dataset(
                MMMU_path, subject,
                split=_split,
            )
            sub_dataset_list.append(sub_dataset)

        dataset = concatenate_datasets(sub_dataset_list)

        if task_cfg["split"] != "test":
            dataset = [s for s in dataset if s['id'].startswith(task_cfg["split"])]

        # dataset = [s for s in dataset if s['image_2'] is not None][1:]

        self.dataset = dataset

        if subset:
            self.dataset = [dataset[i] for i in range(start_idx, min(start_idx + subset, len(dataset)))]
            print(f"Evaluating a subset of dataset: {len(self.dataset)} from {start_idx} to {start_idx + subset}")

    def __len__(self):
        return len(self.dataset)

    def __getitem__(self, idx):
        # ===== single-image =====
        sample = self.dataset[idx]
        sample = process_single_sample(sample)
        sample = construct_prompt(sample, self.task_cfg)

        img = load_image(sample['image'].convert("RGB"), max_num=6, decoded=True).to(torch.bfloat16)

        question_id = sample['id']
        question = sample['final_input_prompt']
        answer = sample['answer']

        question = process_image_tag(question)
        question = self.task_cfg['default_image_token'] + '\n' + question


        if sample['question_type'] == 'multiple-choice':
            index2ans = sample['index2ans']
            all_choices = sample['all_choices']
        else:
            index2ans = {}
            all_choices = []

        return img, question_id, sample['subfield'], sample['question_type'], question, answer, str(index2ans), str \
            (all_choices)



class VizWizEvalDataset(Dataset):
    def __init__(self, args, img_dir, question_path, subset=None):
        self.args = args
        self.img_dir = img_dir
        self.questions = json.load(open(question_path, encoding='utf-8'))

    def __len__(self):
        return len(self.questions)

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.questions[idx]["image"])
        img = load_image(img_path, max_num=6).to(torch.bfloat16)
        question = self.questions[idx]["question"]
        question_id = self.questions[idx]["image"]

        return img, question_id, question


class MMBenchEvalDataset(Dataset):
    def __init__(self, args, gt_path, subset=None):
        self.args = args
        df = pd.read_csv(gt_path, sep='\t')
        self.dataset = []
        for i, row in df.iterrows():
            choices = []
            for choice in ['A', 'B', 'C', 'D']:
                if str(row[choice]) != 'nan':
                    choices.append(row[choice])

            this_sample = {
                'index': row['index'],
                'question': row['question'],
                'hint': row['hint'],
                'category': row['category'],
                'image': Image.open(BytesIO(base64.b64decode(row['image']))),
                'choices': choices
            }

            # Only dev set gives the ground truth answer
            if 'answer' in row.keys():
                this_sample['answer'] = row['answer']
            else:
                this_sample['answer'] = ''

            self.dataset.append(this_sample)

    def __len__(self):
        return len(self.dataset)

    def __getitem__(self, idx):
        img = load_image(self.dataset[idx]["image"].convert("RGB"), max_num=6, decoded=True).to(torch.bfloat16)

        question = self.dataset[idx]["question"]
        hint = self.dataset[idx]["hint"]
        question_id = self.dataset[idx]["index"]
        choices = self.dataset[idx]["choices"]
        answer = self.dataset[idx]["answer"]

        start_chr = 'A'
        choices_str = ''
        index2ans = {}
        all_choices = []
        for choice in choices:
            all_choices.append(start_chr)
            index2ans[start_chr] = choice
            choices_str += f"{start_chr}. {choice}\n"
            start_chr = chr(ord(start_chr) + 1)

        question = question + '\n' + choices_str

        return img, question_id, question, answer, str(index2ans), str(all_choices), self.dataset[idx]["question"]


def get_task_dataloader(task_name, task_cfg, args):
    if "subset" in task_cfg.keys():
        subset = task_cfg["subset"]
    else:
        subset = None

    if task_name == "coco_caption":
        dataset = COCOEvalDataset(args, task_cfg["image_dir"], subset)
    elif task_name == "flickr30k_caption":
        dataset = Flickr30KEvalDataset(args, task_cfg["image_dir"], subset)
    elif task_name == "vqav2":
        dataset = VQAv2EvalDataset(args, task_cfg["image_dir"], task_cfg["gt_path"], subset)
    elif task_name == "textvqa":
        dataset = TextVQAEvalDataset(args, task_cfg["image_dir"], task_cfg["gt_path"], subset)
    elif task_name == "gqa":
        dataset = GQAEvalDataset(args, task_cfg["image_dir"], task_cfg["gt_path"], subset)
    elif task_name == "chartqa":
        dataset = ChartQAEvalDataset(args, task_cfg["image_dir"], task_cfg["gt_path"], subset)
    elif task_name == "okvqa":
        dataset = OKVQAEvalDataset(args, task_cfg["image_dir"], task_cfg["gt_path"], task_cfg["question_path"], subset)
    elif task_name == "vizwiz":
        dataset = VizWizEvalDataset(args, task_cfg["image_dir"], task_cfg["question_path"], subset)
    elif task_name == "docvqa":
        dataset = DocVQAEvalDataset(args, task_cfg["image_dir"], task_cfg["gt_path"], split='val', subset=subset)
    elif task_name == "docvqa_test":
        dataset = DocVQAEvalDataset(args, task_cfg["image_dir"], task_cfg["gt_path"], split='test', subset=subset)
    elif task_name == "realworldqa":
        dataset = RealworldQAEvalDataset(args, task_cfg["image_dir"], task_cfg["gt_path"], subset)
    elif task_name == "mmmu":
        dataset = MMMUEvalDataset(args, task_cfg, subset=args.subset, start_idx=args.start_idx)
    elif task_name == "mmmu_pro":
        dataset = MMMUProEvalDataset(args, task_cfg)
    elif task_name == "mathvista":
        dataset = MathVistaEvalDataset(args, task_cfg)
    elif task_name == "mmbench":
        dataset = MMBenchEvalDataset(args, task_cfg["gt_path"])
    elif task_name == 'ocrbench':
        dataset = OCRBenchEvalDataset(args, task_cfg["image_dir"], task_cfg["gt_path"], subset)
    elif task_name == 'ai2diagram':
        dataset = AI2DiagramEvalDataset(args, task_cfg["image_dir"], task_cfg["gt_path"], subset)
    elif task_name == 'ai2diagram_nomask':
        dataset = AI2DiagramNoMaskEvalDataset(args, task_cfg["image_dir"], task_cfg["gt_path"], subset)
    else:
        raise NotImplementedError(f"Task {task_name} is not supported yet.")

    dataloader = DataLoader(
        dataset,
        batch_size=1,
        shuffle=False,
        pin_memory=True,
    )

    return dataloader