FaceProcessor.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { __extends } from "tslib";
  2. import * as tf from '@tensorflow/tfjs-core';
  3. import { fullyConnectedLayer } from '../common/fullyConnectedLayer';
  4. import { NetInput } from '../dom';
  5. import { NeuralNetwork } from '../NeuralNetwork';
  6. import { extractParams } from './extractParams';
  7. import { extractParamsFromWeigthMap } from './extractParamsFromWeigthMap';
  8. import { seperateWeightMaps } from './util';
  9. var FaceProcessor = /** @class */ (function (_super) {
  10. __extends(FaceProcessor, _super);
  11. function FaceProcessor(_name, faceFeatureExtractor) {
  12. var _this = _super.call(this, _name) || this;
  13. _this._faceFeatureExtractor = faceFeatureExtractor;
  14. return _this;
  15. }
  16. Object.defineProperty(FaceProcessor.prototype, "faceFeatureExtractor", {
  17. get: function () {
  18. return this._faceFeatureExtractor;
  19. },
  20. enumerable: true,
  21. configurable: true
  22. });
  23. FaceProcessor.prototype.runNet = function (input) {
  24. var _this = this;
  25. var params = this.params;
  26. if (!params) {
  27. throw new Error(this._name + " - load model before inference");
  28. }
  29. return tf.tidy(function () {
  30. var bottleneckFeatures = input instanceof NetInput
  31. ? _this.faceFeatureExtractor.forwardInput(input)
  32. : input;
  33. return fullyConnectedLayer(bottleneckFeatures.as2D(bottleneckFeatures.shape[0], -1), params.fc);
  34. });
  35. };
  36. FaceProcessor.prototype.dispose = function (throwOnRedispose) {
  37. if (throwOnRedispose === void 0) { throwOnRedispose = true; }
  38. this.faceFeatureExtractor.dispose(throwOnRedispose);
  39. _super.prototype.dispose.call(this, throwOnRedispose);
  40. };
  41. FaceProcessor.prototype.loadClassifierParams = function (weights) {
  42. var _a = this.extractClassifierParams(weights), params = _a.params, paramMappings = _a.paramMappings;
  43. this._params = params;
  44. this._paramMappings = paramMappings;
  45. };
  46. FaceProcessor.prototype.extractClassifierParams = function (weights) {
  47. return extractParams(weights, this.getClassifierChannelsIn(), this.getClassifierChannelsOut());
  48. };
  49. FaceProcessor.prototype.extractParamsFromWeigthMap = function (weightMap) {
  50. var _a = seperateWeightMaps(weightMap), featureExtractorMap = _a.featureExtractorMap, classifierMap = _a.classifierMap;
  51. this.faceFeatureExtractor.loadFromWeightMap(featureExtractorMap);
  52. return extractParamsFromWeigthMap(classifierMap);
  53. };
  54. FaceProcessor.prototype.extractParams = function (weights) {
  55. var cIn = this.getClassifierChannelsIn();
  56. var cOut = this.getClassifierChannelsOut();
  57. var classifierWeightSize = (cOut * cIn) + cOut;
  58. var featureExtractorWeights = weights.slice(0, weights.length - classifierWeightSize);
  59. var classifierWeights = weights.slice(weights.length - classifierWeightSize);
  60. this.faceFeatureExtractor.extractWeights(featureExtractorWeights);
  61. return this.extractClassifierParams(classifierWeights);
  62. };
  63. return FaceProcessor;
  64. }(NeuralNetwork));
  65. export { FaceProcessor };
  66. //# sourceMappingURL=FaceProcessor.js.map