def _do_infer_annotate(self, img: np.ndarray, ann: Annotation) -> Annotation: result_ann = ann.clone() all_pixelwise_scores_labels = [] for roi in self._sliding_windows.get(ann.img_size): raw_roi_ann = _get_annotation_for_bbox(img, roi, self._model) all_pixelwise_scores_labels.extend(raw_roi_ann.pixelwise_scores_labels) model_img_level_tags = make_renamed_tags(raw_roi_ann.img_tags, self._model_img_tag_meta_mapper, make_renamed_tags) result_ann = result_ann.add_labels( _maybe_make_bbox_label(roi, self._intermediate_bbox_class, tags=model_img_level_tags)) model_class_name_to_id = {name: idx for idx, name in enumerate(set(label.obj_class.name for label in all_pixelwise_scores_labels))} id_to_class_obj = {idx: self._model.model_out_meta.obj_classes.get(name) for name, idx in model_class_name_to_id.items()} summed_scores = np.zeros(ann.img_size + tuple([len(model_class_name_to_id)])) for label in all_pixelwise_scores_labels: class_idx = model_class_name_to_id[label.obj_class.name] label_matching_summer_scores = label.geometry.to_bbox().get_cropped_numpy_slice(summed_scores) label_matching_summer_scores[:, :, class_idx, np.newaxis] += label.geometry.data # TODO consider instead filtering pixels by all-zero scores. if np.sum(summed_scores, axis=2).min() == 0: raise RuntimeError('Wrong sliding window moving, implementation error.') aggregated_model_labels = raw_to_labels.segmentation_array_to_sly_bitmaps(id_to_class_obj, np.argmax(summed_scores, axis=2)) result_ann = result_ann.add_labels( replace_labels_classes(aggregated_model_labels, self._model_class_mapper, skip_missing=True)) return result_ann
def _do_infer_annotate(self, img: np.ndarray, ann: Annotation) -> Annotation: result_ann = ann.clone() model_labels = [] for roi in self._sliding_windows.get(ann.img_size): raw_roi_ann = _get_annotation_for_bbox(img, roi, self._model) all_rectangle_labels = [ label for label in raw_roi_ann.labels if isinstance(label.geometry, Rectangle) ] model_labels.extend( _replace_labels_classes(all_rectangle_labels, self._model_class_mapper, self._model_tag_meta_mapper, skip_missing=True)) model_img_level_tags = make_renamed_tags( raw_roi_ann.img_tags, self._model_tag_meta_mapper, skip_missing=True) result_ann = result_ann.add_labels( _maybe_make_bbox_label(roi, self._intermediate_bbox_class, tags=model_img_level_tags)) nms_conf = self._config.get(NMS_AFTER, {ENABLE: False}) if nms_conf[ENABLE]: result_ann = result_ann.add_labels( self._general_nms(labels=model_labels, iou_thresh=nms_conf[IOU_THRESHOLD], confidence_tag_name=nms_conf.get( CONFIDENCE_TAG_NAME, CONFIDENCE))) else: result_ann = result_ann.add_labels(model_labels) return result_ann
def _do_infer_annotate(self, img: np.ndarray, ann: Annotation) -> Annotation: result_ann = ann.clone() model_labels = [] roi_bbox_labels = [] for roi in self._sliding_windows.get(ann.img_size): raw_roi_ann = _get_annotation_for_bbox(img, roi, self._model) # Accumulate all the labels across the sliding windows to potentially run non-max suppression over them. # Only retain the classes that will be eventually saved to avoid running NMS on objects we will # throw away anyway. model_labels.extend([ label for label in raw_roi_ann.labels if isinstance(label.geometry, Rectangle) and self._model_class_mapper.map(label.obj_class) is not None]) model_img_level_tags = make_renamed_tags( raw_roi_ann.img_tags, self._model_tag_meta_mapper, skip_missing=True) roi_bbox_labels.extend( _maybe_make_bbox_label(roi, self._intermediate_bbox_class, tags=model_img_level_tags)) nms_conf = self._config.get(NMS_AFTER, {ENABLE: False}) if nms_conf[ENABLE]: confidence_tag_name = nms_conf.get(CONFIDENCE_TAG_NAME, CONFIDENCE) model_labels = self._general_nms( labels=model_labels, iou_thresh=nms_conf[IOU_THRESHOLD], confidence_tag_name=confidence_tag_name) model_labels_renamed = _replace_or_drop_labels_classes( model_labels, self._model_class_mapper, self._model_tag_meta_mapper) result_ann = result_ann.add_labels(roi_bbox_labels + model_labels_renamed) return result_ann
def _do_infer_annotate(self, img: np.ndarray, ann: Annotation) -> Annotation: result_labels = [] for src_label, roi in self._all_filtered_bbox_rois( ann, self._config[FROM_CLASSES], self._config[PADDING]): if roi is None: result_labels.append(src_label) else: roi_ann = _get_annotation_for_bbox(img, roi, self._model) result_labels.extend( _replace_labels_classes(roi_ann.labels, self._model_class_mapper, self._model_tag_meta_mapper, skip_missing=True)) model_img_level_tags = make_renamed_tags( roi_ann.img_tags, self._model_tag_meta_mapper, skip_missing=True) if self._config[SAVE]: result_labels.append( Label(geometry=roi, obj_class=self._intermediate_class_mapper.map( src_label.obj_class), tags=model_img_level_tags)) # Regardless of whether we need to save intermediate bounding boxes, also put the inference result tags # onto the original source object from which we created a bounding box. # This is necessary for e.g. classification models to work, so that they put the classification results # onto the original object. result_labels.append(src_label.add_tags(model_img_level_tags)) return ann.clone(labels=result_labels)
def _replace_or_drop_labels_classes(labels, obj_class_mapper: ObjClassMapper, tag_meta_mapper: TagMetaMapper) -> list: result = [] for label in labels: dest_obj_class = obj_class_mapper.map(label.obj_class) if dest_obj_class is not None: renamed_tags = make_renamed_tags(tags=label.tags, tag_meta_mapper=tag_meta_mapper, skip_missing=True) result.append(label.clone(obj_class=dest_obj_class, tags=renamed_tags)) return result
def _do_infer_annotate(self, img: np.ndarray, ann: Annotation) -> Annotation: result_ann = ann.clone() inference_result_ann = self._model.inference(img, ann) result_ann = result_ann.add_labels( replace_labels_classes(inference_result_ann.labels, self._model_class_mapper, skip_missing=True)) renamed_tags = make_renamed_tags(inference_result_ann.img_tags, self._model_img_tag_meta_mapper, skip_missing=True) result_ann = result_ann.add_tags(renamed_tags) return result_ann
def _do_infer_annotate(self, img: np.ndarray, ann: Annotation) -> Annotation: result_ann = ann.clone() roi = _make_cropped_rectangle(ann.img_size, self._config[BOUNDS]) roi_ann = _get_annotation_for_bbox(img, roi, self._model) result_ann = result_ann.add_labels( replace_labels_classes(roi_ann.labels, self._model_class_mapper, skip_missing=True)) img_level_tags = make_renamed_tags(roi_ann.img_tags, self._model_img_tag_meta_mapper, skip_missing=True) result_ann = result_ann.add_labels( _maybe_make_bbox_label(roi, self._intermediate_bbox_class, tags=roi_ann.img_tags)) result_ann = result_ann.add_tags(img_level_tags) return result_ann
def _do_infer_annotate_generic(self, inference_fn, img, ann: Annotation): result_ann = ann.clone() inference_result_ann = inference_fn(img, ann) result_ann = result_ann.add_labels( _replace_or_drop_labels_classes( inference_result_ann.labels, self._model_class_mapper, self._model_tag_meta_mapper)) renamed_tags = make_renamed_tags(inference_result_ann.img_tags, self._model_tag_meta_mapper, skip_missing=True) result_ann = result_ann.add_tags(renamed_tags) return result_ann
def _do_infer_annotate(self, img: np.ndarray, ann: Annotation) -> Annotation: result_labels = [] for src_label, roi in self._all_filtered_bbox_rois(ann, self._config[FROM_CLASSES], self._config[PADDING]): if roi is None: result_labels.append(src_label) else: roi_ann = _get_annotation_for_bbox(img, roi, self._model) result_labels.extend(replace_labels_classes( roi_ann.labels, self._model_class_mapper, skip_missing=True)) model_img_level_tags = make_renamed_tags(roi_ann.img_tags, self._model_img_tag_meta_mapper, skip_missing=True) if self._config[SAVE]: result_labels.append( Label(geometry=roi, obj_class=self._intermediate_class_mapper.map(src_label.obj_class), tags=model_img_level_tags)) result_labels.append(src_label.add_tags(model_img_level_tags)) return ann.clone(labels=result_labels)
def _do_infer_annotate(self, img: np.ndarray, ann: Annotation) -> Annotation: result_ann = ann.clone() roi = _make_cropped_rectangle(ann.img_size, self._config[BOUNDS]) roi_ann = _get_annotation_for_bbox(img, roi, self._model) result_ann = result_ann.add_labels( _replace_or_drop_labels_classes(roi_ann.labels, self._model_class_mapper, self._model_tag_meta_mapper)) img_level_tags = make_renamed_tags(roi_ann.img_tags, self._model_tag_meta_mapper, skip_missing=True) result_ann = result_ann.add_labels( _maybe_make_bbox_label(roi, self._intermediate_bbox_class, tags=img_level_tags)) result_ann = result_ann.add_tags(img_level_tags) if self._config.get(SAVE_PROBABILITIES, False) is True: result_problabels = _replace_or_drop_labels_classes( roi_ann.pixelwise_scores_labels, self._model_class_mapper, self._model_tag_meta_mapper) result_ann = result_ann.add_pixelwise_score_labels(result_problabels) return result_ann
def _do_infer_annotate_generic(self, inference_fn, img, ann: Annotation): result_ann = ann.clone() inference_ann = inference_fn(img, ann) result_labels = _replace_or_drop_labels_classes( inference_ann.labels, self._model_class_mapper, self._model_tag_meta_mapper) result_ann = result_ann.add_labels(result_labels) renamed_tags = make_renamed_tags(inference_ann.img_tags, self._model_tag_meta_mapper, skip_missing=True) result_ann = result_ann.add_tags(renamed_tags) if self._config.get(SAVE_PROBABILITIES, False) is True: result_problabels = _replace_or_drop_labels_classes( inference_ann.pixelwise_scores_labels, self._model_class_mapper, self._model_tag_meta_mapper) result_ann = result_ann.add_pixelwise_score_labels(result_problabels) return result_ann
def _do_infer_annotate(self, img: np.ndarray, ann: Annotation) -> Annotation: result_ann = ann.clone() all_pixelwise_scores_labels = [] for roi in self._sliding_windows.get(ann.img_size): raw_roi_ann = _get_annotation_for_bbox(img, roi, self._model) all_pixelwise_scores_labels.extend(raw_roi_ann.pixelwise_scores_labels) model_img_level_tags = make_renamed_tags(raw_roi_ann.img_tags, self._model_tag_meta_mapper, make_renamed_tags) result_ann = result_ann.add_labels( _maybe_make_bbox_label(roi, self._intermediate_bbox_class, tags=model_img_level_tags)) model_class_name_to_id = {name: idx for idx, name in enumerate(set(label.obj_class.name for label in all_pixelwise_scores_labels))} id_to_class_obj = {idx: self._model.model_out_meta.obj_classes.get(name) for name, idx in model_class_name_to_id.items()} summed_scores = np.zeros(ann.img_size + tuple([len(model_class_name_to_id)])) summed_divisor = np.zeros_like(summed_scores) for label in all_pixelwise_scores_labels: class_idx = model_class_name_to_id[label.obj_class.name] geom_bbox = label.geometry.to_bbox() label_matching_summer_scores = geom_bbox.get_cropped_numpy_slice(summed_scores) label_matching_summer_scores[:, :, class_idx, np.newaxis] += label.geometry.data divisor_slice = geom_bbox.get_cropped_numpy_slice(summed_divisor) divisor_slice[:, :, class_idx, np.newaxis] += 1. # TODO consider instead filtering pixels by all-zero scores. if np.sum(summed_scores, axis=2).min() == 0: raise RuntimeError('Wrong sliding window moving, implementation error.') aggregated_model_labels = raw_to_labels.segmentation_array_to_sly_bitmaps(id_to_class_obj, np.argmax(summed_scores, axis=2)) result_ann = result_ann.add_labels( _replace_or_drop_labels_classes( aggregated_model_labels, self._model_class_mapper, self._model_tag_meta_mapper)) if self._config.get(SAVE_PROBABILITIES, False) is True: # copied fom unet's inference.py mean_scores = summed_scores / summed_divisor accumulated_pixelwise_scores_labels = raw_to_labels.segmentation_scores_to_per_class_labels( id_to_class_obj, mean_scores) result_problabels = _replace_or_drop_labels_classes( accumulated_pixelwise_scores_labels, self._model_class_mapper, self._model_tag_meta_mapper) result_ann = result_ann.add_pixelwise_score_labels(result_problabels) return result_ann