face-api/src/faceRecognitionNet/FaceRecognitionNet.ts

85 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-12-23 18:58:47 +01:00
import * as tf from '../../dist/tfjs.esm';
2020-08-18 13:54:53 +02:00
2020-12-19 17:46:41 +01:00
import { NetInput, TNetInput, toNetInput } from '../dom/index';
2020-08-18 13:54:53 +02:00
import { NeuralNetwork } from '../NeuralNetwork';
2020-12-19 17:46:41 +01:00
import { normalize } from '../ops/index';
2020-08-18 13:54:53 +02:00
import { convDown } from './convLayer';
import { extractParams } from './extractParams';
2021-01-12 16:14:33 +01:00
import { extractParamsFromWeightMap } from './extractParamsFromWeightMap';
2020-08-18 13:54:53 +02:00
import { residual, residualDown } from './residualLayer';
import { NetParams } from './types';
export class FaceRecognitionNet extends NeuralNetwork<NetParams> {
constructor() {
2020-12-23 17:26:55 +01:00
super('FaceRecognitionNet');
2020-08-18 13:54:53 +02:00
}
public forwardInput(input: NetInput): tf.Tensor2D {
2020-12-23 17:26:55 +01:00
const { params } = this;
2020-08-18 13:54:53 +02:00
if (!params) {
2020-12-23 17:26:55 +01:00
throw new Error('FaceRecognitionNet - load model before inference');
2020-08-18 13:54:53 +02:00
}
return tf.tidy(() => {
const batchTensor = tf.cast(input.toBatchTensor(150, true), 'float32');
2020-08-18 13:54:53 +02:00
2020-12-23 17:26:55 +01:00
const meanRgb = [122.782, 117.001, 104.298];
const normalized = normalize(batchTensor, meanRgb).div(tf.scalar(256)) as tf.Tensor4D;
2020-08-18 13:54:53 +02:00
2020-12-23 17:26:55 +01:00
let out = convDown(normalized, params.conv32_down);
out = tf.maxPool(out, 3, 2, 'valid');
2020-08-18 13:54:53 +02:00
2020-12-23 17:26:55 +01:00
out = residual(out, params.conv32_1);
out = residual(out, params.conv32_2);
out = residual(out, params.conv32_3);
2020-08-18 13:54:53 +02:00
2020-12-23 17:26:55 +01:00
out = residualDown(out, params.conv64_down);
out = residual(out, params.conv64_1);
out = residual(out, params.conv64_2);
out = residual(out, params.conv64_3);
2020-08-18 13:54:53 +02:00
2020-12-23 17:26:55 +01:00
out = residualDown(out, params.conv128_down);
out = residual(out, params.conv128_1);
out = residual(out, params.conv128_2);
2020-08-18 13:54:53 +02:00
2020-12-23 17:26:55 +01:00
out = residualDown(out, params.conv256_down);
out = residual(out, params.conv256_1);
out = residual(out, params.conv256_2);
out = residualDown(out, params.conv256_down_out);
2020-08-18 13:54:53 +02:00
2020-12-23 17:26:55 +01:00
const globalAvg = out.mean([1, 2]) as tf.Tensor2D;
const fullyConnected = tf.matMul(globalAvg, params.fc);
2020-08-18 13:54:53 +02:00
2020-12-23 17:26:55 +01:00
return fullyConnected;
});
2020-08-18 13:54:53 +02:00
}
public async forward(input: TNetInput): Promise<tf.Tensor2D> {
2020-12-23 17:26:55 +01:00
return this.forwardInput(await toNetInput(input));
2020-08-18 13:54:53 +02:00
}
public async computeFaceDescriptor(input: TNetInput): Promise<Float32Array|Float32Array[]> {
2021-01-24 17:08:04 +01:00
if (input?.shape?.some((dim) => dim <= 0)) return new Float32Array(128);
2020-12-23 17:26:55 +01:00
const netInput = await toNetInput(input);
2020-08-18 13:54:53 +02:00
const faceDescriptorTensors = tf.tidy(
2020-12-23 17:26:55 +01:00
() => tf.unstack(this.forwardInput(netInput)),
);
2021-01-24 17:08:04 +01:00
const faceDescriptorsForBatch = await Promise.all(faceDescriptorTensors.map((t) => t.data())) as Float32Array[];
2020-12-23 17:26:55 +01:00
faceDescriptorTensors.forEach((t) => t.dispose());
2021-01-24 17:08:04 +01:00
return netInput.isBatchInput ? faceDescriptorsForBatch : faceDescriptorsForBatch[0];
2020-08-18 13:54:53 +02:00
}
protected getDefaultModelName(): string {
2020-12-23 17:26:55 +01:00
return 'face_recognition_model';
2020-08-18 13:54:53 +02:00
}
2021-01-12 16:14:33 +01:00
protected extractParamsFromWeightMap(weightMap: tf.NamedTensorMap) {
return extractParamsFromWeightMap(weightMap);
2020-08-18 13:54:53 +02:00
}
protected extractParams(weights: Float32Array) {
2020-12-23 17:26:55 +01:00
return extractParams(weights);
2020-08-18 13:54:53 +02:00
}
2020-12-23 17:26:55 +01:00
}