Skip to content
Snippets Groups Projects
Commit 2bbde7d4 authored by Vojtech Moravec's avatar Vojtech Moravec
Browse files

Simplified the benchmark classes, use the CliParsedOptions.

parent 06f2c3d0
No related branches found
No related tags found
No related merge requests found
...@@ -23,8 +23,9 @@ abstract class BenchmarkBase { ...@@ -23,8 +23,9 @@ abstract class BenchmarkBase {
protected final int[] planes; protected final int[] planes;
protected final V3i rawImageDims; protected final V3i rawImageDims;
protected boolean hasReferencePlane = false; protected final boolean hasReferencePlane;
protected int referencePlaneIndex = -1; protected final int referencePlaneIndex;
protected final int codebookSize;
protected BenchmarkBase(final String inputFile, protected BenchmarkBase(final String inputFile,
final String outputDirectory, final String outputDirectory,
...@@ -34,6 +35,10 @@ abstract class BenchmarkBase { ...@@ -34,6 +35,10 @@ abstract class BenchmarkBase {
this.outputDirectory = outputDirectory; this.outputDirectory = outputDirectory;
this.planes = planes; this.planes = planes;
this.rawImageDims = rawImageDims; this.rawImageDims = rawImageDims;
hasReferencePlane = false;
referencePlaneIndex = -1;
codebookSize = 256;
} }
protected BenchmarkBase(final ParsedCliOptions options) { protected BenchmarkBase(final ParsedCliOptions options) {
...@@ -41,9 +46,8 @@ abstract class BenchmarkBase { ...@@ -41,9 +46,8 @@ abstract class BenchmarkBase {
this.outputDirectory = options.getOutputFile(); this.outputDirectory = options.getOutputFile();
this.rawImageDims = options.getImageDimension(); this.rawImageDims = options.getImageDimension();
this.hasReferencePlane = options.hasReferencePlaneIndex(); this.hasReferencePlane = options.hasReferencePlaneIndex();
if (this.hasReferencePlane) { this.referencePlaneIndex = options.getReferencePlaneIndex();
this.referencePlaneIndex = options.getReferencePlaneIndex(); this.codebookSize = (int) Math.pow(2, options.getBitsPerPixel());
}
if (options.hasPlaneIndexSet()) { if (options.hasPlaneIndexSet()) {
this.planes = new int[]{options.getPlaneIndex()}; this.planes = new int[]{options.getPlaneIndex()};
......
...@@ -31,57 +31,62 @@ public class ScalarQuantizationBenchmark extends BenchmarkBase { ...@@ -31,57 +31,62 @@ public class ScalarQuantizationBenchmark extends BenchmarkBase {
@Override @Override
public void startBenchmark() { public void startBenchmark() {
// TODO(Moravec): Support parsed CLI options. if (planes.length < 1) {
return;
}
boolean dirCreated = new File(this.outputDirectory).mkdirs(); boolean dirCreated = new File(this.outputDirectory).mkdirs();
ScalarQuantizer quantizer = null;
if (hasReferencePlane) {
final int[] refPlaneData = loadPlaneData(referencePlaneIndex);
if (refPlaneData.length == 0) {
System.err.println("Failed to load reference plane data.");
return;
}
if (useDiffEvolution) {
quantizer = trainDifferentialEvolution(refPlaneData, codebookSize);
} else {
quantizer = trainLloydMaxQuantizer(refPlaneData, codebookSize);
}
}
for (final int planeIndex : planes) { for (final int planeIndex : planes) {
System.out.println(String.format("Loading plane %d ...", planeIndex)); System.out.println(String.format("Loading plane %d ...", planeIndex));
// NOTE(Moravec): Actual planeIndex is zero based. // NOTE(Moravec): Actual planeIndex is zero based.
final int[] planeData = loadPlaneData(planeIndex - 1); final int[] planeData = loadPlaneData(planeIndex);
if (planeData.length == 0) { if (planeData.length == 0) {
System.err.println(String.format("Failed to load plane %d data. Skipping plane.", planeIndex)); System.err.println(String.format("Failed to load plane %d data. Skipping plane.", planeIndex));
return; return;
} }
System.out.println(String.format("|CODEBOOK| = %d", codebookSize));
// Test codebook sizes from 2^2 to 2^8 if (!hasReferencePlane) {
for (int bitCount = 2; bitCount <= 8; bitCount++) {
final int codebookSize = (int) Math.pow(2, bitCount);
System.out.println(String.format("|CODEBOOK| = %d", codebookSize));
ScalarQuantizer quantizer = null;
if (useDiffEvolution) { if (useDiffEvolution) {
quantizer = trainDifferentialEvolution(planeData, codebookSize, planeIndex); quantizer = trainDifferentialEvolution(planeData, codebookSize);
} else { } else {
quantizer = trainLloydMaxQuantizer(planeData, codebookSize, planeIndex); quantizer = trainLloydMaxQuantizer(planeData, codebookSize);
}
if (quantizer == null) {
System.err.println("Failed to initialize scalar quantizer. Skipping plane.");
return;
} }
System.out.println("Scalar quantizer ready."); }
if (quantizer == null) {
// final String method = useDiffEvolution ? "ilshade" : "lloyd"; System.err.println("Failed to initialize scalar quantizer.");
// final String centroidsFile = String.format("p%d_cb%d%s_centroids.txt", return;
// planeIndex, }
// codebookSize, System.out.println("Scalar quantizer ready.");
// method);
// saveCentroids(quantizer.getCentroids(), centroidsFile);
final String quantizedFile = String.format(QUANTIZED_FILE_TEMPLATE, planeIndex, codebookSize);
final String diffFile = String.format(DIFFERENCE_FILE_TEMPLATE, planeIndex, codebookSize);
final String absoluteDiffFile = String.format(ABSOLUTE_DIFFERENCE_FILE_TEMPLATE,
planeIndex,
codebookSize);
final int[] quantizedData = quantizer.quantize(planeData); final String quantizedFile = String.format(QUANTIZED_FILE_TEMPLATE, planeIndex, codebookSize);
final String diffFile = String.format(DIFFERENCE_FILE_TEMPLATE, planeIndex, codebookSize);
final String absoluteDiffFile = String.format(ABSOLUTE_DIFFERENCE_FILE_TEMPLATE,
planeIndex,
codebookSize);
if (!saveQuantizedPlaneData(quantizedData, quantizedFile)) { final int[] quantizedData = quantizer.quantize(planeData);
System.err.println("Failed to save quantized plane.");
return;
}
saveDifference(planeData, quantizedData, diffFile, absoluteDiffFile); if (!saveQuantizedPlaneData(quantizedData, quantizedFile)) {
System.err.println("Failed to save quantized plane.");
return;
} }
saveDifference(planeData, quantizedData, diffFile, absoluteDiffFile);
} }
} }
...@@ -110,29 +115,26 @@ public class ScalarQuantizationBenchmark extends BenchmarkBase { ...@@ -110,29 +115,26 @@ public class ScalarQuantizationBenchmark extends BenchmarkBase {
} }
} }
private ScalarQuantizer trainLloydMaxQuantizer(final int[] data, final int codebookSize, final int planeIndex) { private ScalarQuantizer trainLloydMaxQuantizer(final int[] data, final int codebookSize) {
LloydMaxU16ScalarQuantization lloydMax = new LloydMaxU16ScalarQuantization(data, codebookSize); LloydMaxU16ScalarQuantization lloydMax = new LloydMaxU16ScalarQuantization(data, codebookSize);
QTrainIteration[] trainingReport = lloydMax.train(true); QTrainIteration[] trainingReport = lloydMax.train(true);
saveQTrainLog(String.format("p%d_cb_%d_lloyd.csv", planeIndex, codebookSize), trainingReport); //saveQTrainLog(String.format("p%d_cb_%d_lloyd.csv", planeIndex, codebookSize), trainingReport);
return new ScalarQuantizer(U16.Min, U16.Max, lloydMax.getCentroids()); return new ScalarQuantizer(U16.Min, U16.Max, lloydMax.getCentroids());
} }
private ScalarQuantizer trainDifferentialEvolution(final int[] data, private ScalarQuantizer trainDifferentialEvolution(final int[] data,
final int codebookSize, final int codebookSize) {
final int planeIndex) {
ILShadeSolver ilshade = new ILShadeSolver(codebookSize, 100, 2000, 15); ILShadeSolver ilshade = new ILShadeSolver(codebookSize, 100, 2000, 15);
ilshade.setTrainingData(data); ilshade.setTrainingData(data);
QTrainIteration[] trainingReport = null;
try { try {
trainingReport = ilshade.train(); ilshade.train();
} catch (DeException deEx) { } catch (DeException deEx) {
deEx.printStackTrace(); deEx.printStackTrace();
return null; return null;
} }
saveQTrainLog(String.format("p%d_cb_%d_il_shade.csv", planeIndex, codebookSize), trainingReport);
return new ScalarQuantizer(U16.Min, U16.Max, ilshade.getBestSolution().getAttributes()); return new ScalarQuantizer(U16.Min, U16.Max, ilshade.getBestSolution().getAttributes());
} }
......
...@@ -54,15 +54,33 @@ public class VectorQuantizationBenchmark extends BenchmarkBase { ...@@ -54,15 +54,33 @@ public class VectorQuantizationBenchmark extends BenchmarkBase {
} }
public void startBenchmark(final V2i qVector) { public void startBenchmark(final V2i qVector) {
if (planes.length < 1) {
return;
}
// TODO(Moravec): Support parsed CLI options. // TODO(Moravec): Support parsed CLI options.
if (qVector.getY() > 1) { if (qVector.getY() > 1) {
System.out.println("2D qVector"); System.out.println("2D qVector");
} else { } else {
System.out.println("1D qVector"); System.out.println("1D qVector");
} }
boolean dirCreated = new File(this.outputDirectory).mkdirs(); boolean dirCreated = new File(this.outputDirectory).mkdirs();
VectorQuantizer quantizer = null;
if (hasReferencePlane) {
final ImageU16 plane = loadPlane(referencePlaneIndex);
if (plane == null) {
System.err.println("Failed to load reference plane data.");
return;
}
final int[][] refPlaneData = getPlaneVectors(plane, qVector);
LBGVectorQuantizer vqInitializer = new LBGVectorQuantizer(refPlaneData, codebookSize);
final LBGResult vqResult = vqInitializer.findOptimalCodebook();
quantizer = new VectorQuantizer(vqResult.getCodebook());
}
for (final int planeIndex : planes) { for (final int planeIndex : planes) {
System.out.println(String.format("Loading plane %d ...", planeIndex)); System.out.println(String.format("Loading plane %d ...", planeIndex));
// NOTE(Moravec): Actual planeIndex is zero based. // NOTE(Moravec): Actual planeIndex is zero based.
...@@ -76,35 +94,29 @@ public class VectorQuantizationBenchmark extends BenchmarkBase { ...@@ -76,35 +94,29 @@ public class VectorQuantizationBenchmark extends BenchmarkBase {
final int[][] planeData = getPlaneVectors(plane, qVector); final int[][] planeData = getPlaneVectors(plane, qVector);
System.out.println(String.format("|CODEBOOK| = %d", codebookSize));
// Test codebook sizes from 2^2 to 2^8 if (!hasReferencePlane) {
for (int bitCount = 2; bitCount <= 8; bitCount++) {
final int codebookSize = (int) Math.pow(2, bitCount);
System.out.println(String.format("|CODEBOOK| = %d", codebookSize));
LBGVectorQuantizer vqInitializer = new LBGVectorQuantizer(planeData, codebookSize); LBGVectorQuantizer vqInitializer = new LBGVectorQuantizer(planeData, codebookSize);
LBGResult vqResult = vqInitializer.findOptimalCodebook(); LBGResult vqResult = vqInitializer.findOptimalCodebook();
quantizer = new VectorQuantizer(vqResult.getCodebook());
}
VectorQuantizer quantizer = new VectorQuantizer(vqResult.getCodebook()); final String quantizedFile = String.format(QUANTIZED_FILE_TEMPLATE, planeIndex, codebookSize);
final String diffFile = String.format(DIFFERENCE_FILE_TEMPLATE, planeIndex, codebookSize);
final String quantizedFile = String.format(QUANTIZED_FILE_TEMPLATE, planeIndex, codebookSize); final String absoluteDiffFile = String.format(ABSOLUTE_DIFFERENCE_FILE_TEMPLATE,
final String diffFile = String.format(DIFFERENCE_FILE_TEMPLATE, planeIndex, codebookSize); planeIndex,
final String absoluteDiffFile = String.format(ABSOLUTE_DIFFERENCE_FILE_TEMPLATE, planeIndex, codebookSize); codebookSize);
// final String codebookFile = String.format("p%d_cb%d_vectors.txt", planeIndex, codebookSize);
// saveCodebook(vqResult.getCodebook(), codebookFile);
final int[][] quantizedData = quantizer.quantize(planeData);
final ImageU16 quantizedImage = reconstructImageFromQuantizedVectors(plane, quantizedData, qVector); final int[][] quantizedData = quantizer.quantize(planeData);
if (!saveQuantizedPlaneData(quantizedImage.getData(), quantizedFile)) { final ImageU16 quantizedImage = reconstructImageFromQuantizedVectors(plane, quantizedData, qVector);
System.err.println("Failed to save quantized plane.");
return;
}
saveDifference(plane.getData(), quantizedImage.getData(), diffFile, absoluteDiffFile); if (!saveQuantizedPlaneData(quantizedImage.getData(), quantizedFile)) {
System.err.println("Failed to save quantized plane.");
return;
} }
saveDifference(plane.getData(), quantizedImage.getData(), diffFile, absoluteDiffFile);
} }
} }
......
...@@ -27,7 +27,7 @@ public class ParsedCliOptions { ...@@ -27,7 +27,7 @@ public class ParsedCliOptions {
private int planeIndex; private int planeIndex;
private boolean refPlaneIndexSet = false; private boolean refPlaneIndexSet = false;
private int referencePlaneIndex; private int referencePlaneIndex = -1;
private boolean verbose; private boolean verbose;
...@@ -49,15 +49,22 @@ public class ParsedCliOptions { ...@@ -49,15 +49,22 @@ public class ParsedCliOptions {
String defaultValue = outputFile.getAbsolutePath(); String defaultValue = outputFile.getAbsolutePath();
switch (method) { switch (method) {
case Compress: case Compress: {
defaultValue += CompressorDecompressorBase.EXTENSTION; defaultValue += CompressorDecompressorBase.EXTENSTION;
break; }
break;
case Decompress: { case Decompress: {
if (defaultValue.toUpperCase().endsWith(CompressorDecompressorBase.EXTENSTION)) { if (defaultValue.toUpperCase().endsWith(CompressorDecompressorBase.EXTENSTION)) {
defaultValue = defaultValue.substring(0, defaultValue = defaultValue.substring(0,
defaultValue.length() - CompressorDecompressorBase.EXTENSTION.length()); defaultValue.length() - CompressorDecompressorBase.EXTENSTION.length());
} }
} }
break;
case Benchmark: {
defaultValue = new File(inputFile.getParent(), "benchmark").getAbsolutePath();
}
break;
case PrintHelp: case PrintHelp:
break; break;
case InspectFile: case InspectFile:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment