human/src/posenet/posenet.ts

45 lines
2.0 KiB
TypeScript
Raw Normal View History

2021-05-25 14:58:20 +02:00
/**
* PoseNet module entry point
*/
2021-04-09 14:07:58 +02:00
import { log, join } from '../helpers';
2020-11-18 14:26:28 +01:00
import * as tf from '../../dist/tfjs.esm.js';
2021-04-24 22:04:49 +02:00
import * as poses from './poses';
2021-04-24 17:49:26 +02:00
import * as util from './utils';
2021-05-22 18:33:19 +02:00
import { Body } from '../result';
import { Tensor, GraphModel } from '../tfjs/types';
2021-06-03 15:41:53 +02:00
import { Config } from '../config';
2020-10-12 01:22:43 +02:00
let model: GraphModel;
2021-04-24 22:04:49 +02:00
const poseNetOutputs = ['MobilenetV1/offset_2/BiasAdd'/* offsets */, 'MobilenetV1/heatmap_2/BiasAdd'/* heatmapScores */, 'MobilenetV1/displacement_fwd_2/BiasAdd'/* displacementFwd */, 'MobilenetV1/displacement_bwd_2/BiasAdd'/* displacementBwd */];
2021-06-03 15:41:53 +02:00
export async function predict(input: Tensor, config: Config): Promise<Body[]> {
2021-04-24 22:04:49 +02:00
const res = tf.tidy(() => {
if (!model.inputs[0].shape) return [];
2021-06-05 18:59:11 +02:00
const resized = tf.image.resizeBilinear(input, [model.inputs[0].shape[2], model.inputs[0].shape[1]]);
2021-04-24 22:04:49 +02:00
const normalized = resized.toFloat().div(127.5).sub(1.0);
const results: Array<Tensor> = model.execute(normalized, poseNetOutputs) as Array<Tensor>;
2021-06-05 18:59:11 +02:00
const results3d = results.map((y) => tf.squeeze(y, [0]));
2021-04-24 22:04:49 +02:00
results3d[1] = results3d[1].sigmoid(); // apply sigmoid on scores
return results3d;
2020-12-17 00:36:24 +01:00
});
2021-04-24 22:04:49 +02:00
const buffers = await Promise.all(res.map((tensor) => tensor.buffer()));
for (const t of res) t.dispose();
2020-12-17 00:36:24 +01:00
2021-04-25 19:16:04 +02:00
const decoded = await poses.decode(buffers[0], buffers[1], buffers[2], buffers[3], config.body.maxDetected, config.body.minConfidence);
if (!model.inputs[0].shape) return [];
2021-05-22 18:33:19 +02:00
const scaled = util.scalePoses(decoded, [input.shape[1], input.shape[2]], [model.inputs[0].shape[2], model.inputs[0].shape[1]]) as Body[];
2021-04-24 22:04:49 +02:00
return scaled;
2020-10-12 01:22:43 +02:00
}
2020-11-08 18:26:45 +01:00
2021-06-03 15:41:53 +02:00
export async function load(config: Config): Promise<GraphModel> {
if (!model) {
// @ts-ignore type mismatch for GraphModel
model = await tf.loadGraphModel(join(config.modelBasePath, config.body.modelPath));
if (!model || !model['modelUrl']) log('load model failed:', config.body.modelPath);
else if (config.debug) log('load model:', model['modelUrl']);
} else if (config.debug) log('cached model:', model['modelUrl']);
2021-04-24 22:04:49 +02:00
return model;
2020-10-12 01:22:43 +02:00
}