@vladmandic/face-api
Version:
FaceAPI: AI-powered Face Detection & Rotation Tracking, Face Description & Recognition, Age & Gender & Emotion Prediction for Browser and NodeJS using TensorFlow/JS
53 lines (41 loc) • 1.8 kB
text/typescript
import * as tf from '../../dist/tfjs.esm';
import { NetInput, TNetInput, toNetInput } from '../dom/index';
import { NeuralNetwork } from '../NeuralNetwork';
import { normalize } from '../ops/index';
import { denseBlock3 } from './denseBlock';
import { extractParamsFromWeightMapTiny } from './extractParamsFromWeightMapTiny';
import { extractParamsTiny } from './extractParamsTiny';
import { IFaceFeatureExtractor, TinyFaceFeatureExtractorParams } from './types';
export class TinyFaceFeatureExtractor extends NeuralNetwork<TinyFaceFeatureExtractorParams> implements IFaceFeatureExtractor<TinyFaceFeatureExtractorParams> {
constructor() {
super('TinyFaceFeatureExtractor');
}
public forwardInput(input: NetInput): tf.Tensor4D {
const { params } = this;
if (!params) {
throw new Error('TinyFaceFeatureExtractor - load model before inference');
}
return tf.tidy(() => {
const batchTensor = tf.cast(input.toBatchTensor(112, true), 'float32');
const meanRgb = [122.782, 117.001, 104.298];
const normalized = normalize(batchTensor, meanRgb).div(255) as tf.Tensor4D;
let out = denseBlock3(normalized, params.dense0, true);
out = denseBlock3(out, params.dense1);
out = denseBlock3(out, params.dense2);
out = tf.avgPool(out, [14, 14], [2, 2], 'valid');
return out;
});
}
public async forward(input: TNetInput): Promise<tf.Tensor4D> {
return this.forwardInput(await toNetInput(input));
}
protected getDefaultModelName(): string {
return 'face_feature_extractor_tiny_model';
}
protected extractParamsFromWeightMap(weightMap: tf.NamedTensorMap) {
return extractParamsFromWeightMapTiny(weightMap);
}
protected extractParams(weights: Float32Array) {
return extractParamsTiny(weights);
}
}