示例#1
0
def convert_named_chips(db_dir, img_dpath=None):
    print('\n --- Convert Named Chips ---')
    # --- Initialize ---
    gt_format = '{}_{:d}.jpg'
    print('gt_format (name, num) = %r' % gt_format)
    if img_dpath is None:
        img_dpath = db_dir + '/images'
    print('Converting db_dir=%r and img_dpath=%r' % (db_dir, img_dpath))
    # --- Build Image Table ---
    helpers.print_('Building name table: ')
    gx2_gname = helpers.list_images(img_dpath)
    gx2_gid = range(1, len(gx2_gname) + 1)
    print('There are %d images' % len(gx2_gname))
    # ---- Build Name Table ---
    helpers.print_('Building name table: ')
    name_set = set([])
    for gx, gname in enumerate(gx2_gname):
        name, num = parse.parse(gt_format, gname)
        name_set.add(name)
    nx2_name = ['____', '____'] + list(name_set)
    nx2_nid = [1, 1] + range(2, len(name_set) + 2)
    print('There are %d names' % (len(nx2_name) - 2))
    # ---- Build Chip Table ---
    print('[converdb] Building chip table: ')
    cx2_cid = []
    cx2_theta = []
    cx2_roi = []
    cx2_nx = []
    cx2_gx = []
    cid = 1

    def add_to_hs_tables(gname, name, roi, theta=0):
        cid = len(cx2_cid) + 1
        nx = nx2_name.index(name)
        gx = gx2_gname.index(gname)
        cx2_cid.append(cid)
        cx2_roi.append(roi)
        cx2_nx.append(nx)
        cx2_gx.append(gx)
        cx2_theta.append(theta)
        return cid

    for gx, gname in enumerate(gx2_gname):
        name, num = parse.parse(gt_format, gname)
        img_fpath = join(img_dpath, gname)
        (w, h) = Image.open(img_fpath).size
        roi = [1, 1, w, h]
        cid = add_to_hs_tables(gname, name, roi)
    cx2_nid = np.array(nx2_nid)[cx2_nx]
    cx2_gid = np.array(gx2_gid)[cx2_gx]
    print('There are %d chips' % (cid - 1))

    # Write tables
    internal_dir = join(db_dir, ld2.RDIR_INTERNAL2)
    helpers.ensurepath(internal_dir)
    write_chip_table(internal_dir, cx2_cid, cx2_gid, cx2_nid, cx2_roi,
                     cx2_theta)
    write_name_table(internal_dir, nx2_nid, nx2_name)
    write_image_table(internal_dir, gx2_gid, gx2_gname)
示例#2
0
def convert_named_chips(db_dir, img_dpath=None):
    print('\n --- Convert Named Chips ---')
    # --- Initialize ---
    gt_format = '{}_{:d}.jpg'
    print('gt_format (name, num) = %r' % gt_format)
    if img_dpath is None:
        img_dpath = db_dir + '/images'
    print('Converting db_dir=%r and img_dpath=%r' % (db_dir, img_dpath))
    # --- Build Image Table ---
    helpers.print_('Building name table: ')
    gx2_gname = helpers.list_images(img_dpath)
    gx2_gid   = range(1, len(gx2_gname) + 1)
    print('There are %d images' % len(gx2_gname))
    # ---- Build Name Table ---
    helpers.print_('Building name table: ')
    name_set = set([])
    for gx, gname in enumerate(gx2_gname):
        name, num = parse.parse(gt_format, gname)
        name_set.add(name)
    nx2_name  = ['____', '____'] + list(name_set)
    nx2_nid   = [1, 1] + range(2, len(name_set) + 2)
    print('There are %d names' % (len(nx2_name) - 2))
    # ---- Build Chip Table ---
    print('[converdb] Building chip table: ')
    cx2_cid     = []
    cx2_theta   = []
    cx2_roi     = []
    cx2_nx      = []
    cx2_gx      = []
    cid = 1

    def add_to_hs_tables(gname, name, roi, theta=0):
        cid = len(cx2_cid) + 1
        nx = nx2_name.index(name)
        gx = gx2_gname.index(gname)
        cx2_cid.append(cid)
        cx2_roi.append(roi)
        cx2_nx.append(nx)
        cx2_gx.append(gx)
        cx2_theta.append(theta)
        return cid

    for gx, gname in enumerate(gx2_gname):
        name, num = parse.parse(gt_format, gname)
        img_fpath = join(img_dpath, gname)
        (w, h) = Image.open(img_fpath).size
        roi = [1, 1, w, h]
        cid = add_to_hs_tables(gname, name, roi)
    cx2_nid = np.array(nx2_nid)[cx2_nx]
    cx2_gid = np.array(gx2_gid)[cx2_gx]
    print('There are %d chips' % (cid - 1))

    # Write tables
    internal_dir = join(db_dir, ld2.RDIR_INTERNAL2)
    helpers.ensurepath(internal_dir)
    write_chip_table(internal_dir, cx2_cid, cx2_gid, cx2_nid, cx2_roi, cx2_theta)
    write_name_table(internal_dir, nx2_nid, nx2_name)
    write_image_table(internal_dir, gx2_gid, gx2_gname)
示例#3
0
def init_database_from_images(db_dir,
                              img_dpath=None,
                              gt_format=None,
                              allow_unknown_chips=False):
    # --- Initialize ---
    if img_dpath is None:
        img_dpath = db_dir + '/images'
    print('Converting db_dir=%r and img_dpath=%r' % (db_dir, img_dpath))
    gx2_gid, gx2_gname = imagetables_from_img_dpath(img_dpath)
    name_set = groundtruth_from_imagenames(gx2_gname, gt_format)
    nx2_name, nx2_nid = nametables_from_nameset(name_set)
    # ---- Build Chip Table ---
    helpers.print_('Building chip table: ')
    cx2_cid = []
    cx2_theta = []
    cx2_roi = []
    cx2_nx = []
    cx2_gx = []
    cid = 1

    def add_to_hs_tables(gname, name, roi, theta=0):
        cid = len(cx2_cid) + 1
        nx = nx2_name.index(name)
        gx = gx2_gname.index(gname)
        cx2_cid.append(cid)
        cx2_roi.append(roi)
        cx2_nx.append(nx)
        cx2_gx.append(gx)
        cx2_theta.append(theta)
        return cid

    for gx, gname in enumerate(gx2_gname):
        if gt_format is None:
            name = '____'
        else:
            name, num = parse.parse(gt_format, gname)
        if name == '____' and not allow_unknown_chips:
            continue
        img_fpath = join(img_dpath, gname)
        roi = roi_from_imgsize(img_fpath)
        if not roi is None:
            cid = add_to_hs_tables(gname, name, roi)
    cx2_nid = np.array(nx2_nid)[cx2_nx]
    cx2_gid = np.array(gx2_gid)[cx2_gx]
    print('There are %d chips' % (cid - 1))

    # Write tables
    internal_dir = join(db_dir, ld2.RDIR_INTERNAL2)
    helpers.ensurepath(internal_dir)
    write_chip_table(internal_dir, cx2_cid, cx2_gid, cx2_nid, cx2_roi,
                     cx2_theta)
    write_name_table(internal_dir, nx2_nid, nx2_name)
    write_image_table(internal_dir, gx2_gid, gx2_gname)
    return True
示例#4
0
def init_database_from_images(db_dir, img_dpath=None, gt_format=None,
                              allow_unknown_chips=False):
    # --- Initialize ---
    if img_dpath is None:
        img_dpath = db_dir + '/images'
    print('Converting db_dir=%r and img_dpath=%r' % (db_dir, img_dpath))
    gx2_gid, gx2_gname = imagetables_from_img_dpath(img_dpath)
    name_set = groundtruth_from_imagenames(gx2_gname, gt_format)
    nx2_name, nx2_nid = nametables_from_nameset(name_set)
    # ---- Build Chip Table ---
    helpers.print_('Building chip table: ')
    cx2_cid     = []
    cx2_theta   = []
    cx2_roi     = []
    cx2_nx      = []
    cx2_gx      = []
    cid = 1

    def add_to_hs_tables(gname, name, roi, theta=0):
        cid = len(cx2_cid) + 1
        nx = nx2_name.index(name)
        gx = gx2_gname.index(gname)
        cx2_cid.append(cid)
        cx2_roi.append(roi)
        cx2_nx.append(nx)
        cx2_gx.append(gx)
        cx2_theta.append(theta)
        return cid
    for gx, gname in enumerate(gx2_gname):
        if gt_format is None:
            name = '____'
        else:
            name, num = parse.parse(gt_format, gname)
        if name == '____' and not allow_unknown_chips:
            continue
        img_fpath = join(img_dpath, gname)
        roi = roi_from_imgsize(img_fpath)
        if not roi is None:
            cid = add_to_hs_tables(gname, name, roi)
    cx2_nid = np.array(nx2_nid)[cx2_nx]
    cx2_gid = np.array(gx2_gid)[cx2_gx]
    print('There are %d chips' % (cid - 1))

    # Write tables
    internal_dir      = join(db_dir, ld2.RDIR_INTERNAL2)
    helpers.ensurepath(internal_dir)
    write_chip_table(internal_dir, cx2_cid, cx2_gid, cx2_nid, cx2_roi, cx2_theta)
    write_name_table(internal_dir, nx2_nid, nx2_name)
    write_image_table(internal_dir, gx2_gid, gx2_gname)
    return True
示例#5
0
    def measure_feat_pairs(allres, orgtype='top_true'):
        print('Measure ' + orgtype + ' pairs')
        orgres = allres.__dict__[orgtype]
        entropy_list = []
        scale_list = []
        score_list = []
        lbl = 'Measuring ' + orgtype + ' pair '
        fmt_str = helpers.make_progress_fmt_str(len(orgres), lbl)
        rank_skips = []
        gt_skips = []
        for ix, (qcx, cx, score, rank) in enumerate(orgres.iter()):
            helpers.print_(fmt_str % (ix + 1,))
            # Skip low ranks
            if rank > 5:
                rank_skips.append(qcx)
                continue
            other_cxs = hs.get_other_indexed_cxs(qcx)
            # Skip no groundtruth
            if len(other_cxs) == 0:
                gt_skips.append(qcx)
                continue
            res = qcx2_res[qcx]
            # Get matching feature indexes
            fm = res.cx2_fm[cx]
            # Get their scores
            fs = res.cx2_fs[cx]
            # Get matching descriptors
            printDBG('\nfm.shape=%r' % (fm.shape,))
            desc1 = cx2_desc[qcx][fm[:, 0]]
            desc2 = cx2_desc[cx][fm[:, 1]]
            # Get matching keypoints
            kpts1 = cx2_kpts[qcx][fm[:, 0]]
            kpts2 = cx2_kpts[cx][fm[:, 1]]
            # Get their scale
            scale1_m = sv2.keypoint_scale(kpts1)
            scale2_m = sv2.keypoint_scale(kpts2)
            # Get their entropy
            entropy1 = descriptor_entropy(desc1, bw_factor=1)
            entropy2 = descriptor_entropy(desc2, bw_factor=1)
            # Append to results
            entropy_tup = np.array(zip(entropy1, entropy2))
            scale_tup   = np.array(zip(scale1_m, scale2_m))
            entropy_tup = entropy_tup.reshape(len(entropy_tup), 2)
            scale_tup   = scale_tup.reshape(len(scale_tup), 2)
            entropy_list.append(entropy_tup)
            scale_list.append(scale_tup)
            score_list.append(fs)
        print('Skipped %d total.' % (len(rank_skips) + len(gt_skips),))
        print('Skipped %d for rank > 5, %d for no gt' % (len(rank_skips), len(gt_skips),))
        print(np.unique(map(len, entropy_list)))

        def evstack(tup):
            return np.vstack(tup) if len(tup) > 0 else np.empty((0, 2))

        def ehstack(tup):
            return np.hstack(tup) if len(tup) > 0 else np.empty((0, 2))

        entropy_pairs = evstack(entropy_list)
        scale_pairs   = evstack(scale_list)
        scores        = ehstack(score_list)
        print('\n * Measured %d pairs' % len(entropy_pairs))
        return entropy_pairs, scale_pairs, scores
示例#6
0
def __akmeans_iterate(data, clusters, datax2_clusterx_old, max_iters,
                      flann_params, ave_unchanged_thresh,
                      ave_unchanged_iterwin):
    num_data = data.shape[0]
    num_clusters = clusters.shape[0]
    # Keep track of how many points have changed in each iteration
    xx2_unchanged = np.zeros(ave_unchanged_iterwin, dtype=np.int32) + len(data)
    print('[algos] Running akmeans: data.shape=%r ; num_clusters=%r' %
          (data.shape, num_clusters))
    print('[algos] * max_iters = %r ' % max_iters)
    #print('  * dtype = %r ' % params.__BOW_DTYPE__)
    print('[algos] * ave_unchanged_iterwin=%r ; ave_unchanged_thresh=%r' %
          (ave_unchanged_thresh, ave_unchanged_iterwin))
    print('[algos] Printing akmeans info in format:' +
          'time (iterx, ave(#changed), #unchanged)')
    for xx in xrange(0, max_iters):
        # 1) Find each datapoints nearest cluster center
        tt = helpers.tic()
        helpers.print_('...tic')
        helpers.flush()
        (datax2_clusterx, _dist) = ann_flann_once(clusters, data, 1,
                                                  flann_params)
        ellapsed = helpers.toc(tt)
        helpers.print_('...toc(%.2fs)' % ellapsed)
        helpers.flush()
        # 2) Find new cluster datapoints
        datax_sort = datax2_clusterx.argsort()  # NOQA
        clusterx_sort = datax2_clusterx[datax_sort]
        _L = 0
        clusterx2_dataLRx = [None for _ in xrange(num_clusters)]
        for _R in xrange(len(datax_sort) + 1):  # Slide R
            if _R == num_data or clusterx_sort[_L] != clusterx_sort[_R]:
                clusterx2_dataLRx[clusterx_sort[_L]] = (_L, _R)
                _L = _R
        # 3) Compute new cluster centers
        helpers.print_('+')
        helpers.flush()
        for clusterx, dataLRx in enumerate(clusterx2_dataLRx):
            if dataLRx is None:
                continue  # ON EMPTY CLUSTER
            (_L, _R) = dataLRx
            clusters[clusterx] = np.mean(data[datax_sort[_L:_R]], axis=0)
            #if params.__BOW_DTYPE__ == np.uint8:
            #clusters[clusterx] = np.array(np.round(clusters[clusterx]),
            # dtype=params.__BOW_DTYPE__)
            clusters[clusterx] = np.array(np.round(clusters[clusterx]),
                                          dtype=np.uint8)
        # 4) Check for convergence (no change of cluster id)
        helpers.print_('+')
        helpers.flush()
        num_changed = (datax2_clusterx_old != datax2_clusterx).sum()
        xx2_unchanged[xx % ave_unchanged_iterwin] = num_changed
        ave_unchanged = xx2_unchanged.mean()
        #(iterx, ave(#changed), #unchanged)
        helpers.print_('  (%d, %.2f, %d)\n' % (xx, ave_unchanged, num_changed))
        helpers.flush()
        if ave_unchanged < ave_unchanged_thresh:
            break
        else:  # Iterate
            datax2_clusterx_old = datax2_clusterx
            if xx % 5 == 0:
                sys.stdout.flush()
    print('[algos]  * AKMEANS: converged in %d/%d iters' % (xx + 1, max_iters))
    sys.stdout.flush()
    return (datax2_clusterx, clusters)
示例#7
0
    def measure_feat_pairs(allres, orgtype='top_true'):
        print('Measure ' + orgtype + ' pairs')
        orgres = allres.__dict__[orgtype]
        entropy_list = []
        scale_list = []
        score_list = []
        lbl = 'Measuring ' + orgtype + ' pair '
        fmt_str = helpers.make_progress_fmt_str(len(orgres), lbl)
        rank_skips = []
        gt_skips = []
        for ix, (qcx, cx, score, rank) in enumerate(orgres.iter()):
            helpers.print_(fmt_str % (ix + 1, ))
            # Skip low ranks
            if rank > 5:
                rank_skips.append(qcx)
                continue
            other_cxs = hs.get_other_indexed_cxs(qcx)
            # Skip no groundtruth
            if len(other_cxs) == 0:
                gt_skips.append(qcx)
                continue
            res = qcx2_res[qcx]
            # Get matching feature indexes
            fm = res.cx2_fm[cx]
            # Get their scores
            fs = res.cx2_fs[cx]
            # Get matching descriptors
            printDBG('\nfm.shape=%r' % (fm.shape, ))
            desc1 = cx2_desc[qcx][fm[:, 0]]
            desc2 = cx2_desc[cx][fm[:, 1]]
            # Get matching keypoints
            kpts1 = cx2_kpts[qcx][fm[:, 0]]
            kpts2 = cx2_kpts[cx][fm[:, 1]]
            # Get their scale
            scale1_m = sv2.keypoint_scale(kpts1)
            scale2_m = sv2.keypoint_scale(kpts2)
            # Get their entropy
            entropy1 = descriptor_entropy(desc1, bw_factor=1)
            entropy2 = descriptor_entropy(desc2, bw_factor=1)
            # Append to results
            entropy_tup = np.array(zip(entropy1, entropy2))
            scale_tup = np.array(zip(scale1_m, scale2_m))
            entropy_tup = entropy_tup.reshape(len(entropy_tup), 2)
            scale_tup = scale_tup.reshape(len(scale_tup), 2)
            entropy_list.append(entropy_tup)
            scale_list.append(scale_tup)
            score_list.append(fs)
        print('Skipped %d total.' % (len(rank_skips) + len(gt_skips), ))
        print('Skipped %d for rank > 5, %d for no gt' % (
            len(rank_skips),
            len(gt_skips),
        ))
        print(np.unique(map(len, entropy_list)))

        def evstack(tup):
            return np.vstack(tup) if len(tup) > 0 else np.empty((0, 2))

        def ehstack(tup):
            return np.hstack(tup) if len(tup) > 0 else np.empty((0, 2))

        entropy_pairs = evstack(entropy_list)
        scale_pairs = evstack(scale_list)
        scores = ehstack(score_list)
        print('\n * Measured %d pairs' % len(entropy_pairs))
        return entropy_pairs, scale_pairs, scores
示例#8
0
def __akmeans_iterate(data,
                      clusters,
                      datax2_clusterx_old,
                      max_iters,
                      flann_params,
                      ave_unchanged_thresh,
                      ave_unchanged_iterwin):
    num_data = data.shape[0]
    num_clusters = clusters.shape[0]
    # Keep track of how many points have changed in each iteration
    xx2_unchanged = np.zeros(ave_unchanged_iterwin, dtype=np.int32) + len(data)
    print('[algos] Running akmeans: data.shape=%r ; num_clusters=%r' %
          (data.shape, num_clusters))
    print('[algos] * max_iters = %r ' % max_iters)
    #print('  * dtype = %r ' % params.__BOW_DTYPE__)
    print('[algos] * ave_unchanged_iterwin=%r ; ave_unchanged_thresh=%r' %
          (ave_unchanged_thresh, ave_unchanged_iterwin))
    print('[algos] Printing akmeans info in format:' +
          'time (iterx, ave(#changed), #unchanged)')
    for xx in xrange(0, max_iters):
        # 1) Find each datapoints nearest cluster center
        tt = helpers.tic()
        helpers.print_('...tic')
        helpers.flush()
        (datax2_clusterx, _dist) = ann_flann_once(clusters, data, 1,
                                                  flann_params)
        ellapsed = helpers.toc(tt)
        helpers.print_('...toc(%.2fs)' % ellapsed)
        helpers.flush()
        # 2) Find new cluster datapoints
        datax_sort    = datax2_clusterx.argsort()  # NOQA
        clusterx_sort = datax2_clusterx[datax_sort]
        _L = 0
        clusterx2_dataLRx = [None for _ in xrange(num_clusters)]
        for _R in xrange(len(datax_sort) + 1):  # Slide R
            if _R == num_data or clusterx_sort[_L] != clusterx_sort[_R]:
                clusterx2_dataLRx[clusterx_sort[_L]] = (_L, _R)
                _L = _R
        # 3) Compute new cluster centers
        helpers.print_('+')
        helpers.flush()
        for clusterx, dataLRx in enumerate(clusterx2_dataLRx):
            if dataLRx is None:
                continue  # ON EMPTY CLUSTER
            (_L, _R) = dataLRx
            clusters[clusterx] = np.mean(data[datax_sort[_L:_R]], axis=0)
            #if params.__BOW_DTYPE__ == np.uint8:
            #clusters[clusterx] = np.array(np.round(clusters[clusterx]),
            # dtype=params.__BOW_DTYPE__)
            clusters[clusterx] = np.array(np.round(clusters[clusterx]),
                                          dtype=np.uint8)
        # 4) Check for convergence (no change of cluster id)
        helpers.print_('+')
        helpers.flush()
        num_changed = (datax2_clusterx_old != datax2_clusterx).sum()
        xx2_unchanged[xx % ave_unchanged_iterwin] = num_changed
        ave_unchanged = xx2_unchanged.mean()
        #(iterx, ave(#changed), #unchanged)
        helpers.print_('  (%d, %.2f, %d)\n' % (xx, ave_unchanged, num_changed))
        helpers.flush()
        if ave_unchanged < ave_unchanged_thresh:
            break
        else:  # Iterate
            datax2_clusterx_old = datax2_clusterx
            if xx % 5 == 0:
                sys.stdout.flush()
    print('[algos]  * AKMEANS: converged in %d/%d iters' % (xx + 1, max_iters))
    sys.stdout.flush()
    return (datax2_clusterx, clusters)