Commit d69881f9 authored by rabuske's avatar rabuske
Browse files

Integrated average impurity decrease output

parent 1ab6e498
......@@ -101,27 +101,27 @@ public class ClassifierEvaluation {
// // for()
// SampleQueryConstraints sqc = new SampleQueryConstraints();
sqc.roiWkt = DefaultShapesWKT.Regions.BERLIN_BRANDENBURG;
sqc.startTimestamp = "2017-05-01 00:00:00";
sqc.endTimestamp = "2017-10-01 00:00:00";
sqc.maxCloudcover = 50.0;
sqc.allowedS2L2aClasses = new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
Dataset<Sentinel2LucasSample> trainingSet = TrainingDatasetFactory.deriveFromBandSamples("training_brb", sqc);
LOG.debug("training set: " + trainingSet.getName() + ": | numSamples: " + trainingSet.getInstances().size());
// int id = TrainingDatasetFactory.BRB_2017_TRAINING_SET_ID,
int size = 60;
String name = "BRB_2017", opts = "-num-slots 0 -attribute-importance -U";
SupportedClassifier classifier = SupportedClassifier.RandomForest;
Path imageInput = Paths.get("/home/rabuske/Downloads/S2A_MSIL2A_20170527T102031_N0205_R065_T32UQD_20170527T102301.zip");
File imageOutput = Paths.get("/home/rabuske/Data/S2A_MSIL2A_20170527T102031_N0205_R065_T32UQD_20170527T102301_" + time + ".tif").toFile();
CharSequence text = trainingSet.getName() + " \n" + name + " \n" + classifier.toString() + " \n" + opts + " \n" + Integer.toString(size);
Files.write(text, new File(imageOutput.getAbsolutePath().replace(".tif", ".txt")), Charsets.UTF_8);
classifyScene(newClassifierFromTrainingset(trainingSet, name, classifier, opts, size), imageInput, imageOutput);
// sqc.roiWkt = DefaultShapesWKT.Regions.BERLIN_BRANDENBURG;
// sqc.startTimestamp = "2017-05-01 00:00:00";
// sqc.endTimestamp = "2017-10-01 00:00:00";
// sqc.maxCloudcover = 50.0;
// sqc.allowedS2L2aClasses = new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
// Dataset<Sentinel2LucasSample> trainingSet = TrainingDatasetFactory.deriveFromBandSamples("training_brb", sqc);
// LOG.debug("training set: " + trainingSet.getName() + ": | numSamples: " + trainingSet.getInstances().size());
//
// // int id = TrainingDatasetFactory.BRB_2017_TRAINING_SET_ID,
// int size = 60;
// String name = "BRB_2017", opts = "-num-slots 0 -attribute-importance -U";
// SupportedClassifier classifier = SupportedClassifier.RandomForest;
//
// Path imageInput = Paths.get("/home/rabuske/Downloads/S2A_MSIL2A_20170527T102031_N0205_R065_T32UQD_20170527T102301.zip");
// File imageOutput = Paths.get("/home/rabuske/Data/S2A_MSIL2A_20170527T102031_N0205_R065_T32UQD_20170527T102301_" + time + ".tif").toFile();
//
// CharSequence text = trainingSet.getName() + " \n" + name + " \n" + classifier.toString() + " \n" + opts + " \n" + Integer.toString(size);
//
// Files.write(text, new File(imageOutput.getAbsolutePath().replace(".tif", ".txt")), Charsets.UTF_8);
//
// classifyScene(newClassifierFromTrainingset(trainingSet, name, classifier, opts, size), imageInput, imageOutput);
}
public static Sentinel2LucasClassifier newClassifierFromTrainingset(int trainingsetId, String name, SupportedClassifier type, String options,
......
......@@ -33,6 +33,9 @@ import de.potsdam.gfz.seva.ml.classification.attribute.extractor.Sentinel2BandAt
import de.potsdam.gfz.seva.ml.classification.attribute.extractor.Sentinel2L2aClassMaskExtractor;
import de.potsdam.gfz.seva.ml.classification.sample_selection.PartitionedSampleSectionStrategy;
import de.potsdam.gfz.seva.ml.classification.sample_selection.SelectAllSamplesStrategy;
import weka.attributeSelection.InfoGainAttributeEval;
import weka.classifiers.trees.RandomForest;
import weka.classifiers.trees.RandomTree;
import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;
......@@ -47,22 +50,23 @@ import weka.core.Instances;
public class ParameterCombinationEvaluator implements ClassifierEvaluator {
private static final AutoIndexedAttribute[] ATTRIBUTES = {
// new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.B01)),
new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.BLUE)),
new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.GREEN)),
new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.RED)),
new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.NIR)),
new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.B05)),
new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.B06)),
new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.B07)),
new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.NNIR)),
new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.SWIR1)),
new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.SWIR2)),
new NominalAttribute(new Sentinel2L2aClassMaskExtractor()),
new NumericAttribute(new DayOfYearAttributeExtractor()),
new NumericAttribute(new LatitudeLocationAttributeExtractor()),
new NumericAttribute(new NDVIExtractor()),
new NumericAttribute(new NBRExtractor()),
new NumericAttribute(new MSAVI2Extractor()),
new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.NIR)),
// new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.NNIR)),
// new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.SWIR1)),
// new NumericAttribute(new Sentinel2BandAttributeExtractor(Sentinel2Band.SWIR2)),
// new NominalAttribute(new Sentinel2L2aClassMaskExtractor()),
// new NumericAttribute(new DayOfYearAttributeExtractor()),
// new NumericAttribute(new LatitudeLocationAttributeExtractor()),
// new NumericAttribute(new NDVIExtractor()),
// new NumericAttribute(new NBRExtractor()),
// new NumericAttribute(new MSAVI2Extractor()),
};
/**
......@@ -76,6 +80,7 @@ public class ParameterCombinationEvaluator implements ClassifierEvaluator {
* Percentage (0..1) of correctly classified samples.
*/
double truePositives;
String importances;
@Override
public String toString() {
......@@ -86,7 +91,7 @@ public class ParameterCombinationEvaluator implements ClassifierEvaluator {
}
ret += a.toString() + ",";
}
return ret + ";" + truePositives;
return ret + ";" + importances + ";" + truePositives;
}
}
......@@ -101,7 +106,20 @@ public class ParameterCombinationEvaluator implements ClassifierEvaluator {
*/
SevaDBConnection con = new SevaDBConnection(SevaDB.getConnection());
List<Sentinel2LucasSample> samples = con.getSamples(sqc);
// ParameterCombination pctest = new ParameterCombination();
// for (int i = 0; i < ATTRIBUTES.length; i++) pctest.addFeatureAttribute(ATTRIBUTES[i]);
// pctest.setClassAttribute(new NominalAttribute(new LucasLandcoverAttributeExtractor()));
// pctest.setClassy(SupportedClassifier.RandomForest);
// pctest.setTargetResolution(10);
// pctest.setSampleStrategy(new SelectAllSamplesStrategy());
// pctest.setName("test_classifier");
// pctest.setUsedTrainingSetId(-1);
//
// String[] opts = {"-num-slots", "0" ,"-attribute-importance", "-print", "-U"};
// pctest.setOptions(opts);
//
// evaluateCombination(samples, pctest, resultFilepath);
List<ParameterCombination> combinationList = getPossibleCombinations();
combinationList.forEach(pc -> evaluateCombination(samples, pc, resultFilepath));
......@@ -123,13 +141,16 @@ public class ParameterCombinationEvaluator implements ClassifierEvaluator {
if (((i >> j) & 1) == 1)
pc.addFeatureAttribute(ATTRIBUTES[j]);
}
pc.setClassAttribute(new NominalAttribute(new LucasLandcoverAttributeExtractor()));
pc.setClassy(SupportedClassifier.RandomForest);
pc.setTargetResolution(10);
pc.setSampleStrategy(new SelectAllSamplesStrategy());
pc.setName("test_classifier");
pc.setUsedTrainingSetId(-1);
String[] opts = {"-num-slots", "0" ,"-attribute-importance", "-print", "-U"};
pc.setOptions(opts);
list.add(pc);
}
......@@ -184,7 +205,7 @@ public class ParameterCombinationEvaluator implements ClassifierEvaluator {
// Instantiate and train classifier...
Sentinel2LucasClassifier classifier = new Sentinel2LucasClassifier(pc);
classifier.train(trainingSamples);
/**
* TODO: evaluate classifier by determining the number of true positive
* classifications with the validation set and store result in
......@@ -193,7 +214,15 @@ public class ParameterCombinationEvaluator implements ClassifierEvaluator {
// validate and set parametercombination result
pc.truePositives = validateDataset(classifier, validationSamples);
if (classifier.getCfg().getClassy() == SupportedClassifier.RandomForest) pc.importances = Arrays.toString(( (RandomForest) classifier.getClassy() ).computeAverageImpurityDecreasePerAttribute(null));
// InfoGainAttributeEval eval = new InfoGainAttributeEval();
// eval.buildEvaluator(Sentinel2LucasClassifier.samplesToDataset(pc, trainingSamples));
//
// for(Attribute a : pc.getAttributes()) {
// System.out.println(a.toString() + " infogain: " + eval.evaluateAttribute(a.index()));
// }
// append result to result file
appendToResultFile(pc, resultFilepath);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment