How to use the csbdeep.data.PercentileNormalizer function in csbdeep

To help you get started, we’ve selected a few csbdeep examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CSBDeep / CSBDeep / tests / test_utils.py View on Github external
def test_normalizer(axes):
    rng = np.random.RandomState(42)

    no_normalizer = NoNormalizer(do_after=False)
    paxis = tuple(d for d,a in enumerate(axes) if a != 'C')
    def _percentile(x,p):
        return np.percentile(x,p,axis=paxis,keepdims=True)

    for _ in range(50):
        pmin = rng.uniform(0,50)
        pmax = rng.uniform(pmin+1,100)
        normalizer = PercentileNormalizer(pmin, pmax, do_after=True)

        imdims = list(rng.randint(10,20,size=len(axes)))
        u = rng.uniform(0,10000,size=imdims).astype(np.float32,copy=False)
        u_pmin, u_pmax = _percentile(u,pmin), _percentile(u,pmax)

        assert np.allclose(u, no_normalizer.before(u, axes))
        with pytest.raises(ValueError):
            no_normalizer.after(u, u, axes)

        v = normalizer.before(u, axes)
        v_pmin, v_pmax = _percentile(v,pmin), _percentile(v,pmax)
        assert np.mean(np.abs(v_pmin-0)) < 1e-5 and np.mean(np.abs(v_pmax-1)) < 1e-5

        w = normalizer.after(v, None, axes)[0]
        w_pmin, w_pmax = _percentile(w,pmin), _percentile(w,pmax)
        assert np.allclose(u_pmin,w_pmin) and np.allclose(u_pmax,w_pmax)
github CSBDeep / CSBDeep / csbdeep / models / care_upsampling.py View on Github external
    def predict(self, img, axes, factor, normalizer=PercentileNormalizer(), resizer=PadAndCropResizer(), n_tiles=None):
        """Apply neural network to raw image with low-resolution Z axis.

        See :func:`CARE.predict` for documentation.

        Parameters
        ----------
        factor : float
            Upsampling factor for Z axis. It is important that this is chosen in correspondence
            to the subsampling factor used during training data generation.

        """
        img = self._upsample(img, axes, factor)
        return super(UpsamplingCARE, self).predict(img, axes, normalizer, resizer, n_tiles)
github CSBDeep / CSBDeep / csbdeep / models / care_isotropic.py View on Github external
    def predict(self, img, axes, factor, normalizer=PercentileNormalizer(), resizer=PadAndCropResizer(), batch_size=8):
        """Apply neural network to raw image for isotropic reconstruction.

        See :func:`CARE.predict` for documentation.

        Parameters
        ----------
        factor : float
            Upsampling factor for Z axis. It is important that this is chosen in correspondence
            to the subsampling factor used during training data generation.
        batch_size : int
            Number of image slices that are processed together by the neural network.
            Reduce this value if out of memory errors occur.

        """
        return self._predict_mean_and_scale(img, axes, factor, normalizer, resizer, batch_size)[0]
github CSBDeep / CSBDeep / csbdeep / models / care_standard.py View on Github external
    def predict(self, img, axes, normalizer=PercentileNormalizer(), resizer=PadAndCropResizer(), n_tiles=None):
        """Apply neural network to raw image to predict restored image.

        Parameters
        ----------
        img : :class:`numpy.ndarray`
            Raw input image
        axes : str
            Axes of the input ``img``.
        normalizer : :class:`csbdeep.data.Normalizer` or None
            Normalization of input image before prediction and (potentially) transformation back after prediction.
        resizer : :class:`csbdeep.data.Resizer` or None
            If necessary, input image is resized to enable neural network prediction and result is (possibly)
            resized to yield original image size.
        n_tiles : iterable or None
            Out of memory (OOM) errors can occur if the input image is too large.
            To avoid this problem, the input image is broken up into (overlapping) tiles
github CSBDeep / CSBDeep / csbdeep / models / care_standard.py View on Github external
    def predict_probabilistic(self, img, axes, normalizer=PercentileNormalizer(), resizer=PadAndCropResizer(), n_tiles=None):
        """Apply neural network to raw image to predict probability distribution for restored image.

        See :func:`predict` for parameter explanations.

        Returns
        -------
        :class:`csbdeep.internals.probability.ProbabilisticPrediction`
            Returns the probability distribution of the restored image.

        Raises
        ------
        ValueError
            If this is not a probabilistic model.

        """
        self.config.probabilistic or _raise(ValueError('This is not a probabilistic model.'))
github CSBDeep / CSBDeep / csbdeep / models / care_isotropic.py View on Github external
    def predict_probabilistic(self, img, axes, factor, normalizer=PercentileNormalizer(), resizer=PadAndCropResizer(), batch_size=8):
        """Apply neural network to raw image to predict probability distribution for isotropic restored image.

        See :func:`CARE.predict_probabilistic` for documentation.

        Parameters
        ----------
        factor : float
            Upsampling factor for Z axis. It is important that this is chosen in correspondence
            to the subsampling factor used during training data generation.
        batch_size : int
            Number of image slices that are processed together by the neural network.
            Reduce this value if out of memory errors occur.

        """
        self.config.probabilistic or _raise(ValueError('This is not a probabilistic model.'))
        mean, scale = self._predict_mean_and_scale(img, axes, factor, normalizer, resizer, batch_size)
github CSBDeep / CSBDeep / csbdeep / models / care_upsampling.py View on Github external
    def predict_probabilistic(self, img, axes, factor, normalizer=PercentileNormalizer(), resizer=PadAndCropResizer(), n_tiles=None):
        """Apply neural network to raw image with low-resolution Z axis for probabilistic prediction.

        See :func:`CARE.predict_probabilistic` for documentation.

        Parameters
        ----------
        factor : float
            Upsampling factor for Z axis. It is important that this is chosen in correspondence
            to the subsampling factor used during training data generation.

        """
        img = self._upsample(img, axes, factor)
        return super(UpsamplingCARE, self).predict_probabilistic(img, axes, normalizer, resizer, n_tiles)
github CSBDeep / CSBDeep / csbdeep / scripts / care_predict.py View on Github external
from csbdeep.data import PercentileNormalizer
    sys.stdout.flush()
    sys.stderr.flush()

    # limit gpu memory
    if args.gpu_memory_limit is not None:
        from csbdeep.utils.tf import limit_gpu_memory
        limit_gpu_memory(args.gpu_memory_limit)

    # create CARE model and load weights, create normalizer
    K.clear_session()
    model = CARE(config=None, name=args.model_name, basedir=args.model_basedir)
    if args.model_weights is not None:
        print("Loading network weights from '%s'." % args.model_weights)
        model.load_weights(args.model_weights)
    normalizer = PercentileNormalizer(pmin=args.norm_pmin, pmax=args.norm_pmax, do_after=args.norm_undo)

    n_tiles = args.n_tiles
    if n_tiles is not None and len(n_tiles)==1:
        n_tiles = n_tiles[0]

    processed = []

    # process all files
    for file_in in tqdm(file_list, disable=args.quiet or (n_tiles is not None and np.prod(n_tiles)>1)):
        # construct output file name
        file_out = Path(args.output_dir) / args.output_name.format (
            file_path = str(file_in.relative_to(args.input_dir).parent),
            file_name = file_in.stem, file_ext = file_in.suffix,
            model_name = args.model_name, model_weights = Path(args.model_weights).stem if args.model_weights is not None else None
        )