caffe.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. var caffe = {};
  2. var protobuf = require('./protobuf');
  3. caffe.ModelFactory = class {
  4. match(context) {
  5. const identifier = context.identifier;
  6. const extension = identifier.split('.').pop().toLowerCase();
  7. if (extension == 'caffemodel') {
  8. return 'caffe.pb';
  9. }
  10. if (identifier == 'saved_model.pbtxt' || identifier == 'saved_model.prototxt' ||
  11. identifier.endsWith('predict_net.pbtxt') || identifier.endsWith('predict_net.prototxt') ||
  12. identifier.endsWith('init_net.pbtxt') || identifier.endsWith('init_net.prototxt')) {
  13. return undefined;
  14. }
  15. const tags = context.tags('pbtxt');
  16. if (tags.has('layer') || tags.has('layers')) {
  17. return 'caffe.pbtxt';
  18. }
  19. if (tags.has('net') || tags.has('train_net') || tags.has('net_param')) {
  20. return 'caffe.pbtxt.solver';
  21. }
  22. return undefined;
  23. }
  24. async open(context, target) {
  25. await context.require('./caffe-proto');
  26. caffe.proto = protobuf.get('caffe').caffe;
  27. const openModel = async (context, netParameter) => {
  28. const metadata = await context.metadata('caffe-metadata.json');
  29. return new caffe.Model(metadata, netParameter);
  30. };
  31. const openNetParameterText = (context, identifier, buffer) => {
  32. let netParameter = null;
  33. try {
  34. const reader = protobuf.TextReader.open(buffer);
  35. reader.field = function(tag, message) {
  36. const type = message.constructor.name;
  37. if (tag.endsWith('_param') && (type == 'LayerParameter' || type == 'V1LayerParameter' || type == 'V0LayerParameter')) {
  38. message[tag] = caffe.ModelFactory._decodeText(reader);
  39. return;
  40. } else if (message.constructor.name.endsWith('Parameter') || message.constructor.name === 'ParamSpec') {
  41. if (message[tag]) {
  42. if (!Array.isArray(message[tag])) {
  43. message[tag] = [ message[tag] ];
  44. }
  45. message[tag].push(this.read());
  46. } else {
  47. message[tag] = this.read();
  48. }
  49. return;
  50. }
  51. throw new Error("Unknown field '" + tag + "'" + this.location());
  52. };
  53. reader.enum = function(type) {
  54. const token = this.token();
  55. this.next();
  56. this.semicolon();
  57. if (!Object.prototype.hasOwnProperty.call(type, token)) {
  58. const value = Number.parseInt(token, 10);
  59. if (!Number.isNaN(token - value)) {
  60. return value;
  61. }
  62. return token;
  63. }
  64. return type[token];
  65. };
  66. if (/MobileNetSSD_train_template.prototxt/.exec(identifier)) {
  67. reader.integer = function() {
  68. const token = this.token();
  69. const value = Number.parseInt(token, 10);
  70. this.next();
  71. this.semicolon();
  72. if (Number.isNaN(token - value)) {
  73. return token;
  74. }
  75. return value;
  76. };
  77. }
  78. netParameter = caffe.proto.NetParameter.decodeText(reader);
  79. } catch (error) {
  80. const message = error && error.message ? error.message : error.toString();
  81. throw new caffe.Error('File text format is not caffe.NetParameter (' + message.replace(/\.$/, '') + ').');
  82. }
  83. return openModel(context, netParameter);
  84. };
  85. switch (target) {
  86. case 'caffe.pbtxt.solver': {
  87. const stream = context.stream;
  88. const reader = protobuf.TextReader.open(stream);
  89. reader.field = function(tag, message) {
  90. if (message instanceof caffe.proto.SolverParameter) {
  91. message[tag] = this.read();
  92. return;
  93. }
  94. throw new Error("Unknown field '" + tag + "'" + this.location());
  95. };
  96. const solver = caffe.proto.SolverParameter.decodeText(reader);
  97. if (solver.net_param) {
  98. return openModel(context, solver.net_param);
  99. }
  100. let file = solver.net || solver.train_net;
  101. file = file.split('/').pop();
  102. try {
  103. const stream = await context.request(file, null);
  104. const buffer = stream.peek();
  105. return openNetParameterText(context, file, buffer);
  106. } catch (error) {
  107. const message = error.message ? error.message : error.toString();
  108. throw new caffe.Error("Failed to load '" + file + "' (" + message.replace(/\.$/, '') + ').');
  109. }
  110. }
  111. case 'caffe.pbtxt': {
  112. return openNetParameterText(context, context.identifier, context.stream.peek());
  113. }
  114. case 'caffe.pb': {
  115. let netParameter = null;
  116. try {
  117. const stream = context.stream;
  118. const reader = protobuf.BinaryReader.open(stream);
  119. netParameter = caffe.proto.NetParameter.decode(reader);
  120. } catch (error) {
  121. const message = error && error.message ? error.message : error.toString();
  122. throw new caffe.Error('File format is not caffe.NetParameter (' + message.replace(/\.$/, '') + ').');
  123. }
  124. return openModel(context, netParameter);
  125. }
  126. default: {
  127. throw new caffe.Error("Unsupported Caffe format '" + target + "'.");
  128. }
  129. }
  130. }
  131. static _decodeText(reader) {
  132. const message = {};
  133. reader.start();
  134. while (!reader.end()) {
  135. const tag = reader.tag();
  136. const value = reader.read();
  137. if (!message[tag]) {
  138. message[tag] = value;
  139. } else {
  140. if (!Array.isArray(message[tag])) {
  141. message[tag] = [ message[tag] ];
  142. }
  143. message[tag].push(value);
  144. }
  145. }
  146. return message;
  147. }
  148. };
  149. caffe.Model = class {
  150. constructor(metadata, net) {
  151. this._name = net.name;
  152. if (net.layers && net.layers.length > 0) {
  153. if (net.layers.every((layer) => Object.prototype.hasOwnProperty.call(layer, 'layer'))) {
  154. this._version = 0;
  155. net.layer = net.layers;
  156. } else {
  157. this._version = 1;
  158. net.layer = net.layers;
  159. }
  160. } else if (net.layer && net.layer.length > 0) {
  161. this._version = 2;
  162. }
  163. const phases = new Set();
  164. for (const layer of net.layer) {
  165. for (const include of layer.include) {
  166. if (include.phase !== undefined) {
  167. phases.add(include.phase);
  168. }
  169. }
  170. }
  171. if (phases.size === 0) {
  172. phases.add(-1);
  173. }
  174. this._graphs = [];
  175. for (const phase of phases) {
  176. const graph = new caffe.Graph(metadata, phase, net, this._version);
  177. this._graphs.push(graph);
  178. }
  179. }
  180. get format() {
  181. return 'Caffe' + (this._version ? ' v' + this._version.toString() : '');
  182. }
  183. get graphs() {
  184. return this._graphs;
  185. }
  186. };
  187. caffe.Graph = class {
  188. constructor(metadata, phase, net, version) {
  189. switch (phase) {
  190. case 0: this._phase = 'TRAIN'; break;
  191. case 1: this._phase = 'TEST'; break;
  192. case -1: this._phase = ''; break;
  193. default: this._phase = phase.toString(); break;
  194. }
  195. this._nodes = [];
  196. this._inputs = [];
  197. this._outputs = [];
  198. for (const layer of net.layer) {
  199. layer.input = layer.bottom.slice(0);
  200. layer.output = layer.top.slice(0);
  201. layer.chain = [];
  202. }
  203. const layers = [];
  204. for (const layer of net.layer) {
  205. if (phase === -1 || layer.include.every((include) => include.phase === phase)) {
  206. layers.push(layer);
  207. }
  208. }
  209. const scopes = new Map();
  210. let index = 0;
  211. for (const layer of layers) {
  212. layer.input = layer.input.map((input) => scopes.has(input) ? scopes.get(input) : input);
  213. layer.output = layer.output.map((output) => {
  214. const value = scopes.has(output) ? output + '\n' + index.toString() : output;
  215. scopes.set(output, value);
  216. return value;
  217. });
  218. index++;
  219. }
  220. // Graph Inputs
  221. const usedOutputs = new Set();
  222. for (const layer of layers) {
  223. for (const output of layer.output) {
  224. usedOutputs.add(output);
  225. }
  226. }
  227. const unusedInputs = [];
  228. for (const layer of layers) {
  229. for (const input of layer.input) {
  230. if (!usedOutputs.has(input)) {
  231. unusedInputs.push(input);
  232. }
  233. }
  234. }
  235. const values = new Map();
  236. const value = (name, type) => {
  237. if (!values.has(name)) {
  238. values.set(name, new caffe.Value(name, type));
  239. } else if (type) {
  240. throw new caffe.Error("Duplicate value '" + name + "'.");
  241. }
  242. return values.get(name);
  243. };
  244. const nodes = [];
  245. let lastLayer = null;
  246. let lastTop = null;
  247. while (layers.length > 0) {
  248. let layer = layers.shift();
  249. if (layer.output.length == 1 && layer.input.length == 1 &&
  250. layer.output[0].split('\n').shift() == layer.input[0].split('\n').shift() &&
  251. lastLayer &&
  252. lastTop == layer.output[0].split('\n').shift()) {
  253. lastLayer.chain = lastLayer.chain || [];
  254. lastLayer.chain.push(layer);
  255. } else {
  256. if (layer.type == 'Input' || layer.type == 'Data') {
  257. if (layer.input.length == 0 && layer.output.length == 1 &&
  258. layer.input_param && layer.input_param.shape &&
  259. layer.input_param.shape.length == 1 && layer.input_param.shape[0].dim) {
  260. const shape = new caffe.TensorShape(layer.input_param.shape[0].dim.map((dim) => dim.toNumber()));
  261. const type = new caffe.TensorType(null, shape);
  262. this._inputs.push(new caffe.Argument(layer.output[0], [ value(layer.output[0], type) ]));
  263. layer = null;
  264. }
  265. }
  266. if (layer) {
  267. nodes.push(layer);
  268. lastLayer = null;
  269. lastTop = null;
  270. if (layer.output.length == 1) {
  271. lastLayer = layer;
  272. lastTop = layer.output[0].split('\n').shift();
  273. }
  274. }
  275. }
  276. }
  277. if (net.input) {
  278. for (let i = 0; i < net.input.length; i++) {
  279. const input = net.input[i];
  280. if (this._inputs.some((item) => item.name === input)) {
  281. continue;
  282. }
  283. let inputType = null;
  284. if (net.input_shape && i < net.input_shape.length) {
  285. const blobShape = net.input_shape[i];
  286. if (blobShape && blobShape.dim) {
  287. const shape = new caffe.TensorShape(blobShape.dim.map((dim) => dim.toNumber()));
  288. inputType = new caffe.TensorType(null, shape);
  289. }
  290. }
  291. const dim = i * 4;
  292. if (!inputType && net.input_dim && net.input_dim.length >= dim) {
  293. const shape = new caffe.TensorShape(net.input_dim.slice(dim, dim + 4));
  294. inputType = new caffe.TensorType(null, shape);
  295. }
  296. this._inputs.push(new caffe.Argument(input, [ value(input, inputType, null) ]));
  297. }
  298. }
  299. for (const layer of nodes) {
  300. const node = new caffe.Node(metadata, layer, version, value);
  301. if (layer.chain && layer.chain.length > 0) {
  302. for (const chain of layer.chain) {
  303. node.chain.push(new caffe.Node(metadata, chain, version, value));
  304. }
  305. }
  306. this._nodes.push(node);
  307. }
  308. if (this._inputs.length === 0 && unusedInputs.length === 1) {
  309. this._inputs.push(new caffe.Argument(unusedInputs[0], [ value(unusedInputs[0], null) ]));
  310. }
  311. }
  312. get name() {
  313. return this._phase;
  314. }
  315. get type() {
  316. return '';
  317. }
  318. get inputs() {
  319. return this._inputs;
  320. }
  321. get outputs() {
  322. return this._outputs;
  323. }
  324. get nodes() {
  325. return this._nodes;
  326. }
  327. };
  328. caffe.Argument = class {
  329. constructor(name, value) {
  330. this._name = name;
  331. this._value = value;
  332. }
  333. get name() {
  334. return this._name;
  335. }
  336. get value() {
  337. return this._value;
  338. }
  339. };
  340. caffe.Value = class {
  341. constructor(name, type, initializer) {
  342. if (typeof name !== 'string') {
  343. throw new caffe.Error("Invalid value identifier '" + JSON.stringify(name) + "'.");
  344. }
  345. this._name = name;
  346. this._type = type || null;
  347. this._initializer = initializer || null;
  348. }
  349. get name() {
  350. return this._name;
  351. }
  352. get type() {
  353. return this._type;
  354. }
  355. get initializer() {
  356. return this._initializer;
  357. }
  358. };
  359. caffe.Node = class {
  360. constructor(metadata, layer, version, value) {
  361. this._chain = [];
  362. this._attributes = [];
  363. let type;
  364. switch (version) {
  365. case 0: {
  366. this._name = layer.layer.name;
  367. type = layer.layer.type;
  368. break;
  369. }
  370. case 1: {
  371. this._name = layer.name;
  372. type = caffe.Utility.layerType(layer.type);
  373. break;
  374. }
  375. case 2: {
  376. this._name = layer.name;
  377. type = layer.type;
  378. break;
  379. }
  380. default: {
  381. throw new new caffe.Error("Unsupported Caffe version '" + version + "'.");
  382. }
  383. }
  384. this._type = metadata.type(type) || { name: type };
  385. let initializers = [];
  386. switch (version) {
  387. case 0: {
  388. for (const name of Object.keys(layer.layer)) {
  389. if (name != 'type' && name != 'name' && name != 'blobs' && name != 'blobs_lr') {
  390. const value = layer.layer[name];
  391. const attribute = new caffe.Attribute(metadata.attribute(type, name), name, value);
  392. this._attributes.push(attribute);
  393. }
  394. }
  395. initializers = layer.layer.blobs.map((blob) => new caffe.Tensor(blob));
  396. break;
  397. }
  398. case 1:
  399. case 2: {
  400. for (const layer_kind of Object.keys(layer)) {
  401. if (layer_kind.endsWith('_param') || layer_kind == 'transform_param') {
  402. const param = layer[layer_kind];
  403. if (type == 'Deconvolution') {
  404. type = 'Convolution';
  405. }
  406. const prototype = Object.getPrototypeOf(param);
  407. for (const name of Object.keys(param)) {
  408. const defaultValue = prototype[name];
  409. const value = param[name];
  410. const attribute = new caffe.Attribute(metadata.attribute(type, name), name, value, defaultValue);
  411. this._attributes.push(attribute);
  412. }
  413. }
  414. }
  415. if (layer.include && layer.include.length > 0) {
  416. const attribute = new caffe.Attribute(metadata.attribute(type, 'include'), 'include', layer.include);
  417. this._attributes.push(attribute);
  418. }
  419. if (layer.exclude && layer.exclude.length > 0) {
  420. const attribute = new caffe.Attribute(metadata.attribute(type, 'exclude'), 'exclude', layer.exclude);
  421. this._attributes.push(attribute);
  422. }
  423. if (this._type == 'Data' && layer.input_param && layer.input_param.shape) {
  424. const attribute = new caffe.Attribute(metadata.attribute(type, 'shape'), 'shape', layer.input_param.shape);
  425. this._attributes.push(attribute);
  426. }
  427. initializers = layer.blobs.map((blob) => new caffe.Tensor(blob));
  428. break;
  429. }
  430. default: {
  431. throw new caffe.Error("Unsupported Caffe version '" + version + "'.");
  432. }
  433. }
  434. this._inputs = [];
  435. const inputs = layer.input.concat(initializers);
  436. let inputIndex = 0;
  437. if (this._type && this._type.inputs) {
  438. for (const inputDef of this._type.inputs) {
  439. if (inputIndex < inputs.length || inputDef.option != 'optional') {
  440. const count = inputDef.option == 'variadic' ? inputs.length - inputIndex : 1;
  441. const values = inputs.slice(inputIndex, inputIndex + count).filter((input) => input !== '' || inputDef.option != 'optional').map((input) => {
  442. return input instanceof caffe.Tensor ? new caffe.Value('', input.type, input) : value(input, null, null);
  443. });
  444. const argument = new caffe.Argument(inputDef.name, values);
  445. this._inputs.push(argument);
  446. inputIndex += count;
  447. }
  448. }
  449. }
  450. this._inputs.push(...inputs.slice(inputIndex).map((input) => {
  451. return new caffe.Argument(inputIndex.toString(), [
  452. input instanceof caffe.Tensor ? new caffe.Value('', input.type, input) : value(input, null, null)
  453. ]);
  454. }));
  455. this._outputs = [];
  456. const outputs = layer.output;
  457. let outputIndex = 0;
  458. if (this._type && this._type.outputs) {
  459. for (const outputDef of this._type.outputs) {
  460. if (outputIndex < outputs.length) {
  461. const count = (outputDef.option == 'variadic') ? (outputs.length - outputIndex) : 1;
  462. const values = outputs.slice(outputIndex, outputIndex + count).map((output) => value(output, null, null));
  463. const argument = new caffe.Argument(outputDef.name, values);
  464. this._outputs.push(argument);
  465. outputIndex += count;
  466. }
  467. }
  468. }
  469. this._outputs.push(...outputs.slice(outputIndex).map((output, index) => {
  470. return new caffe.Argument((outputIndex + index).toString(), [ value(output, null, null) ]);
  471. }));
  472. }
  473. get type() {
  474. return this._type;
  475. }
  476. get name() {
  477. return this._name;
  478. }
  479. get inputs() {
  480. return this._inputs;
  481. }
  482. get outputs() {
  483. return this._outputs;
  484. }
  485. get attributes() {
  486. return this._attributes;
  487. }
  488. get chain() {
  489. return this._chain;
  490. }
  491. };
  492. caffe.Attribute = class {
  493. constructor(metadata, name, value, defaultValue) {
  494. this._name = name;
  495. this._value = value;
  496. if (metadata && metadata.type) {
  497. this._type = metadata.type;
  498. }
  499. if (value instanceof caffe.proto.BlobShape) {
  500. this._value = new caffe.TensorShape(value.dim.map((dim) => dim.toNumber()));
  501. this._type = 'shape';
  502. }
  503. if (metadata && Object.prototype.hasOwnProperty.call(metadata, 'visible') && !metadata.visible) {
  504. this._visible = false;
  505. }
  506. if (metadata && Object.prototype.hasOwnProperty.call(metadata, 'default')) {
  507. defaultValue = metadata.default;
  508. }
  509. if (defaultValue !== undefined) {
  510. if (this._value == defaultValue) {
  511. this._visible = false;
  512. } else if (Array.isArray(this._value) && Array.isArray(defaultValue)) {
  513. if (this._value.length == defaultValue.length &&
  514. this._value.every((item, index) => {
  515. return item == defaultValue[index];
  516. })) {
  517. this._visible = false;
  518. }
  519. }
  520. }
  521. if (this._type) {
  522. this._value = caffe.Utility.enum(this._type, this._value);
  523. }
  524. }
  525. get type() {
  526. return this._type;
  527. }
  528. get name() {
  529. return this._name;
  530. }
  531. get value() {
  532. return this._value;
  533. }
  534. get visible() {
  535. return this._visible == false ? false : true;
  536. }
  537. };
  538. caffe.Tensor = class {
  539. constructor(blob) {
  540. let shape = [];
  541. if (Object.prototype.hasOwnProperty.call(blob, 'num') &&
  542. Object.prototype.hasOwnProperty.call(blob, 'channels') &&
  543. Object.prototype.hasOwnProperty.call(blob, 'width') &&
  544. Object.prototype.hasOwnProperty.call(blob, 'height')) {
  545. if (blob.num != 1) {
  546. shape.push(blob.num);
  547. }
  548. if (blob.channels != 1) {
  549. shape.push(blob.channels);
  550. }
  551. if (blob.height != 1) {
  552. shape.push(blob.height);
  553. }
  554. if (blob.width != 1) {
  555. shape.push(blob.width);
  556. }
  557. } else if (Object.prototype.hasOwnProperty.call(blob, 'shape')) {
  558. shape = blob.shape.dim.map((dim) => dim.toNumber());
  559. }
  560. let dataType = '?';
  561. if (blob.data.length > 0) {
  562. dataType = 'float32';
  563. this._values = blob.data;
  564. } else if (blob.double_data.length > 0) {
  565. dataType = 'float64';
  566. this._values = blob.double_data;
  567. }
  568. this._type = new caffe.TensorType(dataType, new caffe.TensorShape(shape));
  569. }
  570. get category() {
  571. return 'Blob';
  572. }
  573. get type() {
  574. return this._type;
  575. }
  576. get layout() {
  577. return '|';
  578. }
  579. get values() {
  580. return this._values;
  581. }
  582. };
  583. caffe.TensorType = class {
  584. constructor(dataType, shape) {
  585. this._dataType = dataType;
  586. this._shape = shape;
  587. }
  588. get dataType() {
  589. return this._dataType;
  590. }
  591. get shape() {
  592. return this._shape;
  593. }
  594. toString() {
  595. return (this.dataType || '?') + this._shape.toString();
  596. }
  597. };
  598. caffe.TensorShape = class {
  599. constructor(dimensions) {
  600. this._dimensions = dimensions;
  601. }
  602. get dimensions() {
  603. return this._dimensions;
  604. }
  605. toString() {
  606. return this._dimensions ? ('[' + this._dimensions.map((dimension) => dimension.toString()).join(',') + ']') : '';
  607. }
  608. };
  609. caffe.Utility = class {
  610. static layerType(type) {
  611. type = type || 0;
  612. if (!caffe.Utility._layerTypeMap) {
  613. caffe.Utility._layerTypeMap = new Map();
  614. const known = { 'BNLL': 'BNLL', 'HDF5': 'HDF5', 'LRN': 'LRN', 'RELU': 'ReLU', 'TANH': 'TanH', 'ARGMAX': 'ArgMax', 'MVN': 'MVN', 'ABSVAL': 'AbsVal' };
  615. for (const key of Object.keys(caffe.proto.V1LayerParameter.LayerType)) {
  616. const value = caffe.proto.V1LayerParameter.LayerType[key];
  617. caffe.Utility._layerTypeMap.set(value, key.split('_').map((item) => known[item] || item.substring(0, 1) + item.substring(1).toLowerCase()).join(''));
  618. }
  619. }
  620. return caffe.Utility._layerTypeMap.has(type) ? caffe.Utility._layerTypeMap.get(type) : type.toString();
  621. }
  622. static enum(name, value) {
  623. let type = caffe.proto;
  624. const parts = name.split('.');
  625. while (type && parts.length > 0) {
  626. type = type[parts.shift()];
  627. }
  628. if (type) {
  629. caffe.Utility._enumKeyMap = caffe.Utility._enumKeyMap || new Map();
  630. if (!caffe.Utility._enumKeyMap.has(name)) {
  631. const map = new Map(Object.entries(type).map((pair) => [ pair[1], pair[0] ]));
  632. caffe.Utility._enumKeyMap.set(name, map);
  633. }
  634. const map = caffe.Utility._enumKeyMap.get(name);
  635. if (map.has(value)) {
  636. return map.get(value);
  637. }
  638. }
  639. return value;
  640. }
  641. };
  642. caffe.Error = class extends Error {
  643. constructor(message) {
  644. super(message);
  645. this.name = 'Error loading Caffe model.';
  646. }
  647. };
  648. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  649. module.exports.ModelFactory = caffe.ModelFactory;
  650. }