server.js 5.7 KB

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