def plot_keypoint_scales(hs, fnum=1): print('[dev] plot_keypoint_scales()') cx2_kpts = hs.feats.cx2_kpts if len(cx2_kpts) == 0: hs.refresh_features() cx2_kpts = hs.feats.cx2_kpts cx2_nFeats = map(len, cx2_kpts) kpts = np.vstack(cx2_kpts) print('[dev] --- LaTeX --- ') _printopts = np.get_printoptions() np.set_printoptions(precision=3) print(util_latex.latex_scalar(r'\# keypoints, ', len(kpts))) print(util_latex.latex_mystats(r'\# keypoints per image', cx2_nFeats)) scales = ktool.get_scales(kpts) scales = np.array(sorted(scales)) print(util_latex.latex_mystats(r'keypoint scale', scales)) np.set_printoptions(**_printopts) print('[dev] ---/LaTeX --- ') # df2.figure(fnum=fnum, docla=True, title='sorted scales') df2.plot(scales) df2.adjust_subplots_safe() #ax = df2.gca() #ax.set_yscale('log') #ax.set_xscale('log') # fnum += 1 df2.figure(fnum=fnum, docla=True, title='hist scales') df2.show_histogram(scales, bins=20) df2.adjust_subplots_safe() #ax = df2.gca() #ax.set_yscale('log') #ax.set_xscale('log') return fnum
def TEST_QUERY(ibs): print('[TEST_QUERY]') valid_aids = ibs.get_valid_aids() print('[TEST_QUERY] len(valid_aids)=%r' % (len(valid_aids))) qaid_list = valid_aids[0:1] print('[TEST_QUERY] len(qaid_list)=%r' % (len(qaid_list))) ibs._init_query_requestor() qreq = ibs.qreq #query_helpers.find_matchable_chips(ibs) aids = ibs.get_recognition_database_aids() qres_dict = ibs.query_all(qaid_list) for qaid in qaid_list: qres = qres_dict[qaid] top_aids = qres.get_top_aids(ibs) top_aids = utool.safe_slice(top_aids, 3) aid2 = top_aids[0] fnum = df2.next_fnum() df2.figure(fnum=fnum, doclf=True) #viz_matches.show_matches(ibs, qres, aid2, fnum=fnum, in_image=True) #viz.show_qres(ibs, qres, fnum=fnum, top_aids=top_aids, ensure=False) interact.ishow_qres(ibs, qres, fnum=fnum, top_aids=top_aids, ensure=False, annote_mode=1) df2.set_figtitle('Query Result') df2.adjust_subplots_safe(top=.8) return locals()
def TEST_QUERY_COMP(ibs): r""" CommandLine: python -m ibeis.tests.test_ibs_query_components --test-TEST_QUERY_COMP Example: >>> # ENABLE_DOCTEST >>> from ibeis.tests.test_ibs_query_components import * # NOQA >>> import ibeis >>> # build test data >>> ibs = ibeis.opendb('testdb1') >>> # execute function >>> TEST_QUERY_COMP(ibs) """ print('[TEST_QUERY_COMP]') aids = ibs.get_valid_aids() index = 0 index = utool.get_argval('--index', type_=int, default=index) qaid_list = utool.safe_slice(aids, index, index + 1) print('[TEST_QUERY_COMP] len(qaid_list)=%r' % (qaid_list)) try: comp_locals_ = query_helpers.get_query_components(ibs, qaid_list) qres_dict = OrderedDict([ ('ORIG', comp_locals_['qres_ORIG']), ('FILT', comp_locals_['qres_FILT']), ('SVER', comp_locals_['qres_SVER']), ]) top_aids = qres_dict['SVER'].get_top_aids() aid2 = top_aids[0] except Exception as ex: if 'qres_dict' in vars(): for name, qres in qres_dict.items(): print(name) print(qres.get_inspect_str()) utool.printex(ex, keys=['qaid_list'], pad_stdout=True) raise for px, (lbl, qres) in enumerate(six.iteritems(qres_dict)): print(lbl) fnum = df2.next_fnum() df2.figure(fnum=fnum, doclf=True) qres.ishow_top(ibs, fnum=fnum, top_aids=top_aids, ensure=False) df2.set_figtitle(lbl) df2.adjust_subplots_safe(top=.8) fnum = df2.next_fnum() qaid2_svtups = comp_locals_['qaid2_svtups'] qaid2_chipmatch_FILT = comp_locals_['qaid2_chipmatch_FILT'] aid1 = qaid = comp_locals_['qaid'] aid2_svtup = qaid2_svtups[aid1] chipmatch_FILT = qaid2_chipmatch_FILT[aid1] viz.show_sver(ibs, aid1, aid2, chipmatch_FILT, aid2_svtup, fnum=fnum) return locals()
def TEST_QUERY(ibs): r""" CommandLine: python -m ibeis.tests.test_ibs_query --test-TEST_QUERY python -m ibeis.tests.test_ibs_query --test-TEST_QUERY --show Example: >>> # ENABLE_DOCTEST >>> from ibeis.tests.test_ibs_query import * # NOQA >>> import plottool as pt >>> import ibeis >>> ibs = ibeis.opendb('testdb1') >>> TEST_QUERY(ibs) >>> pt.show_if_requested() """ print('[TEST_QUERY]') daid_list = ibs.get_valid_aids() print('[TEST_QUERY] len(daid_list)=%r' % (len(daid_list))) qaid_list = daid_list[0:1] print('[TEST_QUERY] len(qaid_list)=%r' % (len(qaid_list))) qres_list = ibs.query_chips(qaid_list, daid_list, use_cache=False, use_bigcache=False) qres_list_ = ibs.query_chips(qaid_list, daid_list) try: vals1 = qres_list vals2 = qres_list_ assert len(vals1) == 1, 'expected 1 qres in result' assert len(vals2) == 1, 'expected 1 qres in result' #assert list(qres_dict.keys()) == list(qres_dict_.keys()), 'qres cache doesnt work. key error' qres1 = vals1[0] qres2 = vals2[0] inspect_str1 = qres1.get_inspect_str(ibs) inspect_str2 = qres2.get_inspect_str(ibs) print(inspect_str1) assert inspect_str1 == inspect_str2, 'qres cache inconsistency' assert vals1 == vals2, 'qres cache doesnt work. val error' except AssertionError as ex: utool.printex(ex, key_list=list(locals().keys())) raise if ut.show_was_requested(): for qres in qres_list: top_aids = qres.get_top_aids() #top_aids = utool.safe_slice(top_aids, 3) aid2 = top_aids[0] fnum = df2.next_fnum() df2.figure(fnum=fnum, doclf=True) qres.ishow_top(ibs, fnum=fnum, top_aids=top_aids, ensure=False, annot_mode=1) df2.set_figtitle('Query Result') df2.adjust_subplots_safe(top=.8) return locals()
def TEST_QUERY_COMP(ibs): print('[TEST_QUERY_COMP]') qaid_list = ibs.get_valid_aids()[0:1] print('[TEST_QUERY_COMP] len(qaid_list)=%r' % (qaid_list)) ibs._init_query_requestor() qreq = ibs.qreq #query_helpers.find_matchable_chips(ibs) aids = ibs.get_recognition_database_aids() index = 0 index = utool.get_arg('--index', type_=int, default=index) qaid_list = utool.safe_slice(aids, index, index + 1) comp_locals_ = query_helpers.get_query_components(ibs, qaid_list) qres_dict = OrderedDict([ ('ORIG', comp_locals_['qres_ORIG']), ('FILT', comp_locals_['qres_FILT']), ('SVER', comp_locals_['qres_SVER']), ]) top_aids = qres_dict['SVER'].get_top_aids(ibs) top_aids = utool.safe_slice(top_aids, 3) aid2 = top_aids[0] for px, (lbl, qres) in enumerate(six.iteritems(qres_dict)): print(lbl) fnum = df2.next_fnum() df2.figure(fnum=fnum, doclf=True) #viz_matches.show_matches(ibs, qres, aid2, fnum=fnum, in_image=True) #viz.show_qres(ibs, qres, fnum=fnum, top_aids=top_aids, ensure=False) interact.ishow_qres(ibs, qres, fnum=fnum, top_aids=top_aids, ensure=False) df2.set_figtitle(lbl) df2.adjust_subplots_safe(top=.8) fnum = df2.next_fnum() qaid2_svtups = comp_locals_['qaid2_svtups'] qaid2_chipmatch_FILT = comp_locals_['qaid2_chipmatch_FILT'] aid1 = qaid = comp_locals_['qaid'] aid2_svtup = qaid2_svtups[aid1] chipmatch_FILT = qaid2_chipmatch_FILT[aid1] viz.show_sver(ibs, aid1, aid2, chipmatch_FILT, aid2_svtup, fnum=fnum) return locals()
def show_descriptors_match_distances(orgres2_distance, fnum=1, db_name='', **kwargs): disttype_list = orgres2_distance.itervalues().next().keys() orgtype_list = orgres2_distance.keys() (nRow, nCol) = len(orgtype_list), len(disttype_list) nColors = nRow * nCol color_list = df2.distinct_colors(nColors) df2.figure(fnum=fnum, docla=True, doclf=True) pnum_ = lambda px: (nRow, nCol, px + 1) plot_type = utool.get_arg('--plot-type', default='plot') # Remember min and max val for each distance type (l1, emd...) distkey2_min = {distkey: np.uint64(-1) for distkey in disttype_list} distkey2_max = {distkey: 0 for distkey in disttype_list} def _distplot(dists, color, label, distkey, plot_type=plot_type): data = sorted(dists) ax = df2.gca() min_ = distkey2_min[distkey] max_ = distkey2_max[distkey] if plot_type == 'plot': df2.plot(data, color=color, label=label, yscale='linear') #xticks = np.linspace(np.min(data), np.max(data), 3) #yticks = np.linspace(0, len(data), 5) #ax.set_xticks(xticks) #ax.set_yticks(yticks) ax.set_ylim(min_, max_) ax.set_xlim(0, len(dists)) ax.set_ylabel('distance') ax.set_xlabel('matches indexes (sorted by distance)') df2.legend(loc='lower right') if plot_type == 'pdf': df2.plot_pdf(data, color=color, label=label) ax.set_ylabel('pr') ax.set_xlabel('distance') ax.set_xlim(min_, max_) df2.legend(loc='upper left') df2.dark_background(ax) df2.small_xticks(ax) df2.small_yticks(ax) px = 0 for orgkey in orgtype_list: for distkey in disttype_list: dists = orgres2_distance[orgkey][distkey] if len(dists) == 0: continue min_ = dists.min() max_ = dists.max() distkey2_min[distkey] = min(distkey2_min[distkey], min_) distkey2_max[distkey] = max(distkey2_max[distkey], max_) for count, orgkey in enumerate(orgtype_list): for distkey in disttype_list: printDBG('[allres-viz] plotting: %r' % ((orgkey, distkey),)) dists = orgres2_distance[orgkey][distkey] df2.figure(fnum=fnum, pnum=pnum_(px)) color = color_list[px] title = distkey + ' ' + orgkey label = 'P(%s | %s)' % (distkey, orgkey) _distplot(dists, color, label, distkey, **kwargs) if count == 0: ax = df2.gca() ax.set_title(distkey) px += 1 subtitle = 'the matching distances between sift descriptors' title = '(sift) matching distances' if db_name != '': title = db_name + ' ' + title df2.set_figtitle(title, subtitle) df2.adjust_subplots_safe()
def show_nearest_descriptors(ibs, qaid, qfx, fnum=None, stride=5, qreq_=None, **kwargs): r""" Args: ibs (ibeis.IBEISController): image analysis api qaid (int): query annotation id qfx (int): query feature index fnum (int): figure number stride (int): consecutive_distance_compare (bool): CommandLine: # Find a good match to inspect python -m ibeis.viz.interact.interact_matches --test-testdata_match_interact --show --db PZ_MTEST --qaid 3 # Now inspect it python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --show --db PZ_MTEST --qaid 3 --qfx 879 python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --show python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --db PZ_MTEST --qaid 3 --qfx 879 --diskshow --save foo.png --dpi=256 SeeAlso: plottool.viz_featrow ~/code/plottool/plottool/viz_featrow.py Example: >>> # DISABLE_DOCTEST >>> from ibeis.viz.viz_nearest_descriptors import * # NOQA >>> import ibeis >>> # build test data >>> qreq_ = ibeis.testdata_qreq_() >>> ibs = ibeis.opendb('PZ_MTEST') >>> qaid = qreq_.qaids[0] >>> qfx = ut.get_argval('--qfx', type_=int, default=879) >>> fnum = None >>> stride = 5 >>> # execute function >>> skip = False >>> result = show_nearest_descriptors(ibs, qaid, qfx, fnum, stride, >>> draw_chip=True, >>> draw_warped=True, >>> draw_unwarped=False, >>> draw_desc=False, qreq_=qreq_) >>> # verify results >>> print(result) >>> pt.show_if_requested() """ consecutive_distance_compare = True draw_chip = kwargs.get('draw_chip', False) draw_desc = kwargs.get('draw_desc', True) draw_warped = kwargs.get('draw_warped', True) draw_unwarped = kwargs.get('draw_unwarped', True) #skip = kwargs.get('skip', True) # Plots the nearest neighbors of a given feature (qaid, qfx) if fnum is None: fnum = df2.next_fnum() try: # Flann NN query (qfx2_daid, qfx2_dfx, qfx2_dist, K, Knorm) = get_annotfeat_nn_index(ibs, qaid, qfx, qreq_=qreq_) # Adds metadata to a feature match def get_extract_tuple(aid, fx, k=-1): rchip = ibs.get_annot_chips(aid) kp = ibs.get_annot_kpts(aid)[fx] sift = ibs.get_annot_vecs(aid)[fx] if not ut.get_argflag('--texknormplot'): aidstr = vh.get_aidstrs(aid) nidstr = vh.get_nidstrs(ibs.get_annot_nids(aid)) id_str = ' ' + aidstr + ' ' + nidstr + ' fx=%r' % (fx,) else: id_str = nidstr = aidstr = '' info = '' if k == -1: if pt.is_texmode(): info = '\\vspace{1cm}' info += 'Query $\\mathbf{d}_i$' info += '\n\\_' info += '\n\\_' else: if len(id_str) > '': info = 'Query: %s' % (id_str,) else: info = 'Query' type_ = 'Query' elif k < K: type_ = 'Match' if ut.get_argflag('--texknormplot') and pt.is_texmode(): #info = 'Match:\n$k=%r$, $\\frac{||\\mathbf{d}_i - \\mathbf{d}_j||}{Z}=%.3f$' % (k, qfx2_dist[0, k]) info = '\\vspace{1cm}' info += 'Match: $\\mathbf{d}_{j_%r}$\n$\\textrm{dist}=%.3f$' % (k, qfx2_dist[0, k]) info += '\n$s_{\\tt{LNBNN}}=%.3f$' % (qfx2_dist[0, K + Knorm - 1] - qfx2_dist[0, k]) else: info = 'Match:%s\nk=%r, dist=%.3f' % (id_str, k, qfx2_dist[0, k]) info += '\nLNBNN=%.3f' % (qfx2_dist[0, K + Knorm - 1] - qfx2_dist[0, k]) elif k < Knorm + K: type_ = 'Norm' if ut.get_argflag('--texknormplot') and pt.is_texmode(): #info = 'Norm: $j_%r$\ndist=%.3f' % (id_str, k, qfx2_dist[0, k]) info = '\\vspace{1cm}' info += 'Norm: $j_%r$\n$\\textrm{dist}=%.3f$' % (k, qfx2_dist[0, k]) info += '\n\\_' else: info = 'Norm: %s\n$k=%r$, dist=$%.3f$' % (id_str, k, qfx2_dist[0, k]) else: raise Exception('[viz] problem k=%r') return (rchip, kp, sift, fx, aid, info, type_) extracted_list = [] # Remember the query sift feature extracted_list.append(get_extract_tuple(qaid, qfx, -1)) origsift = extracted_list[0][2] skipped = 0 for k in range(K + Knorm): #if qfx2_daid[0, k] == qaid and qfx2_dfx[0, k] == qfx: if qfx2_daid[0, k] == qaid: skipped += 1 continue tup = get_extract_tuple(qfx2_daid[0, k], qfx2_dfx[0, k], k) extracted_list.append(tup) # Draw the _select_ith_match plot nRows = len(extracted_list) if stride is None: stride = nRows # Draw selected feature matches prevsift = None px = 0 # plot offset px_shift = 0 # plot stride shift nExtracted = len(extracted_list) featrow_kw = dict( draw_chip=draw_chip, draw_desc=draw_desc, draw_warped=draw_warped, draw_unwarped=draw_unwarped, ) if ut.get_argflag('--texknormplot'): featrow_kw['ell_color'] = pt.ORANGE pass for listx, tup in enumerate(extracted_list): (rchip, kp, sift, fx, aid, info, type_) = tup if listx % stride == 0: # Create a temporary nRows and fnum in case we are splitting # up nearest neighbors into separate figures with stride _fnum = fnum + listx _nRows = min(nExtracted - listx, stride) px_shift = px df2.figure(fnum=_fnum, docla=True, doclf=True) px_ = px - px_shift px = draw_feat_row(rchip, fx, kp, sift, _fnum, _nRows, px=px_, prevsift=prevsift, origsift=origsift, aid=aid, info=info, type_=type_, **featrow_kw) px += px_shift if prevsift is None or consecutive_distance_compare: prevsift = sift df2.adjust_subplots_safe(hspace=.85, wspace=0, top=.95, bottom=.087, left=.05, right=.95) except Exception as ex: print('[viz] Error in show nearest descriptors') print(ex) raise
def show_qres(ibs, qres, **kwargs): """ Display Query Result Logic Defaults to: query chip, groundtruth matches, and top 5 matches """ annote_mode = kwargs.get('annote_mode', 1) % 3 # this is toggled figtitle = kwargs.get('figtitle', '') aug = kwargs.get('aug', '') top_aids = kwargs.get('top_aids', 6) gt_aids = kwargs.get('gt_aids', []) all_kpts = kwargs.get('all_kpts', False) show_query = kwargs.get('show_query', False) in_image = kwargs.get('in_image', False) sidebyside = kwargs.get('sidebyside', True) fnum = df2.kwargs_fnum(kwargs) fig = df2.figure(fnum=fnum, docla=True, doclf=True) if isinstance(top_aids, int): top_aids = qres.get_top_aids(num=top_aids) all_gts = ibs.get_annot_groundtruth(qres.qaid) nTop = len(top_aids) nSelGt = len(gt_aids) nAllGt = len(all_gts) max_nCols = 5 if nTop in [6, 7]: max_nCols = 3 if nTop in [8]: max_nCols = 4 printDBG('[show_qres]========================') printDBG('[show_qres]----------------') printDBG('[show_qres] #nTop=%r #missed_gts=%r/%r' % (nTop, nSelGt, nAllGt)) printDBG('[show_qres] * fnum=%r' % (fnum,)) printDBG('[show_qres] * figtitle=%r' % (figtitle,)) printDBG('[show_qres] * max_nCols=%r' % (max_nCols,)) printDBG('[show_qres] * show_query=%r' % (show_query,)) printDBG('[show_qres] * kwargs=%s' % (utool.dict_str(kwargs),)) printDBG(qres.get_inspect_str()) ranked_aids = qres.get_top_aids() # Build a subplot grid nQuerySubplts = 1 if show_query else 0 nGtSubplts = nQuerySubplts + (0 if gt_aids is None else len(gt_aids)) nTopNSubplts = nTop nTopNCols = min(max_nCols, nTopNSubplts) nGTCols = min(max_nCols, nGtSubplts) nGTCols = max(nGTCols, nTopNCols) nTopNCols = nGTCols nGtRows = 0 if nGTCols == 0 else int(np.ceil(nGtSubplts / nGTCols)) nTopNRows = 0 if nTopNCols == 0 else int(np.ceil(nTopNSubplts / nTopNCols)) nGtCells = nGtRows * nGTCols nRows = nTopNRows + nGtRows # HACK: _color_list = df2.distinct_colors(nTop) aid2_color = {aid: _color_list[ox] for ox, aid in enumerate(top_aids)} # Helpers def _show_query_fn(plotx_shift, rowcols): """ helper for show_qres """ plotx = plotx_shift + 1 pnum = (rowcols[0], rowcols[1], plotx) #print('[viz] Plotting Query: pnum=%r' % (pnum,)) _kwshow = dict(draw_kpts=annote_mode) _kwshow.update(kwargs) _kwshow['prefix'] = 'q' _kwshow['pnum'] = pnum _kwshow['aid2_color'] = aid2_color _kwshow['draw_ell'] = annote_mode >= 1 viz_chip.show_chip(ibs, qres.qaid, annote=False, **_kwshow) def _plot_matches_aids(aid_list, plotx_shift, rowcols): """ helper for show_qres to draw many aids """ def _show_matches_fn(aid, orank, pnum): """ Helper function for drawing matches to one aid """ aug = 'rank=%r\n' % orank #printDBG('[show_qres()] plotting: %r' % (pnum,)) _kwshow = dict(draw_ell=annote_mode, draw_pts=False, draw_lines=annote_mode, ell_alpha=.5, all_kpts=all_kpts) _kwshow.update(kwargs) _kwshow['fnum'] = fnum _kwshow['pnum'] = pnum _kwshow['title_aug'] = aug # If we already are showing the query dont show it here if sidebyside: _kwshow['draw_ell'] = annote_mode == 1 _kwshow['draw_lines'] = annote_mode >= 1 viz_matches.show_matches(ibs, qres, aid, in_image=in_image, **_kwshow) else: _kwshow['draw_ell'] = annote_mode >= 1 if annote_mode == 2: # TODO Find a better name _kwshow['color'] = aid2_color[aid] _kwshow['sel_fx2'] = qres.aid2_fm[aid][:, 1] viz_chip.show_chip(ibs, aid, in_image=in_image, annote=False, **_kwshow) viz_matches.annotate_matches(ibs, qres, aid, in_image=in_image, show_query=not show_query) printDBG('[show_qres()] Plotting Chips %s:' % vh.get_aidstrs(aid_list)) if aid_list is None: return # Do lazy load before show vh.get_chips(ibs, aid_list, **kwargs) vh.get_kpts(ibs, aid_list, **kwargs) for ox, aid in enumerate(aid_list): plotx = ox + plotx_shift + 1 pnum = (rowcols[0], rowcols[1], plotx) oranks = np.where(ranked_aids == aid)[0] if len(oranks) == 0: orank = -1 continue orank = oranks[0] + 1 _show_matches_fn(aid, orank, pnum) shift_topN = nGtCells if nGtSubplts == 1: nGTCols = 1 if nRows == 0: df2.imshow_null(fnum=fnum) else: fig = df2.figure(fnum=fnum, pnum=(nRows, nGTCols, 1), docla=True, doclf=True) #df2.disconnect_callback(fig, 'button_press_event') df2.plt.subplot(nRows, nGTCols, 1) # Plot Query if show_query: _show_query_fn(0, (nRows, nGTCols)) # Plot Ground Truth _plot_matches_aids(gt_aids, nQuerySubplts, (nRows, nGTCols)) _plot_matches_aids(top_aids, shift_topN, (nRows, nTopNCols)) #figtitle += ' q%s name=%s' % (ibsfuncs.aidstr(qres.qaid), ibs.aid2_name(qres.qaid)) figtitle += aug df2.set_figtitle(figtitle, incanvas=not vh.NO_LBL_OVERRIDE) # Result Interaction df2.adjust_subplots_safe() printDBG('[show_qres()] Finished') return fig
def draw(): df2.adjust_subplots_safe() fig_presenter.draw()
def show_nearest_descriptors(ibs, qaid, qfx, fnum=None, stride=5, consecutive_distance_compare=False): # Plots the nearest neighbors of a given feature (qaid, qfx) if fnum is None: fnum = df2.next_fnum() try: # Flann NN query (qfx2_aid, qfx2_fx, qfx2_dist, K, Knorm) = query_helpers.get_annotfeat_nn_index(ibs, qaid, qfx) # Adds metadata to a feature match def get_extract_tuple(aid, fx, k=-1): rchip = ibs.get_annot_chips(aid) kp = ibs.get_annot_kpts(aid)[fx] sift = ibs.get_annot_desc(aid)[fx] if k == -1: info = '\nquery %s, fx=%r' % (vh.get_aidstrs(aid), fx) type_ = 'query' elif k < K: type_ = 'match' info = '\nmatch %s, fx=%r k=%r, dist=%r' % (vh.get_aidstrs(aid), fx, k, qfx2_dist[0, k]) elif k < Knorm + K: type_ = 'norm' info = '\nnorm %s, fx=%r k=%r, dist=%r' % (vh.get_aidstrs(aid), fx, k, qfx2_dist[0, k]) else: raise Exception('[viz] problem k=%r') return (rchip, kp, sift, fx, aid, info, type_) extracted_list = [] extracted_list.append(get_extract_tuple(qaid, qfx, -1)) skipped = 0 for k in xrange(K + Knorm): if qfx2_aid[0, k] == qaid and qfx2_fx[0, k] == qfx: skipped += 1 continue tup = get_extract_tuple(qfx2_aid[0, k], qfx2_fx[0, k], k) extracted_list.append(tup) # Draw the _select_ith_match plot nRows, nCols = len(extracted_list), 3 if stride is None: stride = nRows # Draw selected feature matches prevsift = None px = 0 # plot offset px_shift = 0 # plot stride shift nExtracted = len(extracted_list) for listx, tup in enumerate(extracted_list): (rchip, kp, sift, fx, aid, info, type_) = tup if listx % stride == 0: # Create a temporary nRows and fnum in case we are splitting # up nearest neighbors into separate figures with stride _fnum = fnum + listx _nRows = min(nExtracted - listx, stride) px_shift = px df2.figure(fnum=_fnum, docla=True, doclf=True) printDBG('[viz] ' + info.replace('\n', '')) px_ = px - px_shift px = draw_feat_row(rchip, fx, kp, sift, _fnum, _nRows, nCols, px_, prevsift=prevsift, aid=aid, info=info, type_=type_) + px_shift if prevsift is None or consecutive_distance_compare: prevsift = sift df2.adjust_subplots_safe(hspace=1) except Exception as ex: print('[viz] Error in show nearest descriptors') print(ex) raise
def show_nearest_descriptors(ibs, qaid, qfx, fnum=None, stride=5, qreq_=None, **kwargs): r""" Args: ibs (ibeis.IBEISController): image analysis api qaid (int): query annotation id qfx (int): query feature index fnum (int): figure number stride (int): consecutive_distance_compare (bool): CommandLine: # Find a good match to inspect python -m ibeis.viz.interact.interact_matches --test-testdata_match_interact --show --db PZ_MTEST --qaid 3 # Now inspect it python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --show --db PZ_MTEST --qaid 3 --qfx 879 python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --show python -m ibeis.viz.viz_nearest_descriptors --test-show_nearest_descriptors --db PZ_MTEST --qaid 3 --qfx 879 --diskshow --save foo.png --dpi=256 SeeAlso: plottool.viz_featrow ~/code/plottool/plottool/viz_featrow.py Example: >>> # DISABLE_DOCTEST >>> from ibeis.viz.viz_nearest_descriptors import * # NOQA >>> import ibeis >>> # build test data >>> qreq_ = ibeis.testdata_qreq_() >>> ibs = ibeis.opendb('PZ_MTEST') >>> qaid = qreq_.qaids[0] >>> qfx = ut.get_argval('--qfx', type_=None, default=879) >>> fnum = None >>> stride = 5 >>> # execute function >>> skip = False >>> result = show_nearest_descriptors(ibs, qaid, qfx, fnum, stride, >>> draw_chip=True, >>> draw_warped=True, >>> draw_unwarped=False, >>> draw_desc=False, qreq_=qreq_) >>> # verify results >>> print(result) >>> pt.show_if_requested() """ import plottool as pt # NOQA consecutive_distance_compare = True draw_chip = kwargs.get('draw_chip', False) draw_desc = kwargs.get('draw_desc', True) draw_warped = kwargs.get('draw_warped', True) draw_unwarped = kwargs.get('draw_unwarped', True) #skip = kwargs.get('skip', True) # Plots the nearest neighbors of a given feature (qaid, qfx) if fnum is None: fnum = df2.next_fnum() try: # Flann NN query (qfx, qfx2_daid, qfx2_dfx, qfx2_dist, K, Knorm) = get_annotfeat_nn_index(ibs, qaid, qfx, qreq_=qreq_) # Adds metadata to a feature match def get_extract_tuple(aid, fx, k=-1): rchip = ibs.get_annot_chips(aid) kp = ibs.get_annot_kpts(aid)[fx] sift = ibs.get_annot_vecs(aid)[fx] if not ut.get_argflag('--texknormplot'): aidstr = vh.get_aidstrs(aid) nidstr = vh.get_nidstrs(ibs.get_annot_nids(aid)) id_str = ' ' + aidstr + ' ' + nidstr + ' fx=%r' % (fx, ) else: id_str = nidstr = aidstr = '' info = '' if k == -1: if pt.is_texmode(): info = '\\vspace{1cm}' info += 'Query $\\mathbf{d}_i$' info += '\n\\_' info += '\n\\_' else: if len(id_str) > '': info = 'Query: %s' % (id_str, ) else: info = 'Query' type_ = 'Query' elif k < K: type_ = 'Match' if ut.get_argflag('--texknormplot') and pt.is_texmode(): #info = 'Match:\n$k=%r$, $\\frac{||\\mathbf{d}_i - \\mathbf{d}_j||}{Z}=%.3f$' % (k, qfx2_dist[0, k]) info = '\\vspace{1cm}' info += 'Match: $\\mathbf{d}_{j_%r}$\n$\\textrm{dist}=%.3f$' % ( k, qfx2_dist[0, k]) info += '\n$s_{\\tt{LNBNN}}=%.3f$' % ( qfx2_dist[0, K + Knorm - 1] - qfx2_dist[0, k]) else: info = 'Match:%s\nk=%r, dist=%.3f' % (id_str, k, qfx2_dist[0, k]) info += '\nLNBNN=%.3f' % (qfx2_dist[0, K + Knorm - 1] - qfx2_dist[0, k]) elif k < Knorm + K: type_ = 'Norm' if ut.get_argflag('--texknormplot') and pt.is_texmode(): #info = 'Norm: $j_%r$\ndist=%.3f' % (id_str, k, qfx2_dist[0, k]) info = '\\vspace{1cm}' info += 'Norm: $j_%r$\n$\\textrm{dist}=%.3f$' % ( k, qfx2_dist[0, k]) info += '\n\\_' else: info = 'Norm: %s\n$k=%r$, dist=$%.3f$' % (id_str, k, qfx2_dist[0, k]) else: raise Exception('[viz] problem k=%r') return (rchip, kp, sift, fx, aid, info, type_) extracted_list = [] # Remember the query sift feature extracted_list.append(get_extract_tuple(qaid, qfx, -1)) origsift = extracted_list[0][2] skipped = 0 for k in range(K + Knorm): #if qfx2_daid[0, k] == qaid and qfx2_dfx[0, k] == qfx: if qfx2_daid[0, k] == qaid: skipped += 1 continue tup = get_extract_tuple(qfx2_daid[0, k], qfx2_dfx[0, k], k) extracted_list.append(tup) # Draw the _select_ith_match plot nRows = len(extracted_list) if stride is None: stride = nRows # Draw selected feature matches prevsift = None px = 0 # plot offset px_shift = 0 # plot stride shift nExtracted = len(extracted_list) featrow_kw = dict( draw_chip=draw_chip, draw_desc=draw_desc, draw_warped=draw_warped, draw_unwarped=draw_unwarped, ) if ut.get_argflag('--texknormplot'): featrow_kw['ell_color'] = pt.ORANGE pass for listx, tup in enumerate(extracted_list): (rchip, kp, sift, fx, aid, info, type_) = tup if listx % stride == 0: # Create a temporary nRows and fnum in case we are splitting # up nearest neighbors into separate figures with stride _fnum = fnum + listx _nRows = min(nExtracted - listx, stride) px_shift = px df2.figure(fnum=_fnum, docla=True, doclf=True) px_ = px - px_shift px = draw_feat_row(rchip, fx, kp, sift, _fnum, _nRows, px=px_, prevsift=prevsift, origsift=origsift, aid=aid, info=info, type_=type_, **featrow_kw) px += px_shift if prevsift is None or consecutive_distance_compare: prevsift = sift df2.adjust_subplots_safe(hspace=.85, wspace=0, top=.95, bottom=.087, left=.05, right=.95) except Exception as ex: print('[viz] Error in show nearest descriptors') print(ex) raise
def show_descriptors_match_distances(orgres2_distance, fnum=1, db_name='', **kwargs): disttype_list = orgres2_distance.itervalues().next().keys() orgtype_list = orgres2_distance.keys() (nRow, nCol) = len(orgtype_list), len(disttype_list) nColors = nRow * nCol color_list = df2.distinct_colors(nColors) df2.figure(fnum=fnum, docla=True, doclf=True) pnum_ = lambda px: (nRow, nCol, px + 1) plot_type = utool.get_arg('--plot-type', default='plot') # Remember min and max val for each distance type (l1, emd...) distkey2_min = {distkey: np.uint64(-1) for distkey in disttype_list} distkey2_max = {distkey: 0 for distkey in disttype_list} def _distplot(dists, color, label, distkey, plot_type=plot_type): data = sorted(dists) ax = df2.gca() min_ = distkey2_min[distkey] max_ = distkey2_max[distkey] if plot_type == 'plot': df2.plot(data, color=color, label=label, yscale='linear') #xticks = np.linspace(np.min(data), np.max(data), 3) #yticks = np.linspace(0, len(data), 5) #ax.set_xticks(xticks) #ax.set_yticks(yticks) ax.set_ylim(min_, max_) ax.set_xlim(0, len(dists)) ax.set_ylabel('distance') ax.set_xlabel('matches indexes (sorted by distance)') df2.legend(loc='lower right') if plot_type == 'pdf': df2.plot_pdf(data, color=color, label=label) ax.set_ylabel('pr') ax.set_xlabel('distance') ax.set_xlim(min_, max_) df2.legend(loc='upper left') df2.dark_background(ax) df2.small_xticks(ax) df2.small_yticks(ax) px = 0 for orgkey in orgtype_list: for distkey in disttype_list: dists = orgres2_distance[orgkey][distkey] if len(dists) == 0: continue min_ = dists.min() max_ = dists.max() distkey2_min[distkey] = min(distkey2_min[distkey], min_) distkey2_max[distkey] = max(distkey2_max[distkey], max_) for count, orgkey in enumerate(orgtype_list): for distkey in disttype_list: printDBG('[allres-viz] plotting: %r' % ((orgkey, distkey), )) dists = orgres2_distance[orgkey][distkey] df2.figure(fnum=fnum, pnum=pnum_(px)) color = color_list[px] title = distkey + ' ' + orgkey label = 'P(%s | %s)' % (distkey, orgkey) _distplot(dists, color, label, distkey, **kwargs) if count == 0: ax = df2.gca() ax.set_title(distkey) px += 1 subtitle = 'the matching distances between sift descriptors' title = '(sift) matching distances' if db_name != '': title = db_name + ' ' + title df2.set_figtitle(title, subtitle) df2.adjust_subplots_safe()