def _image_view(sel_aids=sel_aids, **_kwargs): try: viz.show_image(ibs, gid, sel_aids=sel_aids, fnum=self.fnum, **_kwargs) df2.set_figtitle('Image View') except TypeError as ex: ut.printex(ex, ut.dict_str(_kwargs)) raise
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 show_name(ibs, nid, nid2_aids=None, in_image=True, fnum=0, sel_aids=[], subtitle='', annote=False, **kwargs): print('[viz] show_name nid=%r' % nid) aid_list = ibs.get_name_aids(nid) name = ibs.get_name_text((nid,)) ibsfuncs.ensure_annotation_data(ibs, aid_list, chips=(not in_image or annote), feats=annote) print('[viz] show_name=%r aid_list=%r' % (name, aid_list)) nAids = len(aid_list) if nAids > 0: nRows, nCols = ph.get_square_row_cols(nAids) print('[viz*] r=%r, c=%r' % (nRows, nCols)) #gs2 = gridspec.GridSpec(nRows, nCols) pnum_ = df2.get_pnum_func(nRows, nCols) fig = df2.figure(fnum=fnum, pnum=pnum_(0), **kwargs) fig.clf() # Trigger computation of all chips in parallel for px, aid in enumerate(aid_list): show_chip(ibs, aid=aid, pnum=pnum_(px), annote=annote, in_image=in_image) if aid in sel_aids: ax = df2.gca() df2.draw_border(ax, df2.GREEN, 4) #plot_aid3(ibs, aid) if isinstance(nid, np.ndarray): nid = nid[0] if isinstance(name, np.ndarray): name = name[0] else: df2.imshow_null(fnum=fnum, **kwargs) figtitle = 'Name View nid=%r name=%r' % (nid, name) df2.set_figtitle(figtitle)
def make_hud(self): """ Creates heads up display """ # Button positioning nToggle = len(self.toggleable_kws) # horizontal left, horizonal right hl_slot, hr_slot = df2.make_bbox_positioners(y=.02, w=.08, h=.04, xpad=.05, startx=0, stopx=1) prev_rect = hl_slot(0) # left button next_rect = hr_slot(0) # right button tw = df2.width_from(nToggle, pad=.05, start=.13, stop=.87) hlt_slot, hrt_slot = df2.make_bbox_positioners(y=.02, w=tw, h=.04, xpad=.05, startx=.13, stopx=.87) # Create buttons if self.current_pagenum != 0: self.append_button('prev', callback=self.prev_page, rect=prev_rect) if self.current_pagenum != self.nPages - 1: self.append_button('next', callback=self.next_page, rect=next_rect) for count, (text, keyword) in enumerate(six.iteritems(self.toggleable_kws)): callback = functools.partial(self.toggle_kw, keyword=keyword) rect = hlt_slot(count) self.append_button(text, callback=callback, rect=rect) figtitle_fmt = ''' Match Candidates ({start_index}-{stop_index}) / {nCands} page {current_pagenum} / {nPages} ''' # sexy: using object dict as format keywords figtitle = figtitle_fmt.format(**self.__dict__) df2.set_figtitle(figtitle)
def show_name(ibs, nid, in_image=True, fnum=0, sel_aids=[], subtitle='', annote=False, aid_list=None, index_list=None, **kwargs): r""" Args: ibs (IBEISController): ibeis controller object nid (?): in_image (bool): fnum (int): figure number sel_aids (list): subtitle (str): annote (bool): CommandLine: python -m ibeis.viz.viz_name --test-show_name --dpath ~/latex/crall-candidacy-2015 --save 'figures/{name}.jpg' --no-figtitle --notitle --db NNP_Master3 --figsize=9,4 --clipwhite --dpi=180 --adjust=.05 --index_list=[0,1,2,3] --rc=2,4 --append temp_out_figure.tex --name=IBEIS_PZ_0739 --no-draw_lbls --doboth --no-inimage --diskshow python -m ibeis.viz.viz_name --test-show_name --no-figtitle --notitle --db NNP_Master3 --figsize=9,4 --clipwhite --dpi=180 --adjust=.05 --index_list=[0,1,2,3] --rc=2,4 --append temp_out_figure.tex --name=IBEIS_PZ_0739 --no-draw_lbls --doboth --no-inimage --show python -m ibeis.viz.viz_name --test-show_name --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.viz.viz_name import * # NOQA >>> ibs, nid, in_image, index_list = testdata_showname() >>> fnum = 0 >>> sel_aids = [] >>> subtitle = '' >>> annote = False >>> # execute function >>> show_name(ibs, nid, in_image, fnum, sel_aids, subtitle, annote, index_list=index_list) >>> ut.show_if_requested() """ print('[viz_name] show_name nid=%r, index_list=%r, aid_list=%r' % (nid, index_list, aid_list)) if aid_list is None: aid_list = ibs.get_name_aids(nid) else: assert ut.list_all_eq_to(ibs.get_annot_nids(aid_list), nid) if index_list is not None: aid_list = ut.take(aid_list, index_list) name = ibs.get_name_texts((nid,)) print('[viz_name] * name=%r aid_list=%r' % (name, aid_list)) show_multiple_chips(ibs, aid_list, in_image=in_image, fnum=fnum, sel_aids=sel_aids, annote=annote, **kwargs) if isinstance(nid, np.ndarray): nid = nid[0] if isinstance(name, np.ndarray): name = name[0] use_figtitle = not ut.get_argflag('--no-figtitle') if use_figtitle: figtitle = 'Name View nid=%r name=%r' % (nid, name) df2.set_figtitle(figtitle)
def TEST_VIZ(ibs): valid_gids = ibs.get_valid_gids() valid_aids = ibs.get_valid_aids() print('len(valid_aids) = %r' % len(valid_aids)) print('len(valid_gids) = %r' % len(valid_gids)) assert len(valid_gids) > 0, 'database images cannot be empty for test' gindex = 1 gid = valid_gids[gindex] aid_list = ibs.get_image_aids(gid) rindex = 0 aid = aid_list[rindex] qaid = aid aids = aid_list[1:3] aid = aid_list[-1] try: qres = ibs._query_chips4([qaid], valid_aids)[qaid] print(qres) top_aids = qres.get_top_aids(ibs) assert len(top_aids) > 0, 'Results seems to be empty' aid2 = top_aids[0] # 294 query_failed = False except Exception as ex: query_failed = True utool.printex(ex, 'QUERY FAILED!') raise #---------------------- #print('Show Image') viz.show_image(ibs, gid, aids=aids, fnum=1) df2.set_figtitle('Show Image') #---------------------- #print('Show Chip') kpts_kwgs = dict(ell=True, ori=True, rect=True, eig=True, pts=False, kpts_subset=10) viz.show_chip(ibs, aid, in_image=False, fnum=2, **kpts_kwgs) df2.set_figtitle('Show Chip (normal)') viz.show_chip(ibs, aid, in_image=True, fnum=3, **kpts_kwgs) df2.set_figtitle('Show Chip (in_image)') #---------------------- if not query_failed: print('Show Query') viz.show_matches(ibs, qres, aid2, fnum=4) df2.set_figtitle('Show Chipres') viz.show_qres(ibs, qres, fnum=5) df2.set_figtitle('Show QRes') ##---------------------- print('return test locals') return locals()
def _select_ith_match(mx, qaid, aid): #---------------------- # Get info for the _select_ith_match plot annote_ptr[0] = 1 # Get the mx-th feature match aid1, aid2 = qaid, aid fx1, fx2 = fm[mx] fscore2 = qres.aid2_fs[aid2][mx] fk2 = qres.aid2_fk[aid2][mx] kpts1, kpts2 = ibs.get_annot_kpts([aid1, aid2]) desc1, desc2 = ibs.get_annot_desc([aid1, aid2]) kp1, kp2 = kpts1[fx1], kpts2[fx2] sift1, sift2 = desc1[fx1], desc2[fx2] info1 = '\nquery' info2 = '\nk=%r fscore=%r' % (fk2, fscore2) last_state.last_fx = fx1 # Extracted keypoints to draw extracted_list = [(rchip1, kp1, sift1, fx1, aid1, info1), (rchip2, kp2, sift2, fx2, aid2, info2)] # Normalizng Keypoint if hasattr(qres, 'filt2_meta') and 'lnbnn' in qres.filt2_meta: qfx2_norm = qres.filt2_meta['lnbnn'] # Normalizing chip and feature (aid3, fx3, normk) = qfx2_norm[fx1] rchip3 = ibs.get_annot_chips(aid3) kp3 = ibs.get_annot_kpts(aid3)[fx3] sift3 = ibs.get_annot_desc(aid3)[fx3] info3 = '\nnorm %s k=%r' % (vh.get_aidstrs(aid3), normk) extracted_list.append((rchip3, kp3, sift3, fx3, aid3, info3)) else: print('WARNING: meta doesnt exist') #---------------------- # Draw the _select_ith_match plot nRows, nCols = len(extracted_list) + same_fig, 3 # Draw matching chips and features sel_fm = np.array([(fx1, fx2)]) pnum1 = (nRows, 1, 1) if same_fig else (1, 1, 1) _chipmatch_view(pnum1, vert=False, ell_alpha=.4, ell_linewidth=1.8, colors=df2.BLUE, sel_fm=sel_fm, **kwargs) # Draw selected feature matches px = nCols * same_fig # plot offset prevsift = None if not same_fig: fnum2 = fnum + len(viz.FNUMS) fig2 = df2.figure(fnum=fnum2, docla=True, doclf=True) else: fnum2 = fnum for (rchip, kp, sift, fx, aid, info) in extracted_list: px = draw_feat_row(rchip, fx, kp, sift, fnum2, nRows, nCols, px, prevsift=prevsift, aid=aid, info=info) prevsift = sift if not same_fig: ih.connect_callback(fig2, 'button_press_event', _click_matches_click) df2.set_figtitle(figtitle + vh.get_vsstr(qaid, aid))
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 chipmatch_view(self, pnum=(1, 1, 1), **kwargs_): """ just visualizes the matches using some type of lines CommandLine: python -m ibeis.viz.interact.interact_matches --test-chipmatch_view --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.viz.interact.interact_matches import * # NOQA >>> self = testdata_match_interact() >>> self.chipmatch_view() >>> pt.show_if_requested() """ ibs = self.ibs aid = self.daid qaid = self.qaid fnum = self.fnum figtitle = self.figtitle xywh2_ptr = self.xywh2_ptr # drawing mode draw: with/without lines/feats mode = self.mode draw_ell = mode >= 1 draw_lines = mode == 2 self.mode = (self.mode + 1) % 3 df2.figure(fnum=fnum, docla=True, doclf=True) show_matches_kw = self.kwargs.copy() show_matches_kw.update( dict(fnum=fnum, pnum=pnum, draw_lines=draw_lines, draw_ell=draw_ell, colorbar_=True, vert=self.vert)) show_matches_kw.update(kwargs_) if self.use_homog: show_matches_kw['H1'] = self.H1 #show_matches_kw['score'] = self.score show_matches_kw['rawscore'] = self.score #ut.embed() show_matches_kw['aid2_raw_rank'] = self.rank tup = viz.viz_matches.show_matches2(ibs, self.qaid, self.daid, self.fm, self.fs, qreq_=self.qreq_, **show_matches_kw) ax, xywh1, xywh2 = tup xywh2_ptr[0] = xywh2 df2.set_figtitle(figtitle + ' ' + vh.get_vsstr(qaid, aid))
def _chip_view(mode=0, pnum=(1, 1, 1), **kwargs): print('... _chip_view mode=%r' % mode_ptr[0]) kwargs['ell'] = mode_ptr[0] == 1 kwargs['pts'] = mode_ptr[0] == 2 if not ischild: df2.figure(fnum=fnum, pnum=pnum, docla=True, doclf=True) # Toggle no keypoints view viz.show_chip(ibs, aid, fnum=fnum, pnum=pnum, config2_=config2_, **kwargs) df2.set_figtitle('Chip View')
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 dump_orgres_matches(allres, orgres_type): orgres = allres.__dict__[orgres_type] ibs = allres.ibs qrid2_qres = allres.qrid2_qres # loop over each query / result of interest for qrid, rid, score, rank in orgres.iter(): query_gname, _ = os.path.splitext(ibs.tables.gx2_gname[ibs.tables.cx2_gx[qrid]]) result_gname, _ = os.path.splitext(ibs.tables.gx2_gname[ibs.tables.cx2_gx[rid]]) qres = qrid2_qres[qrid] df2.figure(fnum=1, plotnum=121) df2.show_matches_annote_res(qres, ibs, rid, fnum=1, plotnum=121) big_title = 'score=%.2f_rank=%d_q=%s_r=%s' % (score, rank, query_gname, result_gname) df2.set_figtitle(big_title) viz.__dump_or_browse(allres, orgres_type + '_matches' + allres.title_suffix)
def _chipmatch_view(pnum=(1, 1, 1), **kwargs): mode = annote_ptr[0] # drawing mode draw: with/without lines/feats draw_ell = mode >= 1 draw_lines = mode == 2 annote_ptr[0] = (annote_ptr[0] + 1) % 3 df2.figure(fnum=fnum, docla=True, doclf=True) # TODO RENAME This to remove qres and rectify with show_matches tup = viz.show_matches(ibs, qres, aid, fnum=fnum, pnum=pnum, draw_lines=draw_lines, draw_ell=draw_ell, colorbar_=True, **kwargs) ax, xywh1, xywh2 = tup xywh2_ptr[0] = xywh2 df2.set_figtitle(figtitle + ' ' + vh.get_vsstr(qaid, aid))
def dump_orgres_matches(allres, orgres_type): orgres = allres.__dict__[orgres_type] ibs = allres.ibs qcx2_res = allres.qcx2_res # loop over each query / result of interest for qcx, cx, score, rank in orgres.iter(): query_gname, _ = os.path.splitext(ibs.tables.gx2_gname[ibs.tables.cx2_gx[qcx]]) result_gname, _ = os.path.splitext(ibs.tables.gx2_gname[ibs.tables.cx2_gx[cx]]) res = qcx2_res[qcx] df2.figure(fnum=FIGNUM, pnum=121) df2.show_matches3(res, ibs, cx, SV=False, fnum=FIGNUM, pnum=121) df2.show_matches3(res, ibs, cx, SV=True, fnum=FIGNUM, pnum=122) big_title = 'score=%.2f_rank=%d_q=%s_r=%s' % (score, rank, query_gname, result_gname) df2.set_figtitle(big_title) __dump_or_browse(allres.ibs, orgres_type + '_matches' + allres.title_suffix)
def TEST_keypoint(imgBGR, img_fpath, kpts, desc, sel, fnum=1, figtitle=''): from plottool import draw_func2 as df2 from plottool.viz_keypoints import _annotate_kpts, show_keypoints from plottool.viz_featrow import draw_feat_row import plottool import vtool.patch as ptool #----------------------# # --- Extract Data --- # #----------------------# kp = kpts[sel] # Extract patches, gradients, and orientations print('[rotinvar] 1) Extract patch, gradients, and orientations') wpatch, wkp = ptool.get_warped_patch(imgBGR, kp, gray=True) gradx, grady = ptool.patch_gradient(wpatch) gmag = ptool.patch_mag(gradx, grady) gori = ptool.patch_ori(gradx, grady) gori_weights = ptool.gaussian_weight_patch(gmag) # Get orientation histogram print('[rotinvar] 2) Get orientation histogram') hist, centers = ptool.get_orientation_histogram(gori, gori_weights) #----------------------# # --- Draw Results --- # #----------------------# f1_loc = TEST_figure1(wpatch, gradx, grady, gmag, gori, hist, centers, fnum=fnum) df2.set_figtitle(figtitle + 'Dominant Orienation Extraction') TEST_figure2(imgBGR, kpts, desc, sel, fnum=fnum + 1) df2.set_figtitle(figtitle) # TEST_figure2(imgBGR, kpts2, Desc2, sel, fnum=fnum + 2) # df2.set_figtitle('Rotation Invariant') #df2.draw_keypoint_gradient_orientations(imgBGR, kp=kpts2[sel], # sift=desc[sel], mode='vec', # fnum=4) #df2.draw_vector_field(gradx, grady, pnum=(1, 1, 1), fnum=4) #df2.draw_kpts2(np.array([wkp]), sifts=desc[sel:sel + 1], ori=True) return locals()
def dump_orgres_matches(allres, orgres_type): orgres = allres.__dict__[orgres_type] ibs = allres.ibs qrid2_qres = allres.qrid2_qres # loop over each query / result of interest for qrid, rid, score, rank in orgres.iter(): query_gname, _ = os.path.splitext( ibs.tables.gx2_gname[ibs.tables.cx2_gx[qrid]]) result_gname, _ = os.path.splitext( ibs.tables.gx2_gname[ibs.tables.cx2_gx[rid]]) qres = qrid2_qres[qrid] df2.figure(fnum=1, plotnum=121) df2.show_matches_annote_res(qres, ibs, rid, fnum=1, plotnum=121) big_title = 'score=%.2f_rank=%d_q=%s_r=%s' % (score, rank, query_gname, result_gname) df2.set_figtitle(big_title) viz.__dump_or_browse(allres, orgres_type + '_matches' + allres.title_suffix)
def plot_seperability(hs, qcx_list, fnum=1): print('[dev] plot_seperability(fnum=%r)' % fnum) qcx2_res = get_qcx2_res(hs, qcx_list) qcx2_separability = get_seperatbility(hs, qcx2_res) sep_score_list = qcx2_separability.values() df2.figure(fnum=fnum, doclf=True, docla=True) print('[dev] seperability stats: ' + utool.stats_str(sep_score_list)) sorted_sepscores = sorted(sep_score_list) df2.plot(sorted_sepscores, color=df2.DEEP_PINK, label='seperation score', yscale=YSCALE) df2.set_xlabel('true chipmatch index (%d)' % len(sep_score_list)) df2.set_logyscale_from_data(sorted_sepscores) df2.dark_background() rowid = qcx2_res.itervalues().next().rowid df2.set_figtitle('seperability\n' + rowid) df2.legend() fnum += 1 return fnum
def annotationmatch_scores(ibs, qaid_list): print('[dev] annotationmatch_scores') allres = get_allres(ibs, qaid_list) # Get the descriptor distances of true matches orgtype_list = ['false', 'true'] orgtype_list = ['top_false', 'top_true'] #markers_map = {'false': 'o', 'true': 'o-', 'top_true': 'o-', 'top_false': 'o'} markers_map = defaultdict(lambda: 'o') cmatch_scores_map = results_analyzer.get_orgres_annotationmatch_scores(allres, orgtype_list) results_analyzer.print_annotationmatch_scores_map(cmatch_scores_map) #true_cmatch_scores = cmatch_scores_map['true'] #false_cmatch_scores = cmatch_scores_map['false'] scores_list = [cmatch_scores_map[orgtype] for orgtype in orgtype_list] scores_lbls = orgtype_list scores_markers = [markers_map[orgtype] for orgtype in orgtype_list] plottool.plots.draw_scores_cdf(scores_list, scores_lbls, scores_markers) df2.set_figtitle('Chipmatch Scores ' + ibs.qreq.get_cfgstr()) 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 dump_orgres_matches(allres, orgres_type): orgres = allres.__dict__[orgres_type] ibs = allres.ibs qcx2_res = allres.qcx2_res # loop over each query / result of interest for qcx, cx, score, rank in orgres.iter(): query_gname, _ = os.path.splitext( ibs.tables.gx2_gname[ibs.tables.cx2_gx[qcx]]) result_gname, _ = os.path.splitext( ibs.tables.gx2_gname[ibs.tables.cx2_gx[cx]]) res = qcx2_res[qcx] df2.figure(fnum=FIGNUM, pnum=121) df2.show_matches3(res, ibs, cx, SV=False, fnum=FIGNUM, pnum=121) df2.show_matches3(res, ibs, cx, SV=True, fnum=FIGNUM, pnum=122) big_title = 'score=%.2f_rank=%d_q=%s_r=%s' % (score, rank, query_gname, result_gname) df2.set_figtitle(big_title) __dump_or_browse(allres.ibs, orgres_type + '_matches' + allres.title_suffix)
def desc_dists(ibs, qaid_list): """ Plots the distances between matching descriptors with groundtruth (true/false) data """ print('[dev] desc_dists') allres = get_allres(ibs, qaid_list) # Get the descriptor distances of true matches orgtype_list = ['top_false', 'true'] disttype = 'L2' orgres2_distmap = results_analyzer.get_orgres_desc_match_dists(allres, orgtype_list) results_analyzer.print_desc_distances_map(orgres2_distmap) #true_desc_dists = orgres2_distmap['true']['L2'] #false_desc_dists = orgres2_distmap['false']['L2'] #scores_list = [false_desc_dists, true_desc_dists] dists_list = [orgres2_distmap[orgtype][disttype] for orgtype in orgtype_list] dists_lbls = orgtype_list dists_markers = ['x', 'o--'] plottool.plots.draw_scores_cdf(dists_list, dists_lbls, dists_markers) df2.set_figtitle('Descriptor Distance CDF d(x)' + ibs.qreq.get_cfgstr()) return locals()
def plot_tt_bt_tf_matches(ibs, allres, qcx): #print('Visualizing result: ') #res.printme() res = allres.qcx2_res[qcx] ranks = (allres.top_true_qcx_arrays[0][qcx], allres.bot_true_qcx_arrays[0][qcx], allres.top_false_qcx_arrays[0][qcx]) #scores = (allres.top_true_qcx_arrays[1][qcx], #allres.bot_true_qcx_arrays[1][qcx], #allres.top_false_qcx_arrays[1][qcx]) cxs = (allres.top_true_qcx_arrays[2][qcx], allres.bot_true_qcx_arrays[2][qcx], allres.top_false_qcx_arrays[2][qcx]) titles = ('best True rank=' + str(ranks[0]) + ' ', 'worst True rank=' + str(ranks[1]) + ' ', 'best False rank=' + str(ranks[2]) + ' ') df2.figure(fnum=1, pnum=231) res.plot_matches(res, ibs, cxs[0], False, fnum=1, pnum=131, title_aug=titles[0]) res.plot_matches(res, ibs, cxs[1], False, fnum=1, pnum=132, title_aug=titles[1]) res.plot_matches(res, ibs, cxs[2], False, fnum=1, pnum=133, title_aug=titles[2]) fig_title = 'fig q' + ibs.cidstr( qcx) + ' TT BT TF -- ' + allres.title_suffix df2.set_figtitle(fig_title)
def plot_tt_bt_tf_matches(ibs, allres, qcx): #print('Visualizing result: ') #res.printme() res = allres.qcx2_res[qcx] ranks = (allres.top_true_qcx_arrays[0][qcx], allres.bot_true_qcx_arrays[0][qcx], allres.top_false_qcx_arrays[0][qcx]) #scores = (allres.top_true_qcx_arrays[1][qcx], #allres.bot_true_qcx_arrays[1][qcx], #allres.top_false_qcx_arrays[1][qcx]) cxs = (allres.top_true_qcx_arrays[2][qcx], allres.bot_true_qcx_arrays[2][qcx], allres.top_false_qcx_arrays[2][qcx]) titles = ('best True rank=' + str(ranks[0]) + ' ', 'worst True rank=' + str(ranks[1]) + ' ', 'best False rank=' + str(ranks[2]) + ' ') df2.figure(fnum=1, pnum=231) res.plot_matches(res, ibs, cxs[0], False, fnum=1, pnum=131, title_aug=titles[0]) res.plot_matches(res, ibs, cxs[1], False, fnum=1, pnum=132, title_aug=titles[1]) res.plot_matches(res, ibs, cxs[2], False, fnum=1, pnum=133, title_aug=titles[2]) fig_title = 'fig q' + ibs.cidstr(qcx) + ' TT BT TF -- ' + allres.title_suffix df2.set_figtitle(fig_title)
def test_viz_image(img_fpath): # Read image img = cv2.imread(img_fpath) tau = np.pi * 2 # References: tauday.com # Create figure fig = df2.figure(fnum=42, pnum=(1, 1, 1)) # Clear figure fig.clf() # Build parameters bbox_list = [dummy_bbox(img), dummy_bbox(img, (-.25, -.25), .1)] showkw = { 'title': 'test axis title', # The list of bounding boxes to be drawn on the image 'bbox_list': bbox_list, 'theta_list': [tau * .7, tau * .9], 'sel_list': [True, False], 'label_list': ['test label', 'lbl2'], } # Print the keyword arguments to illustrate their format print('showkw = ' + utool.dict_str(showkw)) # Display the image in figure-num 42, using a 1x1 axis grid in the first # axis. Pass showkw as keyword arguments. viz_image2.show_image(img, fnum=42, pnum=(1, 1, 1), **showkw) df2.set_figtitle('Test figure title')
def test_viz_image(img_fpath): # Read image img = cv2.imread(img_fpath) tau = np.pi * 2 # References: tauday.com # Create figure fig = df2.figure(fnum=42, pnum=(1, 1, 1)) # Clear figure fig.clf() # Build parameters bbox_list = [dummy_bbox(img), dummy_bbox(img, (-0.25, -0.25), 0.1)] showkw = { "title": "test axis title", # The list of bounding boxes to be drawn on the image "bbox_list": bbox_list, "theta_list": [tau * 0.7, tau * 0.9], "sel_list": [True, False], "label_list": ["test label", "lbl2"], } # Print the keyword arguments to illustrate their format print("showkw = " + utool.dict_str(showkw)) # Display the image in figure-num 42, using a 1x1 axis grid in the first # axis. Pass showkw as keyword arguments. viz_image2.show_image(img, fnum=42, pnum=(1, 1, 1), **showkw) df2.set_figtitle("Test figure title")
def TEST_keypoint(imgBGR, img_fpath, kpts, desc, sel): import pyhesaff import vtool.patch as ptool from plottool import draw_func2 as df2 #----------------------# # --- Extract Data --- # #----------------------# kp = kpts[sel] # Extract patches, gradients, and orientations print('[rotinvar] 1) Extract patch, gradients, and orientations') wpatch, wkp = ptool.get_warped_patch(imgBGR, kp, gray=True) gradx, grady = ptool.patch_gradient(wpatch, gaussian_weighted=False) gmag = ptool.patch_mag(gradx, grady) gori = ptool.patch_ori(gradx, grady) # Get orientation histogram print('[rotinvar] 2) Get orientation histogram') gori_weights = ptool.gaussian_weight_patch(gmag) hist, centers = ptool.get_orientation_histogram(gori, gori_weights) # Get dominant direction in radians kpts2 = TEST_ptool_find_kpts_direction(imgBGR, kpts) kpts2, desc2 = pyhesaff.vtool_adapt_rotation(img_fpath, kpts) #----------------------# # --- Draw Results --- # #----------------------# f1_loc = TEST_figure1(wpatch, gradx, grady, gmag, gori, hist, centers) df2.set_figtitle('Dominant Orienation Extraction') TEST_figure2(imgBGR, kpts, desc, sel, fnum=2) df2.set_figtitle('Gravity Vector') TEST_figure2(imgBGR, kpts2, desc2, sel, fnum=3) df2.set_figtitle('Rotation Invariant') #df2.draw_keypoint_gradient_orientations(imgBGR, kp=kpts2[sel], # sift=desc[sel], mode='vec', # fnum=4) #df2.draw_vector_field(gradx, grady, pnum=(1, 1, 1), fnum=4) #df2.draw_kpts2(np.array([wkp]), sifts=desc[sel:sel + 1], ori=True) return locals()
def test_sver(chip1, chip2, kpts1, kpts2, fm, nShow=6): r""" Args: chip1 (ndarray[uint8_t, ndim=2]): annotation image data chip2 (ndarray[uint8_t, ndim=2]): annotation image data kpts1 (ndarray[float32_t, ndim=2]): keypoints kpts2 (ndarray[float32_t, ndim=2]): keypoints fm (list): list of feature matches as tuples (qfx, dfx) nShow (int): Returns: ?: CommandLine: python -m vtool.tests.test_spatial_verification --test-test_sver:0 --show python -m vtool.tests.test_spatial_verification --test-test_sver:1 --show Example0: >>> # DISABLE_DOCTEST >>> import plottool as pt >>> from vtool.tests.test_spatial_verification import * # NOQA >>> # build test data >>> nShow = utool.get_argval('--nShow', int, 1) >>> chip1, chip2, kpts1, kpts2, fm = get_dummy_test_vars() >>> # execute function >>> result = test_sver(chip1, chip2, kpts1, kpts2, fm, nShow) >>> # verify results >>> print(result) >>> pt.show_if_requested() Example1: >>> # DISABLE_DOCTEST >>> import plottool as pt >>> from vtool.tests.test_spatial_verification import * # NOQA >>> # build test data >>> nShow = utool.get_argval('--nShow', int, 1) >>> chip1, chip2, kpts1, kpts2, fm = get_dummy_test_vars1() >>> # execute function >>> result = test_sver(chip1, chip2, kpts1, kpts2, fm, nShow) >>> # verify results >>> print(result) >>> pt.show_if_requested() """ xy_thresh_sqrd = ktool.get_diag_extent_sqrd(kpts2) * xy_thresh def pack_errors(xy_err, scale_err, ori_err): """ makes human readable errors """ def _pack(bits, errs, thresh): return utool.indentjoin(['%5s %f < %f' % (bit, err, thresh) for (bit, err) in zip(bits, errs)]) xy_flag = xy_err < xy_thresh_sqrd scale_flag = scale_err < scale_thresh_sqrd ori_flag = ori_err < ori_thresh errors_dict = { 'xy_err': _pack(xy_flag, np.sqrt(xy_err), np.sqrt(xy_thresh_sqrd)), 'scale_err': _pack(scale_flag, np.sqrt(scale_err), np.sqrt(scale_thresh_sqrd)), 'ori_err': _pack(ori_flag, ori_err, ori_thresh), } return errors_dict # Test each affine hypothesis #assert kpts1.dtype == ktool.KPTS_DTYPE, 'bad cast somewhere kpts1.dtype=%r' % (kpts1.dtype) #assert kpts2.dtype == ktool.KPTS_DTYPE, 'bad cast somewhere kpts2.dtype=%r' % (kpts2.dtype) #assert xy_thresh_sqrd.dtype == ktool.KPTS_DTYPE, 'bad cast somewhere #xy_thresh_sqrd.dtype=%r' % (xy_thresh_sqrd.dtype) aff_hypo_tups = sver.get_affine_inliers(kpts1, kpts2, fm, xy_thresh_sqrd, scale_thresh_sqrd, ori_thresh) inliers_list, errors_list, Aff_mats = aff_hypo_tups # Determine best hypothesis nInliers_list = np.array(list(map(len, inliers_list))) best_mxs = nInliers_list.argsort()[::-1] for fnum, mx in enumerate(best_mxs[0:min(len(best_mxs), nShow)]): Aff = Aff_mats[mx] aff_inliers = inliers_list[mx] if utool.get_argflag('--print-error'): errors = pack_errors(*errors_list[mx]) # NOQA print(utool.dict_str(errors, strvals=True)) homog_inliers, homog_errors, H = sver.get_homography_inliers(kpts1, kpts2, fm, aff_inliers, xy_thresh_sqrd) kpts1_At = ktool.transform_kpts(kpts1, Aff) kpts1_Ht = ktool.transform_kpts(kpts1, H) kpts = kpts1 M = H homog_tup = (homog_inliers, H) aff_tup = (aff_inliers, Aff) _args = (chip1, chip2, kpts1, kpts2, fm) _kw = dict(show_assign=True, show_kpts=True, mx=mx, fnum=fnum * 3) draw_sv.show_sv(*_args, aff_tup=aff_tup, homog_tup=homog_tup, **_kw) #draw_sv.show_sv(*_args, aff_tup=aff_tup, mx=mx, fnum=fnum * 3) #draw_sv.show_sv(*_args, homog_tup=homog_tup, mx=mx, fnum=3) df2.set_figtitle('# %r inliers (in rects, hypo in bold)' % (nInliers_list[mx],)) return locals()
def test_sver(chip1, chip2, kpts1, kpts2, fm, nShow=6): r""" Args: chip1 (ndarray[uint8_t, ndim=2]): annotation image data chip2 (ndarray[uint8_t, ndim=2]): annotation image data kpts1 (ndarray[float32_t, ndim=2]): keypoints kpts2 (ndarray[float32_t, ndim=2]): keypoints fm (list): list of feature matches as tuples (qfx, dfx) nShow (int): Returns: ?: CommandLine: python -m vtool.tests.test_spatial_verification --test-test_sver:0 --show python -m vtool.tests.test_spatial_verification --test-test_sver:1 --show Example0: >>> # DISABLE_DOCTEST >>> import plottool as pt >>> from vtool.tests.test_spatial_verification import * # NOQA >>> # build test data >>> nShow = utool.get_argval('--nShow', int, 1) >>> chip1, chip2, kpts1, kpts2, fm = get_dummy_test_vars() >>> # execute function >>> result = test_sver(chip1, chip2, kpts1, kpts2, fm, nShow) >>> # verify results >>> print(result) >>> pt.show_if_requested() Example1: >>> # DISABLE_DOCTEST >>> import plottool as pt >>> from vtool.tests.test_spatial_verification import * # NOQA >>> # build test data >>> nShow = utool.get_argval('--nShow', int, 1) >>> chip1, chip2, kpts1, kpts2, fm = get_dummy_test_vars1() >>> # execute function >>> result = test_sver(chip1, chip2, kpts1, kpts2, fm, nShow) >>> # verify results >>> print(result) >>> pt.show_if_requested() """ xy_thresh_sqrd = ktool.get_diag_extent_sqrd(kpts2) * xy_thresh def pack_errors(xy_err, scale_err, ori_err): """ makes human readable errors """ def _pack(bits, errs, thresh): return utool.indentjoin([ '%5s %f < %f' % (bit, err, thresh) for (bit, err) in zip(bits, errs) ]) xy_flag = xy_err < xy_thresh_sqrd scale_flag = scale_err < scale_thresh_sqrd ori_flag = ori_err < ori_thresh errors_dict = { 'xy_err': _pack(xy_flag, np.sqrt(xy_err), np.sqrt(xy_thresh_sqrd)), 'scale_err': _pack(scale_flag, np.sqrt(scale_err), np.sqrt(scale_thresh_sqrd)), 'ori_err': _pack(ori_flag, ori_err, ori_thresh), } return errors_dict # Test each affine hypothesis #assert kpts1.dtype == ktool.KPTS_DTYPE, 'bad cast somewhere kpts1.dtype=%r' % (kpts1.dtype) #assert kpts2.dtype == ktool.KPTS_DTYPE, 'bad cast somewhere kpts2.dtype=%r' % (kpts2.dtype) #assert xy_thresh_sqrd.dtype == ktool.KPTS_DTYPE, 'bad cast somewhere #xy_thresh_sqrd.dtype=%r' % (xy_thresh_sqrd.dtype) aff_hypo_tups = sver.get_affine_inliers(kpts1, kpts2, fm, xy_thresh_sqrd, scale_thresh_sqrd, ori_thresh) inliers_list, errors_list, Aff_mats = aff_hypo_tups # Determine best hypothesis nInliers_list = np.array(list(map(len, inliers_list))) best_mxs = nInliers_list.argsort()[::-1] for fnum, mx in enumerate(best_mxs[0:min(len(best_mxs), nShow)]): Aff = Aff_mats[mx] aff_inliers = inliers_list[mx] if utool.get_argflag('--print-error'): errors = pack_errors(*errors_list[mx]) # NOQA print(utool.dict_str(errors, strvals=True)) homog_inliers, homog_errors, H = sver.get_homography_inliers( kpts1, kpts2, fm, aff_inliers, xy_thresh_sqrd) kpts1_At = ktool.transform_kpts(kpts1, Aff) kpts1_Ht = ktool.transform_kpts(kpts1, H) kpts = kpts1 M = H homog_tup = (homog_inliers, H) aff_tup = (aff_inliers, Aff) _args = (chip1, chip2, kpts1, kpts2, fm) _kw = dict(show_assign=True, show_kpts=True, mx=mx, fnum=fnum * 3) draw_sv.show_sv(*_args, aff_tup=aff_tup, homog_tup=homog_tup, **_kw) #draw_sv.show_sv(*_args, aff_tup=aff_tup, mx=mx, fnum=fnum * 3) #draw_sv.show_sv(*_args, homog_tup=homog_tup, mx=mx, fnum=3) df2.set_figtitle('# %r inliers (in rects, hypo in bold)' % (nInliers_list[mx], )) return locals()
def show_name(ibs, nid, in_image=True, fnum=0, sel_aids=[], subtitle='', annote=False, aid_list=None, index_list=None, **kwargs): r""" Args: ibs (IBEISController): ibeis controller object nid (?): in_image (bool): fnum (int): figure number sel_aids (list): subtitle (str): annote (bool): CommandLine: python -m ibeis.viz.viz_name --test-show_name --dpath ~/latex/crall-candidacy-2015 --save 'figures/{name}.jpg' --no-figtitle --notitle --db NNP_Master3 --figsize=9,4 --clipwhite --dpi=180 --adjust=.05 --index_list=[0,1,2,3] --rc=2,4 --append temp_out_figure.tex --name=IBEIS_PZ_0739 --no-draw_lbls --doboth --no-inimage --diskshow python -m ibeis.viz.viz_name --test-show_name --no-figtitle --notitle --db NNP_Master3 --figsize=9,4 --clipwhite --dpi=180 --adjust=.05 --index_list=[0,1,2,3] --rc=2,4 --append temp_out_figure.tex --name=IBEIS_PZ_0739 --no-draw_lbls --doboth --no-inimage --show python -m ibeis.viz.viz_name --test-show_name --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.viz.viz_name import * # NOQA >>> ibs, nid, in_image, index_list = testdata_showname() >>> fnum = 0 >>> sel_aids = [] >>> subtitle = '' >>> annote = False >>> # execute function >>> show_name(ibs, nid, in_image, fnum, sel_aids, subtitle, annote, index_list=index_list) >>> ut.show_if_requested() """ print('[viz_name] show_name nid=%r, index_list=%r, aid_list=%r' % (nid, index_list, aid_list)) if aid_list is None: aid_list = ibs.get_name_aids(nid) else: assert ut.list_all_eq_to(ibs.get_annot_nids(aid_list), nid) if index_list is not None: aid_list = ut.take(aid_list, index_list) name = ibs.get_name_texts((nid, )) print('[viz_name] * name=%r aid_list=%r' % (name, aid_list)) show_multiple_chips(ibs, aid_list, in_image=in_image, fnum=fnum, sel_aids=sel_aids, annote=annote, **kwargs) if isinstance(nid, np.ndarray): nid = nid[0] if isinstance(name, np.ndarray): name = name[0] use_figtitle = not ut.get_argflag('--no-figtitle') if use_figtitle: figtitle = 'Name View nid=%r name=%r' % (nid, name) df2.set_figtitle(figtitle)
def dump_feature_pair_analysis(allres): print('[rr2] Doing: feature pair analysis') # TODO: Measure score consistency over a spatial area. # Measures entropy of matching vs nonmatching descriptors # Measures scale of m vs nm desc ibs = allres.ibs qrid2_qres = allres.qrid2_qres def _hist_prob_x(desc, bw_factor): # Choose number of bins based on the bandwidth bin_range = (0, 256) # assuming input is uint8 bins = bin_range[1] // bw_factor bw_factor = bin_range[1] / bins # Compute the probabilty mass function, each w.r.t a single descriptor hist_params = dict(bins=bins, range=bin_range, density=True) hist_func = np.histogram desc_pmf = [hist_func(d, **hist_params)[0] for d in desc] # Compute the probability that you saw what you saw # TODO: could use linear interpolation for a bit more robustness here bin_vals = [ np.array(np.floor(d / bw_factor), dtype=np.uint8) for d in desc ] hist_prob_x = [pmf[vals] for pmf, vals in zip(desc_pmf, bin_vals)] return hist_prob_x def _gkde_prob_x(desc, bw_factor): # Estimate the probabilty density function, each w.r.t a single descriptor gkde_func = scipy.stats.gaussian_kde desc_pdf = [gkde_func(d, bw_factor) for d in desc] gkde_prob_x = [pdf(d) for pdf, d in zip(desc_pdf, desc)] return gkde_prob_x def descriptor_entropy(desc, bw_factor=4): 'computes the shannon entropy of each descriptor in desc' # Compute shannon entropy = -sum(p(x)*log(p(x))) prob_x = _hist_prob_x(desc, bw_factor) entropy = [-(px * np.log2(px)).sum() for px in prob_x] return entropy # Load features if we need to if ibs.feats.cx2_desc.size == 0: print(' * forcing load of descriptors') ibs.load_features() cx2_desc = ibs.feats.cx2_desc cx2_kpts = ibs.feats.cx2_kpts 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 = utool.make_progress_fmt_str(len(orgres), lbl) rank_skips = [] gt_skips = [] for ix, (qrid, rid, score, rank) in enumerate(orgres.iter()): utool.print_(fmt_str % (ix + 1, )) # Skip low ranks if rank > 5: rank_skips.append(qrid) continue other_rids = ibs.get_other_indexed_rids(qrid) # Skip no groundtruth if len(other_rids) == 0: gt_skips.append(qrid) continue qres = qrid2_qres[qrid] # Get matching feature indexes fm = qres.cx2_fm[rid] # Get their scores fs = qres.cx2_fs[rid] # Get matching descriptors printDBG('\nfm.shape=%r' % (fm.shape, )) desc1 = cx2_desc[qrid][fm[:, 0]] desc2 = cx2_desc[rid][fm[:, 1]] # Get matching keypoints kpts1 = cx2_kpts[qrid][fm[:, 0]] kpts2 = cx2_kpts[rid][fm[:, 1]] # Get their scale scale1_m = ktool.get_scales(kpts1) scale2_m = ktool.get_scales(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 tt_entropy, tt_scale, tt_scores = measure_feat_pairs(allres, 'top_true') tf_entropy, tf_scale, tf_scores = measure_feat_pairs(allres, 'top_false') # Measure ratios def measure_ratio(arr): return arr[:, 0] / arr[:, 1] if len(arr) > 0 else np.array([]) tt_entropy_ratio = measure_ratio(tt_entropy) tf_entropy_ratio = measure_ratio(tf_entropy) tt_scale_ratio = measure_ratio(tt_scale) tf_scale_ratio = measure_ratio(tf_scale) title_suffix = allres.title_suffix # Entropy vs Score df2.figure(fnum=1, docla=True) df2.figure(fnum=1, plotnum=(2, 2, 1)) df2.plot2(tt_entropy[:, 0], tt_scores, 'gx', 'entropy1', 'score', 'Top True') df2.figure(fnum=1, plotnum=(2, 2, 2)) df2.plot2(tf_entropy[:, 0], tf_scores, 'rx', 'entropy1', 'score', 'Top False') df2.figure(fnum=1, plotnum=(2, 2, 3)) df2.plot2(tt_entropy[:, 1], tt_scores, 'gx', 'entropy2', 'score', 'Top True') df2.figure(fnum=1, plotnum=(2, 2, 4)) df2.plot2(tf_entropy[:, 1], tf_scores, 'rx', 'entropy2', 'score', 'Top False') df2.set_figtitle('Entropy vs Score -- ' + title_suffix) viz.__dump_or_browse(allres, 'pair_analysis') # Scale vs Score df2.figure(fnum=2, plotnum=(2, 2, 1), docla=True) df2.plot2(tt_scale[:, 0], tt_scores, 'gx', 'scale1', 'score', 'Top True') df2.figure(fnum=2, plotnum=(2, 2, 2)) df2.plot2(tf_scale[:, 0], tf_scores, 'rx', 'scale1', 'score', 'Top False') df2.figure(fnum=2, plotnum=(2, 2, 3)) df2.plot2(tt_scale[:, 1], tt_scores, 'gx', 'scale2', 'score', 'Top True') df2.figure(fnum=2, plotnum=(2, 2, 4)) df2.plot2(tf_scale[:, 1], tf_scores, 'rx', 'scale2', 'score', 'Top False') df2.set_figtitle('Scale vs Score -- ' + title_suffix) viz.__dump_or_browse(allres, 'pair_analysis') # Entropy Ratio vs Score df2.figure(fnum=3, plotnum=(1, 2, 1), docla=True) df2.plot2(tt_entropy_ratio, tt_scores, 'gx', 'entropy-ratio', 'score', 'Top True') df2.figure(fnum=3, plotnum=(1, 2, 2)) df2.plot2(tf_entropy_ratio, tf_scores, 'rx', 'entropy-ratio', 'score', 'Top False') df2.set_figtitle('Entropy Ratio vs Score -- ' + title_suffix) viz.__dump_or_browse(allres, 'pair_analysis') # Scale Ratio vs Score df2.figure(fnum=4, plotnum=(1, 2, 1), docla=True) df2.plot2(tt_scale_ratio, tt_scores, 'gx', 'scale-ratio', 'score', 'Top True') df2.figure(fnum=4, plotnum=(1, 2, 2)) df2.plot2(tf_scale_ratio, tf_scores, 'rx', 'scale-ratio', 'score', 'Top False') df2.set_figtitle('Entropy Ratio vs Score -- ' + title_suffix) viz.__dump_or_browse(allres, 'pair_analysis')
def select_ith_match(self, mx): """ Selects the ith match and visualizes and prints information concerning features weights, keypoint details, and sift descriptions Args: mx (int) - the ith match to visualize qaid (int) - query annotation id aid (int) - database annotation id CommandLine: python -m ibeis.viz.interact.interact_matches --test-select_ith_match --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.viz.interact.interact_matches import * # NOQA >>> self = testdata_match_interact(mx=1) >>> pt.show_if_requested() """ ibs = self.ibs qaid = self.qaid aid = self.daid fnum = self.fnum figtitle = self.figtitle rchip1 = self.rchip1 rchip2 = self.rchip2 aid = self.daid same_fig = self.same_fig self.mx = mx print('+--- SELECT --- ') print('qaid=%r, daid=%r' % (qaid, aid)) print('... selecting mx-th=%r feature match' % mx) if False: print('score stats:') print(ut.get_stats_str(self.fsv, axis=0, newlines=True)) print('fsv[mx] = %r' % (self.fsv[mx],)) print('fs[mx] = %r' % (self.fs[mx],)) """ # test feature weights of actual chips fx1, fx2 = fm[mx] daid = aid ibs.get_annot_fgweights([daid])[0][fx2] ibs.get_annot_fgweights([qaid])[0][fx1] """ #---------------------- # Get info for the select_ith_match plot self.mode = 1 # Get the mx-th feature match fx1, fx2 = self.fm[mx] fscore2 = self.fs[mx] fk2 = self.fk[mx] kpts1 = ibs.get_annot_kpts([self.qaid], config2_=self.query_config2_)[0] kpts2 = ibs.get_annot_kpts([self.daid], config2_=self.data_config2_)[0] desc1 = ibs.get_annot_vecs([self.qaid], config2_=self.query_config2_)[0] desc2 = ibs.get_annot_vecs([self.daid], config2_=self.data_config2_)[0] kp1, kp2 = kpts1[fx1], kpts2[fx2] sift1, sift2 = desc1[fx1], desc2[fx2] info1 = '\nquery' info2 = '\nk=%r fscore=%r' % (fk2, fscore2) #last_state.last_fx = fx1 self.last_fx = fx1 # Extracted keypoints to draw extracted_list = [(rchip1, kp1, sift1, fx1, self.qaid, info1), (rchip2, kp2, sift2, fx2, self.daid, info2)] # Normalizng Keypoint #if hasattr(cm, 'filt2_meta') and 'lnbnn' in cm.filt2_meta: # qfx2_norm = cm.filt2_meta['lnbnn'] # # Normalizing chip and feature # (aid3, fx3, normk) = qfx2_norm[fx1] # rchip3 = ibs.get_annot_chips(aid3) # kp3 = ibs.get_annot_kpts(aid3)[fx3] # sift3 = ibs.get_annot_vecs(aid3)[fx3] # info3 = '\nnorm %s k=%r' % (vh.get_aidstrs(aid3), normk) # extracted_list.append((rchip3, kp3, sift3, fx3, aid3, info3)) #else: # pass # print('WARNING: meta doesnt exist') #---------------------- # Draw the select_ith_match plot nRows, nCols = len(extracted_list) + same_fig, 3 # Draw matching chips and features sel_fm = np.array([(fx1, fx2)]) pnum1 = (nRows, 1, 1) if same_fig else (1, 1, 1) vert = self.vert if self.vert is not None else False self.chipmatch_view(pnum1, ell_alpha=.4, ell_linewidth=1.8, colors=df2.BLUE, sel_fm=sel_fm, vert=vert) # Draw selected feature matches px = nCols * same_fig # plot offset prevsift = None if not same_fig: #fnum2 = fnum + len(viz.FNUMS) fnum2 = self.fnum2 fig2 = df2.figure(fnum=fnum2, docla=True, doclf=True) else: fnum2 = fnum for (rchip, kp, sift, fx, aid, info) in extracted_list: px = viz_featrow.draw_feat_row(rchip, fx, kp, sift, fnum2, nRows, nCols, px, prevsift=prevsift, aid=aid, info=info) prevsift = sift if not same_fig: ih.connect_callback(fig2, 'button_press_event', self.on_click) df2.set_figtitle(figtitle + vh.get_vsstr(qaid, aid))
def select_ith_match(self, mx): """ Selects the ith match and visualizes and prints information concerning features weights, keypoint details, and sift descriptions Args: mx (int) - the ith match to visualize qaid (int) - query annotation id aid (int) - database annotation id CommandLine: python -m ibeis.viz.interact.interact_matches --test-select_ith_match --show Example: >>> # DISABLE_DOCTEST >>> from ibeis.viz.interact.interact_matches import * # NOQA >>> self = testdata_match_interact(mx=1) >>> pt.show_if_requested() """ ibs = self.ibs qaid = self.qaid aid = self.daid fnum = self.fnum figtitle = self.figtitle rchip1 = self.rchip1 rchip2 = self.rchip2 aid = self.daid same_fig = self.same_fig self.mx = mx print('+--- SELECT --- ') print('qaid=%r, daid=%r' % (qaid, aid)) print('... selecting mx-th=%r feature match' % mx) if False: print('score stats:') print(ut.get_stats_str(self.fsv, axis=0, newlines=True)) print('fsv[mx] = %r' % (self.fsv[mx], )) print('fs[mx] = %r' % (self.fs[mx], )) """ # test feature weights of actual chips fx1, fx2 = fm[mx] daid = aid ibs.get_annot_fgweights([daid])[0][fx2] ibs.get_annot_fgweights([qaid])[0][fx1] """ #---------------------- # Get info for the select_ith_match plot self.mode = 1 # Get the mx-th feature match fx1, fx2 = self.fm[mx] fscore2 = self.fs[mx] fk2 = self.fk[mx] kpts1 = ibs.get_annot_kpts([self.qaid], config2_=self.query_config2_)[0] kpts2 = ibs.get_annot_kpts([self.daid], config2_=self.data_config2_)[0] desc1 = ibs.get_annot_vecs([self.qaid], config2_=self.query_config2_)[0] desc2 = ibs.get_annot_vecs([self.daid], config2_=self.data_config2_)[0] kp1, kp2 = kpts1[fx1], kpts2[fx2] sift1, sift2 = desc1[fx1], desc2[fx2] info1 = '\nquery' info2 = '\nk=%r fscore=%r' % (fk2, fscore2) #last_state.last_fx = fx1 self.last_fx = fx1 # Extracted keypoints to draw extracted_list = [(rchip1, kp1, sift1, fx1, self.qaid, info1), (rchip2, kp2, sift2, fx2, self.daid, info2)] # Normalizng Keypoint #if hasattr(cm, 'filt2_meta') and 'lnbnn' in cm.filt2_meta: # qfx2_norm = cm.filt2_meta['lnbnn'] # # Normalizing chip and feature # (aid3, fx3, normk) = qfx2_norm[fx1] # rchip3 = ibs.get_annot_chips(aid3) # kp3 = ibs.get_annot_kpts(aid3)[fx3] # sift3 = ibs.get_annot_vecs(aid3)[fx3] # info3 = '\nnorm %s k=%r' % (vh.get_aidstrs(aid3), normk) # extracted_list.append((rchip3, kp3, sift3, fx3, aid3, info3)) #else: # pass # print('WARNING: meta doesnt exist') #---------------------- # Draw the select_ith_match plot nRows, nCols = len(extracted_list) + same_fig, 3 # Draw matching chips and features sel_fm = np.array([(fx1, fx2)]) pnum1 = (nRows, 1, 1) if same_fig else (1, 1, 1) vert = self.vert if self.vert is not None else False self.chipmatch_view(pnum1, ell_alpha=.4, ell_linewidth=1.8, colors=df2.BLUE, sel_fm=sel_fm, vert=vert) # Draw selected feature matches px = nCols * same_fig # plot offset prevsift = None if not same_fig: #fnum2 = fnum + len(viz.FNUMS) fnum2 = self.fnum2 fig2 = df2.figure(fnum=fnum2, docla=True, doclf=True) else: fnum2 = fnum for (rchip, kp, sift, fx, aid, info) in extracted_list: px = viz_featrow.draw_feat_row(rchip, fx, kp, sift, fnum2, nRows, nCols, px, prevsift=prevsift, aid=aid, info=info) prevsift = sift if not same_fig: ih.connect_callback(fig2, 'button_press_event', self.on_click) df2.set_figtitle(figtitle + vh.get_vsstr(qaid, aid))
def _viz_keypoints(fnum, pnum=(1, 1, 1), **kwargs): df2.figure(fnum=fnum, docla=True, doclf=True) show_keypoints(chip, kpts, fnum=fnum, pnum=pnum, **kwargs) if figtitle is not None: df2.set_figtitle(figtitle)
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 dump_feature_pair_analysis(allres): print('[rr2] Doing: feature pair analysis') # TODO: Measure score consistency over a spatial area. # Measures entropy of matching vs nonmatching descriptors # Measures scale of m vs nm desc ibs = allres.ibs qrid2_qres = allres.qrid2_qres def _hist_prob_x(desc, bw_factor): # Choose number of bins based on the bandwidth bin_range = (0, 256) # assuming input is uint8 bins = bin_range[1] // bw_factor bw_factor = bin_range[1] / bins # Compute the probabilty mass function, each w.r.t a single descriptor hist_params = dict(bins=bins, range=bin_range, density=True) hist_func = np.histogram desc_pmf = [hist_func(d, **hist_params)[0] for d in desc] # Compute the probability that you saw what you saw # TODO: could use linear interpolation for a bit more robustness here bin_vals = [np.array(np.floor(d / bw_factor), dtype=np.uint8) for d in desc] hist_prob_x = [pmf[vals] for pmf, vals in zip(desc_pmf, bin_vals)] return hist_prob_x def _gkde_prob_x(desc, bw_factor): # Estimate the probabilty density function, each w.r.t a single descriptor gkde_func = scipy.stats.gaussian_kde desc_pdf = [gkde_func(d, bw_factor) for d in desc] gkde_prob_x = [pdf(d) for pdf, d in zip(desc_pdf, desc)] return gkde_prob_x def descriptor_entropy(desc, bw_factor=4): 'computes the shannon entropy of each descriptor in desc' # Compute shannon entropy = -sum(p(x)*log(p(x))) prob_x = _hist_prob_x(desc, bw_factor) entropy = [-(px * np.log2(px)).sum() for px in prob_x] return entropy # Load features if we need to if ibs.feats.cx2_desc.size == 0: print(' * forcing load of descriptors') ibs.load_features() cx2_desc = ibs.feats.cx2_desc cx2_kpts = ibs.feats.cx2_kpts 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 = utool.make_progress_fmt_str(len(orgres), lbl) rank_skips = [] gt_skips = [] for ix, (qrid, rid, score, rank) in enumerate(orgres.iter()): utool.print_(fmt_str % (ix + 1,)) # Skip low ranks if rank > 5: rank_skips.append(qrid) continue other_rids = ibs.get_other_indexed_rids(qrid) # Skip no groundtruth if len(other_rids) == 0: gt_skips.append(qrid) continue qres = qrid2_qres[qrid] # Get matching feature indexes fm = qres.cx2_fm[rid] # Get their scores fs = qres.cx2_fs[rid] # Get matching descriptors printDBG('\nfm.shape=%r' % (fm.shape,)) desc1 = cx2_desc[qrid][fm[:, 0]] desc2 = cx2_desc[rid][fm[:, 1]] # Get matching keypoints kpts1 = cx2_kpts[qrid][fm[:, 0]] kpts2 = cx2_kpts[rid][fm[:, 1]] # Get their scale scale1_m = ktool.get_scales(kpts1) scale2_m = ktool.get_scales(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 tt_entropy, tt_scale, tt_scores = measure_feat_pairs(allres, 'top_true') tf_entropy, tf_scale, tf_scores = measure_feat_pairs(allres, 'top_false') # Measure ratios def measure_ratio(arr): return arr[:, 0] / arr[:, 1] if len(arr) > 0 else np.array([]) tt_entropy_ratio = measure_ratio(tt_entropy) tf_entropy_ratio = measure_ratio(tf_entropy) tt_scale_ratio = measure_ratio(tt_scale) tf_scale_ratio = measure_ratio(tf_scale) title_suffix = allres.title_suffix # Entropy vs Score df2.figure(fnum=1, docla=True) df2.figure(fnum=1, plotnum=(2, 2, 1)) df2.plot2(tt_entropy[:, 0], tt_scores, 'gx', 'entropy1', 'score', 'Top True') df2.figure(fnum=1, plotnum=(2, 2, 2)) df2.plot2(tf_entropy[:, 0], tf_scores, 'rx', 'entropy1', 'score', 'Top False') df2.figure(fnum=1, plotnum=(2, 2, 3)) df2.plot2(tt_entropy[:, 1], tt_scores, 'gx', 'entropy2', 'score', 'Top True') df2.figure(fnum=1, plotnum=(2, 2, 4)) df2.plot2(tf_entropy[:, 1], tf_scores, 'rx', 'entropy2', 'score', 'Top False') df2.set_figtitle('Entropy vs Score -- ' + title_suffix) viz.__dump_or_browse(allres, 'pair_analysis') # Scale vs Score df2.figure(fnum=2, plotnum=(2, 2, 1), docla=True) df2.plot2(tt_scale[:, 0], tt_scores, 'gx', 'scale1', 'score', 'Top True') df2.figure(fnum=2, plotnum=(2, 2, 2)) df2.plot2(tf_scale[:, 0], tf_scores, 'rx', 'scale1', 'score', 'Top False') df2.figure(fnum=2, plotnum=(2, 2, 3)) df2.plot2(tt_scale[:, 1], tt_scores, 'gx', 'scale2', 'score', 'Top True') df2.figure(fnum=2, plotnum=(2, 2, 4)) df2.plot2(tf_scale[:, 1], tf_scores, 'rx', 'scale2', 'score', 'Top False') df2.set_figtitle('Scale vs Score -- ' + title_suffix) viz.__dump_or_browse(allres, 'pair_analysis') # Entropy Ratio vs Score df2.figure(fnum=3, plotnum=(1, 2, 1), docla=True) df2.plot2(tt_entropy_ratio, tt_scores, 'gx', 'entropy-ratio', 'score', 'Top True') df2.figure(fnum=3, plotnum=(1, 2, 2)) df2.plot2(tf_entropy_ratio, tf_scores, 'rx', 'entropy-ratio', 'score', 'Top False') df2.set_figtitle('Entropy Ratio vs Score -- ' + title_suffix) viz.__dump_or_browse(allres, 'pair_analysis') # Scale Ratio vs Score df2.figure(fnum=4, plotnum=(1, 2, 1), docla=True) df2.plot2(tt_scale_ratio, tt_scores, 'gx', 'scale-ratio', 'score', 'Top True') df2.figure(fnum=4, plotnum=(1, 2, 2)) df2.plot2(tf_scale_ratio, tf_scores, 'rx', 'scale-ratio', 'score', 'Top False') df2.set_figtitle('Entropy Ratio vs Score -- ' + title_suffix) viz.__dump_or_browse(allres, 'pair_analysis')
def plot_scores2(hs, qcx_list, fnum=1): print('[dev] plot_scores(fnum=%r)' % fnum) qcx2_res = get_qcx2_res(hs, qcx_list) all_score_list = [] gtscore_ys = [] gtscore_xs = [] gtscore_ranks = [] EXCLUDE_ZEROS = False N = 1 # Append all scores to a giant list for res in qcx2_res.itervalues(): cx2_score = res.cx2_score # Get gt scores first #gt_cxs = hs.get_other_indexed_cxs(res.qcx) gt_cxs = np.array(res.topN_cxs(hs, N=N, only_gt=True)) gt_ys = cx2_score[gt_cxs] if EXCLUDE_ZEROS: nonzero_cxs = np.where(cx2_score != 0)[0] gt_cxs = gt_cxs[gt_ys != 0] gt_ranks = res.get_gt_ranks(gt_cxs) gt_cxs = np.array(utool.list_index(nonzero_cxs, gt_cxs)) gt_ys = gt_ys[gt_ys != 0] score_list = cx2_score[nonzero_cxs].tolist() else: score_list = cx2_score.tolist() gt_ranks = res.get_gt_ranks(gt_cxs) gtscore_ys.extend(gt_ys) gtscore_xs.extend(gt_cxs + len(all_score_list)) gtscore_ranks.extend(gt_ranks) # Append all scores all_score_list.extend(score_list) all_score_list = np.array(all_score_list) gtscore_ranks = np.array(gtscore_ranks) gtscore_ys = np.array(gtscore_ys) # Sort all chipmatch scores allx_sorted = all_score_list.argsort() # mapping from sortedallx to allx allscores_sorted = all_score_list[allx_sorted] # Change the groundtruth positions to correspond to sorted cmatch scores # Find position of gtscore_xs in allx_sorted gtscore_sortxs = utool.list_index(allx_sorted, gtscore_xs) gtscore_sortxs = np.array(gtscore_sortxs) # Draw and info rank_bounds = [ (0, 1), (1, 5), (5, None) ] rank_colors = [ df2.TRUE_GREEN, df2.UNKNOWN_PURP, df2.FALSE_RED ] print('[dev] matching chipscore stats: ' + utool.stats_str(all_score_list)) df2.figure(fnum=fnum, doclf=True, docla=True) # Finds the knee df2.plot(allscores_sorted, color=df2.ORANGE, label='all scores') # get positions which are within rank bounds for count, ((low, high), rankX_color) in reversed(list(enumerate(zip(rank_bounds, rank_colors)))): rankX_flag_low = gtscore_ranks >= low if high is not None: rankX_flag_high = gtscore_ranks < high rankX_flag = np.logical_and(rankX_flag_low, rankX_flag_high) else: rankX_flag = rankX_flag_low rankX_allgtx = np.where(rankX_flag)[0] rankX_gtxs = gtscore_sortxs[rankX_allgtx] rankX_gtys = gtscore_ys[rankX_allgtx] rankX_label = '%d <= gt rank' % low if high is not None: rankX_label += ' < %d' % high if len(rankX_gtxs) > 0: df2.plot(rankX_gtxs, rankX_gtys, 'o', color=rankX_color, label=rankX_label) rowid = qcx2_res.itervalues().next().rowid df2.set_logyscale_from_data(allscores_sorted) df2.set_xlabel('chipmatch index') df2.dark_background() df2.set_figtitle('matching scores\n' + rowid) df2.legend(loc='upper left') #xmin = 0 #xmax = utool.order_of_magnitude_ceil(len(allscores_sorted)) #print('len_ = %r' % (len(allscores_sorted),)) #print('xmin = %r' % (xmin,)) #print('xmax = %r' % (xmax,)) #df2.gca().set_xlim(xmin, xmax) df2.update() #utool.embed() # Second Plot #data = sorted(zip(gtscore_sortxs, gtscore_ys, gtscore_ranks)) #gtxs = [x for (x, y, z) in data] #gtys = [y for (x, y, z) in data] #gtrs = [z for (x, y, z) in data] #nongtxs = np.setdiff1d(np.arange(gtxs[0], gtxs[-1]), gtxs) #min_ = min(gtxs) #max_ = len(allscores_sorted) #len_ = max_ - min_ #normsum = 0 #ratsum = 0 #gtxs = np.array(gtxs) #nongtxs = np.array(nongtxs) #for ix in xrange(min_, max_): #nongtxs_ = nongtxs[nongtxs >= ix] #gtxs_ = gtxs[gtxs >= ix] #numer = allscores_sorted[gtxs_].sum() #denom = allscores_sorted[nongtxs_].sum() #ratio = (1 + numer) / (denom + 1) #total_support = (len(gtxs_) + len(nongtxs_)) #normrat = ratio / total_support #ratsum += ratio #normsum += normrat #print(total_support) #print(ratsum / len_) #print(normsum / len_) #print(ratsum / allscores_sorted[min_:max_].sum()) #print(normsum / allscores_sorted[min_:max_].sum()) #index_gap = np.diff(gtxs) #score_gap = np.diff(gtys) #badness = (index_gap - 1) * score_gap #np.arange(len(gtxs)) fnum += 1 return fnum
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 plot_scores(hs, qcx_list, fnum=1): print('[dev] plot_scores(fnum=%r)' % fnum) topN_gt = 1 topN_ranks = 3 qcx2_res = get_qcx2_res(hs, qcx_list) data_scores = [] # matching scores data_qpairs = [] # query info (qcx, cx) data_gtranks = [] # query info (gtrank) # Append all scores to a giant list for res in qcx2_res.itervalues(): # Get gt scores first qcx = res.qcx top_cxs = np.array(res.topN_cxs(hs, N=topN_ranks, only_gt=False)) gt_cxs = np.array(res.topN_cxs(hs, N=topN_gt, only_gt=True)) top_scores = res.cx2_score[top_cxs] np.intersect1d(top_cxs, gt_cxs) # list of ranks if score is ground truth otherwise -1 isgt_ranks = [tx if cx in set(gt_cxs) else -1 for (tx, cx) in enumerate(top_cxs)] qcx_pairs = [(hs.cx2_cid(qcx), hs.cx2_cid(cx)) for cx in top_cxs] # Append all scores data_scores.extend(top_scores.tolist()) data_qpairs.extend(qcx_pairs) data_gtranks.extend(isgt_ranks) data_scores = np.array(data_scores) data_qpairs = np.array(data_qpairs) data_gtranks = np.array(data_gtranks) data_sortx = data_scores.argsort() sorted_scores = data_scores[data_sortx] # Draw and info rank_colorbounds = [ ((-1, 0), df2.GRAY), ((0, 1), df2.TRUE_GREEN), ((1, 5), df2.UNKNOWN_PURP), ((5, None), df2.FALSE_RED), ] print('[dev] matching chipscore stats: ' + utool.stats_str(data_scores)) df2.figure(fnum=fnum, doclf=True, docla=True) # Finds the knee df2.plot(sorted_scores, color=df2.ORANGE, label='all scores') # Plot results with ranks within (low, high) bounds colorbounds_iter = reversed(list(enumerate(rank_colorbounds))) for count, ((low, high), rankX_color) in colorbounds_iter: datarank_flag = utool.inbounds(data_gtranks, low, high) rankX_xs = np.where(datarank_flag[data_sortx])[0] rankX_ys = sorted_scores[rankX_xs] if high is None: rankX_label = '%d <= gt rank' % low else: rankX_label = '%d <= gt rank < %d' % (low, high) if len(rankX_ys) > 0: df2.plot(rankX_xs, rankX_ys, 'o', color=rankX_color, label=rankX_label, alpha=.5) rowid = qcx2_res.itervalues().next().rowid df2.set_logyscale_from_data(data_scores) df2.set_xlabel('chipmatch index') df2.dark_background() df2.set_figtitle('matching scores\n' + rowid) df2.legend(loc='upper left') df2.iup() fnum += 1 score_table = np.vstack((data_scores, data_gtranks, data_qpairs.T)).T score_table = score_table[data_sortx[::-1]] column_labels = ['score', 'gtrank', 'qcid', 'cid'] header = 'score_table\nuid=%r' % rowid column_type = [float, int, int, int] csv_txt = utool.util_csv.numpy_to_csv(score_table, column_labels, header, column_type) print(csv_txt) #utool.embed() return fnum
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()