File size: 2,649 Bytes
e02d437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import random
from tqdm import tqdm
DEFAULT_IMAGE_TOKEN = '<image>'

SHORT_QUESTION = [
    "Can you segment the {class_name} in this image?",
    "Please segment the {class_name} in this image.",
    "What is {class_name} in this image? Please respond with segmentation mask.",
    "What is {class_name} in this image? Please output segmentation mask.",
    "Could you identify and segment the {class_name} in this image?",
    "Would you be able to segment the {class_name} in this image?",
    "Can you provide a segmentation mask for the {class_name} in this image?",
    "Please provide a segmentation mask for the {class_name} in this image.",
    "Could you please segment the {class_name} in this image for me?",
    "What {class_name} is present in this image? Kindly respond with a segmentation mask.",
    "Which part of this image contains {class_name}? Please output with segmentation mask.",
    "Is there a {class_name} in this image? If so, please provide the segmentation mask.",
    "Can you segment out the {class_name} visible in this image?",
    "Would you identify and provide a segmentation mask for the {class_name} in this image?",
]
    
ANSWER_LIST = [
    "It is [SEG].",
    "Sure, [SEG].",
    "Sure, it is [SEG].",
    "Sure, the segmentation result is [SEG].",
    "[SEG].",
]
    


json_data = json.load(open('refcoco+.json'))
final_data = []
idx = 0
for data in tqdm(json_data):
    if len(data['masks'])!=len(data['cat']):
        print('err')
    indices = list(range(len(data['masks'])))

    # 打乱索引
    random.shuffle(indices)

    data['masks'] = [data['masks'][i] for i in indices]
    data['cat'] = [data['cat'][i] for i in indices]
    
    s = 0
    while s<len(data['masks']):
        num = random.randint(2, 5)
        dic = {}
        dic['id'] = f'refcoco+_{idx}'
        idx+=1
        dic['image'] = data['image']
        dic['height'] = data['height']
        dic['width'] = data['width']
        dic['conversations'] = []
        dic['masks'] = []
        for i in range(num):
            if i+s>=len(data['masks']):
                break
            dic['conversations'].append({'from': 'human', 'value': random.choice(SHORT_QUESTION).format(class_name=data['cat'][i+s].lower())})
            dic['conversations'].append({'from': 'gpt', 'value': random.choice(ANSWER_LIST)})
            dic['masks'].append(data['masks'][i+s])
        dic['conversations'][0]['value'] = '<image>\n' + dic['conversations'][0]['value']
        s+=num
        final_data.append(dic)
print(len(final_data))

with open('refcoco+_seg.json', 'w') as f:
    f.write(json.dumps(final_data, indent=4))