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

Added PSNR to statistics next to MSE.

parent 8f6190fe
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module version="4"> <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" version="4" />
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_9"> \ No newline at end of file
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-math3:3.6.1" level="project" />
</component>
</module>
\ No newline at end of file
...@@ -25,7 +25,7 @@ public class DataCompressor { ...@@ -25,7 +25,7 @@ public class DataCompressor {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
final String sourceFile = "D:\\tmp\\server-dump\\initial_load.bin"; final String sourceFile = "D:\\tmp\\server-dump\\initial_load.bin";
final int NumberOfBits = 5; final int NumberOfBits = 8;
final int Dimension = (int) Math.pow(2, NumberOfBits); final int Dimension = (int) Math.pow(2, NumberOfBits);
int[] values = Utils.convertU16BytesToInt(Utils.readFileBytes(sourceFile)); int[] values = Utils.convertU16BytesToInt(Utils.readFileBytes(sourceFile));
...@@ -35,7 +35,7 @@ public class DataCompressor { ...@@ -35,7 +35,7 @@ public class DataCompressor {
} }
private static void benchmarkLloydMax(final int[] values) { private static void benchmarkLloydMax(final int[] values) {
for (int bitCount = 2; bitCount < 3; bitCount++) { for (int bitCount = 2; bitCount < 9; bitCount++) {
LloydMaxIteration[] solutionHistory = lloydMax(bitCount, values); LloydMaxIteration[] solutionHistory = lloydMax(bitCount, values);
String fileName = String.format("lloyd_max_%dbits.csv", bitCount); String fileName = String.format("lloyd_max_%dbits.csv", bitCount);
saveLloydMaxSolutionHistory(solutionHistory, fileName); saveLloydMaxSolutionHistory(solutionHistory, fileName);
...@@ -44,16 +44,16 @@ public class DataCompressor { ...@@ -44,16 +44,16 @@ public class DataCompressor {
private static LloydMaxIteration[] lloydMax(final int noOfBits, final int[] values) { private static LloydMaxIteration[] lloydMax(final int noOfBits, final int[] values) {
LloydMaxU16ScalarQuantization quantization = new LloydMaxU16ScalarQuantization(values, noOfBits); LloydMaxU16ScalarQuantization quantization = new LloydMaxU16ScalarQuantization(values, noOfBits);
return quantization.train(true); return quantization.train(false);
} }
private static void saveLloydMaxSolutionHistory(final LloydMaxIteration[] solutionHistory, String filename) { private static void saveLloydMaxSolutionHistory(final LloydMaxIteration[] solutionHistory, String filename) {
try { try {
FileOutputStream os = new FileOutputStream(filename); FileOutputStream os = new FileOutputStream(filename);
OutputStreamWriter writer = new OutputStreamWriter(os); OutputStreamWriter writer = new OutputStreamWriter(os);
writer.write("Iteration;Mse\n"); writer.write("Iteration;Mse;Psnr\n");
for (final LloydMaxIteration lmi : solutionHistory) { for (final LloydMaxIteration lmi : solutionHistory) {
writer.write(String.format("%d;%.5f\n", lmi.getIteration(), lmi.getMse())); writer.write(String.format("%d;%.5f;%.5f\n", lmi.getIteration(), lmi.getMse(), lmi.getPsnr()));
} }
writer.flush(); writer.flush();
writer.close(); writer.close();
...@@ -65,7 +65,7 @@ public class DataCompressor { ...@@ -65,7 +65,7 @@ public class DataCompressor {
} }
private static void jade(final int dimension, final int[] values) throws IOException { private static void jade(final int dimension, final int[] values) throws IOException {
JadeSolver jadeSolver = new JadeSolver(dimension, 5 * dimension, 250, 0.05, 0.1); JadeSolver jadeSolver = new JadeSolver(dimension, 5 * dimension, 1000, 0.05, 0.1);
jadeSolver.setTrainingData(values); jadeSolver.setTrainingData(values);
DeHistory[] solutionHistory = null; DeHistory[] solutionHistory = null;
......
...@@ -5,11 +5,13 @@ public class DeHistory { ...@@ -5,11 +5,13 @@ public class DeHistory {
private int m_iteration = 0; private int m_iteration = 0;
private double m_avgCost = 0; private double m_avgCost = 0;
private double m_bestCost = 0; private double m_bestCost = 0;
private double m_psnr = 0;
public DeHistory(final int it, final double avgCost, final double bestCost) { public DeHistory(final int it, final double avgCost, final double bestCost, final double psnr) {
m_iteration = it; m_iteration = it;
m_avgCost = avgCost; m_avgCost = avgCost;
m_bestCost = bestCost; m_bestCost = bestCost;
m_psnr = psnr;
} }
public double getBestCost() { public double getBestCost() {
...@@ -24,6 +26,10 @@ public class DeHistory { ...@@ -24,6 +26,10 @@ public class DeHistory {
return m_avgCost; return m_avgCost;
} }
public double getPsnr() {
return m_psnr;
}
public void setAvgCost(double avgCost) { public void setAvgCost(double avgCost) {
this.m_avgCost = avgCost; this.m_avgCost = avgCost;
} }
......
...@@ -48,7 +48,7 @@ public class JadeSolver implements IDESolver { ...@@ -48,7 +48,7 @@ public class JadeSolver implements IDESolver {
public JadeSolver() { public JadeSolver() {
//rg = new MersenneTwister(); //rg = new MersenneTwister();
m_workerCount = Runtime.getRuntime().availableProcessors() - 4; m_workerCount = Runtime.getRuntime().availableProcessors() - 2;
assert (m_workerCount > 0); assert (m_workerCount > 0);
} }
...@@ -438,7 +438,8 @@ public class JadeSolver implements IDESolver { ...@@ -438,7 +438,8 @@ public class JadeSolver implements IDESolver {
// System.out.println(String.format("Generation %d average fitness(COST): %.5f", (generation + 1), avgFitness)); // System.out.println(String.format("Generation %d average fitness(COST): %.5f", (generation + 1), avgFitness));
System.out.println(generationLog.toString()); System.out.println(generationLog.toString());
solutionHistory[generation] = new DeHistory(generation, avgFitness, m_currentPopulationSorted[0].getFitness()); final double best = m_currentPopulationSorted[0].getFitness();
solutionHistory[generation] = new DeHistory(generation, avgFitness, best, Utils.calculatePsnr(best, U16.Max));
} }
return solutionHistory; return solutionHistory;
} }
......
...@@ -5,11 +5,13 @@ import java.util.Arrays; ...@@ -5,11 +5,13 @@ import java.util.Arrays;
public class LloydMaxIteration { public class LloydMaxIteration {
private int m_interation; private int m_interation;
private double m_mse; private double m_mse;
private double m_psnr;
private int[] m_centroids; private int[] m_centroids;
public LloydMaxIteration(final int iter, final double mse, final int[] centroids) { public LloydMaxIteration(final int iter, final double mse, final double psnr, final int[] centroids) {
m_interation = iter; m_interation = iter;
m_mse = mse; m_mse = mse;
m_psnr = psnr;
m_centroids = Arrays.copyOf(centroids, centroids.length); m_centroids = Arrays.copyOf(centroids, centroids.length);
} }
...@@ -21,6 +23,10 @@ public class LloydMaxIteration { ...@@ -21,6 +23,10 @@ public class LloydMaxIteration {
return m_mse; return m_mse;
} }
public double getPsnr() {
return m_psnr;
}
public int[] getCentroids() { public int[] getCentroids() {
return m_centroids; return m_centroids;
} }
......
package quantization.lloyd_max; package quantization.lloyd_max;
import quantization.Quantizer;
import quantization.U16; import quantization.U16;
import quantization.utilities.Stopwatch;
import quantization.utilities.Utils; import quantization.utilities.Utils;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
...@@ -148,9 +150,9 @@ public class LloydMaxU16ScalarQuantization { ...@@ -148,9 +150,9 @@ public class LloydMaxU16ScalarQuantization {
printCurrentConfigration(); printCurrentConfigration();
} }
currentMse = getCurrentMse(); currentMse = getCurrentMse();
System.out.println(String.format("Current MSE: %f", currentMse)); System.out.println(String.format("Initial MSE: %f", currentMse));
int iter = 0; int iter = 0;
solutionHistory.add(new LloydMaxIteration(iter++, currentMse, centroids)); solutionHistory.add(new LloydMaxIteration(iter++, currentMse, Utils.calculatePsnr(currentMse, U16.Max), centroids));
double dist = 1; double dist = 1;
do { do {
...@@ -163,15 +165,15 @@ public class LloydMaxU16ScalarQuantization { ...@@ -163,15 +165,15 @@ public class LloydMaxU16ScalarQuantization {
prevMse = currentMse; prevMse = currentMse;
currentMse = getCurrentMse(); currentMse = getCurrentMse();
solutionHistory.add(new LloydMaxIteration(iter++, currentMse, centroids)); solutionHistory.add(new LloydMaxIteration(iter++, currentMse, Utils.calculatePsnr(currentMse, U16.Max), centroids));
dist = (prevMse - currentMse) / currentMse; dist = (prevMse - currentMse) / currentMse;
System.out.println(String.format("Current MSE: %f", currentMse));
System.out.print(String.format("\rCurrent MSE: %f", currentMse));
} while (dist > 0.001); } while (dist > 0.001);
System.out.println("\nFinished training.");
LloydMaxIteration[] result = solutionHistory.toArray(new LloydMaxIteration[0]); return solutionHistory.toArray(new LloydMaxIteration[0]);
return result;
//recalculateCentroids2();
} }
private void printCurrentConfigration() { private void printCurrentConfigration() {
......
...@@ -5,6 +5,13 @@ import java.io.FileNotFoundException; ...@@ -5,6 +5,13 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
public class Utils { public class Utils {
public static double calculatePsnr(final double mse, final int signalMax)
{
double psnr = 10.0 * Math.log10((Math.pow(signalMax, 2) / mse));
return psnr;
}
public static byte[] readFileBytes(final String path) throws FileNotFoundException { public static byte[] readFileBytes(final String path) throws FileNotFoundException {
FileInputStream fileStream = new FileInputStream(path); FileInputStream fileStream = new FileInputStream(path);
try { try {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment