def annotations_to_instances(annos, image_size, mask_format="polygon"): """ Create an :class:`Instances` object used by the models, from instance annotations in the dataset dict. Args: annos (list[dict]): a list of instance annotations in one image, each element for one instance. image_size (tuple): height, width Returns: Instances: It will contain fields "gt_boxes", "gt_classes", "gt_masks", "gt_keypoints", if they can be obtained from `annos`. This is the format that builtin models expect. """ boxes = [ BoxMode.convert(obj["bbox"], obj["bbox_mode"], BoxMode.XYXY_ABS) for obj in annos ] target = Instances(image_size) boxes = target.gt_boxes = Boxes(boxes) boxes.clip(image_size) classes = [obj["category_id"] for obj in annos] classes = torch.tensor(classes, dtype=torch.int64) target.gt_classes = classes if len(annos) and "viewpoint" in annos[0]: viewpoints = np.array([obj["viewpoint"] for obj in annos]) viewpoints_class = torch.tensor(viewpoints[:, 0], dtype=torch.int64) target.gt_viewpoint = viewpoints_class if len(annos[0]["viewpoint"]) == 2: viewpoints_rads = torch.tensor(viewpoints[:, 1], dtype=torch.float32) target.gt_viewpoint_rads = viewpoints_rads if len(annos) and "bbox3D" in annos[0]: bbox3D = [obj["bbox3D"] for obj in annos] bbox3D = torch.tensor(bbox3D, dtype=torch.float) target.gt_bbox3D = bbox3D if len(annos) and "height" in annos[0]: height = [obj["height"] for obj in annos] height = torch.tensor(height, dtype=torch.float) target.gt_height = height if len(annos) and "segmentation" in annos[0]: polygons = [obj["segmentation"] for obj in annos] if mask_format == "polygon": masks = PolygonMasks(polygons) else: assert mask_format == "bitmask", mask_format masks = BitMasks.from_polygon_masks(polygons, *image_size) target.gt_masks = masks if len(annos) and "keypoints" in annos[0]: kpts = [obj.get("keypoints", []) for obj in annos] target.gt_keypoints = Keypoints(kpts) return target
def annotations_to_instances(annos, image_size, mask_format="polygon"): """ Create an :class:`Instances` object used by the models, from instance annotations in the dataset dict. Args: annos (list[dict]): a list of instance annotations in one image, each element for one instance. image_size (tuple): height, width Returns: Instances: It will contain fields "gt_boxes", "gt_classes", "gt_masks", "gt_keypoints", if they can be obtained from `annos`. This is the format that builtin models expect. """ boxes = [BoxMode.convert(obj["bbox"], obj["bbox_mode"], BoxMode.XYXY_ABS) for obj in annos] target = Instances(image_size) boxes = target.gt_boxes = Boxes(boxes) if 'light' in annos[0].keys(): light = [BoxMode.convert(obj['light'],obj["bbox_mode"], BoxMode.XYXY_ABS) for obj in annos] light = target.gt_light = Boxes(light) light.clip(image_size) boxes.clip(image_size) classes = [obj["category_id"] for obj in annos] classes = torch.tensor(classes, dtype=torch.int64) target.gt_classes = classes if len(annos) and "segmentation" in annos[0]: polygons = [obj["segmentation"] for obj in annos] if mask_format == "polygon": masks = PolygonMasks(polygons) else: assert mask_format == "bitmask", mask_format masks = BitMasks.from_polygon_masks(polygons, *image_size) target.gt_masks = masks if len(annos) and "keypoints" in annos[0]: kpts = [obj.get("keypoints", []) for obj in annos] target.gt_keypoints = Keypoints(kpts) return target
def annotations_to_instances(annos, image_size, mask_format="polygon"): """ Create an :class:`Instances` object used by the models, from instance annotations in the dataset dict. Args: annos (list[dict]): a list of instance annotations in one image, each element for one instance. image_size (tuple): height, width Returns: Instances: It will contain fields "gt_boxes", "gt_classes", "gt_masks", "gt_keypoints", if they can be obtained from `annos`. This is the format that builtin models expect. """ boxes = [ BoxMode.convert(obj["bbox"], obj["bbox_mode"], BoxMode.XYXY_ABS) for obj in annos ] target = Instances(image_size) boxes = target.gt_boxes = Boxes(boxes) boxes.clip(image_size) classes = [obj["category_id"] for obj in annos] classes = torch.tensor(classes, dtype=torch.int64) target.gt_classes = classes if len(annos) and "segmentation" in annos[0]: segm = [obj["segmentation"] for obj in annos] # it may be bitmask instead of polygon visible_segm = [obj["visible_mask"] for obj in annos ] # it may be bitmask instead of polygon if mask_format == "polygon": masks = PolygonMasks(segm) if not isinstance(visible_segm[0], list): visible_masks = visible_segm visible_masks = BitMasks( torch.stack([torch.from_numpy(x) for x in visible_masks])) else: # visible_masks = BitMasks.from_polygon_masks(visible_polygons, *image_size) visible_masks = PolygonMasks(visible_segm) else: assert mask_format == "bitmask", mask_format if not isinstance(segm[0], list): masks = BitMasks( torch.stack([torch.from_numpy(x) for x in segm])) # visible_masks = visible_polygons # visible_masks = BitMasks(torch.stack([torch.from_numpy(x) for x in visible_masks])) else: masks = BitMasks.from_polygon_masks(segm, *image_size) # visible_masks = BitMasks.from_polygon_masks(visible_polygons, *image_size) # print('masks:{}'.format(polygons)) if not isinstance(visible_segm[0], list): visible_masks = visible_segm visible_masks = BitMasks( torch.stack([torch.from_numpy(x) for x in visible_masks])) else: # print('visible_masks:{}'.format(visible_polygons)) visible_masks = BitMasks.from_polygon_masks( visible_segm, *image_size) target.gt_masks = masks target.gt_visible_masks = visible_masks if len(annos) and "keypoints" in annos[0]: kpts = [obj.get("keypoints", []) for obj in annos] target.gt_keypoints = Keypoints(kpts) return target