Lillyr commited on
Commit
e749b44
·
verified ·
1 Parent(s): 53fd923

upload dataset file to repo

Browse files
Files changed (1) hide show
  1. lisa_data/ref_merge.py +110 -0
lisa_data/ref_merge.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import random
3
+ from tqdm import tqdm
4
+ DEFAULT_IMAGE_TOKEN = '<image>'
5
+
6
+ SHORT_QUESTION = [
7
+ "Can you segment the {class_name} in this image?",
8
+ "Please segment the {class_name} in this image.",
9
+ "What is {class_name} in this image? Please respond with segmentation mask.",
10
+ "What is {class_name} in this image? Please output segmentation mask.",
11
+ "Could you identify and segment the {class_name} in this image?",
12
+ "Would you be able to segment the {class_name} in this image?",
13
+ "Can you provide a segmentation mask for the {class_name} in this image?",
14
+ "Please provide a segmentation mask for the {class_name} in this image.",
15
+ "Could you please segment the {class_name} in this image for me?",
16
+ "What {class_name} is present in this image? Kindly respond with a segmentation mask.",
17
+ "Which part of this image contains {class_name}? Please output with segmentation mask.",
18
+ "Is there a {class_name} in this image? If so, please provide the segmentation mask.",
19
+ "Can you segment out the {class_name} visible in this image?",
20
+ "Would you identify and provide a segmentation mask for the {class_name} in this image?",
21
+ ]
22
+
23
+ ANSWER_LIST = [
24
+ "It is [SEG].",
25
+ "Sure, [SEG].",
26
+ "Sure, it is [SEG].",
27
+ "Sure, the segmentation result is [SEG].",
28
+ "[SEG].",
29
+ ]
30
+
31
+ NO_MASK_LIST = [
32
+ "Sorry, there is no {class_name} in the image.",
33
+ "There is no {class_name} in the image.",
34
+ "It seems that there is no {class_name} in the image.",
35
+ "It appears that the image doesn't contain any {class_name}.",
36
+ "Unfortunately, I'm unable to find any {class_name} in the image.",
37
+ "I regret to inform you that there is no {class_name} detected in the image.",
38
+ "The image seems to lack any presence of {class_name}.",
39
+ "It looks like there are no {class_name} visible in the image.",
40
+ "I'm sorry, but there doesn't appear to be any {class_name} in the image.",
41
+ "It seems like the image does not include any {class_name}.",
42
+ "I'm afraid there is no sign of {class_name} in the image.",
43
+ "Regrettably, the image does not feature any {class_name}.",
44
+ "There doesn't seem to be any {class_name} found in the image.",
45
+ ]
46
+
47
+ data_list = [
48
+ 'refcoco.json',
49
+ 'refcocog.json',
50
+ 'refcoco+.json',
51
+ # 'grefcoco.json'
52
+ ]
53
+
54
+ img_dic = {}
55
+ final_data = []
56
+ json_data = json.load(open('grefcoco.json'))
57
+ for data in json_data:
58
+ img_dic[data['image']] = data
59
+
60
+ for d_ in tqdm(data_list):
61
+ json_data = json.load(open(d_))
62
+ for data in tqdm(json_data):
63
+ if data['image'] in img_dic:
64
+ for i in range(len(data['cat'])):
65
+ if data['cat'][i] in img_dic[data['image']]['cat']:
66
+ continue
67
+ img_dic[data['image']]['cat'].append(data['cat'][i])
68
+ img_dic[data['image']]['masks'].append([data['masks'][i]])
69
+
70
+ idx = 0
71
+ for k in tqdm(img_dic):
72
+ data = img_dic[k]
73
+ if len(data['masks'])!=len(data['cat']):
74
+ print('err')
75
+ indices = list(range(len(data['masks'])))
76
+
77
+ # 打乱索引
78
+ random.shuffle(indices)
79
+
80
+ data['masks'] = [data['masks'][i] for i in indices]
81
+ data['cat'] = [data['cat'][i] for i in indices]
82
+
83
+ s = 0
84
+ while s<len(data['masks']):
85
+ num = random.randint(2, 5)
86
+ dic = {}
87
+ dic['id'] = f'ref_{idx}'
88
+ idx+=1
89
+ dic['image'] = data['image']
90
+ dic['height'] = data['height']
91
+ dic['width'] = data['width']
92
+ dic['conversations'] = []
93
+ dic['masks'] = []
94
+ for i in range(num):
95
+ if i+s>=len(data['masks']):
96
+ break
97
+ dic['conversations'].append({'from': 'human', 'value': random.choice(SHORT_QUESTION).format(class_name=data['cat'][i+s].lower())})
98
+ if data['masks'][i+s] is None:
99
+ dic['conversations'].append({'from': 'gpt', 'value': random.choice(NO_MASK_LIST).format(class_name=data['cat'][i+s].lower())})
100
+ else:
101
+ dic['conversations'].append({'from': 'gpt', 'value': random.choice(ANSWER_LIST).replace('[SEG]', '[SEG]'*len(data['masks'][i+s]))})
102
+ for ann in data['masks'][i+s]:
103
+ dic['masks'].append(ann)
104
+ dic['conversations'][0]['value'] = '<image>\n' + dic['conversations'][0]['value']
105
+ s+=num
106
+ final_data.append(dic)
107
+ print(len(final_data))
108
+
109
+ with open('ref_all.json', 'w') as f:
110
+ f.write(json.dumps(final_data, indent=4))