Weka segmentaion and set batch mode

Hi,
i’m using weka in a macro loop to extract the probability map of several images contained in a folder.
I would like to use a “setbatchMode” function to prevent the opening/closing of the weka window and also maybe make by macro faster.
However, Weka seems to be not compatible with that function.
Any thoughts?
Thanks in advance
Alban

1 Like

I think you are right, @Alban_Favre. I tried running the complete macro example and prepending setBatchMode(true); and it generated the following exception on my system:

java.lang.NullPointerException
	at trainableSegmentation.Weka_Segmentation.run(Weka_Segmentation.java:1268)

The line of code in question assumes there will be a window associated with the training image, but in batch mode the window is null.

I also attempted to run TWS headless via fiji --headless -macro complete-macro-example.ijm, which yielded the exception:

java.awt.HeadlessException
	at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204)
	at java.awt.List.<init>(List.java:233)
	at java.awt.List.<init>(List.java:205)
	at trainableSegmentation.Weka_Segmentation.run(Weka_Segmentation.java:1253)
1 Like

The macro scriptability of Trainable Weka Segmentation (TWS) indeed isn’t compatible with batch mode, as it relies on the user interface being displayed.

The recommended way to use TWS headlessly is via scripting (e.g. Beanshell or Groovy) as described on this page:

If you have a more complex macro workflow already and don’t want to re-code everything in a more powerful language, you can also save a script (that just does the segmentation) in e.g. ./Fiji.app/scripts/Plugins/My_Own_Segmentation.groovy and then call run("My Own Segmentation"); from your macro.

5 Likes

Hello @Alban_Favre,

Just to add to what @imagejan said, you have an example script in the Trainable Weka Segmentation scripting wiki page that does exactly what you want.

1 Like

Thank you very much, it worked for me expect for one thing:
By scripting the segmentation function and calling it in a macro I lost the name of each classes on my probability plot.
Before slice1 was named materialx, slice 2 material y etc. and now it is just 1,2;3 4 , Is there a way to recover that?
below my script :

import ij.IJ;
import ij.ImageStack;
import ij.ImagePlus;
import ij.process.ColorSpaceConverter;
import ij.process.ByteProcessor;
import trainableSegmentation.FeatureStack;
import trainableSegmentation.FeatureStackArray;
import trainableSegmentation.WekaSegmentation;
import weka.clusterers.EM;
import weka.clusterers.SimpleKMeans;
import weka.core.WekaPackageManager;
import weka.core.WekaPackageClassLoaderManager;

 
// create segmentator
segmentator = new WekaSegmentation();
// load classifier
segmentator.loadClassifier( "C:/Fiji.app/macros/Classifier/classifierDP4.model");
image = IJ.getImage() ;
               
// apply classifier and get results (0 indicates number of threads is auto-detected)
result = segmentator.applyClassifier( image, 0, true );
result.show();
			
// force garbage collection (important for large images)
result = null; 
image = null;
System.gc();

Thanks
Alban

1 Like

I see, you can always set the slice names after you get the result like this:

for( slice=1; slice <= result.getImageStackSize(); slice ++)
    result.getStack().setSliceLabel( image.getStack().getSliceLabel( slice ), slice );

Thanks but it renames only the first slice with the name of my original image. I would need to rename each slice with the name of each class of my classifier.
Thanks again

My bad, I see what you mean now. It is indeed a bug in applyClassifier. I will fix it in the next release, but for now you can do this:

for( slice=1; slice <= result.getImageStackSize(); slice ++ )
    result.getStack().setSliceLabel( segmentator.getClassLabel( slice-1 ), slice );
1 Like

@Alban_Favre, the new release has fixed that and other issues.

3 Likes