upload dataset file to repo
Browse files- lisa_data/grefcoco.py +68 -0
lisa_data/grefcoco.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from grefer import G_REFER
|
| 3 |
+
import random
|
| 4 |
+
from pycocotools import mask as maskUtils
|
| 5 |
+
from tqdm import tqdm
|
| 6 |
+
def annToMask(mask_ann, h=None, w=None):
|
| 7 |
+
if isinstance(mask_ann, list):
|
| 8 |
+
|
| 9 |
+
rles = maskUtils.frPyObjects(mask_ann, h, w)
|
| 10 |
+
rle = maskUtils.merge(rles)
|
| 11 |
+
elif isinstance(mask_ann['counts'], list):
|
| 12 |
+
# uncompressed RLE
|
| 13 |
+
rle = maskUtils.frPyObjects(mask_ann, h, w)
|
| 14 |
+
else:
|
| 15 |
+
# rle
|
| 16 |
+
rle = mask_ann
|
| 17 |
+
mask = maskUtils.decode(rle)
|
| 18 |
+
return mask
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
refer_api = G_REFER('/mnt/workspace/workgroup/yuanyq/code/LISA/dataset/refer_seg', 'grefs', 'unc')
|
| 22 |
+
ref_ids_train = refer_api.getRefIds(split="train")
|
| 23 |
+
images_ids_train = refer_api.getImgIds(ref_ids=ref_ids_train)
|
| 24 |
+
refs_train = refer_api.loadRefs(ref_ids=ref_ids_train)
|
| 25 |
+
# loaded_images = refer_api.loadImgs(image_ids=images_ids_train)
|
| 26 |
+
annotation = refer_api.Anns
|
| 27 |
+
img2refs = {}
|
| 28 |
+
for ref in refs_train:
|
| 29 |
+
image_id = ref["image_id"]
|
| 30 |
+
img2refs[image_id] = img2refs.get(image_id, []) + [
|
| 31 |
+
ref,
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
final_data = []
|
| 35 |
+
for idx in tqdm(img2refs):
|
| 36 |
+
dic = {}
|
| 37 |
+
data = img2refs[idx]
|
| 38 |
+
img_idx = data[0]['image_id']
|
| 39 |
+
image = refer_api.loadImgs(image_ids=img_idx)[0]
|
| 40 |
+
dic['image'] = 'refer_seg/images/mscoco/images/train2014/'+image['file_name']
|
| 41 |
+
# dic['image'] = 'refer_seg/images/saiapr_tc-12/'+image['file_name']
|
| 42 |
+
dic['height']= image['height']
|
| 43 |
+
dic['width'] = image['width']
|
| 44 |
+
cats = []
|
| 45 |
+
masks = []
|
| 46 |
+
for ann in data:
|
| 47 |
+
ann_idx = ann['ann_id']
|
| 48 |
+
annotation_ = annotation[ann_idx]
|
| 49 |
+
cat_list = set()
|
| 50 |
+
for cat in ann['sentences']:
|
| 51 |
+
cat_list.add(cat['sent'])
|
| 52 |
+
cat_list = list(cat_list)
|
| 53 |
+
cats+=cat_list
|
| 54 |
+
for i in range(len(cat_list)):
|
| 55 |
+
masks.append(annotation_['segmentation'])
|
| 56 |
+
|
| 57 |
+
dic['cat'] = cats
|
| 58 |
+
dic['masks'] = masks
|
| 59 |
+
import pdb
|
| 60 |
+
pdb.set_trace()
|
| 61 |
+
final_data.append(dic)
|
| 62 |
+
|
| 63 |
+
print(len(final_data))
|
| 64 |
+
with open('./refcocog.json', 'w') as f:
|
| 65 |
+
f.write(json.dumps(final_data))
|
| 66 |
+
|
| 67 |
+
# conversations.append({'from': 'human', 'value': random.choice(SHORT_QUESTION).format(class_name=text.lower())})
|
| 68 |
+
# conversations.append({'from': 'gpt', 'value': random.choice(ANSWER_LIST)})
|