seg / lisa_data /ref_merge.py
Lillyr's picture
upload dataset file to repo
e749b44 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].",
]
NO_MASK_LIST = [
"Sorry, there is no {class_name} in the image.",
"There is no {class_name} in the image.",
"It seems that there is no {class_name} in the image.",
"It appears that the image doesn't contain any {class_name}.",
"Unfortunately, I'm unable to find any {class_name} in the image.",
"I regret to inform you that there is no {class_name} detected in the image.",
"The image seems to lack any presence of {class_name}.",
"It looks like there are no {class_name} visible in the image.",
"I'm sorry, but there doesn't appear to be any {class_name} in the image.",
"It seems like the image does not include any {class_name}.",
"I'm afraid there is no sign of {class_name} in the image.",
"Regrettably, the image does not feature any {class_name}.",
"There doesn't seem to be any {class_name} found in the image.",
]
data_list = [
'refcoco.json',
'refcocog.json',
'refcoco+.json',
# 'grefcoco.json'
]
img_dic = {}
final_data = []
json_data = json.load(open('grefcoco.json'))
for data in json_data:
img_dic[data['image']] = data
for d_ in tqdm(data_list):
json_data = json.load(open(d_))
for data in tqdm(json_data):
if data['image'] in img_dic:
for i in range(len(data['cat'])):
if data['cat'][i] in img_dic[data['image']]['cat']:
continue
img_dic[data['image']]['cat'].append(data['cat'][i])
img_dic[data['image']]['masks'].append([data['masks'][i]])
idx = 0
for k in tqdm(img_dic):
data = img_dic[k]
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'ref_{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())})
if data['masks'][i+s] is None:
dic['conversations'].append({'from': 'gpt', 'value': random.choice(NO_MASK_LIST).format(class_name=data['cat'][i+s].lower())})
else:
dic['conversations'].append({'from': 'gpt', 'value': random.choice(ANSWER_LIST).replace('[SEG]', '[SEG]'*len(data['masks'][i+s]))})
for ann in data['masks'][i+s]:
dic['masks'].append(ann)
dic['conversations'][0]['value'] = '<image>\n' + dic['conversations'][0]['value']
s+=num
final_data.append(dic)
print(len(final_data))
with open('ref_all.json', 'w') as f:
f.write(json.dumps(final_data, indent=4))