message.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Experimental
  2. var message = message || {};
  3. message.ModelFactory = class {
  4. match(context) {
  5. const obj = context.open('json');
  6. if (obj && obj.signature && obj.signature.startsWith('netron:')) {
  7. return obj.signature;
  8. }
  9. return '';
  10. }
  11. open(context) {
  12. return Promise.resolve().then(() => {
  13. const obj = context.open('json');
  14. return new message.Model(obj);
  15. });
  16. }
  17. };
  18. message.Model = class {
  19. constructor(data) {
  20. this._format = data.format || '';
  21. this._producer = data.producer || '';
  22. this._version = data.version || '';
  23. this._description = data.description || '';
  24. this._metadata = (data.metadata || []).map((entry) => { return { name: entry.name, value: entry.value }; });
  25. this._graphs = (data.graphs || []).map((graph) => new message.Graph(graph));
  26. }
  27. get format() {
  28. return this._format;
  29. }
  30. get producer() {
  31. return this._producer;
  32. }
  33. get version() {
  34. return this._version;
  35. }
  36. get description() {
  37. return this._description;
  38. }
  39. get metadata() {
  40. return this._metadata;
  41. }
  42. get graphs() {
  43. return this._graphs;
  44. }
  45. };
  46. message.Graph = class {
  47. constructor(data) {
  48. this._inputs = [];
  49. this._outputs = [];
  50. this._nodes = [];
  51. const args = (data.arguments || []).map((argument) => new message.Argument(argument));
  52. for (const parameter of data.inputs || []) {
  53. parameter.arguments = parameter.arguments.map((index) => args[index]).filter((argument) => !argument.initializer);
  54. if (parameter.arguments.filter((argument) => !argument.initializer).length > 0) {
  55. this._inputs.push(new message.Parameter(parameter));
  56. }
  57. }
  58. for (const parameter of data.outputs || []) {
  59. parameter.arguments = parameter.arguments.map((index) => args[index]);
  60. if (parameter.arguments.filter((argument) => !argument.initializer).length > 0) {
  61. this._inputs.push(new message.Parameter(parameter));
  62. }
  63. }
  64. for (const node of data.nodes || []) {
  65. for (const parameter of node.inputs || []) {
  66. parameter.arguments = parameter.arguments.map((index) => args[index]);
  67. }
  68. for (const parameter of node.outputs || []) {
  69. parameter.arguments = parameter.arguments.map((index) => args[index]);
  70. }
  71. this._nodes.push(new message.Node(node));
  72. }
  73. }
  74. get inputs() {
  75. return this._inputs;
  76. }
  77. get outputs() {
  78. return this._outputs;
  79. }
  80. get nodes() {
  81. return this._nodes;
  82. }
  83. };
  84. message.Parameter = class {
  85. constructor(data) {
  86. this._name = data.name || '';
  87. this._arguments = (data.arguments || []);
  88. }
  89. get name() {
  90. return this._name;
  91. }
  92. get arguments() {
  93. return this._arguments;
  94. }
  95. get visible() {
  96. return true;
  97. }
  98. };
  99. message.Argument = class {
  100. constructor(data) {
  101. this._name= data.name || '';
  102. this._type = data.type ? new message.TensorType(data.type) : null;
  103. this._initializer = data.initializer ? new message.Tensor(data.initializer) : null;
  104. }
  105. get name() {
  106. return this._name;
  107. }
  108. get type() {
  109. if (this._initializer && this._initializer.type) {
  110. return this._initializer.type;
  111. }
  112. return this._type;
  113. }
  114. get initializer() {
  115. return this._initializer;
  116. }
  117. };
  118. message.Node = class {
  119. constructor(data) {
  120. this._type = { name: data.type.name, category: data.type.category };
  121. this._name = data.name;
  122. this._inputs = (data.inputs || []).map((input) => new message.Parameter(input));
  123. this._outputs = (data.outputs || []).map((output) => new message.Parameter(output));
  124. this._attributes = (data.attributes || []).map((attribute) => new message.Attribute(attribute));
  125. }
  126. get type() {
  127. return this._type;
  128. }
  129. get name() {
  130. return this._name;
  131. }
  132. get inputs() {
  133. return this._inputs;
  134. }
  135. get outputs() {
  136. return this._outputs;
  137. }
  138. get attributes() {
  139. return this._attributes;
  140. }
  141. };
  142. message.Attribute = class {
  143. constructor(data) {
  144. this._type = data.type || '';
  145. this._name = data.name;
  146. this._value = data.value;
  147. }
  148. get name() {
  149. return this._name;
  150. }
  151. get value() {
  152. return this._value;
  153. }
  154. get type() {
  155. return this._type;
  156. }
  157. };
  158. message.TensorType = class {
  159. constructor(data) {
  160. this._dataType = data.dataType;
  161. this._shape = new message.TensorShape(data.shape);
  162. }
  163. get dataType() {
  164. return this._dataType;
  165. }
  166. get shape() {
  167. return this._shape;
  168. }
  169. toString() {
  170. return this._dataType + this._shape.toString();
  171. }
  172. };
  173. message.TensorShape = class {
  174. constructor(data) {
  175. this._dimensions = data.dimensions;
  176. }
  177. get dimensions() {
  178. return this._dimensions;
  179. }
  180. toString() {
  181. return '[' + this._dimensions.toString() + ']';
  182. }
  183. };
  184. message.Tensor = class {
  185. constructor() {
  186. }
  187. };
  188. message.Error = class extends Error {
  189. constructor(message) {
  190. super(message);
  191. this.name = 'Message Error';
  192. }
  193. };
  194. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  195. module.exports.ModelFactory = message.ModelFactory;
  196. }