message.js 5.5 KB

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