TinyXception.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { __awaiter, __extends, __generator } from "tslib";
  2. import * as tf from '@tensorflow/tfjs-core';
  3. import { depthwiseSeparableConv } from '../common';
  4. import { toNetInput } from '../dom';
  5. import { NeuralNetwork } from '../NeuralNetwork';
  6. import { normalize } from '../ops';
  7. import { range } from '../utils';
  8. import { extractParams } from './extractParams';
  9. import { extractParamsFromWeigthMap } from './extractParamsFromWeigthMap';
  10. function conv(x, params, stride) {
  11. return tf.add(tf.conv2d(x, params.filters, stride, 'same'), params.bias);
  12. }
  13. function reductionBlock(x, params, isActivateInput) {
  14. if (isActivateInput === void 0) { isActivateInput = true; }
  15. var out = isActivateInput ? tf.relu(x) : x;
  16. out = depthwiseSeparableConv(out, params.separable_conv0, [1, 1]);
  17. out = depthwiseSeparableConv(tf.relu(out), params.separable_conv1, [1, 1]);
  18. out = tf.maxPool(out, [3, 3], [2, 2], 'same');
  19. out = tf.add(out, conv(x, params.expansion_conv, [2, 2]));
  20. return out;
  21. }
  22. function mainBlock(x, params) {
  23. var out = depthwiseSeparableConv(tf.relu(x), params.separable_conv0, [1, 1]);
  24. out = depthwiseSeparableConv(tf.relu(out), params.separable_conv1, [1, 1]);
  25. out = depthwiseSeparableConv(tf.relu(out), params.separable_conv2, [1, 1]);
  26. out = tf.add(out, x);
  27. return out;
  28. }
  29. var TinyXception = /** @class */ (function (_super) {
  30. __extends(TinyXception, _super);
  31. function TinyXception(numMainBlocks) {
  32. var _this = _super.call(this, 'TinyXception') || this;
  33. _this._numMainBlocks = numMainBlocks;
  34. return _this;
  35. }
  36. TinyXception.prototype.forwardInput = function (input) {
  37. var _this = this;
  38. var params = this.params;
  39. if (!params) {
  40. throw new Error('TinyXception - load model before inference');
  41. }
  42. return tf.tidy(function () {
  43. var batchTensor = input.toBatchTensor(112, true);
  44. var meanRgb = [122.782, 117.001, 104.298];
  45. var normalized = normalize(batchTensor, meanRgb).div(tf.scalar(256));
  46. var out = tf.relu(conv(normalized, params.entry_flow.conv_in, [2, 2]));
  47. out = reductionBlock(out, params.entry_flow.reduction_block_0, false);
  48. out = reductionBlock(out, params.entry_flow.reduction_block_1);
  49. range(_this._numMainBlocks, 0, 1).forEach(function (idx) {
  50. out = mainBlock(out, params.middle_flow["main_block_" + idx]);
  51. });
  52. out = reductionBlock(out, params.exit_flow.reduction_block);
  53. out = tf.relu(depthwiseSeparableConv(out, params.exit_flow.separable_conv, [1, 1]));
  54. return out;
  55. });
  56. };
  57. TinyXception.prototype.forward = function (input) {
  58. return __awaiter(this, void 0, void 0, function () {
  59. var _a;
  60. return __generator(this, function (_b) {
  61. switch (_b.label) {
  62. case 0:
  63. _a = this.forwardInput;
  64. return [4 /*yield*/, toNetInput(input)];
  65. case 1: return [2 /*return*/, _a.apply(this, [_b.sent()])];
  66. }
  67. });
  68. });
  69. };
  70. TinyXception.prototype.getDefaultModelName = function () {
  71. return 'tiny_xception_model';
  72. };
  73. TinyXception.prototype.extractParamsFromWeigthMap = function (weightMap) {
  74. return extractParamsFromWeigthMap(weightMap, this._numMainBlocks);
  75. };
  76. TinyXception.prototype.extractParams = function (weights) {
  77. return extractParams(weights, this._numMainBlocks);
  78. };
  79. return TinyXception;
  80. }(NeuralNetwork));
  81. export { TinyXception };
  82. //# sourceMappingURL=TinyXception.js.map