2020-08-20 02:10:42 +02:00
|
|
|
import * as tf from '@tensorflow/tfjs-core';
|
|
|
|
import { toNetInput } from '../dom';
|
|
|
|
import { NeuralNetwork } from '../NeuralNetwork';
|
|
|
|
import { normalize } from '../ops';
|
|
|
|
import { denseBlock3 } from './denseBlock';
|
|
|
|
import { extractParamsFromWeigthMapTiny } from './extractParamsFromWeigthMapTiny';
|
|
|
|
import { extractParamsTiny } from './extractParamsTiny';
|
|
|
|
export class TinyFaceFeatureExtractor extends NeuralNetwork {
|
2020-08-18 14:04:33 +02:00
|
|
|
constructor() {
|
|
|
|
super('TinyFaceFeatureExtractor');
|
|
|
|
}
|
|
|
|
forwardInput(input) {
|
|
|
|
const { params } = this;
|
|
|
|
if (!params) {
|
|
|
|
throw new Error('TinyFaceFeatureExtractor - load model before inference');
|
|
|
|
}
|
|
|
|
return tf.tidy(() => {
|
|
|
|
const batchTensor = input.toBatchTensor(112, true);
|
|
|
|
const meanRgb = [122.782, 117.001, 104.298];
|
2020-08-20 02:10:42 +02:00
|
|
|
const normalized = normalize(batchTensor, meanRgb).div(tf.scalar(255));
|
|
|
|
let out = denseBlock3(normalized, params.dense0, true);
|
|
|
|
out = denseBlock3(out, params.dense1);
|
|
|
|
out = denseBlock3(out, params.dense2);
|
2020-08-18 14:04:33 +02:00
|
|
|
out = tf.avgPool(out, [14, 14], [2, 2], 'valid');
|
|
|
|
return out;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
async forward(input) {
|
2020-08-20 02:10:42 +02:00
|
|
|
return this.forwardInput(await toNetInput(input));
|
2020-08-18 14:04:33 +02:00
|
|
|
}
|
|
|
|
getDefaultModelName() {
|
|
|
|
return 'face_feature_extractor_tiny_model';
|
|
|
|
}
|
|
|
|
extractParamsFromWeigthMap(weightMap) {
|
2020-08-20 02:10:42 +02:00
|
|
|
return extractParamsFromWeigthMapTiny(weightMap);
|
2020-08-18 14:04:33 +02:00
|
|
|
}
|
|
|
|
extractParams(weights) {
|
2020-08-20 02:10:42 +02:00
|
|
|
return extractParamsTiny(weights);
|
2020-08-18 14:04:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//# sourceMappingURL=TinyFaceFeatureExtractor.js.map
|