human/src/gear/ssrnet-gender.ts

77 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-05-25 14:58:20 +02:00
/**
* Gender model implementation
*
* Based on: [**SSR-Net**](https://github.com/shamangary/SSR-Net)
2021-05-25 14:58:20 +02:00
*/
2022-10-17 02:28:57 +02:00
import * as tf from 'dist/tfjs.esm.js';
2022-01-17 17:03:21 +01:00
import { log, now } from '../util/util';
2022-01-16 15:49:55 +01:00
import { loadModel } from '../tfjs/load';
2021-11-17 02:16:49 +01:00
import { constants } from '../tfjs/constants';
2021-12-15 15:26:32 +01:00
import type { Gender } from '../result';
2021-09-13 19:28:35 +02:00
import type { Config } from '../config';
2022-10-17 02:28:57 +02:00
import type { GraphModel, Tensor, Tensor4D } from '../tfjs/types';
2021-09-27 19:58:13 +02:00
import { env } from '../util/env';
2020-11-06 17:39:39 +01:00
2021-09-17 17:23:00 +02:00
let model: GraphModel | null;
2022-08-21 19:34:51 +02:00
const last: { gender: Gender, genderScore: number }[] = [];
2021-11-13 18:23:32 +01:00
let lastCount = 0;
2021-10-22 22:09:52 +02:00
let lastTime = 0;
2020-12-11 16:11:49 +01:00
let skipped = Number.MAX_SAFE_INTEGER;
2020-11-06 17:39:39 +01:00
// tuning values
2020-11-06 21:35:58 +01:00
const rgb = [0.2989, 0.5870, 0.1140]; // factors for red/green/blue colors when converting to grayscale
2020-11-06 17:39:39 +01:00
2022-08-21 19:34:51 +02:00
export async function load(config: Config) {
2021-09-17 17:23:00 +02:00
if (env.initial) model = null;
2022-08-21 19:34:51 +02:00
if (!model) model = await loadModel(config.face['ssrnet']?.modelPathGender);
2022-01-17 17:03:21 +01:00
else if (config.debug) log('cached model:', model['modelUrl']);
2021-02-08 18:47:38 +01:00
return model;
2020-11-06 17:39:39 +01:00
}
2022-10-17 02:28:57 +02:00
export async function predict(image: Tensor4D, config: Config, idx, count): Promise<{ gender: Gender, genderScore: number }> {
2021-11-13 18:23:32 +01:00
if (!model) return { gender: 'unknown', genderScore: 0 };
const skipFrame = skipped < (config.face['ssrnet']?.skipFrames || 0);
const skipTime = (config.face['ssrnet']?.skipTime || 0) > (now() - lastTime);
if (config.skipAllowed && skipFrame && skipTime && (lastCount === count) && last[idx]?.gender && (last[idx]?.genderScore > 0)) {
2020-12-11 16:11:49 +01:00
skipped++;
2021-11-13 18:23:32 +01:00
return last[idx];
2020-11-06 19:50:16 +01:00
}
skipped = 0;
2020-11-06 17:39:39 +01:00
return new Promise(async (resolve) => {
2021-09-17 17:23:00 +02:00
if (!model?.inputs[0].shape) return;
2021-11-13 18:23:32 +01:00
const t: Record<string, Tensor> = {};
2023-03-07 00:15:42 +01:00
if (config.face['ssrnet']?.['crop'] > 0) { // optional crop
const crop = config.face['ssrnet']?.['crop'];
const box = [[crop, crop, 1 - crop, 1 - crop]];
t.resize = tf.image.cropAndResize(image, box, [0], [model.inputs[0].shape[2], model.inputs[0].shape[1]]);
} else {
t.resize = tf.image.resizeBilinear(image, [model.inputs[0].shape[2], model.inputs[0].shape[1]], false);
}
2021-11-13 18:23:32 +01:00
t.enhance = tf.tidy(() => {
2023-02-22 12:45:34 +01:00
let normalize: Tensor;
if (model?.inputs?.[0].shape?.[3] === 1) {
const [red, green, blue] = tf.split(t.resize, 3, 3);
const redNorm = tf.mul(red, rgb[0]);
const greenNorm = tf.mul(green, rgb[1]);
const blueNorm = tf.mul(blue, rgb[2]);
const grayscale = tf.addN([redNorm, greenNorm, blueNorm]);
normalize = tf.mul(tf.sub(grayscale, constants.tf05), 2); // range grayscale:-1..1
} else {
normalize = tf.mul(tf.sub(t.resize, constants.tf05), 2); // range rgb:-1..1
}
2021-11-13 18:23:32 +01:00
return normalize;
});
2021-12-15 15:26:32 +01:00
const obj: { gender: Gender, genderScore: number } = { gender: 'unknown', genderScore: 0 };
2022-08-21 19:34:51 +02:00
if (config.face['ssrnet']?.enabled) t.gender = model.execute(t.enhance) as Tensor;
2021-11-13 18:23:32 +01:00
const data = await t.gender.data();
obj.gender = data[0] > data[1] ? 'female' : 'male'; // returns two values 0..1, bigger one is prediction
obj.genderScore = data[0] > data[1] ? (Math.trunc(100 * data[0]) / 100) : (Math.trunc(100 * data[1]) / 100);
Object.keys(t).forEach((tensor) => tf.dispose(t[tensor]));
last[idx] = obj;
lastCount = count;
2021-10-22 22:09:52 +02:00
lastTime = now();
2020-11-06 17:39:39 +01:00
resolve(obj);
});
}