def infer_per_pixel_scores_single_image(model, raw_input, out_shape, apply_softmax=True): """ Performs inference with PyTorch model and resize predictions to a given size. Args: model: PyTorch model inherited from torch.Module class. raw_input: PyTorch Tensor out_shape: Output size (height, width). apply_softmax: Whether to apply softmax function after inference or not. Returns: Inference resulting numpy array resized to a given size. """ model_input = torch.stack([raw_input], 0) # add dim #0 (batch size 1) model_input = cuda_variable(model_input, volatile=True) output = model(model_input) if apply_softmax: output = torch_functional.softmax(output, dim=1) output = output.data.cpu().numpy()[0] # from batch to 3d pred = np.transpose(output, (1, 2, 0)) return sly_image.resize(pred, out_shape)
def scale(img: np.ndarray, ann: Annotation, frow: float = None, fcol: float = None, f: float = None) \ -> (np.ndarray, Annotation): """ Resize the input image array and annotation to the given size. Args: img: Input image array. ann: Input annotation. frow: Desired height scale height value frow: Desired width scale width value f: Desired height and width scale values in one Returns: A tuple containing resized image array and annotation. """ _validate_image_annotation_shape(img, ann) new_size = sly_image.restore_proportional_size(in_size=ann.img_size, frow=frow, fcol=fcol, f=f) res_img = sly_image.resize(img, new_size) res_ann = ann.resize(new_size) return res_img, res_ann
def resize(img: np.ndarray, ann: Annotation, size: tuple) -> (np.ndarray, Annotation): """ Resize the input image array and annotation to the given size. Args: img: Input image array. ann: Input annotation. size: Desired size (height, width) in pixels or -1. If one of values is -1 and "keep": true then for specific width height will be automatically computed to keep aspect ratio. Returns: A tuple containing resized image array and annotation. """ _validate_image_annotation_shape(img, ann) height = take_with_default(size[0], -1) # For backward capability width = take_with_default(size[1], -1) size = (height, width) new_size = sly_image.restore_proportional_size(in_size=ann.img_size, out_size=size) res_img = sly_image.resize(img, new_size) res_ann = ann.resize(new_size) return res_img, res_ann
def _get_sample_impl(self, img_fpath, ann_fpath): img = sly_image.read(img_fpath) ann = self.load_annotation(ann_fpath) gt = self.make_gt(img.shape, ann) img = sly_image.resize(img, self._out_size) return img, gt