def dump_word_patches(ibs, vocabdir, invindex, wx_sample, metrics): """ Dumps word member patches to disk """ wx2_dpath = get_word_dpaths(vocabdir, wx_sample, metrics) # Write each patch from each annotation to disk idx2_daid = invindex.idx2_daid daids = invindex.daids idx2_dfx = invindex.idx2_dfx #maws_list = invindex.idx2_wxs[idxs] # Loop over all annotations skipping the ones without any words in the sample ax2_idxs = [np.where(idx2_daid == aid_)[0] for aid_ in ut.progiter(daids, 'Building Forward Index: ', freq=100)] patchdump_iter = ut.progiter(zip(daids, ax2_idxs), freq=1, lbl='Dumping Selected Patches: ', num=len(daids)) for aid, idxs in patchdump_iter: wxs_list = invindex.idx2_wxs[idxs] if len(set(ut.flatten(wxs_list)).intersection(set(wx_sample))) == 0: # skip this annotation continue fx_list = idx2_dfx[idxs] chip = ibs.get_annot_chips(aid) chip_kpts = ibs.get_annot_kpts(aid) nid = ibs.get_annot_name_rowids(aid) patches, subkpts = ptool.get_warped_patches(chip, chip_kpts) for fx, wxs, patch in zip(fx_list, wxs_list, patches): assert len(wxs) == 1, 'did you multiassign the database? If so implement it here too' for k, wx in enumerate(wxs): if wx not in wx_sample: continue patch_fname = 'patch_nid=%04d_aid=%04d_fx=%04d_k=%d' % (nid, aid, fx, k) fpath = join(wx2_dpath[wx], patch_fname) #gtool.imwrite(fpath, patch, fallback=True) gtool.imwrite_fallback(fpath, patch)
def compute_word_metrics(invindex): invindex.idx2_wxs = np.array(invindex.idx2_wxs) wx2_idxs = invindex.wx2_idxs idx2_dvec = invindex.idx2_dvec words = invindex.words wx2_pdist = {} wx2_wdist = {} wx2_nMembers = {} wx2_pdist_stats = {} wx2_wdist_stats = {} wordidx_iter = ut.progiter(six.iteritems(wx2_idxs), lbl='Word Dists: ', num=len(wx2_idxs), freq=200) for _item in wordidx_iter: wx, idxs = _item dvecs = idx2_dvec.take(idxs, axis=0) word = words[wx:wx + 1] wx2_pdist[wx] = spdist.pdist(dvecs) # pairwise dist between words wx2_wdist[wx] = ut.euclidean_dist(dvecs, word) # dist to word center wx2_nMembers[wx] = len(idxs) for wx, pdist in ut.progiter(six.iteritems(wx2_pdist), lbl='Word pdist Stats: ', num=len(wx2_idxs), freq=2000): wx2_pdist_stats[wx] = ut.get_stats(pdist) for wx, wdist in ut.progiter(six.iteritems(wx2_wdist), lbl='Word wdist Stats: ', num=len(wx2_idxs), freq=2000): wx2_wdist_stats[wx] = ut.get_stats(wdist) ut.print_stats(wx2_nMembers.values(), 'word members') metrics = Metrics(wx2_nMembers, wx2_pdist_stats, wx2_wdist_stats) return metrics
def test_featcfg_combo(ibs, aid, alldictcomb, count, nKpts_list, cfgstr_list): for dict_ in ut.progiter(alldictcomb, lbl='FeatCFG Combo: '): # Set ibs parameters to the current config for key_, val_ in six.iteritems(dict_): ibs.cfg.feat_cfg[key_] = val_ cfgstr_ = ibs.cfg.feat_cfg.get_cfgstr() if count == 0: # On first run just record info kpts = ibs.get_annot_kpts(aid) nKpts_list.append(len(kpts)) cfgstr_list.append(cfgstr_) if count == 1: kpts = ibs.get_annot_kpts(aid) # If second run happens display info cfgpackstr = utool.packstr(cfgstr_, textwidth=80, breakchars=',', newline_prefix='', break_words=False, wordsep=',') title_suffix = (' len(kpts) = %r \n' % len(kpts)) + cfgpackstr viz.show_chip(ibs, aid, fnum=pt.next_fnum(), title_suffix=title_suffix, darken=.8, ell_linewidth=2, ell_alpha=.6)
def dump_word_patches(ibs, vocabdir, invindex, wx_sample, metrics): """ Dumps word member patches to disk """ wx2_dpath = get_word_dpaths(vocabdir, wx_sample, metrics) # Write each patch from each annotation to disk idx2_daid = invindex.idx2_daid daids = invindex.daids idx2_dfx = invindex.idx2_dfx #maws_list = invindex.idx2_wxs[idxs] # Loop over all annotations skipping the ones without any words in the sample ax2_idxs = [ np.where(idx2_daid == aid_)[0] for aid_ in ut.progiter(daids, 'Building Forward Index: ', freq=100) ] patchdump_iter = ut.progiter(zip(daids, ax2_idxs), freq=1, lbl='Dumping Selected Patches: ', num=len(daids)) for aid, idxs in patchdump_iter: wxs_list = invindex.idx2_wxs[idxs] if len(set(ut.flatten(wxs_list)).intersection(set(wx_sample))) == 0: # skip this annotation continue fx_list = idx2_dfx[idxs] chip = ibs.get_annot_chips(aid) chip_kpts = ibs.get_annot_kpts(aid) nid = ibs.get_annot_name_rowids(aid) patches, subkpts = ptool.get_warped_patches(chip, chip_kpts) for fx, wxs, patch in zip(fx_list, wxs_list, patches): assert len( wxs ) == 1, 'did you multiassign the database? If so implement it here too' for k, wx in enumerate(wxs): if wx not in wx_sample: continue patch_fname = 'patch_nid=%04d_aid=%04d_fx=%04d_k=%d' % ( nid, aid, fx, k) fpath = join(wx2_dpath[wx], patch_fname) #gtool.imwrite(fpath, patch, fallback=True) gtool.imwrite_fallback(fpath, patch)
def get_word_dpaths(vocabdir, wx_sample, metrics): """ Gets word folder names and ensure they exist """ ut.ensuredir(vocabdir) wx2_dpath = {wx: join(vocabdir, get_word_dname(wx, metrics)) for wx in wx_sample} iter_ = ut.progiter(six.itervalues(wx2_dpath), lbl='Ensuring word_dpath: ', freq=200) for dpath in iter_: ut.ensuredir(dpath) return wx2_dpath
def get_word_dpaths(vocabdir, wx_sample, metrics): """ Gets word folder names and ensure they exist """ ut.ensuredir(vocabdir) wx2_dpath = { wx: join(vocabdir, get_word_dname(wx, metrics)) for wx in wx_sample } iter_ = ut.progiter(six.itervalues(wx2_dpath), lbl='Ensuring word_dpath: ', freq=200) for dpath in iter_: ut.ensuredir(dpath) return wx2_dpath
def make_wordfigures(ibs, metrics, invindex, figdir, wx_sample, wx2_dpath): """ Builds mosaics of patches assigned to words in sample ouptuts them to disk """ from plottool import draw_func2 as df2 import vtool as vt import parse vocabdir = join(figdir, 'vocab_patches2') ut.ensuredir(vocabdir) dump_word_patches(ibs, vocabdir, invindex, wx_sample, metrics) # COLLECTING PART --- collects patches in word folders #vocabdir seldpath = vocabdir + '_selected' ut.ensurepath(seldpath) # stack for show for wx, dpath in ut.progiter(six.iteritems(wx2_dpath), lbl='Dumping Word Images:', num=len(wx2_dpath), freq=1, backspace=False): #df2.rrr() fpath_list = ut.ls(dpath) fname_list = [basename(fpath_) for fpath_ in fpath_list] patch_list = [vt.imread(fpath_) for fpath_ in fpath_list] # color each patch by nid nid_list = [ int(parse.parse('{}_nid={nid}_{}', fname)['nid']) for fname in fname_list ] nid_set = set(nid_list) nid_list = np.array(nid_list) if len(nid_list) == len(nid_set): # no duplicate names newpatch_list = patch_list else: # duplicate names. do coloring sortx = nid_list.argsort() patch_list = np.array(patch_list, dtype=object)[sortx] fname_list = np.array(fname_list, dtype=object)[sortx] nid_list = nid_list[sortx] colors = (255 * np.array(df2.distinct_colors(len(nid_set)))).astype( np.int32) color_dict = dict(zip(nid_set, colors)) wpad, hpad = 3, 3 newshape_list = [ tuple( (np.array(patch.shape) + (wpad * 2, hpad * 2, 0)).tolist()) for patch in patch_list ] color_list = [color_dict[nid_] for nid_ in nid_list] newpatch_list = [ np.zeros(shape) + color[None, None] for shape, color in zip(newshape_list, color_list) ] for patch, newpatch in zip(patch_list, newpatch_list): newpatch[wpad:-wpad, hpad:-hpad, :] = patch #img_list = patch_list #bigpatch = vt.stack_image_recurse(patch_list) #bigpatch = vt.stack_image_list(patch_list, vert=False) bigpatch = vt.stack_square_images(newpatch_list) bigpatch_fpath = join(seldpath, basename(dpath) + '_patches.png') # def _dictstr(dict_): str_ = ut.dict_str(dict_, newlines=False) str_ = str_.replace('\'', '').replace(': ', '=').strip('{},') return str_ figtitle = '\n'.join([ 'wx=%r' % wx, 'stat(pdist): %s' % _dictstr(metrics.wx2_pdist_stats[wx]), 'stat(wdist): %s' % _dictstr(metrics.wx2_wdist_stats[wx]), ]) metrics.wx2_nMembers[wx] df2.figure(fnum=1, doclf=True, docla=True) fig, ax = df2.imshow(bigpatch, figtitle=figtitle) #fig.show() df2.set_figtitle(figtitle) df2.adjust_subplots(top=.878, bottom=0) df2.save_figure(1, bigpatch_fpath)
def make_wordfigures(ibs, metrics, invindex, figdir, wx_sample, wx2_dpath): """ Builds mosaics of patches assigned to words in sample ouptuts them to disk """ from plottool import draw_func2 as df2 import vtool as vt import parse vocabdir = join(figdir, 'vocab_patches2') ut.ensuredir(vocabdir) dump_word_patches(ibs, vocabdir, invindex, wx_sample, metrics) # COLLECTING PART --- collects patches in word folders #vocabdir seldpath = vocabdir + '_selected' ut.ensurepath(seldpath) # stack for show for wx, dpath in ut.progiter(six.iteritems(wx2_dpath), lbl='Dumping Word Images:', num=len(wx2_dpath), freq=1, backspace=False): #df2.rrr() fpath_list = ut.ls(dpath) fname_list = [basename(fpath_) for fpath_ in fpath_list] patch_list = [gtool.imread(fpath_) for fpath_ in fpath_list] # color each patch by nid nid_list = [int(parse.parse('{}_nid={nid}_{}', fname)['nid']) for fname in fname_list] nid_set = set(nid_list) nid_list = np.array(nid_list) if len(nid_list) == len(nid_set): # no duplicate names newpatch_list = patch_list else: # duplicate names. do coloring sortx = nid_list.argsort() patch_list = np.array(patch_list, dtype=object)[sortx] fname_list = np.array(fname_list, dtype=object)[sortx] nid_list = nid_list[sortx] colors = (255 * np.array(df2.distinct_colors(len(nid_set)))).astype(np.int32) color_dict = dict(zip(nid_set, colors)) wpad, hpad = 3, 3 newshape_list = [tuple((np.array(patch.shape) + (wpad * 2, hpad * 2, 0)).tolist()) for patch in patch_list] color_list = [color_dict[nid_] for nid_ in nid_list] newpatch_list = [np.zeros(shape) + color[None, None] for shape, color in zip(newshape_list, color_list)] for patch, newpatch in zip(patch_list, newpatch_list): newpatch[wpad:-wpad, hpad:-hpad, :] = patch #img_list = patch_list #bigpatch = vt.stack_image_recurse(patch_list) #bigpatch = vt.stack_image_list(patch_list, vert=False) bigpatch = vt.stack_square_images(newpatch_list) bigpatch_fpath = join(seldpath, basename(dpath) + '_patches.png') # def _dictstr(dict_): str_ = ut.dict_str(dict_, newlines=False) str_ = str_.replace('\'', '').replace(': ', '=').strip('{},') return str_ figtitle = '\n'.join([ 'wx=%r' % wx, 'stat(pdist): %s' % _dictstr(metrics.wx2_pdist_stats[wx]), 'stat(wdist): %s' % _dictstr(metrics.wx2_wdist_stats[wx]), ]) metrics.wx2_nMembers[wx] df2.figure(fnum=1, doclf=True, docla=True) fig, ax = df2.imshow(bigpatch, figtitle=figtitle) #fig.show() df2.set_figtitle(figtitle) df2.adjust_subplots(top=.878, bottom=0) df2.save_figure(1, bigpatch_fpath)