upload dataset file to repo
Browse files- lisa_data/short_format.py +70 -0
lisa_data/short_format.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 32 |
+
|
| 33 |
+
json_data = json.load(open('refcoco+.json'))
|
| 34 |
+
final_data = []
|
| 35 |
+
idx = 0
|
| 36 |
+
for data in tqdm(json_data):
|
| 37 |
+
if len(data['masks'])!=len(data['cat']):
|
| 38 |
+
print('err')
|
| 39 |
+
indices = list(range(len(data['masks'])))
|
| 40 |
+
|
| 41 |
+
# 打乱索引
|
| 42 |
+
random.shuffle(indices)
|
| 43 |
+
|
| 44 |
+
data['masks'] = [data['masks'][i] for i in indices]
|
| 45 |
+
data['cat'] = [data['cat'][i] for i in indices]
|
| 46 |
+
|
| 47 |
+
s = 0
|
| 48 |
+
while s<len(data['masks']):
|
| 49 |
+
num = random.randint(2, 5)
|
| 50 |
+
dic = {}
|
| 51 |
+
dic['id'] = f'refcoco+_{idx}'
|
| 52 |
+
idx+=1
|
| 53 |
+
dic['image'] = data['image']
|
| 54 |
+
dic['height'] = data['height']
|
| 55 |
+
dic['width'] = data['width']
|
| 56 |
+
dic['conversations'] = []
|
| 57 |
+
dic['masks'] = []
|
| 58 |
+
for i in range(num):
|
| 59 |
+
if i+s>=len(data['masks']):
|
| 60 |
+
break
|
| 61 |
+
dic['conversations'].append({'from': 'human', 'value': random.choice(SHORT_QUESTION).format(class_name=data['cat'][i+s].lower())})
|
| 62 |
+
dic['conversations'].append({'from': 'gpt', 'value': random.choice(ANSWER_LIST)})
|
| 63 |
+
dic['masks'].append(data['masks'][i+s])
|
| 64 |
+
dic['conversations'][0]['value'] = '<image>\n' + dic['conversations'][0]['value']
|
| 65 |
+
s+=num
|
| 66 |
+
final_data.append(dic)
|
| 67 |
+
print(len(final_data))
|
| 68 |
+
|
| 69 |
+
with open('refcoco+_seg.json', 'w') as f:
|
| 70 |
+
f.write(json.dumps(final_data, indent=4))
|