Search in sources :

Example 1 with Labeling

use of sc.fiji.labkit.ui.labeling.Labeling 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 Labeling

use of sc.fiji.labkit.ui.labeling.Labeling in project labkit-ui by juglab.

the class TrainableSegmentationSegmenter method getClassIndices.

private SparseRandomAccessIntType getClassIndices(Labeling labeling, List<String> classes) {
    SparseRandomAccessIntType result = new SparseRandomAccessIntType(labeling, -1);
    Map<Set<Label>, Integer> classIndices = new HashMap<>();
    Function<Set<Label>, Integer> compute = set -> set.stream().mapToInt(label -> classes.indexOf(label.name())).filter(i -> i >= 0).min().orElse(-1);
    Cursor<?> cursor = labeling.sparsityCursor();
    RandomAccess<LabelingType<Label>> randomAccess = labeling.randomAccess();
    RandomAccess<IntType> out = result.randomAccess();
    while (cursor.hasNext()) {
        cursor.fwd();
        randomAccess.setPosition(cursor);
        Set<Label> labels = randomAccess.get();
        if (labels.isEmpty())
            continue;
        Integer classIndex = classIndices.computeIfAbsent(labels, compute);
        out.setPosition(cursor);
        out.get().set(classIndex);
    }
    return result;
}
Also used : Pair(net.imglib2.util.Pair) GsonUtils(sc.fiji.labkit.pixel_classification.gson.GsonUtils) WekaException(weka.core.WekaException) Context(org.scijava.Context) FloatType(net.imglib2.type.numeric.real.FloatType) DiskCachedCellImgOptions(net.imglib2.cache.img.DiskCachedCellImgOptions) Cursor(net.imglib2.Cursor) SparseRandomAccessIntType(sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType) SingleFeatures(sc.fiji.labkit.pixel_classification.pixel_feature.filter.SingleFeatures) FeatureSettings(sc.fiji.labkit.pixel_classification.pixel_feature.settings.FeatureSettings) Map(java.util.Map) DiskCachedCellImgFactory(net.imglib2.cache.img.DiskCachedCellImgFactory) Views(net.imglib2.view.Views) RandomAccess(net.imglib2.RandomAccess) CancellationException(java.util.concurrent.CancellationException) Set(java.util.Set) Collectors(java.util.stream.Collectors) PrefService(org.scijava.prefs.PrefService) Objects(java.util.Objects) DiskCachedCellImg(net.imglib2.cache.img.DiskCachedCellImg) List(java.util.List) Labelings(sc.fiji.labkit.ui.labeling.Labelings) LabelingType(net.imglib2.roi.labeling.LabelingType) Cast(net.imglib2.util.Cast) RealType(net.imglib2.type.numeric.RealType) ImgPlus(net.imagej.ImgPlus) ARGBType(net.imglib2.type.numeric.ARGBType) IntegerType(net.imglib2.type.numeric.IntegerType) Axes(net.imagej.axis.Axes) Composite(net.imglib2.view.composite.Composite) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Intervals(net.imglib2.util.Intervals) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) Labeling(sc.fiji.labkit.ui.labeling.Labeling) LabkitUtils(sc.fiji.labkit.ui.utils.LabkitUtils) FastRandomForest(hr.irb.fastRandomForest.FastRandomForest) BiConsumer(java.util.function.BiConsumer) AxisType(net.imagej.axis.AxisType) Label(sc.fiji.labkit.ui.labeling.Label) CellLoader(net.imglib2.cache.img.CellLoader) Training(sc.fiji.labkit.pixel_classification.classification.Training) Segmenter(sc.fiji.labkit.ui.segmentation.Segmenter) GlobalSettings(sc.fiji.labkit.pixel_classification.pixel_feature.settings.GlobalSettings) FeatureCalculator(sc.fiji.labkit.pixel_classification.pixel_feature.calculator.FeatureCalculator) GroupedFeatures(sc.fiji.labkit.pixel_classification.pixel_feature.filter.GroupedFeatures) IntType(net.imglib2.type.numeric.integer.IntType) ChannelSetting(sc.fiji.labkit.pixel_classification.pixel_feature.settings.ChannelSetting) RandomAccessible(net.imglib2.RandomAccessible) ImgPlusViews(net.imglib2.img.display.imagej.ImgPlusViews) ImgPlusViewsOld(sc.fiji.labkit.ui.inputimage.ImgPlusViewsOld) javax.swing(javax.swing) Set(java.util.Set) HashMap(java.util.HashMap) Label(sc.fiji.labkit.ui.labeling.Label) SparseRandomAccessIntType(sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType) IntType(net.imglib2.type.numeric.integer.IntType) SparseRandomAccessIntType(sc.fiji.labkit.ui.utils.sparse.SparseRandomAccessIntType) LabelingType(net.imglib2.roi.labeling.LabelingType)

Example 3 with Labeling

use of sc.fiji.labkit.ui.labeling.Labeling in project labkit-ui by juglab.

the class MultiChannelMovieDemo method testInputImageImageForSegmentation.

@Test
public void testInputImageImageForSegmentation() {
    DatasetInputImage inputImage = inputImage5d();
    SegmentationModel segmentationModel = new DefaultSegmentationModel(new Context(), inputImage);
    SegmentationItem segmenter = segmentationModel.segmenterList().addSegmenter(PixelClassificationPlugin.create());
    Labeling labeling1 = labeling5d();
    segmentationModel.imageLabelingModel().labeling().set(labeling1);
    segmenter.train(Collections.singletonList(new ValuePair<>(inputImage.imageForSegmentation(), labeling1)));
    RandomAccessibleInterval<ShortType> result = segmenter.results(segmentationModel.imageLabelingModel()).segmentation();
    Labeling labeling = labeling5d();
    LoopBuilder.setImages(labeling, result).forEachPixel((l, r) -> {
        if (l.contains("foreground"))
            assertEquals(1, r.get());
        if (l.contains("background"))
            assertEquals(0, r.get());
    });
}
Also used : Context(org.scijava.Context) DefaultSegmentationModel(sc.fiji.labkit.ui.models.DefaultSegmentationModel) DatasetInputImage(sc.fiji.labkit.ui.inputimage.DatasetInputImage) SegmentationItem(sc.fiji.labkit.ui.models.SegmentationItem) ValuePair(net.imglib2.util.ValuePair) ShortType(net.imglib2.type.numeric.integer.ShortType) Labeling(sc.fiji.labkit.ui.labeling.Labeling) DefaultSegmentationModel(sc.fiji.labkit.ui.models.DefaultSegmentationModel) SegmentationModel(sc.fiji.labkit.ui.models.SegmentationModel) Test(org.junit.Test)

Example 4 with Labeling

use of sc.fiji.labkit.ui.labeling.Labeling in project labkit-ui by juglab.

the class MultiChannelMovieDemo method labeling5d.

private Labeling labeling5d() {
    Labeling labeling = Labeling.createEmpty(Arrays.asList("background", "foreground"), new FinalInterval(20, 10, 10, 20));
    RandomAccess<LabelingType<Label>> ra = labeling.randomAccess();
    ra.setPosition(new long[] { 1, 0, 0, 1 });
    ra.get().add(labeling.getLabel("foreground"));
    ra.setPosition(new long[] { 4, 0, 0, 1 });
    ra.get().add(labeling.getLabel("foreground"));
    ra.setPosition(new long[] { 5, 0, 0, 1 });
    ra.get().add(labeling.getLabel("background"));
    ra.setPosition(new long[] { 0, 0, 0, 1 });
    ra.get().add(labeling.getLabel("background"));
    return labeling;
}
Also used : FinalInterval(net.imglib2.FinalInterval) Labeling(sc.fiji.labkit.ui.labeling.Labeling) LabelingType(net.imglib2.roi.labeling.LabelingType)

Example 5 with Labeling

use of sc.fiji.labkit.ui.labeling.Labeling in project labkit-ui by juglab.

the class SegmentationUseCaseTest method testMultiChannel.

@Test
public void testMultiChannel() throws InterruptedException {
    Img<UnsignedByteType> img = ArrayImgs.unsignedBytes(new byte[] { -1, 0, -1, 0, -1, -1, 0, 0 }, 2, 2, 2);
    ImgPlus<UnsignedByteType> imgPlus = new ImgPlus<>(img, "Image", new AxisType[] { Axes.X, Axes.Y, Axes.CHANNEL });
    DatasetInputImage inputImage = new DatasetInputImage(imgPlus, BdvShowable.wrap(Views.hyperSlice(img, 2, 0)));
    Labeling labeling = getLabeling();
    SegmentationModel segmentationModel = new DefaultSegmentationModel(new Context(), inputImage);
    ImageLabelingModel imageLabelingModel = segmentationModel.imageLabelingModel();
    imageLabelingModel.labeling().set(labeling);
    SegmentationItem segmenter = segmentationModel.segmenterList().addSegmenter(PixelClassificationPlugin.create());
    segmenter.train(Collections.singletonList(new ValuePair<>(imgPlus, imageLabelingModel.labeling().get())));
    RandomAccessibleInterval<ShortType> result = segmenter.results(imageLabelingModel).segmentation();
    Iterator<ShortType> it = Views.iterable(result).iterator();
    assertEquals(1, it.next().get());
    assertEquals(0, it.next().get());
    assertEquals(0, it.next().get());
    assertEquals(0, it.next().get());
    assertTrue(Intervals.equals(new FinalInterval(2, 2), result));
}
Also used : Context(org.scijava.Context) ImgPlus(net.imagej.ImgPlus) DatasetInputImage(sc.fiji.labkit.ui.inputimage.DatasetInputImage) UnsignedByteType(net.imglib2.type.numeric.integer.UnsignedByteType) ValuePair(net.imglib2.util.ValuePair) ShortType(net.imglib2.type.numeric.integer.ShortType) DefaultSegmentationModel(sc.fiji.labkit.ui.models.DefaultSegmentationModel) SegmentationModel(sc.fiji.labkit.ui.models.SegmentationModel) ImageLabelingModel(sc.fiji.labkit.ui.models.ImageLabelingModel) DefaultSegmentationModel(sc.fiji.labkit.ui.models.DefaultSegmentationModel) SegmentationItem(sc.fiji.labkit.ui.models.SegmentationItem) FinalInterval(net.imglib2.FinalInterval) Labeling(sc.fiji.labkit.ui.labeling.Labeling) Test(org.junit.Test)

Aggregations

Labeling (sc.fiji.labkit.ui.labeling.Labeling)26 LabelingType (net.imglib2.roi.labeling.LabelingType)8 Label (sc.fiji.labkit.ui.labeling.Label)8 FinalInterval (net.imglib2.FinalInterval)7 BitType (net.imglib2.type.logic.BitType)7 Test (org.junit.Test)7 Context (org.scijava.Context)7 ImgPlus (net.imagej.ImgPlus)6 Interval (net.imglib2.Interval)6 Cursor (net.imglib2.Cursor)5 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)5 DatasetInputImage (sc.fiji.labkit.ui.inputimage.DatasetInputImage)5 File (java.io.File)4 List (java.util.List)4 Intervals (net.imglib2.util.Intervals)4 ValuePair (net.imglib2.util.ValuePair)4 Views (net.imglib2.view.Views)4 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3