human/src/embedding/embedding.ts

66 lines
2.6 KiB
TypeScript
Raw Normal View History

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-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-12 00:26:04 +01:00
const distance = embedding1
.map((val, i) => (Math.abs(embedding1[i] - embedding2[i]) ** order)) // distance squared
.reduce((sum, now) => (sum + now), 0) // sum all distances
** (1 / order); // get root of
const res = Math.trunc(1000 * (1 - (20 * distance))) / 1000;
2021-03-11 19:31:36 +01:00
return res;
2020-11-13 22:13:35 +01:00
}
2021-03-12 00:26:04 +01:00
export async function predict(input, 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-12 00:26:04 +01:00
const image = tf.tidy(() => {
const data = tf.image.resizeBilinear(input, [model.inputs[0].shape[2], model.inputs[0].shape[1]], false); // input is already normalized to 0..1
// const box = [[0.05, 0.10, 0.85, 0.90]]; // top, left, bottom, right
// const crop = tf.image.cropAndResize(data, box, [0], [model.inputs[0].shape[2], model.inputs[0].shape[1]]); // optionally do a tight box crop
const norm = data.sub(data.mean()); // trick to normalize around image mean value
return norm;
});
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-12 00:26:04 +01:00
const res = await model.predict({ img_inputs: image });
2021-03-11 19:31:36 +01:00
const scaled = tf.tidy(() => {
2021-03-12 00:26:04 +01:00
const l2 = res.norm('euclidean');
const scale = res.div(l2);
2021-03-11 19:31:36 +01:00
return scale;
});
2021-03-12 00:26:04 +01:00
data = [...scaled.dataSync()]; // convert object array to standard array
tf.dispose(scaled);
2021-03-11 19:31:36 +01:00
tf.dispose(res);
2020-11-13 22:13:35 +01:00
} else {
2021-03-12 00:26:04 +01:00
const profileData = await tf.profile(() => model.predict({ img_inputs: image }));
2020-11-13 22:13:35 +01:00
data = [...profileData.result.dataSync()];
profileData.result.dispose();
profile.run('emotion', profileData);
}
}
2021-03-12 00:26:04 +01:00
image.dispose();
2020-11-13 22:13:35 +01:00
resolve(data);
});
}