def infer_on_img(img, input_size, model): x = cv2.resize(img, input_size[::-1]) x = input_image_normalizer(x) output = model(x.unsqueeze_(0).cuda()) output = functional.softmax(output, dim=1) output = output.data.cpu().numpy()[0] return output
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)
def _infer_on_img(self, img, _): h, w = img.shape[:2] x = cv2.resize(img, tuple(self.input_size_wh)) x = input_image_normalizer(x) x = torch.stack([x], 0) # add dim #0 (batch size 1) x = cuda_variable(x, volatile=True) output = self.model(x) output = functional.softmax(output, dim=1) output = output.data.cpu().numpy()[0] # from batch to 3d pred = np.transpose(output, (1, 2, 0)) pred = cv2.resize(pred, (w, h), cv2.INTER_LINEAR) res = self._postproc(self.out_class_mapping, pred) return res
def _infer_on_img_legacy(self, img, _): h, w = img.shape[:2] x = cv2.resize(img, tuple(self.input_size_wh)) x = input_image_normalizer(x) x = torch.stack([x], 0) # add dim #0 (batch size 1) x = cuda_variable(x, volatile=True) output = self.model(x) output = functional.sigmoid(output) output = output.data.cpu().numpy()[0] # from batch to 3d pred = np.transpose(output, (1, 2, 0)) pred = cv2.resize(pred, (w, h), cv2.INTER_LINEAR) # pred = pred[..., 0] >= 0.5 # use only one channel, fixed threshold pred[..., 1] = pred[..., 0] pred[..., 0] = 1 - pred[..., 1] res = self._postproc(self.out_class_mapping, pred) return res
def inference(self, img): h, w = img.shape[:2] x = cv2.resize(img, tuple(self.input_size_wh)) x = input_image_normalizer(x) x = torch.stack([x], 0) # add dim #0 (batch size 1) x = cuda_variable(x, volatile=True) output = self.model(x) output = functional.softmax(output, dim=1) output = output.data.cpu().numpy()[0] # from batch to 3d pred = np.transpose(output, (1, 2, 0)) pred = cv2.resize(pred, (w, h), cv2.INTER_LINEAR) pred_cls_idx = np.argmax(pred, axis=2) res_figures = sly.prediction_to_sly_bitmaps(self.out_class_mapping, pred_cls_idx) res_ann = sly.Annotation.new_with_objects((w, h), res_figures) return res_ann