TinyFaceFeatureExtractor.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { __awaiter, __extends, __generator } from "tslib";
  2. import * as tf from '@tensorflow/tfjs-core';
  3. import { toNetInput } from '../dom';
  4. import { NeuralNetwork } from '../NeuralNetwork';
  5. import { normalize } from '../ops';
  6. import { denseBlock3 } from './denseBlock';
  7. import { extractParamsFromWeigthMapTiny } from './extractParamsFromWeigthMapTiny';
  8. import { extractParamsTiny } from './extractParamsTiny';
  9. var TinyFaceFeatureExtractor = /** @class */ (function (_super) {
  10. __extends(TinyFaceFeatureExtractor, _super);
  11. function TinyFaceFeatureExtractor() {
  12. return _super.call(this, 'TinyFaceFeatureExtractor') || this;
  13. }
  14. TinyFaceFeatureExtractor.prototype.forwardInput = function (input) {
  15. var params = this.params;
  16. if (!params) {
  17. throw new Error('TinyFaceFeatureExtractor - load model before inference');
  18. }
  19. return tf.tidy(function () {
  20. var batchTensor = input.toBatchTensor(112, true);
  21. var meanRgb = [122.782, 117.001, 104.298];
  22. var normalized = normalize(batchTensor, meanRgb).div(tf.scalar(255));
  23. var out = denseBlock3(normalized, params.dense0, true);
  24. out = denseBlock3(out, params.dense1);
  25. out = denseBlock3(out, params.dense2);
  26. out = tf.avgPool(out, [14, 14], [2, 2], 'valid');
  27. return out;
  28. });
  29. };
  30. TinyFaceFeatureExtractor.prototype.forward = function (input) {
  31. return __awaiter(this, void 0, void 0, function () {
  32. var _a;
  33. return __generator(this, function (_b) {
  34. switch (_b.label) {
  35. case 0:
  36. _a = this.forwardInput;
  37. return [4 /*yield*/, toNetInput(input)];
  38. case 1: return [2 /*return*/, _a.apply(this, [_b.sent()])];
  39. }
  40. });
  41. });
  42. };
  43. TinyFaceFeatureExtractor.prototype.getDefaultModelName = function () {
  44. return 'face_feature_extractor_tiny_model';
  45. };
  46. TinyFaceFeatureExtractor.prototype.extractParamsFromWeigthMap = function (weightMap) {
  47. return extractParamsFromWeigthMapTiny(weightMap);
  48. };
  49. TinyFaceFeatureExtractor.prototype.extractParams = function (weights) {
  50. return extractParamsTiny(weights);
  51. };
  52. return TinyFaceFeatureExtractor;
  53. }(NeuralNetwork));
  54. export { TinyFaceFeatureExtractor };
  55. //# sourceMappingURL=TinyFaceFeatureExtractor.js.map