Newer
Older
import azgracompress.cli.ParsedCliOptions;
import azgracompress.cache.QuantizationCacheManager;
import azgracompress.quantization.scalar.LloydMaxU16ScalarQuantization;
import azgracompress.quantization.scalar.SQCodebook;
import azgracompress.quantization.scalar.ScalarQuantizer;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class ScalarQuantizationBenchmark extends BenchmarkBase {
public ScalarQuantizationBenchmark(final String inputFile,
final String outputDirectory,
final int[] planes,
super(inputFile, outputDirectory, planes, rawImageDims);
public ScalarQuantizationBenchmark(final ParsedCliOptions options) {
super(options);
}
public void startBenchmark() {
if (planes.length < 1) {
return;
}
boolean dirCreated = new File(this.outputDirectory).mkdirs();
System.out.println(String.format("|CODEBOOK| = %d", codebookSize));
ScalarQuantizer quantizer = null;
if (hasCacheFolder) {
System.out.println("Loading codebook from cache");
QuantizationCacheManager cacheManager = new QuantizationCacheManager(cacheFolder);
final SQCodebook codebook = cacheManager.loadSQCodebook(inputFile, codebookSize);
if (codebook == null) {
System.err.println("Failed to read quantization values from cache file.");
return;
}
quantizer = new ScalarQuantizer(codebook);
System.out.println("Created quantizer from cache");
} else if (useMiddlePlane) {
final int middlePlaneIndex = rawImageDims.getZ() / 2;
final int[] middlePlaneData = loadPlaneData(middlePlaneIndex);
if (middlePlaneData.length == 0) {
System.err.println("Failed to load middle plane data.");
return;
}
quantizer = trainLloydMaxQuantizer(middlePlaneData, codebookSize);
System.out.println("Created quantizer from middle plane.");
for (final int planeIndex : planes) {
System.out.println(String.format("Loading plane %d ...", planeIndex));
// NOTE(Moravec): Actual planeIndex is zero based.
final int[] planeData = loadPlaneData(planeIndex);
if (planeData.length == 0) {
System.err.println(String.format("Failed to load plane %d data. Skipping plane.", planeIndex));
return;
}
if (!hasGeneralQuantizer) {
quantizer = trainLloydMaxQuantizer(planeData, codebookSize);
System.out.println("Created plane quantizer");
if (quantizer == null) {
System.err.println("Failed to initialize scalar quantizer.");
return;
}
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);
if (!saveQuantizedPlaneData(quantizedData, quantizedFile)) {
System.err.println("Failed to save quantized plane.");
return;
saveDifference(planeData, quantizedData, diffFile, absoluteDiffFile);
private void saveCentroids(final int[] centroids, final String centroidsFile) {
final String outFile = getFileNamePathIntoOutDir(centroidsFile);
try {
FileOutputStream fileStream = new FileOutputStream(outFile);
OutputStreamWriter writer = new OutputStreamWriter(fileStream);
StringBuilder sb = new StringBuilder();
sb.append(entry);
sb.append('\n');
}
writer.write(sb.toString());
writer.flush();
fileStream.flush();
fileStream.close();
} catch (IOException ioE) {
ioE.printStackTrace();
System.err.println("Failed to save codebook vectors.");
}
}
private ScalarQuantizer trainLloydMaxQuantizer(final int[] data, final int codebookSize) {
LloydMaxU16ScalarQuantization lloydMax = new LloydMaxU16ScalarQuantization(data, codebookSize, workerCount);
QTrainIteration[] trainingReport = lloydMax.train(false);
// saveQTrainLog(String.format("p%d_cb_%d_lloyd.csv", planeIndex, codebookSize), trainingReport);
return new ScalarQuantizer(U16.Min, U16.Max, lloydMax.getCodebook());