2021-02-08 17:39:09 +01:00
|
|
|
import { log } from '../log';
|
2020-11-18 14:26:28 +01:00
|
|
|
import * as tf from '../../dist/tfjs.esm.js';
|
2021-02-13 15:16:41 +01:00
|
|
|
import * as profile from '../profile';
|
2020-11-13 22:13:35 +01:00
|
|
|
|
2021-03-11 16:26:14 +01:00
|
|
|
// original: https://github.com/sirius-ai/MobileFaceNet_TF
|
|
|
|
// modified: https://github.com/sirius-ai/MobileFaceNet_TF/issues/46
|
|
|
|
// download: https://github.com/sirius-ai/MobileFaceNet_TF/files/3551493/FaceMobileNet192_train_false.zip
|
2020-11-13 22:13:35 +01:00
|
|
|
|
2021-03-11 19:31:36 +01:00
|
|
|
/* WiP
|
|
|
|
|
|
|
|
- Should input box be tightly cropped?
|
|
|
|
- What is the best input range? (adjust distance scale accordingly)
|
|
|
|
- How to best normalize output
|
|
|
|
*/
|
|
|
|
|
2021-02-08 18:47:38 +01:00
|
|
|
let model;
|
2020-11-13 22:13:35 +01:00
|
|
|
|
2021-02-08 17:39:09 +01:00
|
|
|
export async function load(config) {
|
2021-02-08 18:47:38 +01:00
|
|
|
if (!model) {
|
|
|
|
model = await tf.loadGraphModel(config.face.embedding.modelPath);
|
2021-03-02 17:27:42 +01:00
|
|
|
if (config.debug) log(`load model: ${config.face.embedding.modelPath.match(/\/(.*)\./)[1]}`);
|
2020-11-13 22:13:35 +01:00
|
|
|
}
|
2021-02-08 18:47:38 +01:00
|
|
|
return model;
|
2020-11-13 22:13:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 19:31:36 +01:00
|
|
|
export function simmilarity(embedding1, embedding2, order = 2) {
|
2021-02-21 20:46:50 +01:00
|
|
|
if (!embedding1 || !embedding2) return 0;
|
2021-02-21 19:34:26 +01:00
|
|
|
if (embedding1?.length === 0 || embedding2?.length === 0) return 0;
|
2020-11-13 22:13:35 +01:00
|
|
|
if (embedding1?.length !== embedding2?.length) return 0;
|
2020-11-23 14:40:17 +01:00
|
|
|
// general minkowski distance
|
|
|
|
// euclidean distance is limited case where order is 2
|
2021-03-11 19:31:36 +01:00
|
|
|
const distance = 50.0 * ((embedding1.map((val, i) => (val - embedding2[i])).reduce((dist, diff) => dist + (diff ** order), 0) ** (1 / order)));
|
|
|
|
const res = (Math.trunc(1000 * (1 - (isNaN(distance) ? 1 : distance))) / 1000);
|
|
|
|
console.log(distance, res);
|
|
|
|
return res;
|
2020-11-13 22:13:35 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 17:39:09 +01:00
|
|
|
export async function predict(image, config) {
|
2021-02-08 18:47:38 +01:00
|
|
|
if (!model) return null;
|
2020-11-13 22:13:35 +01:00
|
|
|
return new Promise(async (resolve) => {
|
2021-03-11 17:44:22 +01:00
|
|
|
const resize = tf.image.resizeBilinear(image, [model.inputs[0].shape[2], model.inputs[0].shape[1]], false); // input is already normalized to 0..1
|
2021-03-11 19:31:36 +01:00
|
|
|
// optionally do a tight box crop
|
|
|
|
/*
|
|
|
|
const box = [[0, 0.2, 0.9, 0.8]]; // top, left, bottom, right
|
|
|
|
const resize = tf.image.cropAndResize(image, box, [0], [model.inputs[0].shape[2], model.inputs[0].shape[1]]);
|
|
|
|
*/
|
|
|
|
// debug visualize box
|
|
|
|
// const canvas = document.getElementById('compare-canvas');
|
|
|
|
// await tf.browser.toPixels(resize.squeeze(), canvas);
|
|
|
|
const norm = resize.sub(0.5);
|
|
|
|
// optionally normalizes with mean value being at point 0, better than fixed range -0.5..0.5
|
|
|
|
/*
|
|
|
|
const mean = resize.mean();
|
|
|
|
const norm = resize.sub(mean);
|
|
|
|
*/
|
|
|
|
resize.dispose();
|
2021-02-08 18:47:38 +01:00
|
|
|
let data: Array<[]> = [];
|
2020-11-13 22:13:35 +01:00
|
|
|
if (config.face.embedding.enabled) {
|
|
|
|
if (!config.profile) {
|
2021-03-11 19:31:36 +01:00
|
|
|
const res = await model.predict({ img_inputs: norm });
|
|
|
|
/*
|
|
|
|
const scaled = tf.tidy(() => {
|
|
|
|
// run l2 normalization on output
|
|
|
|
const sqr = res.square();
|
|
|
|
const sum = sqr.sum();
|
|
|
|
const sqrt = sum.sqrt();
|
|
|
|
const l2 = res.div(sqrt);
|
|
|
|
// scale outputs
|
|
|
|
const range = l2.max().sub(l2.min());
|
|
|
|
const scale = l2.mul(2).div(range);
|
|
|
|
return scale;
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
data = [...res.dataSync()]; // convert object array to standard array
|
|
|
|
tf.dispose(res);
|
2020-11-13 22:13:35 +01:00
|
|
|
} else {
|
2021-03-11 19:31:36 +01:00
|
|
|
const profileData = await tf.profile(() => model.predict({ img_inputs: norm }));
|
2020-11-13 22:13:35 +01:00
|
|
|
data = [...profileData.result.dataSync()];
|
|
|
|
profileData.result.dispose();
|
|
|
|
profile.run('emotion', profileData);
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 19:31:36 +01:00
|
|
|
norm.dispose();
|
2020-11-13 22:13:35 +01:00
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
}
|