def _create_dp_data(self, anns, blob_def=None): dp_data = DensePoseDataRelative(anns) if blob_def is not None: dp_data.segm[blob_def["c"][0] - blob_def["r"]:blob_def["c"][0] + blob_def["r"], blob_def["c"][1] - blob_def["r"]:blob_def["c"][1] + blob_def["r"], ] = 1 return dp_data
def _extract_data_for_visualizers_from_entry( cls: type, vis_specs: List[str], entry: Dict[str, Any] ): dp_list = [] bbox_list = [] for annotation in entry["annotations"]: is_valid, _ = DensePoseDataRelative.validate_annotation(annotation) if not is_valid: continue bbox = torch.as_tensor(annotation["bbox"]) bbox_list.append(bbox) dp_data = DensePoseDataRelative(annotation) dp_list.append(dp_data) datas = [] for vis_spec in vis_specs: datas.append(bbox_list if "bbox" == vis_spec else (bbox_list, dp_list)) return datas
def test_chart_based_annotations_accumulator_gt_densepose(self): data_relative_keys = [ DensePoseDataRelative.X_KEY, DensePoseDataRelative.Y_KEY, DensePoseDataRelative.I_KEY, DensePoseDataRelative.U_KEY, DensePoseDataRelative.V_KEY, DensePoseDataRelative.S_KEY, ] annotations = [ DensePoseDataRelative({k: [0] for k in data_relative_keys}) ] * n_instances instances.gt_densepose = DensePoseList(annotations, instances.gt_boxes, image_shape) accumulator = ChartBasedAnnotationsAccumulator() accumulator.accumulate(instances) bbox_xywh_est = BoxMode.convert( instances.proposal_boxes.tensor.clone(), BoxMode.XYXY_ABS, BoxMode.XYWH_ABS) bbox_xywh_gt = BoxMode.convert(instances.gt_boxes.tensor.clone(), BoxMode.XYXY_ABS, BoxMode.XYWH_ABS) expected_values = { "s_gt": [ torch.zeros((3, DensePoseDataRelative.MASK_SIZE, DensePoseDataRelative.MASK_SIZE)) ] * n_instances, "bbox_xywh_est": bbox_xywh_est.split(1), "bbox_xywh_gt": bbox_xywh_gt.split(1), "point_bbox_with_dp_indices": [torch.tensor([i]) for i in range(n_instances)], "point_bbox_indices": [torch.tensor([i]) for i in range(n_instances)], "bbox_indices": list(range(n_instances)), "nxt_bbox_with_dp_index": n_instances, "nxt_bbox_index": n_instances, } default_value = [torch.tensor([0])] * 3 for key in accumulator.__dict__: to_test = getattr(accumulator, key) gt_value = expected_values.get(key, default_value) if key in ["nxt_bbox_with_dp_index", "nxt_bbox_index"]: self.assertEqual(to_test, gt_value) elif key == "bbox_indices": self.assertListEqual(to_test, gt_value) else: self.assertTrue( torch.allclose(torch.stack(to_test), torch.stack(gt_value)))
def generic_test(self, dataset_info, n_inst, loader_fun): datasets_root = DENSEPOSE_ANNOTATIONS_DIR annotations_fpath = maybe_prepend_base_path( datasets_root, dataset_info.annotations_fpath) images_root = maybe_prepend_base_path(datasets_root, dataset_info.images_root) image_annotation_dicts = loader_fun( annotations_json_file=annotations_fpath, image_root=images_root, dataset_name=dataset_info.name, ) num_valid = sum(1 for image_annotation_dict in image_annotation_dicts for ann in image_annotation_dict["annotations"] if DensePoseDataRelative.validate_annotation(ann)[0]) self.assertEqual(num_valid, n_inst)
def __call__(self, instances: Instances) -> DensePoseList: """ Convert DensePose predictions (an instance of `DensePoseChartPredictorOutput`) into DensePose annotations data (an instance of `DensePoseList`) """ boxes_xyxy_abs = instances.pred_boxes.tensor.clone().cpu() boxes_xywh_abs = BoxMode.convert(boxes_xyxy_abs, BoxMode.XYXY_ABS, BoxMode.XYWH_ABS) dp_datas = [] for i in range(len(boxes_xywh_abs)): annotation_i = self._sample(instances[i], make_int_box(boxes_xywh_abs[i])) annotation_i[DensePoseDataRelative.S_KEY] = self._resample_mask( # pyre-ignore[6] instances[i].pred_densepose ) dp_datas.append(DensePoseDataRelative(annotation_i)) # create densepose annotations on CPU dp_list = DensePoseList(dp_datas, boxes_xyxy_abs, instances.image_size) return dp_list
def _transform_densepose(self, annotation, transforms): if not self.densepose_on: return annotation # Handle densepose annotations is_valid, reason_not_valid = DensePoseDataRelative.validate_annotation( annotation) if is_valid: densepose_data = DensePoseDataRelative(annotation, cleanup=True) densepose_data.apply_transform(transforms, self.densepose_transform_data) annotation["densepose"] = densepose_data else: # logger = logging.getLogger(__name__) # logger.debug("Could not load DensePose annotation: {}".format(reason_not_valid)) DensePoseDataRelative.cleanup_annotation(annotation) # NOTE: annotations for certain instances may be unavailable. # 'None' is accepted by the DensePostList data structure. annotation["densepose"] = None return annotation