File size: 4,405 Bytes
7cdf421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os.path

from torch.utils.data import Dataset
from tqdm import tqdm
import pandas as pd
import re
import random
import numpy as np
import torch


def load_alpaca(data_path, sample_data=False, sample_numer=1000, save_dir=''):
    """
    sample and process the alpaca dataset in to the following format:
    [
        {
            "image_name": "00000000000",
            "output_modality": "text",
            "conversation": [
                {
                    "from": "human",
                    "value": "Give three tips for staying healthy.",
                    "input_modality": "text"
                },
                {
                    "from": "gpt",
                    "value": "1. Eat a balanced and nutritious diet: ...",
                    "caption": "",
                    "output_modality": "text"
                }
            ]
        },
        ...
    ]
    """
    with open(data_path, 'r') as f:
        data = json.load(f)
    print('the total instance is {}'.format(len(data)))
    if sample_data and sample_numer > 0:
        data = random.sample(data, sample_numer)
    res = []
    for d in data:
        _temp = dict()
        _temp['image_name'] = '00000000000'
        _temp['output_modality'] = 'text'
        conversation = []

        conversation.append(
            {'from': 'human',
             'value': d['instruction'] + d['input'],
             'input_modality': 'text'}
        )
        conversation.append(
            {'from': 'gpt',
             'value': d['output'],
             'caption': '',
             'output_modality': 'text'}
        )
        _temp['conversation'] = conversation
        res.append(_temp)
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    save_path = os.path.join(save_dir, os.path.basename(data_path))
    with open(save_path, 'w', encoding='utf-8') as f:
        json.dump(res, f, indent=4)
    return res


def load_llava(data_path, sample_data=False, sample_numer=1000, save_dir=''):
    """
    sample and process the llava instruction dataset into the following format:
    [
        {
            "image_name": "00000000000.jpg",
            "output_modality": "text",
            "conversation": [
                {
                    "from": "human",
                    "value": "Give three tips for staying healthy.",
                    "input_modality": "image"
                },
                {
                    "from": "gpt",
                    "value": "1. Eat a balanced and nutritious diet: ...",
                    "caption": "",
                    "output_modality": "text"
                }
            ]
        },
        ...
    ]
    """
    with open(data_path, 'r') as f:
        data = json.load(f)
    print('the total instance is {}'.format(len(data)))
    if sample_data and sample_numer > 0:
        res = random.sample(data, sample_numer)
    else:
        res = data
    # res = data
    save_path = os.path.join(save_dir, os.path.basename(data_path))
    for x in res:
        i = 0
        x['output_modality'] = 'text'
        for j in x['conversation']:
            if j['from'] == 'gpt':
                j['caption'] = ''
                j['output_modality'] = 'text'
            elif j['from'] == 'human':
                if i == 0:
                    j['input_modality'] = 'image'
                    i += 1
                else:
                    j['input_modality'] = 'text'
    with open(save_path, 'w', encoding='utf-8') as f:
        json.dump(res, f, indent=4)
    return res


def load_t2x(data_path):
    with open(data_path, 'r', encoding='utf-8') as f:
        data = json.load(f)
    return data


if __name__ == '__main__':
    save_dir = '../../data/IT_data/T+X-T_data'
    res = []

    # audios = load_t2x(os.path.join(save_dir, 'audio_t2x.json'))
    # videos = load_t2x(os.path.join(save_dir, 'video_t2x.json'))
    # images = load_t2x(os.path.join(save_dir, 'image_t2x.json'))
    # sample_number = max(len(audios), len(videos), len(images))
    #
    # print(sample_number)
    sample_number = 1000

    print('Load aplaca dataset ...')
    text = load_alpaca('../../data/IT_data/T+X-T_data/alpaca/alpaca.json', False, sample_number, save_dir)
    res.extend(text)

    print('Load llava dataset ...')
    data = load_llava('../../data/IT_data/T+X-T_data/llava/llava.json', False, sample_number, save_dir)