Ejemplo n.º 1
0
def vr_make_meta_gt_visual_phrase():
    m = h5py.File('data/sg_vrd_meta.h5','r',driver='core')
    h5f  = h5py.File('data/sg_vrd_vp_meta.h5')

    triplets = {}
    cnt = 0
    zl.tick()
    for k in m['gt/train'].keys():
        if cnt %1000==0:

            print cnt,zl.tock()
            zl.tick()
        cnt+=1
        gt_boxes = []
        gt_labels = []
        sub_boxes = m['gt/train/%s/sub_boxes'%k][...]
        obj_boxes = m['gt/train/%s/obj_boxes'%k][...]
        rlp_labels = m['gt/train/%s/rlp_labels'%k][...]
        for i in xrange(rlp_labels.shape[0]):
            sub_box = sub_boxes[i]
            obj_box = obj_boxes[i]
            rlp_label = rlp_labels[i]
            joint_box = [min(sub_box[0],obj_box[0]), min(sub_box[1],obj_box[1]),max(sub_box[2],obj_box[2]),max(sub_box[3],obj_box[3])]
            s_lbl = zl.idx2name_cls(m,rlp_label[0])
            o_lbl = zl.idx2name_cls(m,rlp_label[2])
            p_lbl = zl.idx2name_pre(m,rlp_label[1])
            spo = '%s_%s_%s'%(s_lbl,p_lbl,o_lbl)
            lbl = zl.name2idx_tri(h5f,spo)
            gt_boxes.append(joint_box)
            gt_labels.append(lbl)
        h5f.create_dataset('gt/train/%s/labels'%k,data = np.array(gt_labels).astype(np.int16))
        h5f.create_dataset('gt/train/%s/boxes'%k,data = np.array(gt_boxes).astype(np.int16))
Ejemplo n.º 2
0
def run_retrieval_vp():
    h5f = h5py.File('output/precalc/vg1_2_vp2016_test_new.hdf5')
    # print h5f[h5f.keys()[0]].keys()
    # exit(0)
    #h5f = h5py.file('output/results/lu_method_results.hdf5')
    data_root = '/home/zawlin/g/py-faster-rcnn/data/vg1_2_2016/Data/test/'
    m = h5py.File('/home/zawlin/Dropbox/proj/vg1_2_meta.h5', 'r', 'core')
    m_vp = h5py.File('/home/zawlin/Dropbox/proj/vg1_2_vp_meta.h5', 'r', 'core')
    thresh = 0.0
    filters = []
    rois = []
    images = {}
    imids = h5f.keys()
    results = {}
    cnt = 0
    pre = 'teddy bear_sit on_blanket'
    # pre = 'paw_in front of_cat'
    # pre = 'racket_hold by_player'
    r_idx = zl.name2idx_tri(m_vp, pre)
    # sub_lbl = pre.split('_')[0]
    # pre_lbl = pre.split('_')[1]
    # obj_lbl = pre.split('_')[2]
    # sub_idx = zl.name2idx_cls(m,sub_lbl)
    # pre_idx = zl.name2idx_pre(m,pre_lbl)
    # obj_idx = zl.name2idx_cls(m,obj_lbl)
    # rlp_label = np.array([sub_idx,pre_idx,obj_idx]).astype(np.int16)
    results = {}

    for imid in imids:
        if cnt % 100 == 0:
            print cnt, zl.tock()
            zl.tick()
        cnt += 1
        # rlp_labels = h5f[imid+'/labels'][...]
        rlp_confs = h5f[imid + '/confs'][:, r_idx]
        ind = np.argsort(rlp_confs)[::-1]
        rlp_confs = rlp_confs[ind[:5]]
        results[imid] = np.average(rlp_confs)
        if rlp_confs.shape[0] == 0:
            results[imid] = 0.0
            continue
        zl.tick()
        indexor = np.arange(rlp_labels.shape[0])
        # ind = indexor[np.all(rlp_labels==rlp_label,axis=1)]
        ind = np.where(rlp_labels == r_idx)[0]
        if ind.shape[0] == 0:
            results[imid] = 0.0
            continue
        confs = rlp_confs[ind]
        results[imid] = np.average(confs)
    results_sorted = zl.sort_dict_by_val(results)
    example_folder = 'output/examples_retr_vg_vp/%s/' % pre
    zl.make_dirs(example_folder)
    cnt = 0
    for imid, v in results_sorted[:200]:
        boxes = h5f[imid + '/boxes'][...]
        rlp_labels = h5f[imid + '/labels'][...]
        rlp_confs = h5f[imid + '/confs'][...]

        # indexor = np.arange(rlp_labels.shape[0])
        # ind = indexor[np.all(rlp_labels==rlp_label,axis=1)]
        ind = np.where(rlp_labels == r_idx)[0]
        if ind.shape[0] != 0:
            boxes = boxes[ind]
            sb = boxes[0]
            # ob = obj_boxes[0]
            im = cv2.imread(data_root + zl.imid2path(m, imid))
            cv2.rectangle(im, (sb[0], sb[1]), (sb[2], sb[3]), (0, 200, 0), 1)
            # cv2.rectangle(im,(ob[0],ob[1]),(ob[2],ob[3]),(0,0,200),1)
            cv2.imwrite(example_folder + str(cnt) + '_%s.jpg' % imid, im)
            #cv2.imshow('im',im)
            cnt += 1
Ejemplo n.º 3
0
def run_retrieval_vp_all():
    h5f = h5py.File('output/precalc/vg1_2_vp2016_test_new.hdf5', 'r')
    # print h5f[h5f.keys()[0]].keys()
    # exit(0)
    #h5f = h5py.file('output/results/lu_method_results.hdf5')
    img_set_file = 'data/vg1_2_2016/ImageSets/test.txt'
    imlist = {
        line.strip().split(' ')[1]: line.strip().split(' ')[0]
        for line in open(img_set_file)
    }
    data_root = '/home/zawlin/g/py-faster-rcnn/data/vg1_2_2016/Data/test/'
    m = h5py.File('/home/zawlin/Dropbox/proj/vg1_2_meta.h5', 'r', 'core')
    m_vp = h5py.File('/home/zawlin/Dropbox/proj/vg1_2_vp_meta.h5', 'r', 'core')
    thresh = 0.0
    filters = []
    rois = []
    images = {}
    imids = h5f.keys()
    results = {}
    cnt = 0
    # pre = 'teddy bear_sit on_blanket'
    pre = 'paw_in front of_cat'
    # pre = 'racket_hold by_player'
    r_idx = zl.name2idx_tri(m_vp, pre)
    # sub_lbl = pre.split('_')[0]
    # pre_lbl = pre.split('_')[1]
    # obj_lbl = pre.split('_')[2]
    # sub_idx = zl.name2idx_cls(m,sub_lbl)
    # pre_idx = zl.name2idx_pre(m,pre_lbl)
    # obj_idx = zl.name2idx_cls(m,obj_lbl)
    # rlp_label = np.array([sub_idx,pre_idx,obj_idx]).astype(np.int16)
    results = {}

    for imid in imids:
        # if cnt>2000:
        # break
        if cnt % 100 == 0:
            print cnt, zl.tock()
            zl.tick()
        cnt += 1
        # rlp_labels = h5f[imid+'/labels'][...]
        if imid + '/scores' not in h5f:
            print imid
            continue
            # exit(0)
        rlp_confs = h5f[imid + '/scores'][:, r_idx]
        # print np.argmax(rlp_confs)
        # exit(0)
        # ind = np.argsort(rlp_confs)[::-1]
        rlp_confs = rlp_confs[np.argmax(rlp_confs)]
        # print rlp_confs
        results[imid] = np.average(rlp_confs)
        # print np.average(rlp_confs)
    results_sorted = zl.sort_dict_by_val(results)
    example_folder = 'output/examples_retr_vg_vp/%s/' % pre
    zl.make_dirs(example_folder)
    cnt = 0
    for imid, v in results_sorted[:200]:
        if imid + '/scores' not in h5f:
            continue
        #rlp_labels = h5f[imid+'/labels'][...]
        impath = imlist[imid]
        imid_orig = impath.split('/')[1].replace('.jpg', '')
        rlp_confs = h5f[imid + '/scores'][:, r_idx]
        ind = np.argsort(rlp_confs)[::-1]
        rlp_confs = rlp_confs[ind[:1]]
        boxes = h5f[imid + '/boxes'][...][ind, :]
        # indexor = np.arange(rlp_labels.shape[0])
        # ind = indexor[np.all(rlp_labels==rlp_label,axis=1)]
        #boxes = boxes[ind]
        sb = boxes[0]
        # ob = obj_boxes[0]
        im = cv2.imread(data_root + zl.imid2path(m, imid_orig))
        cv2.rectangle(im, (sb[0], sb[1]), (sb[2], sb[3]), (0, 200, 0), 1)
        # cv2.rectangle(im,(ob[0],ob[1]),(ob[2],ob[3]),(0,0,200),1)
        cv2.imwrite(example_folder + str(cnt) + '_%s.jpg' % imid, im)
        #cv2.imshow('im',im)
        cnt += 1