Search in sources :

Example 1 with Segmenter

use of sc.fiji.labkit.ui.segmentation.Segmenter in project labkit-ui by juglab.

the class TrainableSegmentationSegmenter method train.

@Override
public void train(List<Pair<ImgPlus<?>, Labeling>> trainingData) {
    try {
        initFeatureSettings(trainingData);
        List<String> classes = collectLabels(trainingData.stream().map(Pair::getB).collect(Collectors.toList()));
        sc.fiji.labkit.pixel_classification.classification.Segmenter segmenter = new sc.fiji.labkit.pixel_classification.classification.Segmenter(context, classes, featureSettings, new FastRandomForest());
        segmenter.setUseGpu(useGpu);
        Training training = segmenter.training();
        for (Pair<ImgPlus<?>, Labeling> pair : trainingData) trainStack(training, classes, pair.getB(), pair.getA(), segmenter.features());
        training.train();
        this.segmenter = segmenter;
    } catch (RuntimeException e) {
        Throwable cause = e.getCause();
        if (cause instanceof WekaException && cause.getMessage().contains("Not enough training instances"))
            throw new CancellationException("The training requires some labeled regions.");
        throw e;
    }
}
Also used : WekaException(weka.core.WekaException) ImgPlus(net.imagej.ImgPlus) Segmenter(sc.fiji.labkit.ui.segmentation.Segmenter) Training(sc.fiji.labkit.pixel_classification.classification.Training) FastRandomForest(hr.irb.fastRandomForest.FastRandomForest) CancellationException(java.util.concurrent.CancellationException) Labeling(sc.fiji.labkit.ui.labeling.Labeling) Pair(net.imglib2.util.Pair)

Example 2 with Segmenter

use of sc.fiji.labkit.ui.segmentation.Segmenter in project labkit-ui by juglab.

the class DefaultSegmentationModel method getPredictions.

public List<RandomAccessibleInterval<FloatType>> getPredictions() {
    ImgPlus<?> image = imageLabelingModel().imageForSegmentation().get();
    Stream<Segmenter> trainedSegmenters = getTrainedSegmenters();
    return trainedSegmenters.map(segmenter -> {
        int numberOfClasses = segmenter.classNames().size();
        RandomAccessibleInterval<FloatType> prediction = new CellImgFactory<>(new FloatType()).create(DimensionUtils.appendDimensionToInterval(image, 0, numberOfClasses - 1));
        segmenter.predict(image, prediction);
        return prediction;
    }).collect(Collectors.toList());
}
Also used : Segmenter(sc.fiji.labkit.ui.segmentation.Segmenter) ImgPlus(net.imagej.ImgPlus) IntegerType(net.imglib2.type.numeric.IntegerType) Pair(net.imglib2.util.Pair) AbstractList(java.util.AbstractList) DimensionUtils(sc.fiji.labkit.ui.utils.DimensionUtils) InputImage(sc.fiji.labkit.ui.inputimage.InputImage) Collectors(java.util.stream.Collectors) Context(org.scijava.Context) ValuePair(net.imglib2.util.ValuePair) FloatType(net.imglib2.type.numeric.real.FloatType) List(java.util.List) CellImgFactory(net.imglib2.img.cell.CellImgFactory) Stream(java.util.stream.Stream) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) Labeling(sc.fiji.labkit.ui.labeling.Labeling) NativeType(net.imglib2.type.NativeType) CellImgFactory(net.imglib2.img.cell.CellImgFactory) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) Segmenter(sc.fiji.labkit.ui.segmentation.Segmenter) FloatType(net.imglib2.type.numeric.real.FloatType)

Aggregations

ImgPlus (net.imagej.ImgPlus)2 Pair (net.imglib2.util.Pair)2 Labeling (sc.fiji.labkit.ui.labeling.Labeling)2 Segmenter (sc.fiji.labkit.ui.segmentation.Segmenter)2 FastRandomForest (hr.irb.fastRandomForest.FastRandomForest)1 AbstractList (java.util.AbstractList)1 List (java.util.List)1 CancellationException (java.util.concurrent.CancellationException)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)1 CellImgFactory (net.imglib2.img.cell.CellImgFactory)1 NativeType (net.imglib2.type.NativeType)1 IntegerType (net.imglib2.type.numeric.IntegerType)1 FloatType (net.imglib2.type.numeric.real.FloatType)1 ValuePair (net.imglib2.util.ValuePair)1 Context (org.scijava.Context)1 Training (sc.fiji.labkit.pixel_classification.classification.Training)1 InputImage (sc.fiji.labkit.ui.inputimage.InputImage)1 DimensionUtils (sc.fiji.labkit.ui.utils.DimensionUtils)1