hanslovsky

weka spark classification

Mar 27th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. package de.hanslovsky.zspacing.spark.experiments.mask.weka;
  2.  
  3. import java.io.File;
  4. import java.lang.invoke.MethodHandles;
  5. import java.util.ArrayList;
  6.  
  7. import org.apache.log4j.Level;
  8. import org.apache.log4j.LogManager;
  9. import org.apache.log4j.Logger;
  10. import org.apache.spark.SparkConf;
  11. import org.apache.spark.api.java.JavaPairRDD;
  12. import org.apache.spark.api.java.JavaSparkContext;
  13. import org.apache.spark.broadcast.Broadcast;
  14. import org.janelia.thickness.kryo.KryoSerialization;
  15. import org.janelia.thickness.utility.Utility;
  16.  
  17. import ij.ImagePlus;
  18. import ij.ImageStack;
  19. import ij.io.FileSaver;
  20. import ij.process.ImageProcessor;
  21. import scala.Tuple2;
  22. import trainableSegmentation.WekaSegmentation;
  23.  
  24. public class WekaClassifierSpark
  25. {
  26.  
  27.     public static final Logger LOG = LogManager.getLogger( MethodHandles.lookup().lookupClass() );
  28.     static
  29.     {
  30.         LOG.setLevel( Level.INFO );
  31.     }
  32.  
  33.     public static void main( final String[] args )
  34.     {
  35.  
  36.         final SparkConf conf = new SparkConf()
  37.                 .setAppName( MethodHandles.lookup().lookupClass().getSimpleName() )
  38.                 .set( "spark.network.timeout", "600" )
  39.                 .set( "spark.serializer", "org.apache.spark.serializer.KryoSerializer" )
  40.                 .set( "spark.kryoserializer.buffer.max", "1g" )
  41.                 .set( "spark.kryo.unsafe", "true" ) // supposed to give huge
  42.                 // performance boost for
  43.                 // primitive arrays
  44.                 .set( "spark.kryo.registrator", KryoSerialization.Registrator.class.getName() );
  45.         final JavaSparkContext sc = new JavaSparkContext( conf );
  46.         Logger.getRootLogger().setLevel( Level.ERROR );
  47.  
  48.         final String classifierPath = args[ 0 ];
  49.         final String source = args[ 1 ];
  50.         final String target = args[ 2 ];
  51.         final int start = Integer.parseInt( args[ 3 ] );
  52.         final int stop = Integer.parseInt( args[ 4 ] );
  53.         final int step = Integer.parseInt( args[ 5 ] );
  54.         final int classLabel = args.length > 6 ? Integer.parseInt( args[ 6 ] ) : -1;
  55.  
  56.         run( sc, classifierPath, source, target, start, stop, step, classLabel );
  57.  
  58.         sc.stop();
  59.  
  60.     }
  61.  
  62.     public static void run( final JavaSparkContext sc, final String classifierPath, final String source, final String target, final int start, final int stop, final int step, final int classLabel )
  63.     {
  64.         final ArrayList< Integer > indices = Utility.arange( start, stop, step );
  65.         final JavaPairRDD< Integer, ImageProcessor > images = sc.parallelize( indices ).mapToPair( i -> {
  66.             final String fp = String.format( source, i );
  67.             LOG.debug( "Loading image locted at " + fp );
  68.             return new Tuple2<>( i, new ImagePlus( fp ).getProcessor() );
  69.         } );
  70.         final JavaPairRDD< ?, ImageProcessor[] > classified = run( sc, images, classifierPath );
  71.  
  72.         new File( String.format( target, start ) ).getParentFile().mkdirs();
  73.         if ( classLabel >= 0 )
  74.         {
  75.             final int count = classified.map( t -> {
  76.                 final ImageProcessor fp = t._2()[ classLabel ];
  77.                 final int length = fp.getWidth() * fp.getHeight();
  78.                 for ( int i = 0; i < length; ++i )
  79.                     if ( fp.getf( i ) < 0.2f )
  80.                         fp.setf( i, 0.0f );
  81.                 return new FileSaver( new ImagePlus( "", fp ) ).saveAsTiff( String.format( target, t._1() ) ) ? 1 : 0;
  82.             } ).reduce( ( i1, i2 ) -> i1 + i2 );
  83.             LOG.info( "Classified and wrote " + count + "/" + indices.size() + "images " );
  84.         }
  85.         else
  86.         {
  87.             final int count = classified.map( t -> new FileSaver( new ImagePlus( "", toStack( t._2() ) ) ).saveAsTiff( String.format( target, t._1() ) ) ? 1 : 0 ).reduce( ( i1, i2 ) -> i1 + i2 );
  88.             LOG.info( "Classified and wrote " + count + "/" + indices.size() + "images " );
  89.         }
  90.     }
  91.  
  92.     public static < K > JavaPairRDD< K, ImageProcessor[] > run( final JavaSparkContext sc, final JavaPairRDD< K, ImageProcessor > imgs, final String classifierPath )
  93.     {
  94.  
  95.         final Broadcast< String > classifierPathBC = sc.broadcast( classifierPath );
  96.  
  97.         final JavaPairRDD< K, ImageProcessor[] > classified = imgs.mapValues( img -> {
  98.             final ImagePlus imp = new ImagePlus( "", img );
  99.             final WekaSegmentation seg = new WekaSegmentation( imp );
  100.             final String cp = classifierPathBC.getValue();
  101.             LOG.debug( "Loading classifier from " + cp + " . File exists? " + new File( cp ).exists() );
  102.             seg.loadClassifier( cp );
  103.             final ImagePlus probs = seg.applyClassifier( imp, 1, true );
  104.             final ImageProcessor[] result = new ImageProcessor[ probs.getStackSize() ];
  105.             final ImageStack s = probs.getStack();
  106.             for ( int i = 0; i < result.length; ++i )
  107.                 result[ i ] = s.getProcessor( i + 1 );
  108.             return result;
  109.         } );
  110.  
  111.         return classified;
  112.     }
  113.  
  114.     public static ImageStack toStack( final ImageProcessor[] ips )
  115.     {
  116.         final int w = ips[ 0 ].getWidth();
  117.         final int h = ips[ 0 ].getHeight();
  118.         final ImageStack is = new ImageStack( w, h );
  119.         for ( final ImageProcessor ip : ips )
  120.             is.addSlice( ip );
  121.         return is;
  122.     }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment