in tensorflow_datasets/vision_language/refcoco/refcoco.py [0:0]
def _generate_examples(refcoco_json, dataset, dataset_partition, split):
"""Generates examples of images and its refexps & ground truth bboxes.
Args:
refcoco_json: contents of the annotation file.
dataset: str specifying the dataset (refcoco, refcoco+, refcocog)
dataset_partition: str specifying the partition for the dataset
split: str specifying the split of the dataset_partition
Yields:
image_id and example tuple
"""
refcoco_anns = refcoco_json['ref']
coco_anns = refcoco_json['coco_anns']
# Collect all referring expressions for a given image.
imageid2annref = collections.defaultdict(list)
for r in refcoco_anns:
if r['dataset'] == dataset and r[
'dataset_partition'] == dataset_partition and r['split'] == split:
imageid2annref[r['image_id']].append(r)
# Process all the referring expressions and ground truth annotations for
# a given COCO image.
for image_id in sorted(imageid2annref.keys()):
coco_image = coco_anns[str(image_id)]
image_info = coco_image['info']
example = {
'image_filename': image_info['file_name'],
'image/id': image_id,
'coco_annotations': [],
'objects': [],
}
# Collect ground truth bboxes.
for ann in sorted(coco_image['anns'], key=operator.itemgetter('id')):
example['coco_annotations'].append(_extract_annotation(ann, image_info))
# Collect referring expressions.
for r in sorted(imageid2annref[image_id],
key=operator.itemgetter('ref_id')):
obj = _extract_annotation(r['ann'], image_info)
refexp = []
for s in sorted(r['sentences'], key=operator.itemgetter('sent_id')):
refexp.append({
'raw': s['raw'],
'refexp_id': s['sent_id'],
})
# Match the referring expression to its corresponding bbox in the ground
# truth list.
gt_box_index = [
i for i, v in enumerate(example['coco_annotations'])
if v['id'] == r['ann']['id']
]
if len(gt_box_index) != 1:
raise ValueError(f'gt_box_index does not have length 1: {gt_box_index}')
gt_box_index = gt_box_index[0]
obj.update({
'refexp': refexp,
'gt_box_index': gt_box_index,
})
example['objects'].append(obj)
yield image_id, example