def evaluate(self, output, batch): detection = output['detection'] score = detection[:, 4].detach().cpu().numpy() label = detection[:, 5].detach().cpu().numpy().astype(int) py = output['py'][-1].detach().cpu().numpy() * snake_config.down_ratio if len(py) == 0: return img_id = int(batch['meta']['img_id'][0]) center = batch['meta']['center'][0].detach().cpu().numpy() scale = batch['meta']['scale'][0].detach().cpu().numpy() h, w = batch['inp'].size(2), batch['inp'].size(3) trans_output_inv = data_utils.get_affine_transform(center, scale, 0, [w, h], inv=1) img = self.coco.loadImgs(img_id)[0] ori_h, ori_w = img['height'], img['width'] py = [data_utils.affine_transform(py_, trans_output_inv) for py_ in py] rles = snake_eval_utils.coco_poly_to_rle(py, ori_h, ori_w) coco_dets = [] for i in range(len(rles)): detection = { 'image_id': img_id, 'category_id': self.contiguous_category_id_to_json_id[label[i]], 'segmentation': rles[i], 'score': float('{:.2f}'.format(score[i])) } coco_dets.append(detection) self.results.extend(coco_dets) self.img_ids.append(img_id)
def poly_rle_nms(polys, scores, img_size, nms_thresh=0.3): assert len(scores) == len(polys), 'poly.num != scores.num' _h, _w = img_size[0], img_size[1] rles = snake_eval_utils.coco_poly_to_rle(polys, _h, _w) iscrowd = [int(0) for k in range(len(polys))] ious = maskUtils.iou(rles, rles, iscrowd) order = scores.argsort()[::-1] keep = [] while order.size > 0: i = order[0] keep.append(i) ovr = ious[i][order[1:]].astype(np.float32) inds = np.where(ovr <= nms_thresh)[0] order = order[inds + 1] return keep
def poly_rle_nms(img, polys, scores, nms_thresh=0.3): assert len(scores) == len(polys), 'poly.num != scores.num' org_h, org_w, _ = img.shape rles = snake_eval_utils.coco_poly_to_rle(polys, org_h, org_w) print('encoding rles done.') iscrowd = [int(0) for k in range(len(all_boundaries))] ious = maskUtils.iou(rles, rles, iscrowd) print('ious:', ious) order = scores.argsort()[::-1] keep = [] while order.size>0: i = order[0] keep.append(i) ovr = ious[i][order[1:]].astype(np.float32) inds = np.where(ovr <= nms_thresh)[0] order = order[inds+1] return keep