File size: 2,876 Bytes
550bb8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd4f334
550bb8f
 
 
bd4f334
550bb8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd4f334
550bb8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import torch
import os
import json

PROMPT='''The given video consists of multiple short clips, each showing a different segment needed to complete the task: {goal}. 
These clips are randomly shuffled, and your job is to arrange them in the correct order to complete the task: {goal}. 
The clip numbers are mentioned at the beginning of each clip as Clip 1, Clip 2, and so on.
In order to solve this task, first, you should identify the activity that is performed in each clip, and then use your reasoning and common sense to arrange these clips to successfully complete the task.

The final output should be in this format:

Correct order: <mention the Clip numbers separated by a comma>
'''

prompts=dict(
    default=PROMPT,
    rrd="",
)
valid_modes=list(prompts.keys())

class VCRBench(torch.utils.data.Dataset):
    """basic dataset for inference"""

    def __init__(
        self,
        question_file,
        video_root,
        mode='default',
        video_load_fn=None,
        load_video_kwargs={}
    ) -> None:
        super(VCRBench, self).__init__()
        self.data = json.load(open(question_file))
        self.video_load_fn = video_load_fn
        self.load_video_kwargs = load_video_kwargs
        assert mode in valid_modes, print(f"choose from: {valid_modes}")
        self.mode = mode
        self.prompt = prompts[mode]
        self.video_root = os.path.join(video_root, 'videos')

    def __len__(self) -> int:
        return len(self.data)
    
    def prepare_question(self, sample):
        if self.mode in ['default']:
            prompt=self.prompt.format(goal=sample['goal'])
        elif self.mode in ['rrd']:
            prompt=sample['goal']
        else:
            raise ValueError()
        return prompt
    
    def prepare_answer(self, sample):
        gt = [o+1 for o in sample['ground_truth']]
        return gt

    def prepare_sample(self, sample):
        video_file = os.path.join(self.video_root, sample['video_file'])
        if self.video_load_fn:
            video = self.video_load_fn(video_file, **self.load_video_kwargs)
        else:
            video = video_file

        return {'video': video, 
                'video_file': sample['video_file'],
                'qid': sample['qid'],
                'question': self.prepare_question(sample), 
                'answer': self.prepare_answer(sample)}

    def __getitem__(self, i):
        sample = self.data[i]
        return self.prepare_sample(sample)
    

if __name__=='__main__':

    dataset=VCRBench(question_file="./data.json", 
                    video_root="./",
                    mode='default', 
                    )
        
    for sample in dataset:
        print(f"question: {sample['question']}")
        print(f"video file: {sample['video_file']}")
        print(f"answer: {sample['answer']}")
        print('*'*10)

        break