Пример #1
0
def viz_factor_graph(gm):
    """
    ut.qtensure()
    gm = build_factor_graph(G, nodes, edges , n_annots, n_names, lookup_annot_idx,
                            use_unaries=True, edge_probs=None, operator='multiplier')
    """
    ut.qtensure()
    import networkx
    from networkx.drawing.nx_agraph import graphviz_layout
    networkx.graphviz_layout = graphviz_layout
    opengm.visualizeGm(gm, show=False, layout="neato", plotUnaries=True,
                        iterations=1000, plotFunctions=True,
                        plotNonShared=False, relNodeSize=1.0)

    _ = pt.show_nx(gm.G)  # NOQA
Пример #2
0
def compare_data(Y_list_):
    import wbia

    qreq_ = wbia.testdata_qreq_(
        defaultdb='Oxford',
        a='oxford',
        p=
        'smk:nWords=[64000],nAssign=[1],SV=[False],can_match_sameimg=True,dim_size=None',
    )
    qreq_.ensure_data()

    gamma1s = []
    gamma2s = []

    logger.info(len(Y_list_))
    logger.info(len(qreq_.daids))

    dinva = qreq_.dinva
    bady = []
    for Y in Y_list_:
        aid = Y.aid
        gamma1 = Y.gamma
        if aid in dinva.aid_to_idx:
            idx = dinva.aid_to_idx[aid]
            gamma2 = dinva.gamma_list[idx]
            gamma1s.append(gamma1)
            gamma2s.append(gamma2)
        else:
            bady += [Y]
            logger.info(Y.nid)
            # logger.info(Y.qual)

    # ibs = qreq_.ibs
    # z = ibs.annots([a.aid for a in bady])

    import wbia.plottool as pt

    ut.qtensure()
    gamma1s = np.array(gamma1s)
    gamma2s = np.array(gamma2s)
    sortx = gamma1s.argsort()
    pt.plot(gamma1s[sortx], label='script')
    pt.plot(gamma2s[sortx], label='pipe')
    pt.legend()
Пример #3
0
def sift_test():
    """
    Play with SIFT equations using python so I can see and compare results.
    """
    import numpy as np
    # Sample measurement from lena
    sift_raw = np.array([
        48.0168,
        130.017,
        159.065,
        54.5727,
        63.7103,
        14.3629,
        27.0228,
        15.3527,
        40.5067,
        165.721,
        511.036,
        196.888,
        4.72748,
        8.85093,
        15.9457,
        14.4198,
        49.7571,
        209.104,
        452.047,
        223.972,
        2.66391,
        16.8975,
        21.7488,
        13.6855,
        0.700244,
        10.2518,
        312.483,
        282.647,
        1.82898,
        3.01759,
        0.448028,
        0,
        144.834,
        300.438,
        131.837,
        40.3284,
        11.1998,
        9.68647,
        7.68484,
        29.166,
        425.953,
        386.903,
        352.388,
        267.883,
        12.9652,
        18.833,
        8.55462,
        71.7924,
        112.282,
        295.512,
        678.599,
        419.405,
        21.3151,
        91.9408,
        22.8681,
        9.83749,
        3.06347,
        97.6562,
        458.799,
        221.873,
        68.1473,
        410.764,
        48.9493,
        2.01682,
        194.794,
        43.7171,
        16.2078,
        17.5604,
        48.8504,
        48.3823,
        45.7636,
        299.432,
        901.565,
        188.732,
        32.6512,
        23.6874,
        55.379,
        272.264,
        68.2334,
        221.37,
        159.631,
        44.1475,
        126.636,
        95.1978,
        74.1097,
        1353.24,
        239.319,
        33.5368,
        5.62254,
        69.0013,
        51.7629,
        9.55458,
        26.4599,
        699.623,
        208.78,
        2.09156,
        135.278,
        19.5378,
        52.0265,
        51.8445,
        49.1938,
        9.04161,
        11.6605,
        87.4498,
        604.012,
        85.6801,
        42.9738,
        75.8549,
        183.65,
        206.912,
        34.2781,
        95.0146,
        13.4201,
        83.7426,
        440.322,
        83.0038,
        125.663,
        457.333,
        52.6424,
        4.93713,
        0.38947,
        244.762,
        291.113,
        7.50165,
        8.16208,
        73.2169,
        21.9674,
        0.00429259,
    ])

    import vtool as vt

    # CONFIRMED: One normalization followed by another does not do anything
    #sift_root1 = vt.normalize(sift_root1, ord=2)
    #sift_root1 = vt.normalize(sift_root1, ord=1)

    sift_clip = sift_raw.copy()
    sift_clip = vt.normalize(sift_clip, ord=2)
    sift_clip[sift_clip > .2] = .2
    sift_clip = vt.normalize(sift_clip, ord=2)

    siff_ell2 = vt.normalize(sift_raw, ord=2)

    siff_ell1 = vt.normalize(sift_raw, ord=1)

    # Two versions of root SIFT
    # They are equlivalent
    # taken from https://hal.inria.fr/hal-00840721/PDF/RR-8325.pdf
    normalize1 = lambda x: vt.normalize(x, ord=1)  # NOQA
    normalize2 = lambda x: vt.normalize(x, ord=2)  # NOQA

    assert np.all(
        np.isclose(np.sqrt(normalize1(sift_raw)),
                   normalize2(np.sqrt(sift_raw))))

    # How do we genralize this for alpha != .5?
    # Just always L2 normalize afterwords?
    alpha = .2
    powerlaw = lambda x: np.power(x, alpha)  # NOQA
    sift_root1 = normalize2(powerlaw(normalize1(sift_raw)))
    sift_root2 = normalize2(powerlaw(sift_raw))
    flags = np.isclose(sift_root1, sift_root2)
    print(flags)
    assert np.all(flags)

    #sift_root_quant = np.clip((sift_root1 * 512), 0, 255).astype(np.uint8)
    #p = (np.bincount(sift_root_quant) / 128)
    #entropy = -np.nansum(p * np.log2(p))

    s = sift_raw[0:10]
    np.sqrt(s) / (np.sqrt(s).sum()**2)
    np.power(normalize1(s), 2)
    #b = powerlaw(normalize1(s))
    #print(np.isclose(a, b))

    np.isclose(normalize1(s), normalize1(normalize2(s)))

    # Another root SIFT version from
    # https://hal.inria.fr/hal-00688169/document
    # but this doesnt seem to work with uint8 representations
    sift_root3 = np.sqrt(sift_raw)
    sift_root3 = sift_root3 / np.sqrt(np.linalg.norm(sift_root3))

    import plottool as pt
    import utool as ut
    ut.qtensure()

    fig = pt.figure(fnum=1, pnum=None)

    def draw_sift(sift, pnum, title, **kwargs):
        ax = fig.add_subplot(*pnum)
        pt.draw_sifts(ax, sift[None, :], **kwargs)
        ax.set_xlim(-1, 1)
        ax.set_ylim(-1, 1)
        ax.grid(False)
        ax.set_aspect('equal')
        ax.set_xticks([])
        ax.set_yticks([])
        if title:
            ax.set_title(title)

    fig.clf()
    pnum_ = pt.make_pnum_nextgen(2, 4)
    draw_sift(sift_raw, pnum_(), 'raw/max(raw)', fidelity=sift_raw.max())
    draw_sift(sift_clip, pnum_(), 'clip', fidelity=1.0)
    draw_sift(siff_ell2, pnum_(), 'l2', fidelity=1.0)
    draw_sift(siff_ell1, pnum_(), 'l1', fidelity=1.0)
    draw_sift(sift_root1, pnum_(), 'root1', fidelity=1.0)
    draw_sift(sift_root2, pnum_(), 'root2', fidelity=1.0)
    draw_sift(sift_root3, pnum_(), 'root3', fidelity=2.0)
Пример #4
0
def fix_annotmatch_pzmaster1():
    """
    PZ_Master1 had annotmatch rowids that did not agree with the current name
    labeling. Looking at the inconsistencies in the graph interface was too
    cumbersome, because over 3000 annots were incorrectly grouped together.

    This function deletes any annotmatch rowid that is not consistent with the
    current labeling so we can go forward with using the new AnnotInference
    object
    """
    import wbia

    ibs = wbia.opendb('PZ_Master1')
    infr = wbia.AnnotInference(ibs=ibs, aids=ibs.get_valid_aids(), verbose=5)
    infr.initialize_graph()
    annots = ibs.annots()
    aid_to_nid = ut.dzip(annots.aids, annots.nids)

    if False:
        infr.reset_feedback()
        infr.ensure_mst()
        infr.apply_feedback_edges()
        infr.relabel_using_reviews()
        infr.start_qt_interface()

    # Get annotmatch rowids that agree with current labeling
    if False:
        annotmatch = ibs.db.get_table_as_pandas('annotmatch')
        import pandas as pd

        flags1 = pd.isnull(annotmatch['annotmatch_evidence_decision'])
        flags2 = annotmatch['annotmatch_tag_text'] == ''
        bad_part = annotmatch[flags1 & flags2]
        rowids = bad_part.index.tolist()
        ibs.delete_annotmatch(rowids)

    if False:
        # Delete bidirectional annotmatches
        annotmatch = ibs.db.get_table_as_pandas('annotmatch')
        df = annotmatch.set_index(['annot_rowid1', 'annot_rowid2'])

        # Find entires that have both directions
        pairs1 = annotmatch[['annot_rowid1', 'annot_rowid2']].values
        f_edges = {tuple(p) for p in pairs1}
        b_edges = {tuple(p[::-1]) for p in pairs1}
        isect_edges = {tuple(sorted(p)) for p in b_edges.intersection(f_edges)}
        isect_edges1 = list(isect_edges)
        isect_edges2 = [p[::-1] for p in isect_edges]

        # cols = ['annotmatch_evidence_decision', 'annotmatch_tag_text']
        import pandas as pd

        custom_ = {
            (559, 4909): (False, ['photobomb']),
            (7918, 8041): (False, ['photobomb']),
            (6634, 6754): (False, ['photobomb']),
            (3707, 3727): (False, ['photobomb']),
            (86, 103): (False, ['photobomb']),
        }
        extra_ = {}

        fixme_edges = []

        d1 = df.loc[isect_edges1].reset_index(drop=False)
        d2 = df.loc[isect_edges2].reset_index(drop=False)
        flags = d1['annotmatch_evidence_decision'] != d2[
            'annotmatch_evidence_decision']
        from wbia.tag_funcs import _parse_tags

        for f, r1, r2 in zip(flags, d1.iterrows(), d2.iterrows()):
            v1, v2 = r1[1], r2[1]
            aid1 = v1['annot_rowid1']
            aid2 = v1['annot_rowid2']
            truth_real = (ibs.const.EVIDENCE_DECISION.POSITIVE
                          if aid_to_nid[aid1] == aid_to_nid[aid2] else
                          ibs.const.EVIDENCE_DECISION.NEGATIVE)
            truth1 = v1['annotmatch_evidence_decision']
            truth2 = v2['annotmatch_evidence_decision']
            t1 = _parse_tags(v1['annotmatch_tag_text'])
            t2 = _parse_tags(v2['annotmatch_tag_text'])
            newtag = ut.union_ordered(t1, t2)
            if (aid1, aid2) in custom_:
                continue
            fixme_flag = False
            if not pd.isnull(truth1):
                if truth_real != truth1:
                    fixme_flag = True
            if not pd.isnull(truth2):
                if truth_real != truth2:
                    fixme_flag = True
            if fixme_flag:
                logger.info('newtag = %r' % (newtag, ))
                logger.info('truth_real = %r' % (truth_real, ))
                logger.info('truth1 = %r' % (truth1, ))
                logger.info('truth2 = %r' % (truth2, ))
                logger.info('aid1 = %r' % (aid1, ))
                logger.info('aid2 = %r' % (aid2, ))
                fixme_edges.append((aid1, aid2))
            else:
                extra_[(aid1, aid2)] = (truth_real, newtag)

        extra_.update(custom_)
        new_pairs = extra_.keys()
        new_truths = ut.take_column(ut.dict_take(extra_, new_pairs), 0)
        new_tags = ut.take_column(ut.dict_take(extra_, new_pairs), 1)
        new_tag_texts = [';'.join(t) for t in new_tags]
        aids1, aids2 = ut.listT(new_pairs)

        # Delete the old
        ibs.delete_annotmatch((d1['annotmatch_rowid'].values.tolist() +
                               d2['annotmatch_rowid'].values.tolist()))

        # Add the new
        ams = ibs.add_annotmatch_undirected(aids1, aids2)
        ibs.set_annotmatch_evidence_decision(ams, new_truths)
        ibs.set_annotmatch_tag_text(ams, new_tag_texts)

        if False:
            import wbia.guitool as gt

            gt.ensure_qapp()
            ut.qtensure()
            from wbia.gui import inspect_gui

            inspect_gui.show_vsone_tuner(ibs, aid1, aid2)

        # pairs2 = pairs1.T[::-1].T
        # idx1, idx2 = ut.isect_indices(list(map(tuple, pairs1)),
        #                               list(map(tuple, pairs2)))
        # r_edges = list(set(map(tuple, map(sorted, pairs1[idx1]))))
        # unique_pairs = list(set(map(tuple, map(sorted, pairs1[idx1]))))
        # df = annotmatch.set_index(['annot_rowid1', 'annot_rowid2'])

    x = ut.ddict(list)
    annotmatch = ibs.db.get_table_as_pandas('annotmatch')
    import ubelt as ub

    _iter = annotmatch.iterrows()
    prog = ub.ProgIter(_iter, length=len(annotmatch))
    for k, m in prog:
        aid1 = m['annot_rowid1']
        aid2 = m['annot_rowid2']
        if m['annotmatch_evidence_decision'] == ibs.const.EVIDENCE_DECISION.POSITIVE:
            if aid_to_nid[aid1] == aid_to_nid[aid2]:
                x['agree1'].append(k)
            else:
                x['disagree1'].append(k)
        elif m['annotmatch_evidence_decision'] == ibs.const.EVIDENCE_DECISION.NEGATIVE:
            if aid_to_nid[aid1] == aid_to_nid[aid2]:
                x['disagree2'].append(k)
            else:
                x['agree2'].append(k)

    ub.map_vals(len, x)
    ut.dict_hist(annotmatch.loc[x['disagree1']]['annotmatch_tag_text'])

    disagree1 = annotmatch.loc[x['disagree1']]
    pb_disagree1 = disagree1[disagree1['annotmatch_tag_text'] == 'photobomb']
    aids1 = pb_disagree1['annot_rowid1'].values.tolist()
    aids2 = pb_disagree1['annot_rowid2'].values.tolist()
    aid_pairs = list(zip(aids1, aids2))
    infr = wbia.AnnotInference.from_pairs(aid_pairs, ibs=ibs, verbose=5)
    if False:
        feedback = infr.read_wbia_annotmatch_feedback(edges=infr.edges())
        infr.external_feedback = feedback
        infr.apply_feedback_edges()
        infr.start_qt_interface(loop=False)

    # Delete these values
    if False:
        nonpb_disagree1 = disagree1[
            disagree1['annotmatch_tag_text'] != 'photobomb']
        disagree2 = annotmatch.loc[x['disagree2']]
        ibs.delete_annotmatch(nonpb_disagree1['annotmatch_rowid'])
        ibs.delete_annotmatch(disagree2['annotmatch_rowid'])

    # ut.dict_hist(disagree1['annotmatch_tag_text'])
    import networkx as nx

    graph = nx.Graph()
    graph.add_edges_from(
        zip(pb_disagree1['annot_rowid1'], pb_disagree1['annot_rowid2']))
    list(nx.connected_components(graph))

    set(annotmatch.loc[x['disagree2']]['annotmatch_tag_text'])
Пример #5
0
def fix_bidirectional_annotmatch(ibs):
    import wbia

    infr = wbia.AnnotInference(ibs=ibs, aids='all', verbose=5)
    infr.initialize_graph()
    annots = ibs.annots()
    aid_to_nid = ut.dzip(annots.aids, annots.nids)

    # Delete bidirectional annotmatches
    annotmatch = ibs.db.get_table_as_pandas('annotmatch')
    df = annotmatch.set_index(['annot_rowid1', 'annot_rowid2'])

    # Find entires that have both directions
    pairs1 = annotmatch[['annot_rowid1', 'annot_rowid2']].values
    f_edges = {tuple(p) for p in pairs1}
    b_edges = {tuple(p[::-1]) for p in pairs1}
    isect_edges = {tuple(sorted(p)) for p in b_edges.intersection(f_edges)}
    logger.info('Found %d bidirectional edges' % len(isect_edges))
    isect_edges1 = list(isect_edges)
    isect_edges2 = [p[::-1] for p in isect_edges]

    import pandas as pd

    extra_ = {}
    fixme_edges = []
    d1 = df.loc[isect_edges1].reset_index(drop=False)
    d2 = df.loc[isect_edges2].reset_index(drop=False)
    flags = d1['annotmatch_evidence_decision'] != d2[
        'annotmatch_evidence_decision']
    from wbia.tag_funcs import _parse_tags

    for f, r1, r2 in zip(flags, d1.iterrows(), d2.iterrows()):
        v1, v2 = r1[1], r2[1]
        aid1 = v1['annot_rowid1']
        aid2 = v1['annot_rowid2']
        truth_real = (ibs.const.EVIDENCE_DECISION.POSITIVE
                      if aid_to_nid[aid1] == aid_to_nid[aid2] else
                      ibs.const.EVIDENCE_DECISION.NEGATIVE)
        truth1 = v1['annotmatch_evidence_decision']
        truth2 = v2['annotmatch_evidence_decision']
        t1 = _parse_tags(v1['annotmatch_tag_text'])
        t2 = _parse_tags(v2['annotmatch_tag_text'])
        newtag = ut.union_ordered(t1, t2)
        fixme_flag = False
        if not pd.isnull(truth1):
            if truth_real != truth1:
                fixme_flag = True
        if not pd.isnull(truth2):
            if truth_real != truth2:
                fixme_flag = True
        if fixme_flag:
            logger.info('--')
            logger.info('t1, t2 = %r, %r' % (t1, t2))
            logger.info('newtag = %r' % (newtag, ))
            logger.info('truth_real, truth1, truth2 = %r, %r, %r' %
                        (truth_real, truth1, truth2))
            logger.info('aid1, aid2 = %r, %r' % (aid1, aid2))
            fixme_edges.append(tuple(sorted((aid1, aid2))))
        else:
            extra_[(aid1, aid2)] = (truth_real, newtag)

    if len(fixme_edges) > 0:
        # need to manually fix these edges
        fix_infr = wbia.AnnotInference.from_pairs(fixme_edges,
                                                  ibs=ibs,
                                                  verbose=5)
        feedback = fix_infr.read_wbia_annotmatch_feedback(
            only_existing_edges=True)
        infr = fix_infr

        fix_infr.external_feedback = feedback
        fix_infr.apply_feedback_edges()
        fix_infr.start_qt_interface(loop=False)
        # DELETE OLD EDGES TWICE
        ams = ibs.get_annotmatch_rowid_from_edges(fixme_edges)
        ibs.delete_annotmatch(ams)
        ams = ibs.get_annotmatch_rowid_from_edges(fixme_edges)
        ibs.delete_annotmatch(ams)

        # MANUALLY CALL THIS ONCE FINISHED
        # TO ONLY CHANGE ANNOTMATCH EDGES
        infr.write_wbia_staging_feedback()
        infr.write_wbia_annotmatch_feedback()

    # extra_.update(custom_)
    new_pairs = extra_.keys()
    new_truths = ut.take_column(ut.dict_take(extra_, new_pairs), 0)
    new_tags = ut.take_column(ut.dict_take(extra_, new_pairs), 1)
    new_tag_texts = [';'.join(t) for t in new_tags]
    aids1, aids2 = ut.listT(new_pairs)

    # Delete the old
    ibs.delete_annotmatch((d1['annotmatch_rowid'].values.tolist() +
                           d2['annotmatch_rowid'].values.tolist()))

    # Add the new
    ams = ibs.add_annotmatch_undirected(aids1, aids2)
    ibs.set_annotmatch_evidence_decision(ams, new_truths)
    ibs.set_annotmatch_tag_text(ams, new_tag_texts)

    if False:
        import wbia.guitool as gt

        gt.ensure_qapp()
        ut.qtensure()
        from wbia.gui import inspect_gui

        inspect_gui.show_vsone_tuner(ibs, aid1, aid2)