Ejemplo n.º 1
0
def get_triplet_stats(split):
    m = h5py.File('/media/zawlin/ssd/Dropbox/proj/sg_vrd_meta.h5')
    triplets = {}
    triplets_idx = {}
    for imid in m['gt/%s/' % split].keys():
        rlp_labels = m['gt/%s/%s/rlp_labels' % (split, imid)][...]
        sub_boxes = m['gt/%s/%s/sub_boxes' % (split, imid)][...]
        obj_boxes = m['gt/%s/%s/obj_boxes' % (split, imid)][...]
        for i in xrange(rlp_labels.shape[0]):
            p_idx = rlp_labels[i][1]
            s_idx = rlp_labels[i][0]
            o_idx = rlp_labels[i][2]
            p_str = zl.idx2name_pre(m, p_idx)
            s_str = zl.idx2name_cls(m, s_idx)
            o_str = zl.idx2name_cls(m, o_idx)
            sb = sub_boxes[i]
            ob = obj_boxes[i]
            #triplet = str(sub_cls_idx)+'_'+str(pre_idx)+'_'+str(obj_cls_idx)
            triplet_idx = str(s_idx) + '_' + str(p_idx) + '_' + str(o_idx)
            triplet = s_str + '_' + p_str + '_' + o_str
            if triplet not in triplets:
                triplets[triplet] = 0
            if triplet_idx not in triplets_idx:
                triplets_idx[triplet_idx] = 0
            triplets[triplet] += 1
            triplets_idx[triplet_idx] += 1

    triplets_sorted = sorted(triplets.items(),
                             key=operator.itemgetter(1),
                             reverse=True)

    triplets_idx_sorted = sorted(triplets_idx.items(),
                                 key=operator.itemgetter(1),
                                 reverse=True)
    return triplets, triplets_sorted
Ejemplo n.º 2
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.º 3
0
def visualize_gt_data():
    m = h5py.File('/media/zawlin/ssd/Dropbox/proj/sg_vrd_meta.h5')
    split = 'test'
    triplets = {}
    root = 'data/sg_vrd_2016/Data/sg_%s_images/' % split
    for imid in m['gt/%s/' % split].keys():
        im_path = root + imid + '.jpg'
        im = cv2.imread(im_path)
        rlp_labels = m['gt/%s/%s/rlp_labels' % (split, imid)][...]
        sub_boxes = m['gt/%s/%s/sub_boxes' % (split, imid)][...]
        obj_boxes = m['gt/%s/%s/obj_boxes' % (split, imid)][...]
        for i in xrange(rlp_labels.shape[0]):
            p_idx = rlp_labels[i][1]
            s_idx = rlp_labels[i][0]
            o_idx = rlp_labels[i][2]
            p_str = zl.idx2name_pre(m, p_idx)
            s_str = zl.idx2name_cls(m, s_idx)
            o_str = zl.idx2name_cls(m, o_idx)
            sb = sub_boxes[i]
            ob = obj_boxes[i]
            cv2.rectangle(im, (sb[0], sb[1]), (sb[2], sb[3]), (0, 200, 0), 2)
            cv2.rectangle(im, (ob[0], ob[1]), (ob[2], ob[3]), (0, 0, 200), 2)
            #triplet = str(sub_cls_idx)+'_'+str(pre_idx)+'_'+str(obj_cls_idx)
            triplet = s_str + '_' + p_str + '_' + o_str
            print triplet
            if triplet not in triplets:
                triplets[triplet] = 0
            triplets[triplet] += 1
        cv2.imshow('im', im)
        c = cv2.waitKey(0) & 0xFF
        if c == 27:
            exit(0)
Ejemplo n.º 4
0
def vg_make_meta_visual_phrase():
    m = h5py.File('data/vg1_2_meta.h5', 'r', driver='core')

    h5f = h5py.File('data/vg1_2_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
        # 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_bbox = [min(sub_bbox[0],obj_bbox[0]), min(sub_bbox[1],obj_bbox[1]),max(sub_bbox[2],obj_bbox[2]),max(sub_bbox[3],obj_bbox[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)
            # spo = '%d_%d_%d'%(rlp_label[0],rlp_label[1],rlp_label[2])
            if spo not in triplets:
                triplets[spo] = 0
            triplets[spo] += 1
    zl.save('output/pkl/triplets_train_vp.pkl', triplets)
    triplets_sorted = zl.sort_dict_by_val(triplets)

    triplets_ok = []

    for k, v in triplets_sorted:
        triplets_ok.append(k)
        print k, v
    triplets_ok = sorted(triplets_ok)
    triplets_ok = ['__background__'] + triplets_ok
    for i in xrange(len(triplets_ok)):
        h5f['meta/tri/idx2name/%d' % i] = triplets_ok[i]
        h5f['meta/tri/name2idx/%s' % triplets_ok[i]] = i
    print len(triplets_ok)
Ejemplo n.º 5
0
def run_retrieval_vp_v2():
    # h5_path = 'output/sg_vrd_2016_result.classeme.hdf5'
    h5_path = 'output/precalc/vg1_2_vp2016_test_nms2_.4.hdf5'
    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')
    cache_path = 'output/cache/%s.pkl' % h5_path.split('/')[-1]
    data_root = '/home/zawlin/data/data_vrd/vg_1.2/'
    rlp_map = []
    for i in xrange(1, len(m_vp['meta/tri/idx2name'].keys())):
        tri = str(m_vp['meta/tri/idx2name'][str(i)][...])
        s_lbl = tri.split('_')[0]
        p_lbl = tri.split('_')[1]
        o_lbl = tri.split('_')[2]
        rlp_label = [
            zl.name2idx_cls(m, s_lbl),
            zl.name2idx_pre(m, p_lbl),
            zl.name2idx_cls(m, o_lbl)
        ]
        rlp_map.append(rlp_label)
    rlp_map = np.array(rlp_map)
    if os.path.exists(cache_path):
        print 'load from cache'
        h5f = zl.load(cache_path)
    else:
        h5_in = h5py.File(h5_path, 'r')
        h5f = {}
        print 'preloading data'
        for i in h5_in:
            h5f[i] = {}
            h5f[i]['labels'] = h5_in[i]['labels'][...]
            h5f[i]['confs'] = h5_in[i]['confs'][...]
            h5f[i]['boxes'] = h5_in[i]['boxes'][...]
        zl.save(cache_path, h5f)
        print 'preloading data done'
    retr_meta = zl.load('output/pkl/vg_retr_meta.pkl')
    thresh = 0.0
    images = {}
    imids = h5f.keys()
    results = {}
    cnt = 0
    r_acc_100 = 0
    r_acc_50 = 0

    tp_total = 0
    gt_total = 0
    median = []
    total = 0
    retr_meta = zl.load('output/pkl/vg_retr_meta.pkl')
    for k in xrange(len(retr_meta['rlp_labels'])):
        total += retr_meta['counts'][k]
        if k > 1000:
            print total / 1000.
            exit(0)
            break
        continue
        cnt += 1
        rlp_label = retr_meta['rlp_labels'][k]
        gt_files = retr_meta['files'][k]
        # print gt_files
        # exit(0)
        # for f in gt_files:
        # impath= zl.imid2path(m,f)
        # print impath
        # im= cv2.imread(data_root+impath)
        # cv2.imshow('im',im)
        # cv2.waitKey(0)
        results = {}
        zl.tick()
        ranks = []
        for imid in imids:
            labels = h5f[imid]['labels'] - 1
            rlp_confs = h5f[imid]['confs']
            rlp_labels = rlp_map[labels]
            if rlp_labels.shape[0] == 0:
                results[imid] = 0.0
                continue
            indexor = np.arange(rlp_labels.shape[0])
            ind = indexor[np.all(rlp_labels == rlp_label, axis=1)]
            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)
        total_gt = len(gt_files) + 0.0
        gt_total += total_gt + 0.0
        tp_50 = 0.
        tp_100 = 0.
        found = False
        s_lbl = zl.idx2name_cls(m, rlp_label[0])
        p_lbl = zl.idx2name_pre(m, rlp_label[1])
        o_lbl = zl.idx2name_cls(m, rlp_label[2])
        lbl_str = '%s_%s_%s' % (s_lbl, p_lbl, o_lbl)

        delay = 0
        for i in xrange(len(results_sorted)):
            imid, v = results_sorted[i]
            impath = zl.imid2path(m, imid)
            if found and i >= 5:
                break
            # print gt_files
            cor = imid in gt_files
            if cor:
                if not found:
                    found = True
                    median.append(i)
                if i < 5:
                    tp_100 += 1
                    tp_total += 1
                    if i < 50: tp_50 += 1
            # if True:
            # cor_or_not = str(cor)
            # if cor :delay=0
            # if delay ==0:
            # im = cv2.imread(data_root+impath)
            # cv2.putText(im, cor_or_not, (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1)
            # cv2.putText(im, lbl_str, (50, 80), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1)
            # cv2.putText(im, str(i), (50, 100), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1)
            # cv2.imshow('im',im)
            # c = cv2.waitKey(delay)&0xFF
            # if c ==27:
            # exit(0)
            # if c == ord('s'):
            # delay = 1-delay

            # if c == ord('c'):
            # delay = 1

        r_50 = tp_50 / 5  #total_gt
        r_100 = tp_100 / 5  #total_gt
        r_acc_50 += r_50
        r_acc_100 += r_100
        med = np.median(median)
        print '%d %f %f %f %f %d %f' % (cnt, r_50, r_100, r_acc_50 / cnt,
                                        r_acc_100 / cnt, med, zl.tock())
Ejemplo n.º 6
0
def run():
    prototxt = 'models/sg_vrd/rel_iccv/test_iccv_gt.prototxt'
    obj_detector_model='data/models/vrd_rfcn/vrd_resnet50_rfcn_iter_70000.caffemodel'
    relation_model='output/sg_vrd_rfcn/psroi_context_tri_sum_cached_iter_75500.caffemodel'
    caffe.set_mode_gpu()
    caffe.set_device(0)
    net = caffe.Net(prototxt, caffe.TEST)
    net.copy_from(obj_detector_model)
    net.copy_from(relation_model)

    m = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_meta.h5', 'r')
    cnt = 0
    root_img = '/home/zawlin/Dropbox/iccv17_hw/_results_from_zl/img/'
    root_cls = '/home/zawlin/Dropbox/iccv17_hw/_results_from_zl/cls/'
    import glog
    for k in m['gt/test'].keys():
        if not os.path.exists(root_img+k):
            os.makedirs(root_img+k)
        cnt += 1
        glog.info(cnt)
        if cnt >80:
            break
        rlp_labels = m['gt/test/%s/rlp_labels'%k][...]
        sub_boxes  = m['gt/test/%s/sub_boxes'%k][...].astype(np.float)
        obj_boxes  = m['gt/test/%s/obj_boxes'%k][...].astype(np.float)

        if sub_boxes.shape[0]>0:
            zeros = np.zeros((sub_boxes.shape[0],1), dtype=np.float)
            # first index is always zero since we do one image by one image
            sub_boxes = np.concatenate((zeros, sub_boxes),axis=1)
            obj_boxes = np.concatenate((zeros, obj_boxes),axis=1)
            im_path = C.get_sg_vrd_path_test(k)
            im = cv2.imread(im_path)
            for i in xrange(sub_boxes.shape[0]):
                # sb = sub_boxes[i][1:].astype(np.int)
                # ob = obj_boxes[i][1:].astype(np.int)
                rlp = rlp_labels[i]
                rel = zl.idx2name_cls(m,rlp[0])+' '+zl.idx2name_pre(m,rlp[1])+' '+zl.idx2name_cls(m,rlp[2])
                x1,y1,x2,y2 = union(sub_boxes[i][1:],obj_boxes[i][1:])
                cv2.rectangle(im,(x1,y1),(x2,y2),(255,0,0),2)
                cv2.putText(im,rel,(x1,y1),cv2.FONT_HERSHEY_SIMPLEX,1.0,(0,0,255),2)
                # cv2.rectangle(im,(sb[0],sb[1]),(sb[2],sb[3]),(255,0,0),2)
                # cv2.rectangle(im,(ob[0],ob[1]),(ob[2],ob[3]),(255,0,0),2)
            cv2.imshow('im',im)
            cv2.imwrite(root_img+k+'/_.orig.jpg',im)
            im_detect(net,im_path,sub_boxes,obj_boxes)
            rfcn_sub_rel = net.blobs['rfcn_sub_rel'].data[0]
            rfcn_obj_rel = net.blobs['rfcn_obj_rel'].data[0]
            rfcn_union_rel = net.blobs['rfcn_union_rel'].data[0]

            for pi in xrange(70):
                index = pi
                head, last = index * 9, (index + 1)*9
                feat_sub = rfcn_sub_rel[head:last]
                feat_obj= rfcn_obj_rel[head:last]
                feat_union= rfcn_union_rel[head:last]
                im_vis_sub = get_vis(feat_sub)
                im_vis_obj = get_vis(feat_obj)
                im_vis_union = get_vis(feat_union)

                pre = zl.idx2name_pre(m,pi)
                cv2.imwrite(root_img+k+'/%s_sub.jpg'%pre,im_vis_sub)
                cv2.imwrite(root_img+k+'/%s_obj.jpg'%pre,im_vis_obj)
                cv2.imwrite(root_img+k+'/%s_union.jpg'%pre,im_vis_union)
                if not os.path.exists(root_cls+pre):
                    os.makedirs(root_cls+pre)
                cv2.imwrite(root_cls+pre+'/%s_sub.jpg'%k,im_vis_sub)
                cv2.imwrite(root_cls+pre+'/%s_obj.jpg'%k,im_vis_obj)
                cv2.imwrite(root_cls+pre+'/%s_union.jpg'%k,im_vis_union)

                #cv2.imshow(pre+'sub',im_vis_sub)
                #cv2.imshow(pre+'obj',im_vis_obj)
                #cv2.imshow(pre+'union',im_vis_union)

                #if cv2.waitKey(0)==27:
                #    exit(0)
        else:
            #todo
            #print nothing
            pass

    cv2.waitKey(0)
Ejemplo n.º 7
0
def run_retrieval_zeroshot():
    # h5_path = 'output/sg_vrd_2016_result_all_19500.hdf5'
    # h5_path = 'output/sg_vrd_2016_result_diff_all_5000.hdf5'
    # h5_path= 'output/results/lu_method_results.hdf5'
    # h5_path = 'output/sg_vrd_2016_result.hdf5.dd'
    # h5_path = 'output/results/lu_method_results_max.hdf5'
    h5_path = 'output/results/lu_visual_method_results.hdf5'

    data_root = 'data/sg_vrd_2016/Data/sg_test_images/'
    m = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_meta.h5', 'r', 'core')
    zeroshots = m['meta/zeroshots'][...]
    gt_cache_path = 'output/cache/sg_vrd_gt_cache.pkl'
    gt_h5f = {}
    np.random.seed(76)
    if os.path.exists(gt_cache_path):
        print 'load gt from cache'
        gt_h5f = zl.load(gt_cache_path)
    else:
        print 'cacheing gt'
        for k in m['gt/test']:
            gt_h5f[k] = {}
            sub_boxes = m['gt/test'][k]['sub_boxes'][...]
            rlp_labels = m['gt/test'][k]['rlp_labels'][...]
            obj_boxes = m['gt/test'][k]['obj_boxes'][...]
            gt_h5f[k]['sub_boxes'] = sub_boxes
            gt_h5f[k]['obj_boxes'] = obj_boxes
            gt_h5f[k]['rlp_labels'] = rlp_labels
        print 'caching gt done'
        zl.save(gt_cache_path, gt_h5f)
    cache_path = 'output/cache/%s.pkl' % h5_path.split('/')[-1]
    # h5f = h5py.File('output/sg_vrd_2016_result.classeme.hdf5')
    if os.path.exists(cache_path):
        print 'load from cache'
        h5f = zl.load(cache_path)
    else:
        h5_in = h5py.File(h5_path, 'r')
        h5f = {}
        print 'preloading data'
        for i in h5_in:
            h5f[i] = {}
            h5f[i]['rlp_labels'] = h5_in[i]['rlp_labels'][...]
            h5f[i]['rlp_confs'] = h5_in[i]['rlp_confs'][...]
            h5f[i]['sub_boxes'] = h5_in[i]['sub_boxes'][...]
            h5f[i]['obj_boxes'] = h5_in[i]['obj_boxes'][...]
        zl.save(cache_path, h5f)
        print 'preloading data done'
    #h5f = h5py.file('output/results/lu_method_results.hdf5')
    retr_meta = zl.load('output/pkl/vr_retr_meta.pkl')
    thresh = 0.0
    images = {}
    imids = h5f.keys()
    results = {}
    cnt = 0
    r_acc_100 = 0
    r_acc_50 = 0

    tp_total = 0
    gt_total = 0
    median = []
    for k in xrange(len(retr_meta['rlp_labels'])):
        # if k>1000:
        # break
        rlp_label = retr_meta['rlp_labels'][k]
        if not np.any(np.all(zeroshots == rlp_label, axis=1)): continue
        gt_files = retr_meta['files'][k]

        cnt += 1
        results = {}
        zl.tic()
        for imid in imids:
            rlp_labels = h5f[imid]['rlp_labels']
            rlp_confs = h5f[imid]['rlp_confs']
            if rlp_confs.shape[0] == 0:
                results[imid] = 0.0
                continue
            indexor = np.arange(rlp_labels.shape[0])
            ind = indexor[np.all(rlp_labels == rlp_label, axis=1)]
            if ind.shape[0] == 0:
                results[imid] = 0.0
                continue
            confs = rlp_confs[ind]
            results[imid] = np.random.uniform(-1, 1)  #np.average(confs)

        results_sorted = zl.sort_dict_by_val(results)
        total_gt = len(gt_files) + 0.0
        gt_total += total_gt + 0.0
        tp_50 = 0.
        tp_100 = 0.
        found = False
        delay = 0
        s_lbl = zl.idx2name_cls(m, rlp_label[0])
        p_lbl = zl.idx2name_pre(m, rlp_label[1])
        o_lbl = zl.idx2name_cls(m, rlp_label[2])
        lbl_str = '%s_%s_%s' % (s_lbl, p_lbl, o_lbl)
        r_at_k = 5
        for i in xrange(len(results_sorted)):
            imid, v = results_sorted[i]
            if found and i >= r_at_k:
                break
            cor = imid in gt_files
            if cor:
                if not found:
                    found = True
                    median.append(i)
                if i < r_at_k:
                    tp_100 += 1
                    tp_total += 1
                    if i < 50: tp_50 += 1
            # if True:
            # cor_or_not = str(cor)
            # if cor :delay=0
            # if delay ==0:
            # im = cv2.imread(data_root+imid+'.jpg')
            # cv2.putText(im, cor_or_not, (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1)
            # cv2.putText(im, lbl_str, (50, 80), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1)
            # cv2.putText(im, str(i), (50, 100), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1)
            # cv2.imshow('im',im)
            # c = cv2.waitKey(delay)&0xFF
            # if c ==27:
            # exit(0)
            # if c == ord('s'):
            # delay = 1-delay

            # if c == ord('c'):
            # delay = 1
        r_50 = tp_50 / r_at_k  #total_gt
        r_100 = tp_100 / r_at_k  #total_gt
        r_acc_50 += r_50
        r_acc_100 += r_100
        med = np.median(median)
        print '%d %f %f %f %f %d %f' % (cnt, r_50, r_100, r_acc_50 / cnt,
                                        r_acc_100 / cnt, med, zl.toc())
Ejemplo n.º 8
0
def gen_meta_for_retrieval():
    out_pkl = 'output/pkl/vr_retr_meta.pkl'
    m = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_meta.h5', 'r')
    data_root = 'data/sg_vrd_2016/Data/sg_test_images/'

    rlp_labels = []
    files = []
    counts = []
    sub_boxes = []
    obj_boxes = []

    for k in m['gt/test']:
        gt_rlp_labels = m['gt/test'][k]['rlp_labels'][...]
        gt_sub_boxes = m['gt/test'][k]['sub_boxes'][...]
        gt_obj_boxes = m['gt/test'][k]['obj_boxes'][...]
        for i in xrange(gt_rlp_labels.shape[0]):
            gt_rlp_label = gt_rlp_labels[i]
            gt_sub_box = gt_sub_boxes[i]
            gt_obj_box = gt_obj_boxes[i]
            if len(rlp_labels) == 0:
                rlp_labels.append(gt_rlp_label.tolist())
                files.append([k])
                sub_boxes.append([gt_sub_box.tolist()])
                obj_boxes.append([gt_obj_box.tolist()])
                counts.append(1)
                continue
            bInd = np.all(gt_rlp_label == rlp_labels, axis=1)
            ind = np.arange(len(rlp_labels))[bInd]
            if len(ind) == 0:
                rlp_labels.append(gt_rlp_label.tolist())
                files.append([k])
                counts.append(1)
                sub_boxes.append([gt_sub_box.tolist()])
                obj_boxes.append([gt_obj_box.tolist()])
            else:
                files[ind].append(k)
                counts[ind] = counts[ind] + 1
                sub_boxes[ind].append(gt_sub_box.tolist())
                obj_boxes[ind].append(gt_obj_box.tolist())
                # rlp_labels.append(gt_rlp_label.tolist())
                # files.append([k])
                # counts.append(1)
    rlp_labels = np.array(rlp_labels)
    files = np.array(files)
    counts = np.array(counts)
    sub_boxes = np.array(sub_boxes)
    obj_boxes = np.array(obj_boxes)
    ind = np.argsort(counts)[::-1]

    counts = counts[ind]
    files = files[ind]
    rlp_labels = rlp_labels[ind]
    sub_boxes = sub_boxes[ind]
    obj_boxes = obj_boxes[ind]
    print sub_boxes[:4]
    for i in xrange(20):
        rlp_label = rlp_labels[i]
        print files[i]
        s_lbl = zl.idx2name_cls(m, rlp_label[0])
        p_lbl = zl.idx2name_pre(m, rlp_label[1])
        o_lbl = zl.idx2name_cls(m, rlp_label[2])
        print s_lbl, p_lbl, o_lbl
        for j in xrange(len(files[i])):
            s_box = sub_boxes[i][j]
            o_box = obj_boxes[i][j]
            fpath = files[i][j]
            impath = data_root + fpath + '.jpg'
            im = cv2.imread(impath)
            cv2.rectangle(im, (s_box[0], s_box[1]), (s_box[2], s_box[3]),
                          (0, 255, 0), 1)
            cv2.rectangle(im, (o_box[0], o_box[1]), (o_box[2], o_box[3]),
                          (255, 0, 0), 1)
            cv2.imshow('im', im)
            cv2.waitKey(0)
    retr_meta = {
        'counts': counts,
        'files': files,
        'rlp_labels': rlp_labels,
        'sub_boxes': sub_boxes,
        'obj_boxes': obj_boxes
    }
    zl.save(out_pkl, retr_meta)