def init_synsets(scene_graph, synset_file):
    """
    Convert synsets in a scene graph from strings to Synset objects.
    """
    syn_data = json.load(open(synset_file, 'r'))
    syn_class = {s['synset_name']: Synset(
        s['synset_name'], s['synset_definition']) for s in syn_data}

    for obj in scene_graph.objects:
        new_obj_synsets = []

        for sn in obj.synsets:
            if isinstance(sn, Synset):
                new_obj_synsets.append(sn)
            elif isinstance(sn, str):
                if sn in syn_class:
                    new_obj_synsets.append(syn_class[sn])
                else:
                    new_obj_synsets.append(Synset(sn, wn.synset(sn)))

        obj.synsets = new_obj_synsets

    for rel in scene_graph.relationships:
        rel.synset = [syn_class[sn] for sn in rel.synset]
    for attr in scene_graph.attributes:
        attr.synset = [syn_class[sn] for sn in attr.synset]

    return scene_graph
Esempio n. 2
0
def parse_synset(canon):
    """
    Helper to Extract Synset from canon object.
    """
    if len(canon) == 0:
        return None
    return Synset(canon[0]["synset_name"], canon[0]["synset_definition"])
Esempio n. 3
0
def parse_QA(data, image_map):
    """
    Helper to parse a list of question answers.
    """
    qas = []
    for info in data:
        qos = []
        aos = []
        if 'question_objects' in info:
            for qo in info['question_objects']:
                synset = Synset(qo['synset_name'], qo['synset_definition'])
                qos.append(QAObject(qo['entity_idx_start'], qo[
                           'entity_idx_end'], qo['entity_name'], synset))
        if 'answer_objects' in info:
            for ao in info['answer_objects']:
                synset = Synset(ao['synset_name'], ao['synset_definition'])
                aos.append(QAObject(ao['entity_idx_start'], ao[
                           'entity_idx_end'], ao['entity_name'], synset))
        qas.append(QA(info['qa_id'], image_map[info['image_id']],
                      info['question'], info['answer'], qos, aos))
    return qas
Esempio n. 4
0
def init_synsets(scene_graph, synset_file):
    """
    Convert synsets in a scene graph from strings to Synset objects.
    """
    syn_data = json.load(open(synset_file, "r"))
    syn_class = {
        s["synset_name"]: Synset(s["synset_name"], s["synset_definition"])
        for s in syn_data
    }

    for obj in scene_graph.objects:
        obj.synsets = [
            syn_class[sn] if sn in syn_class else Synset(sn, wn.synset(sn))
            for sn in obj.synsets
        ]
    for rel in scene_graph.relationships:
        rel.synset = [syn_class[sn] for sn in rel.synset]
    for attr in scene_graph.attributes:
        attr.synset = [syn_class[sn] for sn in attr.synset]

    return scene_graph
Esempio n. 5
0
def parse_QA(data, image_map):
    """
    Helper to parse a list of question answers.
    """
    qas = []
    for info in data:
        qos = []
        aos = []
        if "question_objects" in info:
            for qo in info["question_objects"]:
                synset = Synset(qo["synset_name"], qo["synset_definition"])
                qos.append(
                    QAObject(
                        qo["entity_idx_start"],
                        qo["entity_idx_end"],
                        qo["entity_name"],
                        synset,
                    ))
        if "answer_objects" in info:
            for ao in info["answer_objects"]:
                synset = Synset(ao["synset_name"], ao["synset_definition"])
                aos.append(
                    QAObject(
                        ao["entity_idx_start"],
                        ao["entity_idx_end"],
                        ao["entity_name"],
                        synset,
                    ))
        qas.append(
            QA(
                info["qa_id"],
                image_map[info["image_id"]],
                info["question"],
                info["answer"],
                qos,
                aos,
            ))
    return qas