def _draw_batch_preds(harn, batch, outputs, lim=16): """ Example: >>> # xdoctest: +REQUIRES(--slow) >>> kw = {'workers': 0, 'xpu': 'cpu', 'batch_size': 8} >>> harn = setup_harn(cmdline=False, **kw).initialize() >>> batch = harn._demo_batch(tag='train') >>> outputs, loss_parts = harn.run_batch(batch) >>> toshow = harn._draw_batch_preds(batch, outputs) >>> # xdoctest: +REQUIRES(--show) >>> import kwplot >>> kwplot.autompl() >>> kwplot.imshow(toshow) """ import cv2 im = batch['im'].data.cpu().numpy() class_true = batch['class_idxs'].data.cpu().numpy() class_pred = outputs['class_probs'].data.cpu().numpy().argmax(axis=1) batch_imgs = [] for bx in range(min(len(class_true), lim)): orig_img = im[bx].transpose(1, 2, 0) out_size = class_pred[bx].shape[::-1] orig_img = cv2.resize(orig_img, tuple(map(int, out_size))) orig_img = kwimage.ensure_alpha_channel(orig_img) pred_heatmap = kwimage.Heatmap( class_idx=class_pred[bx], classes=harn.classes ) true_heatmap = kwimage.Heatmap( class_idx=class_true[bx], classes=harn.classes ) # TODO: scale up to original image size pred_img = pred_heatmap.draw_on(orig_img, channel='idx', with_alpha=.5) true_img = true_heatmap.draw_on(orig_img, channel='idx', with_alpha=.5) true_img = kwimage.ensure_uint255(true_img) pred_img = kwimage.ensure_uint255(pred_img) true_img = kwimage.draw_text_on_image( true_img, 'true', org=(0, 0), valign='top', color='blue') pred_img = kwimage.draw_text_on_image( pred_img, 'pred', org=(0, 0), valign='top', color='blue') item_img = kwimage.stack_images([pred_img, true_img], axis=1) batch_imgs.append(item_img) toshow = kwimage.stack_images_grid(batch_imgs, chunksize=2, overlap=-32) return toshow
def _draw_batch(harn, batch, decoded, limit=32): """ Example: >>> from ggr_matching import * >>> harn = setup_harn().initialize() >>> batch = harn._demo_batch(0, tag='vali') >>> outputs, loss = harn.run_batch(batch) >>> decoded = harn._decode(outputs) >>> stacked = harn._draw_batch(batch, decoded, limit=42) >>> # xdoctest: +REQUIRES(--show) >>> import kwplot >>> kwplot.autompl() >>> kwplot.imshow(stacked, colorspace='rgb', doclf=True) >>> kwplot.show_if_requested() """ tostack = [] fontkw = {'fontScale': 1.0, 'thickness': 2} n = min(limit, len(decoded['triple_imgs'][0])) dsize = (300, 300) for i in range(n): ims = [g[i].transpose(1, 2, 0) for g in decoded['triple_imgs']] ims = [cv2.resize(g, dsize) for g in ims] ims = [kwimage.atleast_3channels(g) for g in ims] triple_nxs = [n[i] for n in decoded['triple_nxs']] text = 'distAP={:.3g} -- distAN={:.3g} -- {}'.format( decoded['distAP'][i], decoded['distAN'][i], str(triple_nxs), ) color = ('dodgerblue' if decoded['distAP'][i] < decoded['distAN'][i] else 'orangered') img = kwimage.stack_images(ims, overlap=-2, axis=1, bg_value=(10 / 255, 40 / 255, 30 / 255)) img = (img * 255).astype(np.uint8) img = kwimage.draw_text_on_image(img, text, org=(2, img.shape[0] - 2), color=color, **fontkw) tostack.append(img) stacked = kwimage.stack_images_grid(tostack, overlap=-10, bg_value=(30, 10, 40), axis=1, chunksize=3) return stacked
from scipy import integrate fpath = ub.grabdata('https://i.redd.it/ywip9sbwysy71.jpg') data = kwimage.imread(fpath) subdata = data[242:-22, 22:300] img = subdata inty = integrate.cumtrapz(img, axis=0) intx = integrate.cumtrapz(img, axis=1) dery = np.gradient(img, axis=0) derx = np.gradient(img, axis=1) der_canvas = kwarray.normalize(kwimage.stack_images([dery, derx], axis=0)) int_canvas = kwarray.normalize(kwimage.stack_images([inty, intx], axis=0)) der_canvas = kwimage.ensure_uint255(der_canvas) int_canvas = kwimage.ensure_uint255(int_canvas) der_canvas = kwimage.draw_header_text(der_canvas, 'derivative', color='white') int_canvas = kwimage.draw_header_text(int_canvas, 'antiderivative', color='white') canvas = kwimage.stack_images([der_canvas, int_canvas], axis=1) # kwimage.imwrite('ftfy.jpg', canvas) kwplot.autompl() kwplot.imshow(canvas)
if len(t) == 1: fontScale = 4 thickness = 6 else: fontScale = 1 thickness = 4 if a is None: text = t else: text = a + '\n\n' + t key = kwimage.draw_text_on_image(blank_key.copy(), text=text, halign='center', valign='center', color='white', fontScale=4, thickness=thickness) row_keys.append(key) row = kwimage.stack_images(row_keys, axis=1, pad=1) row_stack.append(row) left_side = kwimage.stack_images(row_stack, axis=0, pad=1) right_rows = [] alt_text0 = [None, None, None, None, None, None, None, None] row_text0 = ['Prt\nScn', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12', 'DEL'] right_rows += [(alt_text0, row_text0)] alt_text1 = [None, '^', '&', '*', '(', ')', '_', '+'] row_text1 = ['win', '6', '7', '8', '9', '0', '-', '='] right_rows += [(alt_text1, row_text1)]