예제 #1
0
        for hypo in hypos:
            print('%s -> %s' % (hypo.name(), hyper.name()))
            hypernyms.append([hypo.index(), hyper.index()])
            nodes.insert(0, hypo)

    # save hypernym dataset
    hypernyms = np.array(hypernyms)
    import h5py
    f = h5py.File(hypernym_save_path, 'w')
    f.create_dataset('hypernyms', data=hypernyms)
    f.close()


if __name__ == '__main__':

    dataset = 'vrd'
    data_config = DatasetConfig(dataset)

    if dataset == 'vrd':
        from open_relation.dataset.vrd.label_hier.obj_hier import objnet
    else:
        from open_relation.dataset.vg.label_hier.obj_hier import objnet

    label2index = objnet.label2index()

    hypernym_save_path = os.path.join(global_config.project_root,
                                      'open_relation', 'label_embedding',
                                      'object', dataset + '_dataset',
                                      'wordnet_with_' + dataset + '.h5')
    generate_direct_hypernyms(objnet, hypernym_save_path)



def eval4(label_vecs, label2index, label1, label2):
    ind1 = label2index[label1]
    ind2=  label2index[label2]
    vec1 = label_vecs[ind1]
    vec2 = label_vecs[ind2]
    sub = vec1 - vec2
    sub[sub < 0] = 0
    sub_square = sub * sub
    E = np.sqrt(np.sum(sub_square))
    print('P( %s , %s ) = %.2f' % (label1, label2, E))



if __name__ == '__main__':
    # label vectors
    weight_path = data_config.extra_config[target].config['label_vec_path']
    label_vec_file = h5py.File(weight_path, 'r')
    label_vecs = np.array(label_vec_file['label_vec'])

    # label mapping
    label2index = classnet.label2index()
    index2label = classnet.index2label()

    eval2(label_vecs, classnet)
    # eval4(label_vecs, label2index, 'shirt', 'garment.n.01')
    # objnet.get_node_by_name('person').show_hyper_paths()