예제 #1
0
def vr_vphrase_make_voc_format(split_type):
    if split_type != 'train' and split_type != 'test':
        print 'error'
        exit(0)
    m = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_meta.h5')
    m_vp = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_vphrase_meta.h5')
    root = '/home/zawlin/data/data_vrd/vrd/sg_vp/'
    anno_root = root + 'Annotations/' + split_type + '/'
    data_root = root + 'Data/' + split_type + '/'
    zl.make_dirs(anno_root)
    zl.make_dirs(data_root)
    cnt = 0
    zl.tick()
    for k in m_vp['gt/%s' % split_type].keys():
        if cnt % 1000 == 0:
            print cnt, zl.tock()
            zl.tick()
        cnt += 1
        # todo for vg
        # im_data= db.image_data.find_one({'image_id':imid})
        # im_path_full = im_data['url'].replace('https://cs.stanford.edu/people/rak248/','')
        # im_path_folder = im_path_full.split('/')[0]
        # im_path_file = im_path_full.split('/')[1]
        # im_src_path = vr_root+'%s/%s'%(im_path_folder,im_path_file)
        # im_dst_path = data_root+'%s/%s'%(im_path_folder,im_path_file)
        # zl.copy_file(im_src_path,im_dst_path)
        voc_datum = {
            "folder": '',
            "source": {
                "database": "sg vrd visual phrase"
            },
            "filename": k + '.jpg'
        }
        m['train/%s/w' % k][...]
        w, h = int(m['train/%s/w' % k][...]), int(m['train/%s/h' % k][...])
        voc_datum['size'] = {'width': w, 'height': h}

        objs = []
        gt_boxes = m_vp['gt/%s/%s/boxes' % (split_type, k)][...]
        gt_labels = m_vp['gt/%s/%s/labels' % (split_type, k)][...]
        for i in xrange(gt_boxes.shape[0]):
            gt_box = gt_boxes[i]
            gt_label = gt_labels[i]
            ymin, ymax, xmin, xmax = gt_box[1], gt_box[3], gt_box[0], gt_box[2]
            bbox = {'ymin': ymin, 'ymax': ymax, 'xmin': xmin, 'xmax': xmax}
            name = zl.idx2name_tri(m_vp, gt_label)
            obj = {'name': name, 'bndbox': bbox}
            objs.append(obj)

        voc_datum['object'] = objs
        #write to xml
        dst_path = os.path.join(
            anno_root, voc_datum["folder"],
            voc_datum["filename"][:voc_datum["filename"].rfind('.')] + '.xml')
        voc_datum = {'annotation': voc_datum}
        f = open(dst_path, 'w')
        f.write(dict2xml(voc_datum) + '\n')
        f.close()
    print 'images with annotation=%d\n' % cnt
예제 #2
0
def vr_vphrase_make_voc_format(split_type):
    if split_type !='train' and split_type!='test':
        print 'error'
        exit(0)
    m = h5py.File('data/sg_vrd_meta.h5')
    m_vp = h5py.File('data/sg_vrd_vphrase_meta.h5')
    root = 'data/sg_vrd_2016_vp/'
    anno_root= root+'Annotations/'+split_type+'/'
    data_root= root+'Data/'+split_type+'/'
    zl.make_dirs(anno_root)
    zl.make_dirs(data_root)
    cnt = 0
    zl.tick()
    for k in m_vp['gt/%s'%split_type].keys():
        if cnt%1000==0:
            print cnt,zl.tock()
            zl.tick()
        cnt+=1
        voc_datum = {"folder": '',
                     "source": {"database":"sg vrd visual phrase"},
                     "filename":k+'.jpg'
                     }
        m['train/%s/w'%k][...]
        w, h = int(m['train/%s/w'%k][...]),int(m['train/%s/h'%k][...])
        voc_datum['size']={'width':w,'height':h}

        objs = []
        gt_boxes = m_vp['gt/%s/%s/boxes'%(split_type,k)][...]
        gt_labels = m_vp['gt/%s/%s/labels'%(split_type,k)][...]
        for i in xrange(gt_boxes.shape[0]):
            gt_box = gt_boxes[i]
            gt_label = gt_labels[i]
            ymin, ymax, xmin, xmax = gt_box[1],gt_box[3],gt_box[0],gt_box[2]
            bbox = {'ymin':ymin,'ymax':ymax,'xmin':xmin,'xmax':xmax}
            name = zl.idx2name_tri(m_vp,gt_label)
            obj = {'name':name,
                   'bndbox':bbox}
            objs.append(obj)

        voc_datum['object']=objs
        #write to xml
        dst_path = os.path.join(anno_root,voc_datum["folder"], voc_datum["filename"][:voc_datum["filename"].rfind('.')]+'.xml')
        voc_datum={'annotation':voc_datum}
        f = open(dst_path,'w')
        f.write(dict2xml(voc_datum)+'\n')
        f.close()
    print 'images with annotation=%d\n'%cnt
예제 #3
0
def run_test_visualize():
    caffe.set_mode_gpu()
    caffe.set_device(0)
    m = h5py.File('/home/zawlin/Dropbox/proj/vg1_2_meta.h5', 'r', 'core')
    net = caffe.Net(
        'models/vg1_2/vgg16/faster_rcnn_end2end/test.prototxt',
        'output/models/vg1_2_vgg16_faster_rcnn_finetune_iter_120000.caffemodel',
        caffe.TEST)

    net2 = caffe.Net(
        'models/vg1_2/vgg16/faster_rcnn_end2end/test.prototxt',
        'output/models/vg1_2_vgg16_faster_rcnn_finetune_iter_55000.caffemodel',
        caffe.TEST)
    # net.name = os.path.splitext(os.path.basename(args.caffemodel))[0]
    net.name = 'sgvrd'
    # if not cfg.TEST.HAS_RPN:
    # imdb.set_proposal_method(cfg.TEST.PROPOSAL_METHOD)
    """Test a Fast R-CNN network on an image database."""
    # all detections are collected into:
    #    all_boxes[cls][image] = N x 5 array of detections in
    #    (x1, y1, x2, y2, score)

    root = 'data/vg1_2_2016/Data/test/'
    cnt = 0
    cv2.namedWindow('im', 0)
    cv2.namedWindow('im_single', 0)
    images = []
    for path, subdirs, files in os.walk(root):
        for name in files:
            cnt += 1
            imid = name.split('.')[0]
            fpath = os.path.join(path, name)
            images.append(fpath)
    random.shuffle(images)
    for fpath in images:

        imid = fpath.split('/')[-1].replace('.jpg', '')
        print imid
        imorig = cv2.imread(fpath)
        dets1 = detect(imorig, net, 0.01, imid, m)
        dets2 = detect(imorig, net2, 0.01, imid, m)
        zl.sort_dict_by_key_val(dets1, 'score')
        zl.sort_dict_by_key_val(dets2, 'score')
        dets1 = dets1[::-1]
        dets2 = dets2[::-1]
        # print dets1[:4]
        # exit(0)
        cv2.namedWindow('ctrl')
        cv2.destroyWindow('ctrl')
        cv2.namedWindow('ctrl')
        cv2.createTrackbar('t1', 'ctrl', 200, 1000, nothing)
        cv2.createTrackbar('t2', 'ctrl', 200, 1000, nothing)
        cv2.createTrackbar('t1idx', 'ctrl', 0, len(dets1) - 1, nothing)
        cv2.createTrackbar('t2idx', 'ctrl', 0, len(dets2) - 1, nothing)
        while True:
            saved_idx = 0
            im = imorig.copy()
            im_single = imorig.copy()
            t1 = cv2.getTrackbarPos('t1', 'ctrl') / 1000.
            t2 = cv2.getTrackbarPos('t2', 'ctrl') / 1000.

            t1idx = cv2.getTrackbarPos('t1idx', 'ctrl')
            t2idx = cv2.getTrackbarPos('t2idx', 'ctrl')
            d1 = dets1[t1idx]
            cls_name = d1['cls_name'] + '.' + str(t1idx)
            di = [d1['x1'], d1['y1'], d1['x2'], d1['y2']]
            x, y = int(di[0]), int(di[1])
            if x < 10:
                x = 15
            if y < 10:
                y = 15
            cv2.putText(im_single, cls_name, (x, y), cv2.FONT_HERSHEY_COMPLEX,
                        1, (0, 0, 255), 1)
            cv2.rectangle(im_single, (di[0], di[1]), (di[2], di[3]),
                          (255, 0, 0), 2)

            d2 = dets2[t2idx]
            cls_name = d2['cls_name'] + '.' + str(t2idx)
            di = [d2['x1'], d2['y1'], d2['x2'], d2['y2']]
            x, y = int(di[0]), int(di[1])
            if x < 10:
                x = 15
            if y < 10:
                y = 15
            cv2.putText(im_single, cls_name, (x, y), cv2.FONT_HERSHEY_COMPLEX,
                        1, (0, 0, 255), 1)
            cv2.rectangle(im_single, (di[0], di[1]), (di[2], di[3]),
                          (0, 255, 0), 2)

            for i in xrange(len(dets1)):
                d = dets1[i]
                score = d['score']
                if score < t1: continue
                cls_name = d['cls_name'] + '.' + str(i)
                di = [d['x1'], d['y1'], d['x2'], d['y2']]

                x, y = int(di[0]), int(di[1])
                if x < 10:
                    x = 15
                if y < 10:
                    y = 15
                cv2.putText(im, cls_name, (x, y), cv2.FONT_HERSHEY_COMPLEX, 1,
                            (0, 0, 255), 1)
                cv2.rectangle(im, (di[0], di[1]), (di[2], di[3]), (255, 0, 0),
                              2)
                #print '%s %f %d %d %d %f\n' % (imid,  score, di[0], di[1], di[2], di[3])
            for i in xrange(len(dets2)):
                d = dets2[i]
                score = d['score']

                if score < t2: continue
                cls_name = d['cls_name'] + '.' + str(i)
                di = [d['x1'], d['y1'], d['x2'], d['y2']]

                x, y = int(di[0]), int(di[1])
                if x < 10:
                    x = 15
                if y < 10:
                    y = 15
                cv2.putText(im, cls_name, (x, y), cv2.FONT_HERSHEY_COMPLEX, 1,
                            (0, 0, 255), 1)
                cv2.rectangle(im, (di[0], di[1]), (di[2], di[3]), (0, 255, 0),
                              2)
                #print '%s %f %d %d %d %f\n' % (imid,  score, di[0], di[1], di[2], di[3])
            cv2.imshow('im', im)
            cv2.imshow('im_single', im_single)
            c = cv2.waitKey(100) & 0xFF
            if c == ord('s'):
                im_folder = 'output/results/examples_obj/' + imid
                zl.make_dirs(im_folder)
                if not os.path.exists('output/results/examples_obj/' + imid +
                                      '/orig_' + imid + '.jpg'):
                    cv2.imwrite(
                        'output/results/examples_obj/' + imid + '/orig_' +
                        imid + '.jpg', imorig)
                cv2.imwrite(
                    'output/results/examples_obj/' + imid + '/single_' + imid +
                    '.%d.jpg' % saved_idx, im_single)
                saved_idx += 1
            elif c == ord(' '):
                break
            elif c == 27:
                exit(0)
예제 #4
0
def vg_make_voc_format(split_type):
    if split_type != 'train' and split_type != 'test':
        print 'error'
        exit(0)
    vg_root = 'data/vg_1.2/'
    anno_root = 'data/vg_1.2/voc_format/Annotations/' + split_type + '/'
    data_root = 'data/vg_1.2/voc_format/Data/' + split_type + '/'

    zl.make_dirs(anno_root + 'VG_100K_2')
    zl.make_dirs(anno_root + 'VG_100K')
    zl.make_dirs(data_root + 'VG_100K_2')
    zl.make_dirs(data_root + 'VG_100K')
    client = MongoClient("mongodb://localhost:27017")
    db = client.visual_genome_1_2
    if split_type == 'train':
        db_objs = db.relationships_objects_train.find(no_cursor_timeout=True)
    else:
        db_objs = db.relationships_objects_test.find(no_cursor_timeout=True)

    cnt = 0
    zl.tick()
    for db_obj in db_objs:
        if cnt % 1000 == 0:
            print cnt, zl.tock()
            zl.tick()
        cnt += 1
        imid = db_obj['image_id']
        im_data = db.image_data.find_one({'image_id': imid})
        im_path_full = im_data['url'].replace(
            'https://cs.stanford.edu/people/rak248/', '')
        im_path_folder = im_path_full.split('/')[0]
        im_path_file = im_path_full.split('/')[1]
        im_src_path = vg_root + '%s/%s' % (im_path_folder, im_path_file)
        im_dst_path = data_root + '%s/%s' % (im_path_folder, im_path_file)
        zl.copy_file(im_src_path, im_dst_path)
        voc_datum = {
            "folder": im_path_folder,
            "source": {
                "database": "visual genome 1.2"
            },
            "filename": im_path_file
        }

        w, h = im_data['width'], im_data['height']
        voc_datum['size'] = {'width': w, 'height': h}

        objs = []
        for o in db_obj['objects']:
            ymin, ymax, xmin, xmax = o['y'], o['y'] + o['h'], o[
                'x'], o['x'] + o['w']
            bbox = {'ymin': ymin, 'ymax': ymax, 'xmin': xmin, 'xmax': xmax}
            obj = {'name': o['name'], 'bndbox': bbox}
            objs.append(obj)
        voc_datum['object'] = objs
        #write to xml
        dst_path = os.path.join(
            anno_root, voc_datum["folder"],
            voc_datum["filename"][:voc_datum["filename"].rfind('.')] + '.xml')
        voc_datum = {'annotation': voc_datum}
        f = open(dst_path, 'w')
        f.write(dict2xml(voc_datum) + '\n')
        f.close()
    print 'images with annotation=%d\n' % cnt
예제 #5
0
def vg_vphrase_make_voc_format(split_type):
    if split_type != 'train' and split_type != 'test':
        print 'error'
        exit(0)
    m = h5py.File('data/vg1_2_meta.h5')
    m_vp = h5py.File('data/vg1_2_vp_meta.h5')
    vg_root = 'data/vg_1.2/'
    root = 'data/vg_1.2/voc_format_vp/'
    anno_root = root + 'Annotations/' + split_type + '/'
    data_root = root + 'Data/' + split_type + '/'
    zl.make_dirs(anno_root + 'VG_100K_2')
    zl.make_dirs(anno_root + 'VG_100K')
    zl.make_dirs(data_root + 'VG_100K_2')
    zl.make_dirs(data_root + 'VG_100K')

    client = MongoClient("mongodb://localhost:27017")
    db = client.visual_genome_1_2
    imdatas = {}
    for imdata in db.image_data.find(no_cursor_timeout=True):
        imid = str(imdata['image_id'])
        imdatas[imid] = imdata
    imid2path = {}
    for k in m['meta/imid2path'].keys():
        imid2path[k] = str(m['meta/imid2path/%s' % k][...])

    cnt = 0
    zl.tick()
    for k in m_vp['gt/%s' % split_type].keys():
        if cnt % 1000 == 0:
            print cnt, zl.tock()
            zl.tick()
        cnt += 1
        # todo for vg
        im_path = imid2path[k]
        im_src_path = vg_root + im_path
        im_dst_path = data_root + im_path
        zl.copy_file(im_src_path, im_dst_path)
        voc_datum = {
            "folder": im_path.split('/')[0],
            "source": {
                "database": "sg vrd visual phrase"
            },
            "filename": im_path.split('/')[1]
        }
        #todo,remove mongodb from this processing stage
        imdata = imdatas[k]
        w, h = imdata['width'], imdata['height']
        voc_datum['size'] = {'width': w, 'height': h}

        objs = []
        gt_boxes = m_vp['gt/%s/%s/boxes' % (split_type, k)][...]
        gt_labels = m_vp['gt/%s/%s/labels' % (split_type, k)][...]
        for i in xrange(gt_boxes.shape[0]):
            gt_box = gt_boxes[i]
            gt_label = gt_labels[i]
            ymin, ymax, xmin, xmax = gt_box[1], gt_box[3], gt_box[0], gt_box[2]
            bbox = {'ymin': ymin, 'ymax': ymax, 'xmin': xmin, 'xmax': xmax}
            name = zl.idx2name_tri(m_vp, gt_label)
            obj = {'name': name, 'bndbox': bbox}
            objs.append(obj)

        voc_datum['object'] = objs
        #write to xml
        dst_path = os.path.join(
            anno_root, voc_datum["folder"],
            voc_datum["filename"][:voc_datum["filename"].rfind('.')] + '.xml')
        voc_datum = {'annotation': voc_datum}
        f = open(dst_path, 'w')
        f.write(dict2xml(voc_datum) + '\n')
        f.close()
    print 'images with annotation=%d\n' % cnt
예제 #6
0
def run_retrieval():
    h5f = h5py.File('output/vg_results/vg1_2_2016_result_all_100000.all.hdf5')
    #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')
    thresh = 0.0
    filters = []
    rois = []
    images = {}
    imids = h5f.keys()
    results = {}
    cnt = 0
    # pre = 'teddy bear_sit on_blanket'
    pre = 'cloth_next to_suitcase'
    # pre = 'paw_in front of_cat'
    # pre = 'racket_hold by_player'
    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 + '/rlp_labels'][...]
        rlp_confs = h5f[imid + '/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)]
        if ind.shape[0] == 0:
            results[imid] = 0.0
            continue
        confs = rlp_confs[ind]
        # print confs
        # print np.average(confs)
        results[imid] = np.average(confs)
    results_sorted = zl.sort_dict_by_val(results)
    example_folder = 'output/examples_retr_vg/%s/' % pre
    zl.make_dirs(example_folder)
    cnt = 0
    for imid, v in results_sorted[:20]:
        sub_boxes = h5f[imid + '/sub_boxes'][...]
        obj_boxes = h5f[imid + '/obj_boxes'][...]
        rlp_labels = h5f[imid + '/rlp_labels'][...]
        rlp_confs = h5f[imid + '/rlp_confs'][...]

        indexor = np.arange(rlp_labels.shape[0])
        ind = indexor[np.all(rlp_labels == rlp_label, axis=1)]
        if ind.shape[0] != 0:
            sub_boxes = sub_boxes[ind]
            obj_boxes = obj_boxes[ind]
            sb = sub_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
예제 #7
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
예제 #8
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
예제 #9
0
def run_retrieval_n2():
    #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('data/sg_vrd_meta.h5', 'r', 'core')
    gt_cache_path = 'output/cache/sg_vrd_gt_cache.pkl'
    gt_h5f = {}
    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]
        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.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

        ex_cnt = 0
        for i in xrange(len(results_sorted)):
            imid, v = results_sorted[i]
            if found and ex_cnt >= 100 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)
                zl.make_dirs('output/examples_vr_insufficient_gt/' + lbl_str +
                             '/')
                outpath = 'output/examples_vr_insufficient_gt/' + lbl_str + '/' + str(
                    i) + '.jpg'
                if cor: delay = 0
                im = cv2.imread(data_root + imid + '.jpg')
                if not cor and ex_cnt < 100:
                    cv2.imwrite(outpath, im)
                    ex_cnt += 1
                if delay == 0:
                    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(1)&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())