Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
feat(ssim): make Weber algorithm the default (#273)
Browse files Browse the repository at this point in the history
Switch from the default algorithm to Weber's which is faster and
produces almost identical results

BREAKING CHANGE
  • Loading branch information
obartra authored Sep 2, 2020
1 parent ca72429 commit 19e71c6
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 336 deletions.
128 changes: 64 additions & 64 deletions spec/e2e/ivc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,115 @@
import { resolve } from "path";
import scores from "../samples/IVC_color.json";
import weberScores from "../samples/IVC_color-weber.json";
import bezkrovnyScores from "../samples/IVC_color-bezkrovny.json";
import { ssim } from "../../dist";
import { roundTo } from "../helpers/round";
import { getJSONScores } from "../helpers/getJSONScores";
import { resolve } from 'path'
import scores from '../samples/IVC_color.json'
import weberScores from '../samples/IVC_color-weber.json'
import bezkrovnyScores from '../samples/IVC_color-bezkrovny.json'
import { ssim } from '../../dist'
import { roundTo } from '../helpers/round'
import { getJSONScores } from '../helpers/getJSONScores'

const path = resolve(__dirname, "../samples/IVC_SubQualityDB/color");
const path = resolve(__dirname, '../samples/IVC_SubQualityDB/color')

type MSSIMValues = { [key: string]: number };
type MSSIMValues = { [key: string]: number }

describe("IVC", () => {
it("should match stored mssims (ssim fast)", async () => {
const expected = await getJSONScores(scores, path, "bmp");
describe('IVC', () => {
it('should match stored mssims (ssim fast)', async () => {
const expected = await getJSONScores(scores, path, 'bmp')
const results = Object.entries(expected)
.map(([key, { file, reference }]): [string, number] => {
const { mssim } = ssim(reference, file, { ssim: "fast" });
const { mssim } = ssim(reference, file, { ssim: 'fast' })

return [key, roundTo(mssim, 3)];
return [key, roundTo(mssim, 3)]
})
.reduce(
(acc, [key, result]) => ({
...acc,
[key]: result,
}),
{} as MSSIMValues
);
)

expect(scores as MSSIMValues).toEqual(results);
}, 500000);
expect(scores as MSSIMValues).toEqual(results)
}, 500000)

it("should match stored mssims (weber)", async () => {
const expected = await getJSONScores(weberScores, path, "bmp");
const start = new Date().getTime();
it('should match stored mssims (weber)', async () => {
const expected = await getJSONScores(weberScores, path, 'bmp')
const start = new Date().getTime()
const results = Object.entries(expected)
.map(([key, { file, reference }]): [string, number] => {
const { mssim } = ssim(reference, file, { ssim: "weber" });
return [key, roundTo(mssim, 3)];
const { mssim } = ssim(reference, file, { ssim: 'weber' })
return [key, roundTo(mssim, 3)]
})
.reduce(
(acc, [key, result]) => ({
...acc,
[key]: result,
}),
{} as MSSIMValues
);
)

const end = new Date().getTime();
const referenceScores = scores as MSSIMValues;
const newV: any = {};
let newMean = 0;
let newS = 0;
const end = new Date().getTime()
const referenceScores = scores as MSSIMValues
const newV: any = {}
let newMean = 0
let newS = 0
for (let score in referenceScores) {
const refVal = referenceScores[score];
const weberNewVal = results[score];
const distWeberNew = Math.abs(refVal - weberNewVal);
const refVal = referenceScores[score]
const weberNewVal = results[score]
const distWeberNew = Math.abs(refVal - weberNewVal)
newV[score] = {
mssim: weberNewVal,
distance: distWeberNew,
ref: refVal,
};
}
const newMeanR =
newMean + (distWeberNew - newMean) / Object.keys(newV).length;
newS = newS + (distWeberNew - newMean) * (distWeberNew - newMeanR);
newMean = newMeanR;
newMean + (distWeberNew - newMean) / Object.keys(newV).length
newS = newS + (distWeberNew - newMean) * (distWeberNew - newMeanR)
newMean = newMeanR
}

const newVar = newS / (Object.keys(newV).length - 1);
const newVar = newS / (Object.keys(newV).length - 1)

expect(roundTo(newMean, 4)).toMatchInlineSnapshot(`0.0202`);
expect(roundTo(newVar, 6)).toMatchInlineSnapshot(`0.000211`);
expect(results).toEqual(weberScores as MSSIMValues);
}, 70000);
expect(roundTo(newMean, 4)).toMatchInlineSnapshot(`0.0202`)
expect(roundTo(newVar, 6)).toMatchInlineSnapshot(`0.000211`)
expect(results).toEqual(weberScores as MSSIMValues)
}, 70000)

it("should match stored mssims (bezkrovny)", async () => {
const expected = await getJSONScores(weberScores, path, "bmp");
const start = new Date().getTime();
it('should match stored mssims (bezkrovny)', async () => {
const expected = await getJSONScores(weberScores, path, 'bmp')
const start = new Date().getTime()
const results = Object.entries(expected)
.map(([key, { file, reference }]): [string, number] => {
const { mssim } = ssim(reference, file, { ssim: "bezkrovny" });
return [key, roundTo(mssim, 3)];
const { mssim } = ssim(reference, file, { ssim: 'bezkrovny' })
return [key, roundTo(mssim, 3)]
})
.reduce(
(acc, [key, result]) => ({
...acc,
[key]: result,
}),
{} as MSSIMValues
);
const end = new Date().getTime();
)
const end = new Date().getTime()

const referenceScores = scores as MSSIMValues;
const newV: any = {};
let newMean = 0;
let newS = 0;
const referenceScores = scores as MSSIMValues
const newV: any = {}
let newMean = 0
let newS = 0
for (let score in referenceScores) {
const refVal = referenceScores[score];
const newVal = results[score];
const distNew = Math.abs(refVal - newVal);
const refVal = referenceScores[score]
const newVal = results[score]
const distNew = Math.abs(refVal - newVal)
newV[score] = {
mssim: newVal,
distance: distNew,
ref: refVal,
};
const meanR = newMean + (distNew - newMean) / Object.keys(newV).length;
newS = newS + (distNew - newMean) * (distNew - meanR);
newMean = meanR;
}
const meanR = newMean + (distNew - newMean) / Object.keys(newV).length
newS = newS + (distNew - newMean) * (distNew - meanR)
newMean = meanR
}
const newVar = newS / (Object.keys(newV).length - 1);
expect(roundTo(newMean, 4)).toMatchInlineSnapshot(`0.0155`);
expect(roundTo(newVar, 6)).toMatchInlineSnapshot(`0.000153`);
expect(results).toEqual(bezkrovnyScores as MSSIMValues);
}, 70000);
});
const newVar = newS / (Object.keys(newV).length - 1)
expect(roundTo(newMean, 4)).toMatchInlineSnapshot(`0.0155`)
expect(roundTo(newVar, 6)).toMatchInlineSnapshot(`0.000153`)
expect(results).toEqual(bezkrovnyScores as MSSIMValues)
}, 70000)
})
Loading

0 comments on commit 19e71c6

Please sign in to comment.