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
def _draw_batch(harn, batch, decoded, limit=32): """ Example: >>> # xdoctest: +REQUIRES(--download) >>> harn = setup_harn().initialize() >>> # >>> batch = harn._demo_batch(0, tag='test') >>> outputs, loss = harn.run_batch(batch) >>> bx = harn.bxs[harn.current_tag] >>> decoded = harn._decode(outputs, batch['label']) >>> fpath = harn._draw_batch(bx, batch, decoded, limit=42) >>> print('fpath = {!r}'.format(fpath)) >>> # xdoctest: +REQUIRES(--show) >>> import kwplot >>> kwplot.autompl() >>> kwplot.imshow(fpath, colorspace='rgb', doclf=True) >>> kwplot.show_if_requested() """ import kwimage import kwplot inputs = batch['input'] inputs = inputs[0:limit] input_shape = inputs.shape dims = [160] * (len(input_shape) - 2) min_, max_ = inputs.min(), inputs.max() inputs = (inputs - min_) / (max_ - min_) inputs = torch.nn.functional.interpolate(inputs, size=dims) inputs = (inputs * 255).byte() inputs = inputs.data.cpu().numpy() dset = harn.datasets[harn.current_tag] true_cxs = batch['label'].data.cpu().numpy() pred_cxs = decoded['pred_cxs'].data.cpu().numpy() class_probs = decoded['class_probs'].data.cpu().numpy() todraw = [] for im, pcx, tcx, probs in zip(inputs, pred_cxs, true_cxs, class_probs): im_ = im.transpose(1, 2, 0) im_ = kwimage.convert_colorspace(im_, 'gray', 'rgb') im_ = np.ascontiguousarray(im_) im_ = kwplot.draw_clf_on_image(im_, dset.classes, tcx, probs) todraw.append(im_) stacked = kwimage.stack_images_grid(todraw, overlap=-10, bg_value=(10, 40, 30), chunksize=8) return stacked
def _draw_batch(harn, batch, outputs, limit=32): """ Example: >>> # xdoctest: +REQUIRES(--download) >>> harn = setup_harn(batch_size=3).initialize() >>> batch = harn._demo_batch(0, tag='train') >>> outputs, loss = harn.run_batch(batch) >>> stacked = harn._draw_batch(batch, outputs, limit=12) >>> # xdoctest: +REQUIRES(--show) >>> import kwplot >>> kwplot.autompl() >>> kwplot.imshow(stacked, colorspace='rgb', doclf=True) >>> kwplot.show_if_requested() """ import kwimage inputs = batch['inputs']['rgb'][0:limit].data.cpu().numpy() true_cxs = batch['labels']['class_idxs'].data.cpu().numpy() class_probs = outputs['class_probs'].data.cpu().numpy() pred_cxs = kwarray.ArrayAPI.numpy(outputs['pred_cxs']) dset = harn.datasets[harn.current_tag] classes = dset.classes todraw = [] for im, pcx, tcx, probs in zip(inputs, pred_cxs, true_cxs, class_probs): im_ = im.transpose(1, 2, 0) # Renormalize and resize image for drawing min_, max_ = im_.min(), im_.max() im_ = ((im_ - min_) / (max_ - min_) * 255).astype(np.uint8) im_ = np.ascontiguousarray(im_) im_ = kwimage.imresize(im_, dsize=(200, 200), interpolation='nearest') # Draw classification information on the image im_ = kwimage.draw_clf_on_image(im_, classes=classes, tcx=tcx, pcx=pcx, probs=probs) todraw.append(im_) stacked = kwimage.stack_images_grid(todraw, overlap=-10, bg_value=(10, 40, 30), chunksize=8) return stacked
def draw_batch(harn, batch, outputs, batch_dets, idx=None, thresh=None, orig_img=None, num_extra=3): """ Returns: np.ndarray: numpy image Example: >>> # DISABLE_DOCTSET >>> harn = setup_harn(bsize=1, datasets='special:voc', pretrained='lightnet') >>> harn.initialize() >>> batch = harn._demo_batch(0, 'train') >>> outputs, loss = harn.run_batch(batch) >>> batch_dets = harn.raw_model.coder.decode_batch(outputs) >>> stacked = harn.draw_batch(batch, outputs, batch_dets) >>> # xdoc: +REQUIRES(--show) >>> kwplot.autompl() # xdoc: +SKIP >>> kwplot.imshow(stacked) >>> kwplot.show_if_requested() """ import cv2 inputs = batch['im'] labels = batch['label'] orig_sizes = labels['orig_sizes'] classes = harn.datasets['train'].sampler.classes if idx is None: idxs = range(len(inputs)) else: idxs = [idx] imgs = [] for idx in idxs: chw01 = inputs[idx] pred_dets = batch_dets[idx] # pred_dets.meta['classes'] = classes import kwimage true_dets = kwimage.Detections( boxes=kwimage.Boxes(labels['cxywh'][idx], 'cxywh'), class_idxs=labels['class_idxs'][idx].view(-1), weights=labels['weight'][idx], classes=classes, ) pred_dets = pred_dets.numpy() true_dets = true_dets.numpy() true_dets = true_dets.compress(true_dets.class_idxs != -1) if thresh is not None: pred_dets = pred_dets.compress(pred_dets.scores > thresh) # only show so many predictions num_max = len(true_dets) + num_extra sortx = pred_dets.argsort(reverse=True) pred_dets = pred_dets.take(sortx[0:num_max]) hwc01 = chw01.cpu().numpy().transpose(1, 2, 0) inp_size = np.array(hwc01.shape[0:2][::-1]) true_dets.boxes.scale(inp_size, inplace=True) pred_dets.boxes.scale(inp_size, inplace=True) letterbox = harn.datasets[harn.current_tag].letterbox orig_size = orig_sizes[idx].cpu().numpy() target_size = inp_size img = letterbox._img_letterbox_invert(hwc01, orig_size, target_size) img = np.clip(img, 0, 1) # we are given the original image, to avoid artifacts from # inverting a downscale assert orig_img is None or orig_img.shape == img.shape true_dets.data['boxes'] = letterbox._boxes_letterbox_invert( true_dets.boxes, orig_size, target_size) pred_dets.data['boxes'] = letterbox._boxes_letterbox_invert( pred_dets.boxes, orig_size, target_size) # shift, scale, embed_size = letterbox._letterbox_transform(orig_size, target_size) # fig = kwplot.figure(doclf=True, fnum=1) # kwplot.imshow(img, colorspace='rgb') canvas = (img * 255).astype(np.uint8) canvas = true_dets.draw_on(canvas, color='green') canvas = pred_dets.draw_on(canvas, color='blue') canvas = cv2.resize(canvas, (300, 300)) imgs.append(canvas) stacked = imgs[0] if len(imgs) == 1 else kwimage.stack_images_grid( imgs) return stacked
def draw_batch(harn, batch, outputs, batch_dets, idx=None, thresh=None, orig_img=None): """ Returns: np.ndarray: numpy image Example: >>> # DISABLE_DOCTSET >>> harn = setup_yolo_harness(bsize=1) >>> harn.initialize() >>> batch = harn._demo_batch(0, 'train') >>> weights_fpath = light_yolo.demo_voc_weights() >>> state_dict = harn.xpu.load(weights_fpath)['weights'] >>> harn.model.module.load_state_dict(state_dict) >>> outputs, loss = harn.run_batch(batch) >>> harn.on_batch(batch, outputs, loss) >>> # xdoc: +REQUIRES(--show) >>> import kwplot >>> batch_dets = harn.model.module.postprocess(outputs) >>> kwplot.autompl() # xdoc: +SKIP >>> stacked = harn.draw_batch(batch, outputs, batch_dets, thresh=0.01) >>> kwplot.imshow(stacked) >>> kwplot.show_if_requested() """ import cv2 import kwimage inputs, labels = batch targets = labels['targets'] orig_sizes = labels['orig_sizes'] if idx is None: idxs = range(len(inputs)) else: idxs = [idx] imgs = [] for idx in idxs: chw01 = inputs[idx] target = targets[idx].view(-1, 5) pred_dets = batch_dets[idx] label_names = harn.datasets[harn.current_tag].label_names pred_dets.meta['classes'] = label_names true_dets = kwimage.Detections(boxes=kwimage.Boxes( target[:, 1:5], 'cxywh'), class_idxs=target[:, 0].int(), classes=label_names) pred_dets = pred_dets.numpy() true_dets = true_dets.numpy() true_dets = true_dets.compress(true_dets.class_idxs != -1) if thresh is not None: pred_dets = pred_dets.compress(pred_dets.scores > thresh) hwc01 = chw01.cpu().numpy().transpose(1, 2, 0) inp_size = np.array(hwc01.shape[0:2][::-1]) true_dets.boxes.scale(inp_size, inplace=True) pred_dets.boxes.scale(inp_size, inplace=True) letterbox = harn.datasets[harn.current_tag].letterbox orig_size = orig_sizes[idx].cpu().numpy() target_size = inp_size img = letterbox._img_letterbox_invert(hwc01, orig_size, target_size) img = np.clip(img, 0, 1) # we are given the original image, to avoid artifacts from # inverting a downscale assert orig_img is None or orig_img.shape == img.shape true_dets.data['boxes'] = letterbox._boxes_letterbox_invert( true_dets.boxes, orig_size, target_size) pred_dets.data['boxes'] = letterbox._boxes_letterbox_invert( pred_dets.boxes, orig_size, target_size) # shift, scale, embed_size = letterbox._letterbox_transform(orig_size, target_size) # fig = kwplot.figure(doclf=True, fnum=1) # kwplot.imshow(img, colorspace='rgb') canvas = (img * 255).astype(np.uint8) canvas = true_dets.draw_on(canvas, color='green') canvas = pred_dets.draw_on(canvas, color='blue') canvas = cv2.resize(canvas, (300, 300)) imgs.append(canvas) # if IS_PROFILING: # torch.cuda.synchronize() stacked = imgs[0] if len(imgs) == 1 else kwimage.stack_images_grid( imgs) return stacked