def verify_data(orig_ann: Annotation, classes_matching: dict, res_project_meta: ProjectMeta) -> Annotation: ann = orig_ann.clone() imsize = ann.img_size for first_class, second_class in classes_matching.items(): mask1 = np.zeros(imsize, dtype=np.bool) mask2 = np.zeros(imsize, dtype=np.bool) for label in ann.labels: if label.obj_class.name == first_class: label.geometry.draw(mask1, True) elif label.obj_class.name == second_class: label.geometry.draw(mask2, True) iou_value = _compute_masks_iou(mask1, mask2) tag_meta = res_project_meta.img_tag_metas.get(make_iou_tag_name(first_class)) tag = Tag(tag_meta, iou_value) ann.add_tag(tag) fp_mask = _create_fp_mask(mask1, mask2) if fp_mask.sum() != 0: fp_object_cls = res_project_meta.obj_classes.get(make_false_positive_name(first_class)) fp_geom = Bitmap(data=fp_mask) fp_label = Label(fp_geom, fp_object_cls) ann.add_label(fp_label) fn_mask = _create_fn_mask(mask1, mask2) if fn_mask.sum() != 0: fn_object_cls = res_project_meta.obj_classes.get(make_false_negative_name(first_class)) fn_geom = Bitmap(data=fn_mask) fn_label = Label(fn_geom, fn_object_cls) ann.add_label(fn_label) return ann
def infer_on_image(image, graph, model, idx_to_class_title, project_meta, confidence_tag_meta, min_confidence=0): with graph.as_default(): [results] = model.detect([image], verbose=0) res_labels = [] for mask_idx, class_id in enumerate(results['class_ids']): confidence = results['scores'][mask_idx] if confidence < min_confidence: continue bool_mask = results['masks'][:, :, mask_idx] != 0 class_geometry = Bitmap(data=bool_mask) cls_title = idx_to_class_title[class_id] label = Label(geometry=class_geometry, obj_class=project_meta.get_obj_class(cls_title)) confidence_tag = Tag(confidence_tag_meta, value=round(float(confidence), 4)) label = label.add_tag(confidence_tag) res_labels.append(label) return res_labels
def detection_preds_to_sly_rects( idx_to_class, network_prediction: DetectionNetworkPrediction, img_shape, min_score_threshold, score_tag_meta) -> list: """ Converts network detection results to Supervisely Labels with Rectangle geometry. Args: idx_to_class: Dict matching predicted boxes with appropriate ObjClass. network_prediction: Network predictions packed into DetectionNetworkPrediction instance. img_shape: Size(height, width) of image that was used for inference. min_score_threshold: All detections with less scores will be dropped. score_tag_meta: TagMeta instance for score tags. Returns: A list containing labels with detection rectangles. """ labels = [] thr_mask = np.squeeze(network_prediction.scores) > min_score_threshold for box, class_id, score in zip( np.squeeze(network_prediction.boxes)[thr_mask], np.squeeze(network_prediction.classes)[thr_mask], np.squeeze(network_prediction.scores)[thr_mask]): xmin = round(float(box[1] * img_shape[1])) ymin = round(float(box[0] * img_shape[0])) xmax = round(float(box[3] * img_shape[1])) ymax = round(float(box[2] * img_shape[0])) rect = Rectangle(top=ymin, left=xmin, bottom=ymax, right=xmax) class_obj = idx_to_class[int(class_id)] label = Label(geometry=rect, obj_class=class_obj) score_tag = Tag(score_tag_meta, value=round(float(score), 4)) label = label.add_tag(score_tag) labels.append(label) return labels
def inference(self, img, ann): output = infer_on_img(img, self.input_size, self.model) tag_id = np.argmax(output) score = output[tag_id] tag_name = self.idx_to_classification_tags[tag_id] tag = Tag(self.classification_tags.get(tag_name), round(float(score), 4)) tags = TagCollection([tag]) return Annotation(ann.img_size, img_tags=tags)
def setUp(self): self._obj_class_gt = ObjClass(name='a', geometry_type=Rectangle) self._obj_class_pred = ObjClass(name='b', geometry_type=Rectangle) self._confidence_tag_meta = TagMeta(name='confidence', value_type=TagValueType.ANY_NUMBER) self._meta = ProjectMeta( obj_classes=ObjClassCollection([self._obj_class_gt, self._obj_class_pred]), tag_metas=TagMetaCollection([self._confidence_tag_meta])) # Will match self._pred_obj_1 self._gt_obj_1 = Label(obj_class=self._obj_class_gt, geometry=Rectangle(0, 0, 10, 10)) # Will match self._pred_obj_3 self._gt_obj_2 = Label(obj_class=self._obj_class_gt, geometry=Rectangle(13, 13, 15, 15)) # Will be a false negative self._gt_obj_3 = Label(obj_class=self._obj_class_gt, geometry=Rectangle(43, 43, 45, 45)) # Will match self._gt_obj_1 self._pred_obj_1 = Label( obj_class=self._obj_class_pred, geometry=Rectangle(0, 0, 9, 9), tags=TagCollection([Tag(meta=self._confidence_tag_meta, value=0.7)])) # Will be a false positive (self._pred_obj_1 has higher IoU). self._pred_obj_2 = Label( obj_class=self._obj_class_pred, geometry=Rectangle(0, 0, 8, 8), tags=TagCollection([Tag(meta=self._confidence_tag_meta, value=0.6)])) # Will match self._gt_obj_2 self._pred_obj_3 = Label( obj_class=self._obj_class_pred, geometry=Rectangle(13, 13, 15, 15), tags=TagCollection([Tag(meta=self._confidence_tag_meta, value=0.1)])) # More false positives. self._pred_objs_fp = [ Label(obj_class=self._obj_class_pred, geometry=Rectangle(20, 20, 30, 30), tags=TagCollection([Tag(meta=self._confidence_tag_meta, value=v / 100)])) for v in range(15, 85, 10)] self._metric_calculator = MAPMetric(class_mapping={'a': 'b'}, iou_threshold=0.5)
def _replace_labels_classes(labels, obj_class_mapper: ObjClassMapper, tags_meta_mapper: TagMetaMapper, skip_missing=False) -> list: result = [] for label in labels: dest_obj_class = obj_class_mapper.map(label.obj_class) if dest_obj_class is not None: mapped_tags = TagMetaCollection(items=[ Tag(meta=tags_meta_mapper.map(tag.meta), value=tag.value) for tag in label.tags ]) result.append( label.clone(obj_class=dest_obj_class, tags=mapped_tags)) elif not skip_missing: raise KeyError( 'Object class {} could not be mapped to a destination object class.' .format(label.obj_class.name)) return result
def from_json(cls, data, tag_meta_collection): tags = [ Tag.from_json(tag_json, tag_meta_collection) for tag_json in data ] return cls(tags)