FaceProcessor.js 3.3 KB

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