seg / lisa_data /short_format.py
Lillyr's picture
upload dataset file to repo
e02d437 verified
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))