2020-12-23 18:58:47 +01:00
|
|
|
import * as tf from '../../dist/tfjs.esm';
|
2020-08-18 13:54:53 +02:00
|
|
|
|
|
|
|
import { fullyConnectedLayer } from '../common/fullyConnectedLayer';
|
2020-12-19 17:46:41 +01:00
|
|
|
import { NetInput } from '../dom/index';
|
2020-08-18 13:54:53 +02:00
|
|
|
import {
|
|
|
|
FaceFeatureExtractorParams,
|
|
|
|
IFaceFeatureExtractor,
|
|
|
|
TinyFaceFeatureExtractorParams,
|
|
|
|
} from '../faceFeatureExtractor/types';
|
|
|
|
import { NeuralNetwork } from '../NeuralNetwork';
|
|
|
|
import { extractParams } from './extractParams';
|
|
|
|
import { extractParamsFromWeigthMap } from './extractParamsFromWeigthMap';
|
|
|
|
import { NetParams } from './types';
|
|
|
|
import { seperateWeightMaps } from './util';
|
|
|
|
|
|
|
|
export abstract class FaceProcessor<
|
|
|
|
TExtractorParams extends FaceFeatureExtractorParams | TinyFaceFeatureExtractorParams
|
|
|
|
>
|
|
|
|
extends NeuralNetwork<NetParams> {
|
|
|
|
protected _faceFeatureExtractor: IFaceFeatureExtractor<TExtractorParams>
|
|
|
|
|
|
|
|
constructor(_name: string, faceFeatureExtractor: IFaceFeatureExtractor<TExtractorParams>) {
|
2020-12-23 17:26:55 +01:00
|
|
|
super(_name);
|
|
|
|
this._faceFeatureExtractor = faceFeatureExtractor;
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get faceFeatureExtractor(): IFaceFeatureExtractor<TExtractorParams> {
|
2020-12-23 17:26:55 +01:00
|
|
|
return this._faceFeatureExtractor;
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract getDefaultModelName(): string
|
2020-12-23 17:26:55 +01:00
|
|
|
|
2020-08-18 13:54:53 +02:00
|
|
|
protected abstract getClassifierChannelsIn(): number
|
2020-12-23 17:26:55 +01:00
|
|
|
|
2020-08-18 13:54:53 +02:00
|
|
|
protected abstract getClassifierChannelsOut(): number
|
|
|
|
|
|
|
|
public runNet(input: NetInput | tf.Tensor4D): 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(`${this._name} - load model before inference`);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return tf.tidy(() => {
|
|
|
|
const bottleneckFeatures = input instanceof NetInput
|
|
|
|
? this.faceFeatureExtractor.forwardInput(input)
|
2020-12-23 17:26:55 +01:00
|
|
|
: input;
|
|
|
|
return fullyConnectedLayer(bottleneckFeatures.as2D(bottleneckFeatures.shape[0], -1), params.fc);
|
|
|
|
});
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public dispose(throwOnRedispose: boolean = true) {
|
2020-12-23 17:26:55 +01:00
|
|
|
this.faceFeatureExtractor.dispose(throwOnRedispose);
|
|
|
|
super.dispose(throwOnRedispose);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public loadClassifierParams(weights: Float32Array) {
|
2020-12-23 17:26:55 +01:00
|
|
|
const { params, paramMappings } = this.extractClassifierParams(weights);
|
|
|
|
this._params = params;
|
|
|
|
this._paramMappings = paramMappings;
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public extractClassifierParams(weights: Float32Array) {
|
2020-12-23 17:26:55 +01:00
|
|
|
return extractParams(weights, this.getClassifierChannelsIn(), this.getClassifierChannelsOut());
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected extractParamsFromWeigthMap(weightMap: tf.NamedTensorMap) {
|
2020-12-23 17:26:55 +01:00
|
|
|
const { featureExtractorMap, classifierMap } = seperateWeightMaps(weightMap);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
this.faceFeatureExtractor.loadFromWeightMap(featureExtractorMap);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
return extractParamsFromWeigthMap(classifierMap);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected extractParams(weights: Float32Array) {
|
2020-12-23 17:26:55 +01:00
|
|
|
const cIn = this.getClassifierChannelsIn();
|
|
|
|
const cOut = this.getClassifierChannelsOut();
|
|
|
|
const classifierWeightSize = (cOut * cIn) + cOut;
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
const featureExtractorWeights = weights.slice(0, weights.length - classifierWeightSize);
|
|
|
|
const classifierWeights = weights.slice(weights.length - classifierWeightSize);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
this.faceFeatureExtractor.extractWeights(featureExtractorWeights);
|
|
|
|
return this.extractClassifierParams(classifierWeights);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
2020-12-23 17:26:55 +01:00
|
|
|
}
|