ncnn.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /* jshint esversion: 6 */
  2. var ncnn = ncnn || {};
  3. var base = base || require('./base');
  4. // https://github.com/Tencent/ncnn/wiki/param-and-model-file-structure
  5. // https://github.com/Tencent/ncnn/wiki/operation-param-weight-table
  6. ncnn.ModelFactory = class {
  7. match(context) {
  8. const identifier = context.identifier.toLowerCase();
  9. if (identifier.endsWith('.param') || identifier.endsWith('.cfg.ncnn')) {
  10. const reader = base.TextReader.open(context.stream.peek(), 2048);
  11. const signature = reader.read();
  12. if (signature !== undefined) {
  13. if (signature.trim() === '7767517') {
  14. return true;
  15. }
  16. const header = signature.trim().split(' ');
  17. if (header.length === 2 && header.every((value) => value >>> 0 === parseFloat(value))) {
  18. return true;
  19. }
  20. }
  21. }
  22. if (identifier.endsWith('.param.bin')) {
  23. const stream = context.stream;
  24. if (stream.length > 4) {
  25. const buffer = stream.peek(4);
  26. const signature = (buffer[0] | buffer[1] << 8 | buffer[2] << 16 | buffer [3] << 24) >>> 0;
  27. if (signature == 0x007685DD) {
  28. return true;
  29. }
  30. }
  31. }
  32. if (identifier.endsWith('.bin') || identifier.endsWith('.weights.ncnn')) {
  33. if (identifier == 'snapshot_blob.bin' || identifier === 'v8_context_snapshot.bin') {
  34. return false;
  35. }
  36. const stream = context.stream;
  37. if (stream.length > 4) {
  38. const buffer = stream.peek(4);
  39. const signature = (buffer[0] | buffer[1] << 8 | buffer[2] << 16 | buffer [3] << 24) >>> 0;
  40. if (signature === 0x00000000 || signature === 0x00000001 ||
  41. signature === 0x01306B47 || signature === 0x000D4B38 || signature === 0x0002C056) {
  42. return true;
  43. }
  44. }
  45. }
  46. return false;
  47. }
  48. open(context) {
  49. return ncnn.Metadata.open(context).then((metadata) => {
  50. const identifier = context.identifier.toLowerCase();
  51. const openBinary = (param, bin) => {
  52. const reader = new ncnn.BinaryParamReader(metadata, param);
  53. return new ncnn.Model(metadata, reader, bin);
  54. };
  55. const openText = (param, bin) => {
  56. const reader = new ncnn.TextParamReader(param);
  57. return new ncnn.Model(metadata, reader, bin);
  58. };
  59. let bin = null;
  60. if (identifier.endsWith('.param') || identifier.endsWith('.cfg.ncnn')) {
  61. if (identifier.endsWith('.param')) {
  62. bin = context.identifier.substring(0, context.identifier.length - 6) + '.bin';
  63. }
  64. else if (identifier.endsWith('.cfg.ncnn')) {
  65. bin = context.identifier.substring(0, context.identifier.length - 9) + '.weights.ncnn';
  66. }
  67. return context.request(bin, null).then((stream) => {
  68. const buffer = stream.read();
  69. return openText(context.stream.peek(), buffer);
  70. }).catch(() => {
  71. return openText(context.stream.peek(), null);
  72. });
  73. }
  74. else if (identifier.endsWith('.param.bin')) {
  75. bin = context.identifier.substring(0, context.identifier.length - 10) + '.bin';
  76. return context.request(bin, null).then((stream) => {
  77. const buffer = stream.read();
  78. return openBinary(context.stream.peek(), buffer);
  79. }).catch(() => {
  80. return openBinary(context.stream.peek(), null);
  81. });
  82. }
  83. else if (identifier.endsWith('.bin') || identifier.endsWith('.weights.ncnn')) {
  84. let text = null;
  85. if (identifier.endsWith('bin')) {
  86. text = context.identifier.substring(0, context.identifier.length - 4) + '.param';
  87. }
  88. else if (identifier.endsWith('.weights.ncnn')) {
  89. text = context.identifier.substring(0, context.identifier.length - 13) + '.cfg.ncnn';
  90. }
  91. return context.request(text, null).then((stream) => {
  92. const buffer = stream.peek();
  93. return openText(buffer, context.stream.peek());
  94. }).catch(() => {
  95. return context.request(text + '.bin', null).then((stream) => {
  96. const buffer = stream.peek();
  97. return openBinary(buffer, context.stream.peek());
  98. });
  99. });
  100. }
  101. });
  102. }
  103. };
  104. ncnn.Model = class {
  105. constructor(metadata, param, bin) {
  106. this._graphs = [
  107. new ncnn.Graph(metadata, param, bin)
  108. ];
  109. }
  110. get format() {
  111. return 'ncnn';
  112. }
  113. get graphs() {
  114. return this._graphs;
  115. }
  116. };
  117. ncnn.Graph = class {
  118. constructor(metadata, param, bin) {
  119. this._inputs = [];
  120. this._outputs = [];
  121. this._nodes = [];
  122. const blobReader = new ncnn.BlobReader(bin);
  123. const layers = param.layers;
  124. const args = new Map();
  125. const arg = (name, type) => {
  126. if (!args.has(name)) {
  127. args.set(name, new ncnn.Argument(name, type, null));
  128. }
  129. return args.get(name);
  130. };
  131. for (const layer of layers) {
  132. const attributes = layer.attributes;
  133. for (const pair of attributes) {
  134. const key = pair[0];
  135. const list = pair[1];
  136. if (key === '30' && Array.isArray(list)) {
  137. const value = list.map((item) => parseInt(item, 10));
  138. for (const output of layer.outputs || []) {
  139. if (value.length > 0 && value[0] <= value.length - 1) {
  140. const shape = new Array(value.shift());
  141. for (let i = 0; i < shape.length; i++) {
  142. shape[i] = value.shift();
  143. }
  144. const type = new ncnn.TensorType('?', new ncnn.TensorShape(shape));
  145. arg(output, type, null);
  146. }
  147. attributes.delete(key);
  148. }
  149. }
  150. }
  151. }
  152. for (const layer of layers) {
  153. if (layer.type == 'Input') {
  154. const values = Array.from(layer.attributes.values());
  155. const dimensions = values.map((value) => !isNaN(parseInt(value, 10)) ? parseInt(value, 10) : value);
  156. const shape = new ncnn.TensorShape(dimensions);
  157. const type = new ncnn.TensorType('float32', shape);
  158. const input = new ncnn.Parameter(layer.name, true, layer.outputs.map((output) => new ncnn.Argument(output, type, null)));
  159. this._inputs.push(input);
  160. }
  161. else {
  162. const node = new ncnn.Node(metadata, blobReader, layer, arg);
  163. this._nodes.push(node);
  164. }
  165. }
  166. }
  167. get inputs() {
  168. return this._inputs;
  169. }
  170. get outputs() {
  171. return this._outputs;
  172. }
  173. get nodes() {
  174. return this._nodes;
  175. }
  176. };
  177. ncnn.Parameter = class {
  178. constructor(name, visible, args) {
  179. this._name = name;
  180. this._visible = visible;
  181. this._arguments = args;
  182. }
  183. get name() {
  184. return this._name;
  185. }
  186. get visible() {
  187. return this._visible;
  188. }
  189. get arguments() {
  190. return this._arguments;
  191. }
  192. };
  193. ncnn.Argument = class {
  194. constructor(name, type, initializer) {
  195. if (typeof name !== 'string') {
  196. throw new ncnn.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
  197. }
  198. this._name = name;
  199. this._type = type || null;
  200. this._initializer = initializer || null;
  201. }
  202. get name() {
  203. return this._name;
  204. }
  205. get type() {
  206. if (this._initializer) {
  207. return this._initializer.type;
  208. }
  209. return this._type;
  210. }
  211. get initializer() {
  212. return this._initializer;
  213. }
  214. };
  215. ncnn.Node = class {
  216. constructor(metadata, blobReader, layer, arg) {
  217. this._metadata = metadata;
  218. this._inputs = [];
  219. this._outputs = [];
  220. this._chain = [];
  221. this._type = layer.type;
  222. this._name = layer.name || '';
  223. const operator = metadata.operator(this._type);
  224. if (operator) {
  225. this._type = operator;
  226. }
  227. const schema = metadata.type(this._type);
  228. const attributeMetadata = schema && schema.attributes ? schema && schema.attributes : [];
  229. const attributes = layer.attributes;
  230. const inputs = layer.inputs || [];
  231. let inputIndex = 0;
  232. if (schema && schema.inputs) {
  233. for (const inputDef of schema.inputs) {
  234. if (inputIndex < inputs.length || inputDef.option != 'optional') {
  235. const inputCount = (inputDef.option == 'variadic') ? (inputs.length - inputIndex) : 1;
  236. const inputArguments = inputs.slice(inputIndex, inputIndex + inputCount).filter((id) => id != '' || inputDef.option != 'optional').map((id) => arg(id));
  237. this._inputs.push(new ncnn.Parameter(inputDef.name, true, inputArguments));
  238. inputIndex += inputCount;
  239. }
  240. }
  241. }
  242. this._inputs.push(...inputs.slice(inputIndex).map((input, index) => {
  243. const inputName = ((inputIndex + index) == 0) ? 'input' : (inputIndex + index).toString();
  244. return new ncnn.Parameter(inputName, true, [ arg(input) ]);
  245. }));
  246. const outputs = layer.outputs || [];
  247. let outputIndex = 0;
  248. if (schema && schema.outputs) {
  249. for (const outputDef of schema.outputs) {
  250. if (outputIndex < outputs.length || outputDef.option != 'optional') {
  251. const outputCount = (outputDef.option == 'variadic') ? (outputs.length - outputIndex) : 1;
  252. const outputArguments = outputs.slice(outputIndex, outputIndex + outputCount).map((id) => arg(id));
  253. this._outputs.push(new ncnn.Parameter(outputDef.name, true, outputArguments));
  254. outputIndex += outputCount;
  255. }
  256. }
  257. }
  258. this._outputs.push(...outputs.slice(outputIndex).map((output, index) => {
  259. const outputName = ((outputIndex + index) == 0) ? 'output' : (outputIndex + index).toString();
  260. return new ncnn.Parameter(outputName, true, [ arg(output) ]);
  261. }));
  262. switch (this._type) {
  263. case 'BatchNorm': {
  264. const channels = parseInt(attributes.get('0') || 0, 10);
  265. this._weight(blobReader, 'slope', [ channels ], 'float32');
  266. this._weight(blobReader, 'mean', [ channels ], 'float32');
  267. this._weight(blobReader, 'variance', [ channels ], 'float32');
  268. this._weight(blobReader, 'bias', [ channels ], 'float32');
  269. break;
  270. }
  271. case 'InnerProduct': {
  272. const activation_names = [ '', 'ReLU', 'Leaky ReLU', 'Clip', 'Sigmoid', 'Mish' ];
  273. const activation_type = parseInt(attributes.get('9') || 0, 10);
  274. if (activation_type > 0 && activation_type < activation_names.length) {
  275. const layer = {
  276. type: activation_names[activation_type],
  277. attributes: new Map()
  278. };
  279. this._chain.push(new ncnn.Node(metadata, blobReader, layer, arg));
  280. }
  281. const num_output = parseInt(attributes.get('0') || 0, 10);
  282. const weight_data_size = parseInt(attributes.get('2') || 0, 10);
  283. this._weight(blobReader, 'weight', [ num_output, weight_data_size / num_output ]);
  284. if (parseInt(attributes.get('1') || 0, 10) === 1) {
  285. this._weight(blobReader, 'bias', [ num_output ], 'float32');
  286. }
  287. attributes.delete('2');
  288. break;
  289. }
  290. case 'Bias': {
  291. const bias_data_size = parseInt(attributes.get('0') || 0, 10);
  292. this._weight(blobReader, 'bias', [ bias_data_size ], 'float32');
  293. break;
  294. }
  295. case 'Embed': {
  296. const num_output = parseInt(attributes.get('0') || 0, 10);
  297. const weight_data_size = parseInt(attributes.get('3') || 0, 10);
  298. this._weight(blobReader, 'weight', [ weight_data_size ]);
  299. if (parseInt(attributes.get('2') || 0, 10) === 1) {
  300. this._weight(blobReader, 'bias', [ num_output], 'float32');
  301. }
  302. attributes.get('3');
  303. break;
  304. }
  305. case 'Convolution':
  306. case 'ConvolutionDepthWise':
  307. case 'Deconvolution':
  308. case 'DeconvolutionDepthWise': {
  309. const activation_names = [ '', 'ReLU', 'LeakyReLU', 'Clip', 'Sigmoid', 'Mish' ];
  310. const activation_type = parseInt(attributes.get('9') || 0, 10);
  311. if (activation_type > 0 && activation_type < activation_names.length) {
  312. const layer = {
  313. type: activation_names[activation_type],
  314. attributes: new Map()
  315. };
  316. this._chain.push(new ncnn.Node(metadata, blobReader, layer, arg));
  317. }
  318. const num_output = parseInt(attributes.get('0') || 0, 10);
  319. const kernel_w = parseInt(attributes.get('1') || 0, 10);
  320. const kernel_h = parseInt(attributes.get('11') || kernel_w, 10);
  321. const weight_data_size = parseInt(attributes.get('6') || 0, 10);
  322. this._weight(blobReader, 'weight', [ num_output, weight_data_size / ( num_output * kernel_w * kernel_h), kernel_w, kernel_h ]);
  323. if (parseInt(attributes.get('5') || 0, 10) === 1) {
  324. this._weight(blobReader, 'bias', [ num_output ], 'float32');
  325. }
  326. attributes.delete('6');
  327. break;
  328. }
  329. case 'Dequantize': {
  330. if (parseInt(attributes.get('1') || 0, 10) === 1) {
  331. const bias_data_size = parseInt(attributes.get('2') || 0, 10);
  332. this._weight(blobReader, 'bias', [ bias_data_size ], 'float32');
  333. }
  334. break;
  335. }
  336. case 'Requantize': {
  337. if (parseInt(attributes.get('2') || 0, 10) === 1) {
  338. const bias_data_size = parseInt(attributes.get('3') || 0, 10);
  339. this._weight(blobReader, 'bias', [ bias_data_size ], 'float32');
  340. }
  341. break;
  342. }
  343. case 'InstanceNorm': {
  344. const affine = parseInt(attributes.get('2') || 1, 10);
  345. if (affine === 1) {
  346. const channels = parseInt(attributes.get('0') || 0, 10);
  347. this._weight(blobReader, 'gamma', [ channels ], 'float32');
  348. this._weight(blobReader, 'beta', [ channels ], 'float32');
  349. }
  350. break;
  351. }
  352. case 'Scale': {
  353. const scale_data_size = parseInt(attributes.get('0') || 0, 10);
  354. if (scale_data_size != -233) {
  355. this._weight(blobReader, 'scale', [ scale_data_size], 'float32');
  356. if (attributes.get('1') == '1') {
  357. this._weight(blobReader, 'bias', [ scale_data_size ], 'float32');
  358. }
  359. }
  360. break;
  361. }
  362. case 'Normalize': {
  363. const scale_data_size = parseInt(attributes.get('3') || 0, 10);
  364. this._weight(blobReader, 'scale', [ scale_data_size ], 'float32');
  365. break;
  366. }
  367. case 'PReLU': {
  368. const num_slope = parseInt(attributes.get('0') || 0, 10);
  369. this._weight(blobReader, 'slope', [ num_slope ], 'float32');
  370. break;
  371. }
  372. case 'Padding': {
  373. const per_channel_pad_data_size = parseInt(attributes.get('6') || 0, 10);
  374. this._weight(blobReader, 'per_channel_pad_data', [ per_channel_pad_data_size ], 'float32');
  375. break;
  376. }
  377. case 'MemoryData': {
  378. const w = parseInt(attributes.get('0') || 0, 10);
  379. const h = parseInt(attributes.get('1') || 0, 10);
  380. const c = parseInt(attributes.get('2') || 0, 10);
  381. if (c != 0) {
  382. this._weight(blobReader, 'data', [ c, h, w ], 'float32');
  383. }
  384. else if (h != 0) {
  385. this._weight(blobReader, 'data', [ h, w ], 'float32');
  386. }
  387. else if (w != 0) {
  388. this._weight(blobReader, 'data', [ w ], 'float32');
  389. }
  390. else {
  391. this._weight(blobReader, 'data', [ 1 ], 'float32');
  392. }
  393. break;
  394. }
  395. case 'GroupNorm': {
  396. const affine = parseInt(attributes.get('3') || 1, 10);
  397. if (affine === 1) {
  398. const channels = parseInt(attributes.get('1') || 0, 10);
  399. this._weight(blobReader, 'gamma', [ channels ], 'float32');
  400. this._weight(blobReader, 'beta', [ channels ], 'float32');
  401. }
  402. break;
  403. }
  404. case 'LayerNorm': {
  405. const channels = parseInt(attributes.get('0') || 0, 10);
  406. this._weight(blobReader, 'gamma', [ channels ], 'float32');
  407. this._weight(blobReader, 'beta', [ channels ], 'float32');
  408. break;
  409. }
  410. case 'RNN': {
  411. const num_output = parseInt(attributes.get('0') || 0, 10);
  412. const weight_data_size = parseInt(attributes.get('1') || 0, 10);
  413. const direction = parseInt(attributes.get('2') || 0, 10);
  414. const num_directions = direction == 2 ? 2 : 1;
  415. this._weight(blobReader, 'weight_xc', [ num_directions, num_output, weight_data_size / num_directions / num_output ]);
  416. this._weight(blobReader, 'bias_c', [ num_directions, num_output ]);
  417. this._weight(blobReader, 'weight_hc', [ num_directions, num_output, num_output ]);
  418. attributes.delete('1');
  419. break;
  420. }
  421. case 'LSTM': {
  422. const num_output = parseInt(attributes.get('0') || 0, 10);
  423. const weight_data_size = parseInt(attributes.get('1') || 0, 10);
  424. const direction = parseInt(attributes.get('2') || 0, 10);
  425. const num_directions = direction == 2 ? 2 : 1;
  426. this._weight(blobReader, 'weight_xc', [ num_directions, 4, num_output, weight_data_size / num_directions / num_output / 4 ]);
  427. this._weight(blobReader, 'bias_c', [ num_directions, 4, num_output ]);
  428. this._weight(blobReader, 'weight_hc', [ num_directions, 4, num_output, num_output ]);
  429. attributes.delete('1');
  430. break;
  431. }
  432. case 'GRU': {
  433. const num_output = parseInt(attributes.get('0') || 0, 10);
  434. const weight_data_size = parseInt(attributes.get('1') || 0, 10);
  435. const direction = parseInt(attributes.get('2') || 0, 10);
  436. const num_directions = direction == 2 ? 2 : 1;
  437. this._weight(blobReader, 'weight_xc', [ num_directions, 3, num_output, weight_data_size / num_directions / num_output / 3 ]);
  438. this._weight(blobReader, 'bias_c', [ num_directions, 4, num_output ]);
  439. this._weight(blobReader, 'weight_hc', [ num_directions, 3, num_output, num_output ]);
  440. attributes.delete('1');
  441. break;
  442. }
  443. }
  444. this._attributes = Array.from(attributes).map((attribute) => {
  445. const key = attribute[0];
  446. const value = attribute[1];
  447. const metadata = attributeMetadata[key];
  448. return new ncnn.Attribute(metadata, key, value);
  449. });
  450. }
  451. get type() {
  452. return this._type;
  453. }
  454. get name() {
  455. return this._name;
  456. }
  457. get metadata() {
  458. return this._metadata.type(this._type);
  459. }
  460. get attributes() {
  461. return this._attributes;
  462. }
  463. get inputs() {
  464. return this._inputs;
  465. }
  466. get outputs() {
  467. return this._outputs;
  468. }
  469. get chain() {
  470. return this._chain;
  471. }
  472. _weight(blobReader, name, dimensions, dataType) {
  473. const blob = blobReader.read(dimensions, dataType);
  474. dataType = blob ? (blob.dataType || '?') : (dataType || '?');
  475. const data = blob ? blob.data : null;
  476. this._inputs.push(new ncnn.Parameter(name, true, [
  477. new ncnn.Argument('', null, new ncnn.Tensor(new ncnn.TensorType(dataType, new ncnn.TensorShape(dimensions)), data))
  478. ]));
  479. }
  480. };
  481. ncnn.Attribute = class {
  482. constructor(metadata, key, value) {
  483. this._type = '';
  484. this._name = key;
  485. this._value = value;
  486. if (metadata) {
  487. this._name = metadata.name;
  488. if (metadata.type) {
  489. this._type = metadata.type;
  490. }
  491. switch (this._type) {
  492. case 'int32': {
  493. this._value = parseInt(this._value, 10);
  494. break;
  495. }
  496. case 'float32': {
  497. this._value = parseFloat(this._value);
  498. break;
  499. }
  500. case 'float32[]': {
  501. this._value = this._value.map((v) => parseFloat(v));
  502. break;
  503. }
  504. default: {
  505. if (this._type) {
  506. this._value = ncnn.Utility.value(this._value, this._type);
  507. }
  508. break;
  509. }
  510. }
  511. if (Object.prototype.hasOwnProperty.call(metadata, 'visible') && !metadata.visible) {
  512. this._visible = false;
  513. }
  514. else if (Object.prototype.hasOwnProperty.call(metadata, 'default')) {
  515. if (this._value == metadata.default || (this._value && this._value.toString() == metadata.default.toString())) {
  516. this._visible = false;
  517. }
  518. }
  519. }
  520. }
  521. get type() {
  522. return this._type;
  523. }
  524. get name() {
  525. return this._name;
  526. }
  527. get value() {
  528. return this._value;
  529. }
  530. get visible() {
  531. return this._visible == false ? false : true;
  532. }
  533. };
  534. ncnn.Tensor = class {
  535. constructor(type, data) {
  536. this._type = type;
  537. this._data = data;
  538. }
  539. get kind() {
  540. return 'Weight';
  541. }
  542. get type() {
  543. return this._type;
  544. }
  545. get state() {
  546. return this._context().state || null;
  547. }
  548. get value() {
  549. const context = this._context();
  550. if (context.state) {
  551. return null;
  552. }
  553. context.limit = Number.MAX_SAFE_INTEGER;
  554. return this._decode(context, 0);
  555. }
  556. toString() {
  557. const context = this._context();
  558. if (context.state) {
  559. return '';
  560. }
  561. context.limit = 10000;
  562. const value = this._decode(context, 0);
  563. return JSON.stringify(value, null, 4);
  564. }
  565. _context() {
  566. const context = {};
  567. context.index = 0;
  568. context.count = 0;
  569. context.state = null;
  570. if (this._type.dataType == '?') {
  571. context.state = 'Tensor has unknown data type.';
  572. return context;
  573. }
  574. if (!this._type.shape) {
  575. context.state = 'Tensor has no dimensions.';
  576. return context;
  577. }
  578. if (!this._data) {
  579. context.state = 'Tensor data is empty.';
  580. return context;
  581. }
  582. switch (this._type.dataType) {
  583. case 'float16':
  584. case 'float32':
  585. context.data = new DataView(this._data.buffer, this._data.byteOffset, this._data.byteLength);
  586. break;
  587. default:
  588. context.state = 'Tensor data type is not implemented.';
  589. break;
  590. }
  591. context.dataType = this._type.dataType;
  592. context.shape = this._type.shape.dimensions;
  593. return context;
  594. }
  595. _decode(context, dimension) {
  596. const shape = context.shape.length !== 0 ? context.shape : [ 1 ];
  597. const results = [];
  598. const size = shape[dimension];
  599. if (dimension == shape.length - 1) {
  600. for (let i = 0; i < size; i++) {
  601. if (context.count > context.limit) {
  602. results.push('...');
  603. return results;
  604. }
  605. switch (this._type.dataType) {
  606. case 'float32':
  607. results.push(context.data.getFloat32(context.index, true));
  608. context.index += 4;
  609. context.count++;
  610. break;
  611. case 'float16':
  612. results.push(context.data.getFloat16(context.index, true));
  613. context.index += 2;
  614. context.count++;
  615. break;
  616. }
  617. }
  618. }
  619. else {
  620. for (let j = 0; j < size; j++) {
  621. if (context.count > context.limit) {
  622. results.push('...');
  623. return results;
  624. }
  625. results.push(this._decode(context, dimension + 1));
  626. }
  627. }
  628. if (context.shape.length == 0) {
  629. return results[0];
  630. }
  631. return results;
  632. }
  633. };
  634. ncnn.TensorType = class {
  635. constructor(dataType, shape) {
  636. this._dataType = dataType || '?';
  637. this._shape = shape;
  638. }
  639. get dataType() {
  640. return this._dataType;
  641. }
  642. get shape() {
  643. return this._shape;
  644. }
  645. toString() {
  646. return this._dataType + this._shape.toString();
  647. }
  648. };
  649. ncnn.TensorShape = class {
  650. constructor(dimensions) {
  651. this._dimensions = dimensions;
  652. }
  653. get dimensions() {
  654. return this._dimensions;
  655. }
  656. toString() {
  657. return this._dimensions ? ('[' + this._dimensions.map((dimension) => dimension ? dimension.toString() : '?').join(',') + ']') : '';
  658. }
  659. };
  660. ncnn.Metadata = class {
  661. static open(context) {
  662. if (ncnn.Metadata._metadata) {
  663. return Promise.resolve(ncnn.Metadata._metadata);
  664. }
  665. return context.request('ncnn-metadata.json', 'utf-8', null).then((data) => {
  666. ncnn.Metadata._metadata = new ncnn.Metadata(data);
  667. return ncnn.Metadata._metadata;
  668. }).catch(() => {
  669. ncnn.Metadata._metadata = new ncnn.Metadata(null);
  670. return ncnn.Metadata._metadatas;
  671. });
  672. }
  673. constructor(data) {
  674. this._operatorMap = new Map();
  675. this._map = new Map();
  676. this._attributeCache = new Map();
  677. if (data) {
  678. const items = JSON.parse(data);
  679. for (const item of items) {
  680. if (item.name) {
  681. this._map.set(item.name, item);
  682. if (Object.prototype.hasOwnProperty.call(item, 'operator')) {
  683. this._operatorMap.set(item.operator, item.name);
  684. }
  685. }
  686. }
  687. }
  688. }
  689. operator(code) {
  690. return this._operatorMap.get(code);
  691. }
  692. type(name) {
  693. return this._map.get(name);
  694. }
  695. attribute(type, name) {
  696. const key = type + ':' + name;
  697. if (!this._attributeCache.has(key)) {
  698. const schema = this.type(type);
  699. if (schema && schema.attributes && schema.attributes.length > 0) {
  700. for (const attribute of schema.attributes) {
  701. this._attributeCache.set(type + ':' + attribute.name, attribute);
  702. }
  703. }
  704. if (!this._attributeCache.has(key)) {
  705. this._attributeCache.set(key, null);
  706. }
  707. }
  708. return this._attributeCache.get(key);
  709. }
  710. };
  711. ncnn.Utility = class {
  712. static value(value, type) {
  713. ncnn.Utility._enum = ncnn.Utility._enum || new Map([
  714. [ 'BinaryOpType', [ 'Add', 'Sub', 'Mul', 'Div', 'Max', 'Min', 'Pow', 'RSub', 'RDiv' ] ],
  715. [ 'EltwiseType', [ 'Prod', 'Sum', 'Max' ] ],
  716. [ 'PoolingType', [ 'Max', 'Average' ] ],
  717. [ 'InterpResizeType', [ '', 'Nearest', 'Bilinear', 'Bicubic' ] ],
  718. [ 'PermuteOrderType', [ 'WHC', 'HWC', 'WCH', 'CWH', 'HCW', 'CHW'] ]
  719. ]);
  720. if (this._enum.has(type) && typeof value === 'string') {
  721. const index = parseInt(value, 10);
  722. const list = this._enum.get(type);
  723. if (Number.isInteger(index) && index < list.length) {
  724. return list[index];
  725. }
  726. }
  727. return value;
  728. }
  729. };
  730. ncnn.TextParamReader = class {
  731. constructor(buffer) {
  732. const reader = base.TextReader.open(buffer);
  733. const lines = [];
  734. for (;;) {
  735. const line = reader.read();
  736. if (line === undefined) {
  737. break;
  738. }
  739. lines.push(line.trim());
  740. }
  741. const signature = lines.shift();
  742. const header = (signature !== '7767517' ? signature : lines.shift()).split(' ');
  743. if (header.length !== 2 || !header.every((value) => value >>> 0 === parseFloat(value))) {
  744. throw new ncnn.Error('Invalid header.');
  745. }
  746. const layers = [];
  747. while (lines.length > 0) {
  748. const line = lines.shift();
  749. if (line.length > 0) {
  750. const columns = line.split(' ').filter((s) => s.length != 0);
  751. const layer = {};
  752. layer.type = columns.shift();
  753. layer.name = columns.shift();
  754. const inputCount = parseInt(columns.shift(), 10);
  755. const outputCount = parseInt(columns.shift(), 10);
  756. layer.inputs = columns.splice(0, inputCount);
  757. layer.outputs = columns.splice(0, outputCount);
  758. layer.attributes = new Map();
  759. const attributes = layer.attributes;
  760. let index = 0;
  761. for (const column of columns) {
  762. const parts = column.split('=');
  763. if (parts.length > 2) {
  764. throw new ncnn.Attribute("Invalid attribute '" + column + "'.");
  765. }
  766. let key = (parts.length === 2) ? parts[0].trim() : index.toString();
  767. let value = (parts.length === 2) ? parts[1].trim() : parts[0].trim();
  768. const keyInt = parseInt(key, 10);
  769. if (keyInt < 0) {
  770. value = value.split(',').map((v) => v.trim());
  771. value.shift();
  772. key = (-(keyInt + 23300)).toString();
  773. }
  774. attributes.set(key, value);
  775. index++;
  776. }
  777. layers.push(layer);
  778. }
  779. }
  780. this._layers = layers;
  781. }
  782. get layers() {
  783. return this._layers;
  784. }
  785. };
  786. ncnn.BinaryParamReader = class {
  787. constructor(metadata, buffer) {
  788. const reader = new ncnn.BinaryReader(buffer);
  789. if (reader.int32() !== 0x007685DD) {
  790. throw new ncnn.Error('Invalid signature.');
  791. }
  792. const layerCount = reader.int32();
  793. /* const blobCount = */ reader.int32();
  794. this._layers = [];
  795. for (let i = 0; i < layerCount; i++) {
  796. const typeIndex = reader.int32();
  797. const operator = metadata.operator(typeIndex);
  798. const layer = {
  799. type: operator || typeIndex.toString(),
  800. name: i.toString(),
  801. attributes: new Map(),
  802. inputs: [],
  803. outputs: []
  804. };
  805. const inputCount = reader.int32();
  806. const outputCount = reader.int32();
  807. for (let j = 0; j < inputCount; j++) {
  808. layer.inputs.push(reader.int32().toString());
  809. }
  810. for (let j = 0; j < outputCount; j++) {
  811. layer.outputs.push(reader.int32().toString());
  812. }
  813. const attributes = layer.attributes;
  814. let id = reader.int32();
  815. while (id != -233) {
  816. const isArray = id <= -23300;
  817. if (isArray) {
  818. id = -id - 23300;
  819. }
  820. const key = id.toString();
  821. if (isArray) {
  822. const length = reader.int32();
  823. const values = [];
  824. for (let i = 0; i < length; i++) {
  825. values.push(reader.int32());
  826. }
  827. attributes.set(key, values);
  828. }
  829. else {
  830. const value = reader.int32();
  831. attributes.set(key, value);
  832. }
  833. id = reader.int32();
  834. }
  835. this._layers.push(layer);
  836. }
  837. }
  838. get layers() {
  839. return this._layers;
  840. }
  841. };
  842. ncnn.BlobReader = class {
  843. constructor(buffer) {
  844. this._buffer = buffer;
  845. this._position = 0;
  846. }
  847. read(shape, dataType) {
  848. if (this._buffer) {
  849. if (!dataType) {
  850. if (this._buffer && this._position + 4 < this._buffer.length) {
  851. const f0 = this._buffer[this._position++];
  852. const f1 = this._buffer[this._position++];
  853. const f2 = this._buffer[this._position++];
  854. const f3 = this._buffer[this._position++];
  855. const type = f0 | f1 << 8 | f2 << 16 | f3 << 24;
  856. switch (type) {
  857. case 0x00000000:
  858. dataType = 'float32';
  859. break;
  860. case 0x01306B47:
  861. dataType = 'float16';
  862. break;
  863. case 0x000D4B38:
  864. dataType = 'int8';
  865. break;
  866. case 0x00000001:
  867. dataType = 'qint8';
  868. break;
  869. case 0x0002C056: // size * sizeof(float) - raw data with extra scaling
  870. default:
  871. throw new ncnn.Error("Unknown weight type '" + type + "'.");
  872. }
  873. }
  874. else {
  875. this._buffer = null;
  876. }
  877. }
  878. let data = null;
  879. let size = 1;
  880. if (shape) {
  881. for (const dimension of shape) {
  882. size *= dimension;
  883. }
  884. }
  885. else {
  886. this._buffer = null;
  887. }
  888. if (this._buffer) {
  889. if (dataType) {
  890. const position = this._position;
  891. switch (dataType) {
  892. case 'float32':
  893. size *= 4;
  894. this._position += size;
  895. data = this._buffer.subarray(position, this._position);
  896. break;
  897. case 'float16':
  898. size *= 2;
  899. this._position += size;
  900. data = this._buffer.subarray(position, this._position);
  901. break;
  902. case 'int8':
  903. this._position += size;
  904. data = this._buffer.subarray(position, this._position);
  905. break;
  906. case 'qint8':
  907. this._position += size + 1024;
  908. data = null;
  909. break;
  910. default:
  911. throw new ncnn.Error("Unknown weight type '" + dataType + "'.");
  912. }
  913. }
  914. }
  915. return { dataType: dataType, data: data };
  916. }
  917. return null;
  918. }
  919. };
  920. ncnn.BinaryReader = class {
  921. constructor(buffer) {
  922. this._buffer = buffer;
  923. this._dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  924. this._position = 0;
  925. }
  926. skip(size) {
  927. this._position += size;
  928. if (this._position > this._buffer.length) {
  929. throw new ncnn.Error('Expected ' + (this._position - this._buffer.length) + ' more bytes. The file might be corrupted. Unexpected end of file.');
  930. }
  931. }
  932. int32() {
  933. const position = this._position;
  934. this.skip(4);
  935. return this._dataView.getInt32(position, true);
  936. }
  937. };
  938. ncnn.Error = class extends Error {
  939. constructor(message) {
  940. super(message);
  941. this.name = 'Error loading ncnn model.';
  942. }
  943. };
  944. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  945. module.exports.ModelFactory = ncnn.ModelFactory;
  946. }