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
Exemplo n.º 2
0
    def inference(self, img, ann):
        # Rescale with proportions and pad image to model input size
        min_side_coef = min(self.input_size[0] / float(img.shape[0]),
                            self.input_size[1] / float(img.shape[1]))
        img_resized = cv2.resize(img,
                                 dsize=None,
                                 fx=min_side_coef,
                                 fy=min_side_coef,
                                 interpolation=cv2.INTER_CUBIC)
        img_padded = cv2.copyMakeBorder(
            img_resized,
            0,
            self.input_size[0] - img_resized.shape[0],
            0,
            self.input_size[1] - img_resized.shape[1],
            cv2.BORDER_CONSTANT,
            value=0)

        preds = self.sess.run(self.pred_holder,
                              feed_dict={self.image_tensor: img_padded})[0]
        preds = np.argmax(preds, axis=2)

        # Un-pad and rescale prediction to original image size
        preds = preds[0:img_resized.shape[0], 0:img_resized.shape[1]]
        preds = cv2.resize(preds, (img.shape[1], img.shape[0]),
                           interpolation=cv2.INTER_NEAREST)
        labels = raw_to_labels.segmentation_array_to_sly_bitmaps(
            self.out_class_mapping, preds)
        return Annotation(img_size=ann.img_size, labels=labels)
Exemplo n.º 3
0
 def inference(self, img, ann):
     resized_img = cv2.resize(img, self.input_size[::-1])
     model_input = input_image_normalizer(resized_img)
     pixelwise_probas_array = pytorch_inference.infer_per_pixel_scores_single_image(
         self.model, model_input, img.shape[:2])
     labels = raw_to_labels.segmentation_array_to_sly_bitmaps(
         self.out_class_mapping, np.argmax(pixelwise_probas_array, axis=2))
     pixelwise_scores_labels = raw_to_labels.segmentation_scores_to_per_class_labels(
         self.out_class_mapping, pixelwise_probas_array)
     return Annotation(ann.img_size, labels=labels, pixelwise_scores_labels=pixelwise_scores_labels)
Exemplo n.º 4
0
    def inference(self, img, ann):
        h, w = img.shape[:2]
        # Resize to requested model input size
        input_image = sly.image.resize(img, self.input_size)
        input_image_var = np.expand_dims(input_image.astype(np.float32), 0)
        raw_pixelwise_probas_array = self.session.run(self.logits, feed_dict={self.inputs: input_image_var})
        # Resize back to the original
        pixelwise_probas_array = cv2.resize(np.squeeze(raw_pixelwise_probas_array[0]), (w, h), cv2.INTER_LINEAR)

        labels = raw_to_labels.segmentation_array_to_sly_bitmaps(self.out_class_mapping,
                                                                 np.argmax(pixelwise_probas_array, axis=2))

        pixelwise_scores_labels = raw_to_labels.segmentation_scores_to_per_class_labels(self.out_class_mapping,
                                                                                        pixelwise_probas_array)
        return sly.Annotation(ann.img_size, labels=labels, pixelwise_scores_labels=pixelwise_scores_labels)
Exemplo n.º 5
0
    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
Exemplo n.º 6
0
    def inference(self, img, ann):
        # Resize the image to the dimenstions the model has been trained on.
        # Even though the model preserves input dimensionality and in principle can be used with the full-size original
        # input directly, it is better to resize to preserve the scale of image features consistent with what the model
        # has seen during training.
        img_resized = sly.image.resize(img, self.input_size)

        # Height x Width x Channels -> Channels x Height x Width (pytorch convention), conversion to float,
        # change intensity range from 0...255 to 0...1
        img_chw = to_tensor(img_resized)

        # Add a dummy batch dimension.
        img_bchw = img_chw[None, ...]

        # Copy the data to the GPU to feed the model for inference.
        with torch.no_grad():
            model_input_cuda = Variable(img_bchw).cuda()

        # Run inference.
        class_scores_cuda = self._model(model_input_cuda)

        # Copy inference results back to CPU RAM, drop the batch dimension.
        class_scores = np.squeeze(class_scores_cuda.data.cpu().numpy(), axis=0)

        # Reorder dimensions back to the common Height x Width x Channels format.
        class_scores_hwc = np.transpose(class_scores, [1, 2, 0])

        # Resize back to the original size. Use nearest neighbor interpolation to avoid interpolation artifacts in the
        # scores space.
        class_scores_original_size = sly.image.resize_inter_nearest(class_scores_hwc, out_size=img.shape[:2])

        # Find the most likely class ID for every pixel.
        class_ids = np.argmax(class_scores_original_size, axis=2)

        # Convert the raw segmentation array to supervisely labels, one per output class.
        labels = raw_to_labels.segmentation_array_to_sly_bitmaps(idx_to_class=self.out_class_mapping, pred=class_ids)

        # Wrap the resulting labels into an image annotation and return.
        return sly.Annotation(img_size=img.shape[:2], labels=labels)