view.js 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  1. /* jshint esversion: 6 */
  2. var view = view || {};
  3. var base = base || require('./base');
  4. var zip = zip || require('./zip');
  5. var gzip = gzip || require('./gzip');
  6. var tar = tar || require('./tar');
  7. var json = json || require('./json');
  8. var protobuf = protobuf || require('./protobuf');
  9. var python = python || require('./python');
  10. var sidebar = sidebar || require('./view-sidebar');
  11. var grapher = grapher || require('./view-grapher');
  12. view.View = class {
  13. constructor(host, id) {
  14. this._host = host;
  15. this._id = id ? ('-' + id) : '';
  16. this._host.initialize(this).then(() => {
  17. this._model = null;
  18. this._selection = [];
  19. this._sidebar = new sidebar.Sidebar(this._host, id);
  20. this._showAttributes = false;
  21. this._showInitializers = true;
  22. this._showNames = false;
  23. this._showHorizontal = false;
  24. this._searchText = '';
  25. this._modelFactoryService = new view.ModelFactoryService(this._host);
  26. this._getElementById('zoom-in-button').addEventListener('click', () => {
  27. this.zoomIn();
  28. });
  29. this._getElementById('zoom-out-button').addEventListener('click', () => {
  30. this.zoomOut();
  31. });
  32. this._getElementById('sidebar').addEventListener('mousewheel', (e) => {
  33. this._preventZoom(e);
  34. }, { passive: true });
  35. this._host.document.addEventListener('keydown', () => {
  36. this.clearSelection();
  37. });
  38. this._host.start();
  39. switch (this._host.environment('zoom')) {
  40. case 'scroll': {
  41. const userAgent = navigator.userAgent.toLowerCase();
  42. const safari = userAgent.indexOf('safari') !== -1 && userAgent.indexOf('chrome') === -1;
  43. const elements = [ 'graph', 'toolbar' ];
  44. for (const id of elements) {
  45. const element = this._getElementById(id);
  46. element.addEventListener('mousewheel', (e) => {
  47. this._mouseWheelHandler(e);
  48. });
  49. element.addEventListener('scroll', (e) => {
  50. this._scrollHandler(e);
  51. });
  52. element.addEventListener('wheel', (e) => {
  53. this._mouseWheelHandler(e);
  54. });
  55. if (safari) {
  56. element.addEventListener('gesturestart', (e) => {
  57. e.preventDefault();
  58. this._gestureZoom = this._zoom;
  59. }, false);
  60. element.addEventListener('gesturechange', (e) => {
  61. e.preventDefault();
  62. this._updateZoom(this._gestureZoom * e.scale, e);
  63. }, false);
  64. element.addEventListener('gestureend', (e) => {
  65. e.preventDefault();
  66. this._updateZoom(this._gestureZoom * e.scale, e);
  67. }, false);
  68. }
  69. else {
  70. element.addEventListener('touchstart', (e) => {
  71. if (e.touches.length === 2) {
  72. this._touchPoints = Array.from(e.touches);
  73. this._touchZoom = this._zoom;
  74. }
  75. }, { passive: true });
  76. element.addEventListener('touchmove', (e) => {
  77. if (Array.isArray(this._touchPoints) && this._touchPoints.length === 2 && e.touches.length === 2) {
  78. const distance = (points) => {
  79. const dx =(points[1].clientX - points[0].clientX);
  80. const dy =(points[1].clientY - points[0].clientY);
  81. return Math.sqrt(dx * dx + dy * dy);
  82. };
  83. const d1 = distance(Array.from(e.touches));
  84. const d2 = distance(this._touchPoints);
  85. if (d2 !== 0) {
  86. const points = this._touchPoints;
  87. const e = {
  88. pageX: (points[1].pageX + points[0].pageX) / 2,
  89. pageY: (points[1].pageY + points[0].pageY) / 2
  90. };
  91. const zoom = d2 === 0 ? d1 : d1 / d2;
  92. this._updateZoom(this._touchZoom * zoom, e);
  93. }
  94. }
  95. }, { passive: true });
  96. element.addEventListener('touchcancel', () => {
  97. delete this._touchPoints;
  98. delete this._touchZoom;
  99. }, { passive: true });
  100. element.addEventListener('touchend', () => {
  101. delete this._touchPoints;
  102. delete this._touchZoom;
  103. }, { passive: true });
  104. }
  105. }
  106. break;
  107. }
  108. case 'd3': {
  109. this._getElementById('toolbar').addEventListener('mousewheel', (e) => {
  110. this._preventZoom(e);
  111. }, { passive: true });
  112. break;
  113. }
  114. }
  115. }).catch((err) => {
  116. this.error(err, null, null);
  117. });
  118. }
  119. show(page) {
  120. if (!page) {
  121. page = (!this._model && !this._activeGraph) ? 'welcome' : 'default';
  122. }
  123. this._host.screen(page);
  124. if (this._sidebar) {
  125. this._sidebar.close();
  126. }
  127. this._host.document.body.setAttribute('class', page);
  128. }
  129. cut() {
  130. this._host.document.execCommand('cut');
  131. }
  132. copy() {
  133. this._host.document.execCommand('copy');
  134. }
  135. paste() {
  136. this._host.document.execCommand('paste');
  137. }
  138. selectAll() {
  139. this._host.document.execCommand('selectall');
  140. }
  141. find() {
  142. if (this._activeGraph) {
  143. this.clearSelection();
  144. const graphElement = this._getElementById('canvas');
  145. const view = new sidebar.FindSidebar(this._host, graphElement, this._activeGraph);
  146. view.on('search-text-changed', (sender, text) => {
  147. this._searchText = text;
  148. });
  149. view.on('select', (sender, selection) => {
  150. this._sidebar.close();
  151. this.select(selection);
  152. });
  153. this._sidebar.open(view.content, 'Find');
  154. view.focus(this._searchText);
  155. }
  156. }
  157. get model() {
  158. return this._model;
  159. }
  160. toggleAttributes() {
  161. this._showAttributes = !this._showAttributes;
  162. this._reload();
  163. }
  164. get showAttributes() {
  165. return this._showAttributes;
  166. }
  167. toggleInitializers() {
  168. this._showInitializers = !this._showInitializers;
  169. this._reload();
  170. }
  171. get showInitializers() {
  172. return this._showInitializers;
  173. }
  174. toggleNames() {
  175. this._showNames = !this._showNames;
  176. this._reload();
  177. }
  178. get showNames() {
  179. return this._showNames;
  180. }
  181. toggleDirection() {
  182. this._showHorizontal = !this._showHorizontal;
  183. this._reload();
  184. }
  185. get showHorizontal() {
  186. return this._showHorizontal;
  187. }
  188. _reload() {
  189. this.show('welcome spinner');
  190. if (this._model && this._activeGraph) {
  191. this._updateGraph(this._model, this._activeGraph).catch((error) => {
  192. if (error) {
  193. this.error(error, 'Graph update failed.', 'welcome');
  194. }
  195. });
  196. }
  197. }
  198. _timeout(time) {
  199. return new Promise((resolve) => {
  200. setTimeout(() => { resolve(); }, time);
  201. });
  202. }
  203. _getElementById(id) {
  204. return this._host.document.getElementById(id + this._id);
  205. }
  206. zoomIn() {
  207. switch (this._host.environment('zoom')) {
  208. case 'scroll':
  209. this._updateZoom(this._zoom * 1.1);
  210. break;
  211. case 'd3':
  212. if (this._zoom) {
  213. this._zoom.scaleBy(d3.select(this._getElementById('canvas')), 1.2);
  214. }
  215. break;
  216. }
  217. }
  218. zoomOut() {
  219. switch (this._host.environment('zoom')) {
  220. case 'scroll':
  221. this._updateZoom(this._zoom * 0.9);
  222. break;
  223. case 'd3':
  224. if (this._zoom) {
  225. this._zoom.scaleBy(d3.select(this._getElementById('canvas')), 0.8);
  226. }
  227. break;
  228. }
  229. }
  230. resetZoom() {
  231. switch (this._host.environment('zoom')) {
  232. case 'scroll':
  233. this._updateZoom(1);
  234. break;
  235. case 'd3':
  236. if (this._zoom) {
  237. this._zoom.scaleTo(d3.select(this._getElementById('canvas')), 1);
  238. }
  239. break;
  240. }
  241. }
  242. _preventZoom(e) {
  243. if (e.shiftKey || e.ctrlKey) {
  244. e.preventDefault();
  245. }
  246. }
  247. _updateZoom(zoom, e) {
  248. const graphElement = this._getElementById('graph');
  249. const min = Math.min(Math.max(graphElement.clientHeight / this._height, 0.2), 1);
  250. zoom = Math.min(zoom, 1.4);
  251. zoom = Math.max(min, zoom);
  252. const scrollLeft = this._scrollLeft || graphElement.scrollLeft;
  253. const scrollTop = this._scrollTop || graphElement.scrollTop;
  254. const x = (e ? e.pageX : (graphElement.clientWidth / 2)) + scrollLeft;
  255. const y = (e ? e.pageY : (graphElement.clientHeight / 2)) + scrollTop;
  256. const canvasElement = this._getElementById('canvas');
  257. canvasElement.style.width = zoom * this._width;
  258. canvasElement.style.height = zoom * this._height;
  259. this._scrollLeft = ((x * zoom) / this._zoom) - (x - scrollLeft);
  260. this._scrollTop = ((y * zoom) / this._zoom) - (y - scrollTop);
  261. this._scrollLeft = Math.max(0, this._scrollLeft);
  262. this._scrollTop = Math.max(0, this._scrollTop);
  263. graphElement.scrollLeft = this._scrollLeft;
  264. graphElement.scrollTop = this._scrollTop;
  265. this._zoom = zoom;
  266. }
  267. _mouseWheelHandler(e) {
  268. if (e.shiftKey || e.ctrlKey) {
  269. this._updateZoom(this._zoom + (e.wheelDelta * 1.0 / 4000.0), e);
  270. e.preventDefault();
  271. }
  272. }
  273. _scrollHandler(e) {
  274. if (this._scrollLeft && e.target.scrollLeft !== Math.floor(this._scrollLeft)) {
  275. delete this._scrollLeft;
  276. }
  277. if (this._scrollTop && e.target.scrollTop !== Math.floor(this._scrollTop)) {
  278. delete this._scrollTop;
  279. }
  280. }
  281. select(selection) {
  282. this.clearSelection();
  283. if (selection && selection.length > 0) {
  284. const graphElement = this._getElementById('graph');
  285. switch (this._host.environment('zoom')) {
  286. case 'd3': {
  287. let x = 0;
  288. let y = 0;
  289. for (const element of selection) {
  290. element.classList.add('select');
  291. this._selection.push(element);
  292. const transform = element.transform.baseVal.consolidate();
  293. const box = element.getBBox();
  294. const ex = transform ? transform.matrix.e : box.x + (box.width / 2);
  295. const ey = transform ? transform.matrix.f : box.y + (box.height / 2);
  296. x += ex;
  297. y += ey;
  298. }
  299. x = x / selection.length;
  300. y = y / selection.length;
  301. const canvasElement = this._getElementById('canvas');
  302. const canvasRect = canvasElement.getBoundingClientRect();
  303. this._zoom.transform(d3.select(canvasElement), d3.zoomIdentity.translate((canvasRect.width / 2) - x, (canvasRect.height / 2) - y));
  304. break;
  305. }
  306. case 'scroll': {
  307. let x = 0;
  308. let y = 0;
  309. for (const element of selection) {
  310. element.classList.add('select');
  311. this._selection.push(element);
  312. const rect = element.getBoundingClientRect();
  313. x += rect.left + (rect.width / 2);
  314. y += rect.top + (rect.height / 2);
  315. }
  316. x = x / selection.length;
  317. y = y / selection.length;
  318. const rect = graphElement.getBoundingClientRect();
  319. const left = (graphElement.scrollLeft + x - rect.left) - (rect.width / 2);
  320. const top = (graphElement.scrollTop + y - rect.top) - (rect.height / 2);
  321. graphElement.scrollTo({ left: left, top: top, behavior: 'smooth' });
  322. break;
  323. }
  324. }
  325. }
  326. }
  327. clearSelection() {
  328. while (this._selection.length > 0) {
  329. const element = this._selection.pop();
  330. element.classList.remove('select');
  331. }
  332. }
  333. error(err, name, screen) {
  334. if (this._sidebar) {
  335. this._sidebar.close();
  336. }
  337. this._host.exception(err, false);
  338. const knowns = [
  339. { name: '', message: /^Invalid argument identifier/, url: 'https://github.com/lutzroeder/netron/issues/540' },
  340. { name: '', message: /^Cannot read property/, url: 'https://github.com/lutzroeder/netron/issues/647' },
  341. { name: '', message: /^Failed to render tensor/, url: 'https://github.com/lutzroeder/netron/issues/681' },
  342. { name: 'Error', message: /^EPERM: operation not permitted/, url: 'https://github.com/lutzroeder/netron/issues/551' },
  343. { name: 'Error', message: /^EACCES: permission denied/, url: 'https://github.com/lutzroeder/netron/issues/504' },
  344. { name: 'RangeError', message: /^Offset is outside the bounds of the DataView/, url: 'https://github.com/lutzroeder/netron/issues/563' },
  345. { name: 'RangeError', message: /^start offset of Int32Array/, url: 'https://github.com/lutzroeder/netron/issues/565' },
  346. { name: 'RangeError', message: /^Maximum call stack size exceeded/, url: 'https://github.com/lutzroeder/netron/issues/589' },
  347. { name: 'RangeError', message: /^Invalid string length/, url: 'https://github.com/lutzroeder/netron/issues/648' },
  348. { name: 'Error loading model.', message: /^Unsupported file content \(/, url: 'https://github.com/lutzroeder/netron/issues/550' },
  349. { name: 'Error loading model.', message: /^Unsupported Protocol Buffers content/, url: 'https://github.com/lutzroeder/netron/issues/593' },
  350. { name: 'Error loading model.', message: /^Unsupported Protocol Buffers text content/, url: 'https://github.com/lutzroeder/netron/issues/594' },
  351. { name: 'Error loading model.', message: /^Unsupported JSON content/, url: 'https://github.com/lutzroeder/netron/issues/595' },
  352. { name: 'Error loading Caffe model.', message: /^File format is not caffe\.NetParameter/, url: 'https://github.com/lutzroeder/netron/issues/563' },
  353. { name: 'Error loading Darknet model.', message: /^Invalid tensor shape/, url: 'https://github.com/lutzroeder/netron/issues/541' },
  354. { name: 'Error loading Keras model.', message: /^Unsupported data object header version/, url: 'https://github.com/lutzroeder/netron/issues/548' },
  355. { name: 'Error loading MNN model.', message: /^File format is not mnn\.Net/, url: 'https://github.com/lutzroeder/netron/issues/746' },
  356. { name: 'Error loading PyTorch model.', message: /^File does not contain root module or state dictionary/, url: 'https://github.com/lutzroeder/netron/issues/543' },
  357. { name: 'Error loading PyTorch model.', message: /^Module does not contain modules/, url: 'https://github.com/lutzroeder/netron/issues/544' },
  358. { name: 'Error loading PyTorch model.', message: /^Failed to resolve module/, url: 'https://github.com/lutzroeder/netron/issues/545' },
  359. { name: 'Error loading PyTorch model.', message: /^Unsupported function/, url: 'https://github.com/lutzroeder/netron/issues/546' },
  360. { name: 'Error loading PyTorch model.', message: /^Unsupported uninitialized argument/, url: 'https://github.com/lutzroeder/netron/issues/547' },
  361. { name: 'Error loading ONNX model.', message: /^File format is not onnx\.ModelProto/, url: 'https://github.com/lutzroeder/netron/issues/549' },
  362. { name: 'Error loading TensorFlow Lite model.', message: /^Offset is outside the bounds of the DataView/, url: 'https://github.com/lutzroeder/netron/issues/563' },
  363. { name: 'Error loading UFF model.', message: /^Unknown attribute/, url: 'https://github.com/lutzroeder/netron/issues/649' }
  364. ];
  365. const known = knowns.find((known) => (known.name.length === 0 || known.name === err.name) && err.message.match(known.message));
  366. const message = err.message + (known ? '\n\nPlease provide information about this issue at ' + known.url + '.' : '');
  367. name = name || err.name;
  368. this._host.error(name, message);
  369. this.show(screen !== undefined ? screen : 'welcome');
  370. if (known) {
  371. this._host.openURL(known.url);
  372. }
  373. }
  374. accept(file) {
  375. return this._modelFactoryService.accept(file);
  376. }
  377. open(context) {
  378. this._host.event('Model', 'Open', 'Size', context.stream ? context.stream.length : 0);
  379. this._sidebar.close();
  380. return this._timeout(2).then(() => {
  381. return this._modelFactoryService.open(context).then((model) => {
  382. const format = model.format;
  383. if (format) {
  384. this._host.event('Model', 'Format', format + (model.producer ? ' (' + model.producer + ')' : ''));
  385. }
  386. return this._timeout(20).then(() => {
  387. const graph = model.graphs.length > 0 ? model.graphs[0] : null;
  388. return this._updateGraph(model, graph);
  389. });
  390. });
  391. });
  392. }
  393. _updateActiveGraph(name) {
  394. this._sidebar.close();
  395. if (this._model) {
  396. const model = this._model;
  397. const graph = model.graphs.filter(graph => name === graph.name).shift();
  398. if (graph) {
  399. this.show('welcome spinner');
  400. this._timeout(200).then(() => {
  401. return this._updateGraph(model, graph).catch((error) => {
  402. if (error) {
  403. this.error(error, 'Graph update failed.', 'welcome');
  404. }
  405. });
  406. });
  407. }
  408. }
  409. }
  410. _updateGraph(model, graph) {
  411. return this._timeout(100).then(() => {
  412. if (graph && graph != this._activeGraph) {
  413. const nodes = graph.nodes;
  414. if (nodes.length > 1400) {
  415. if (!this._host.confirm('Large model detected.', 'This graph contains a large number of nodes and might take a long time to render. Do you want to continue?')) {
  416. this._host.event('Graph', 'Render', 'Skip', nodes.length);
  417. this.show(null);
  418. return null;
  419. }
  420. }
  421. }
  422. return this.renderGraph(model, graph).then(() => {
  423. this._model = model;
  424. this._activeGraph = graph;
  425. this.show('default');
  426. return this._model;
  427. }).catch((error) => {
  428. return this.renderGraph(this._model, this._activeGraph).then(() => {
  429. this.show('default');
  430. throw error;
  431. }).catch(() => {
  432. throw error;
  433. });
  434. });
  435. });
  436. }
  437. renderGraph(model, graph) {
  438. try {
  439. const graphElement = this._getElementById('graph');
  440. const canvasElement = this._getElementById('canvas');
  441. while (canvasElement.lastChild) {
  442. canvasElement.removeChild(canvasElement.lastChild);
  443. }
  444. if (!graph) {
  445. return Promise.resolve();
  446. }
  447. else {
  448. switch (this._host.environment('zoom')) {
  449. case 'scroll':
  450. this._zoom = 1;
  451. canvasElement.style.position = 'static';
  452. canvasElement.style.margin = 'auto';
  453. break;
  454. case 'd3':
  455. this._zoom = null;
  456. canvasElement.style.position = 'absolute';
  457. canvasElement.style.margin = '0';
  458. break;
  459. }
  460. const groups = graph.groups;
  461. const nodes = graph.nodes;
  462. this._host.event('Graph', 'Render', 'Size', nodes.length);
  463. const options = {};
  464. options.nodesep = 25;
  465. options.ranksep = 20;
  466. const rotate = graph.nodes.every((node) => node.inputs.filter((input) => input.arguments.every((argument) => !argument.initializer)).length === 0 && node.outputs.length === 0);
  467. const showHorizontal = rotate ? !this._showHorizontal : this._showHorizontal;
  468. if (showHorizontal) {
  469. options.rankdir = "LR";
  470. }
  471. if (nodes.length > 1500) {
  472. options.ranker = 'longest-path';
  473. }
  474. const viewGraph = new view.Graph(this, groups, options);
  475. const clusters = new Set();
  476. const clusterParentMap = new Map();
  477. if (groups) {
  478. for (const node of nodes) {
  479. if (node.group) {
  480. const path = node.group.split('/');
  481. while (path.length > 0) {
  482. const name = path.join('/');
  483. path.pop();
  484. clusterParentMap.set(name, path.join('/'));
  485. }
  486. }
  487. }
  488. }
  489. for (const node of nodes) {
  490. const viewNode = viewGraph.createNode(node);
  491. const inputs = node.inputs;
  492. for (const input of inputs) {
  493. for (const argument of input.arguments) {
  494. if (argument.name != '' && !argument.initializer) {
  495. viewGraph.createArgument(argument).to(viewNode);
  496. }
  497. }
  498. }
  499. let outputs = node.outputs;
  500. if (node.chain && node.chain.length > 0) {
  501. const chainOutputs = node.chain[node.chain.length - 1].outputs;
  502. if (chainOutputs.length > 0) {
  503. outputs = chainOutputs;
  504. }
  505. }
  506. for (const output of outputs) {
  507. for (const argument of output.arguments) {
  508. if (!argument) {
  509. throw new view.Error("Invalid null argument in '" + model.format + "'.");
  510. }
  511. if (argument.name != '') {
  512. viewGraph.createArgument(argument).from(viewNode);
  513. }
  514. }
  515. }
  516. if (node.controlDependencies && node.controlDependencies.length > 0) {
  517. for (const name of node.controlDependencies) {
  518. viewGraph.createArgument({ name: name, controlDependency: true }).to(viewNode);
  519. }
  520. }
  521. const createCluster = function(name) {
  522. if (!clusters.has(name)) {
  523. viewGraph.setNode({ name: name, rx: 5, ry: 5});
  524. clusters.add(name);
  525. const parent = clusterParentMap.get(name);
  526. if (parent) {
  527. createCluster(parent);
  528. viewGraph.setParent(name, parent);
  529. }
  530. }
  531. };
  532. if (groups) {
  533. let groupName = node.group;
  534. if (groupName && groupName.length > 0) {
  535. if (!clusterParentMap.has(groupName)) {
  536. const lastIndex = groupName.lastIndexOf('/');
  537. if (lastIndex != -1) {
  538. groupName = groupName.substring(0, lastIndex);
  539. if (!clusterParentMap.has(groupName)) {
  540. groupName = null;
  541. }
  542. }
  543. else {
  544. groupName = null;
  545. }
  546. }
  547. if (groupName) {
  548. createCluster(groupName);
  549. viewGraph.setParent(viewNode.name, groupName);
  550. }
  551. }
  552. }
  553. }
  554. for (const input of graph.inputs) {
  555. const viewInput = viewGraph.createInput(input);
  556. for (const argument of input.arguments) {
  557. viewGraph.createArgument(argument).from(viewInput);
  558. }
  559. }
  560. for (const output of graph.outputs) {
  561. const viewOutput = viewGraph.createOutput(output);
  562. for (const argument of output.arguments) {
  563. viewGraph.createArgument(argument).to(viewOutput);
  564. }
  565. }
  566. // Workaround for Safari background drag/zoom issue:
  567. // https://stackoverflow.com/questions/40887193/d3-js-zoom-is-not-working-with-mousewheel-in-safari
  568. const backgroundElement = this._host.document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  569. backgroundElement.setAttribute('id', 'background');
  570. if (this._host.environment('zoom') === 'd3') {
  571. backgroundElement.setAttribute('width', '100%');
  572. backgroundElement.setAttribute('height', '100%');
  573. }
  574. backgroundElement.setAttribute('fill', 'none');
  575. backgroundElement.setAttribute('pointer-events', 'all');
  576. canvasElement.appendChild(backgroundElement);
  577. const originElement = this._host.document.createElementNS('http://www.w3.org/2000/svg', 'g');
  578. originElement.setAttribute('id', 'origin');
  579. canvasElement.appendChild(originElement);
  580. viewGraph.build(this._host.document, originElement);
  581. let svg = null;
  582. switch (this._host.environment('zoom')) {
  583. case 'd3': {
  584. svg = d3.select(canvasElement);
  585. this._zoom = d3.zoom();
  586. this._zoom(svg);
  587. this._zoom.scaleExtent([ 0.1, 1.4 ]);
  588. this._zoom.on('zoom', (event) => {
  589. originElement.setAttribute('transform', event.transform.toString());
  590. });
  591. this._zoom.transform(svg, d3.zoomIdentity);
  592. break;
  593. }
  594. case 'scroll': {
  595. this._zoom = 1;
  596. break;
  597. }
  598. }
  599. return this._timeout(20).then(() => {
  600. viewGraph.layout();
  601. const elements = Array.from(canvasElement.getElementsByClassName('graph-input') || []);
  602. if (elements.length === 0) {
  603. const nodeElements = Array.from(canvasElement.getElementsByClassName('graph-node') || []);
  604. if (nodeElements.length > 0) {
  605. elements.push(nodeElements[0]);
  606. }
  607. }
  608. switch (this._host.environment('zoom')) {
  609. case 'd3': {
  610. const svgSize = canvasElement.getBoundingClientRect();
  611. if (elements && elements.length > 0) {
  612. // Center view based on input elements
  613. const xs = [];
  614. const ys = [];
  615. for (let i = 0; i < elements.length; i++) {
  616. const transform = elements[i].transform.baseVal.consolidate();
  617. if (transform) {
  618. xs.push(transform.matrix.e);
  619. ys.push(transform.matrix.f);
  620. }
  621. }
  622. let x = xs[0];
  623. const y = ys[0];
  624. if (ys.every(y => y === ys[0])) {
  625. x = xs.reduce((a, b) => a + b, 0) / xs.length;
  626. }
  627. const sx = (svgSize.width / (this._showHorizontal ? 4 : 2)) - x;
  628. const sy = (svgSize.height / (this._showHorizontal ? 2 : 4)) - y;
  629. this._zoom.transform(svg, d3.zoomIdentity.translate(sx, sy));
  630. }
  631. else {
  632. this._zoom.transform(svg, d3.zoomIdentity.translate((svgSize.width - viewGraph.graph().width) / 2, (svgSize.height - viewGraph.graph().height) / 2));
  633. }
  634. break;
  635. }
  636. case 'scroll': {
  637. const size = canvasElement.getBBox();
  638. const margin = 100;
  639. const width = Math.ceil(margin + size.width + margin);
  640. const height = Math.ceil(margin + size.height + margin);
  641. originElement.setAttribute('transform', 'translate(' + margin.toString() + ', ' + margin.toString() + ') scale(1)');
  642. backgroundElement.setAttribute('width', width);
  643. backgroundElement.setAttribute('height', height);
  644. this._width = width;
  645. this._height = height;
  646. this._zoom = 1;
  647. delete this._scrollLeft;
  648. delete this._scrollRight;
  649. canvasElement.setAttribute('viewBox', '0 0 ' + width + ' ' + height);
  650. canvasElement.setAttribute('width', width);
  651. canvasElement.setAttribute('height', height);
  652. this._updateZoom(this._zoom);
  653. if (elements && elements.length > 0) {
  654. // Center view based on input elements
  655. const xs = [];
  656. const ys = [];
  657. for (let i = 0; i < elements.length; i++) {
  658. const element = elements[i];
  659. const rect = element.getBoundingClientRect();
  660. xs.push(rect.left + (rect.width / 2));
  661. ys.push(rect.top + (rect.height / 2));
  662. }
  663. let x = xs[0];
  664. const y = ys[0];
  665. if (ys.every(y => y === ys[0])) {
  666. x = xs.reduce((a, b) => a + b, 0) / xs.length;
  667. }
  668. // const canvasRect = graphElement.getBoundingClientRect();
  669. const graphRect = graphElement.getBoundingClientRect();
  670. // const sx = (canvasRect.width / (this._showHorizontal ? 4 : 2)) - x;
  671. // const sy = (canvasRect.height / (this._showHorizontal ? 2 : 4)) - y;
  672. const left = (graphElement.scrollLeft + x - graphRect.left) - (graphRect.width / 2);
  673. const top = (graphElement.scrollTop + y - graphRect.top) - (graphRect.height / 2);
  674. graphElement.scrollTo({ left: left, top: top, behavior: 'auto' });
  675. }
  676. else {
  677. const canvasRect = graphElement.getBoundingClientRect();
  678. const graphRect = graphElement.getBoundingClientRect();
  679. const left = (graphElement.scrollLeft + (canvasRect.width / 2) - graphRect.left) - (graphRect.width / 2);
  680. const top = (graphElement.scrollTop + (canvasRect.height / 2) - graphRect.top) - (graphRect.height / 2);
  681. graphElement.scrollTo({ left: left, top: top, behavior: 'auto' });
  682. }
  683. break;
  684. }
  685. }
  686. return;
  687. });
  688. }
  689. }
  690. catch (error) {
  691. return Promise.reject(error);
  692. }
  693. }
  694. applyStyleSheet(element, name) {
  695. let rules = [];
  696. for (const styleSheet of this._host.document.styleSheets) {
  697. if (styleSheet && styleSheet.href && styleSheet.href.endsWith('/' + name)) {
  698. rules = styleSheet.cssRules;
  699. break;
  700. }
  701. }
  702. const nodes = element.getElementsByTagName('*');
  703. for (const node of nodes) {
  704. for (const rule of rules) {
  705. if (node.matches(rule.selectorText)) {
  706. for (const item of rule.style) {
  707. node.style[item] = rule.style[item];
  708. }
  709. }
  710. }
  711. }
  712. }
  713. export(file) {
  714. const lastIndex = file.lastIndexOf('.');
  715. const extension = (lastIndex != -1) ? file.substring(lastIndex + 1) : '';
  716. if (this._activeGraph && (extension === 'png' || extension === 'svg')) {
  717. const graphElement = this._getElementById('canvas');
  718. const exportElement = graphElement.cloneNode(true);
  719. this.applyStyleSheet(exportElement, 'view-grapher.css');
  720. exportElement.setAttribute('id', 'export');
  721. exportElement.removeAttribute('width');
  722. exportElement.removeAttribute('height');
  723. exportElement.style.removeProperty('opacity');
  724. exportElement.style.removeProperty('display');
  725. const backgroundElement = exportElement.querySelector('#background');
  726. const originElement = exportElement.querySelector('#origin');
  727. originElement.setAttribute('transform', 'translate(0,0) scale(1)');
  728. backgroundElement.removeAttribute('width');
  729. backgroundElement.removeAttribute('height');
  730. const parentElement = graphElement.parentElement;
  731. parentElement.insertBefore(exportElement, graphElement);
  732. const size = exportElement.getBBox();
  733. parentElement.removeChild(exportElement);
  734. parentElement.removeChild(graphElement);
  735. parentElement.appendChild(graphElement);
  736. const delta = (Math.min(size.width, size.height) / 2.0) * 0.1;
  737. const width = Math.ceil(delta + size.width + delta);
  738. const height = Math.ceil(delta + size.height + delta);
  739. originElement.setAttribute('transform', 'translate(' + delta.toString() + ', ' + delta.toString() + ') scale(1)');
  740. exportElement.setAttribute('width', width);
  741. exportElement.setAttribute('height', height);
  742. backgroundElement.setAttribute('width', width);
  743. backgroundElement.setAttribute('height', height);
  744. backgroundElement.setAttribute('fill', '#fff');
  745. const data = new XMLSerializer().serializeToString(exportElement);
  746. if (extension === 'svg') {
  747. const blob = new Blob([ data ], { type: 'image/svg' });
  748. this._host.export(file, blob);
  749. }
  750. if (extension === 'png') {
  751. const imageElement = new Image();
  752. imageElement.onload = () => {
  753. const max = Math.max(width, height);
  754. const scale = Math.min(24000.0 / max, 2.0);
  755. const canvas = this._host.document.createElement('canvas');
  756. canvas.width = Math.ceil(width * scale);
  757. canvas.height = Math.ceil(height * scale);
  758. const context = canvas.getContext('2d');
  759. context.scale(scale, scale);
  760. context.drawImage(imageElement, 0, 0);
  761. canvas.toBlob((blob) => {
  762. if (blob) {
  763. this._host.export(file, blob);
  764. }
  765. else {
  766. const err = new Error();
  767. err.name = 'Error exporting image.';
  768. err.message = 'Image may be too large to render as PNG.';
  769. this._host.exception(err, false);
  770. this._host.error(err.name, err.message);
  771. }
  772. }, 'image/png');
  773. };
  774. imageElement.src = 'data:image/svg+xml;base64,' + this._host.window.btoa(unescape(encodeURIComponent(data)));
  775. }
  776. }
  777. }
  778. showModelProperties() {
  779. if (this._model) {
  780. const modelSidebar = new sidebar.ModelSidebar(this._host, this._model, this._activeGraph);
  781. modelSidebar.on('update-active-graph', (sender, name) => {
  782. this._updateActiveGraph(name);
  783. });
  784. this._sidebar.open(modelSidebar.render(), 'Model Properties');
  785. }
  786. }
  787. showNodeProperties(node, input) {
  788. if (node) {
  789. const nodeSidebar = new sidebar.NodeSidebar(this._host, node);
  790. nodeSidebar.on('show-documentation', (/* sender, e */) => {
  791. this.showNodeDocumentation(node);
  792. });
  793. nodeSidebar.on('export-tensor', (sender, tensor) => {
  794. this._host.require('./numpy').then((numpy) => {
  795. const defaultPath = tensor.name ? tensor.name.split('/').join('_').split(':').join('_').split('.').join('_') : 'tensor';
  796. this._host.save('NumPy Array', 'npy', defaultPath, (file) => {
  797. try {
  798. const dataTypeMap = new Map([
  799. [ 'float16', 'f2' ], [ 'float32', 'f4' ], [ 'float64', 'f8' ],
  800. [ 'int8', 'i1' ], [ 'int16', 'i2'], [ 'int32', 'i4' ], [ 'int64', 'i8' ],
  801. [ 'uint8', 'u1' ], [ 'uint16', 'u2' ], [ 'uint32', 'u4' ], [ 'uint64', 'u8' ],
  802. [ 'qint8', 'i1' ], [ 'qint16', 'i2' ],
  803. [ 'quint8', 'u1' ], [ 'quint16', 'u2' ]
  804. ]);
  805. const array = new numpy.Array();
  806. array.shape = tensor.type.shape.dimensions;
  807. array.data = tensor.value;
  808. array.dataType = dataTypeMap.has(tensor.type.dataType) ? dataTypeMap.get(tensor.type.dataType) : tensor.type.dataType;
  809. const blob = new Blob([ array.toBuffer() ], { type: 'application/octet-stream' });
  810. this._host.export(file, blob);
  811. }
  812. catch (error) {
  813. this.error(error, 'Error saving NumPy tensor.', null);
  814. }
  815. });
  816. }).catch(() => {
  817. });
  818. });
  819. nodeSidebar.on('error', (sender, error) => {
  820. if (this._model) {
  821. error.message = error.message.replace(/\.$/, '') + " in format '" + this._model.format + "'.";
  822. }
  823. this.error(error, null, null);
  824. });
  825. if (input) {
  826. nodeSidebar.toggleInput(input.name);
  827. }
  828. this._sidebar.open(nodeSidebar.render(), 'Node Properties');
  829. }
  830. }
  831. showNodeDocumentation(node) {
  832. const metadata = node.metadata;
  833. if (metadata) {
  834. const documentationSidebar = new sidebar.DocumentationSidebar(this._host, metadata);
  835. documentationSidebar.on('navigate', (sender, e) => {
  836. this._host.openURL(e.link);
  837. });
  838. this._sidebar.push(documentationSidebar.render(), 'Documentation');
  839. }
  840. }
  841. };
  842. view.Graph = class extends grapher.Graph {
  843. constructor(view, compound, options) {
  844. super(compound);
  845. this.view = view;
  846. this._arguments = new Map();
  847. this._nodeKey = 0;
  848. this.setGraph(options);
  849. }
  850. createNode(node) {
  851. const value = new view.Node(this, node);
  852. value.name = this._nodeKey++;
  853. this.setNode(value);
  854. return value;
  855. }
  856. createInput(input) {
  857. const value = new view.Input(this, input);
  858. value.name = this._nodeKey++;
  859. this.setNode(value);
  860. return value;
  861. }
  862. createOutput(output) {
  863. const value = new view.Output(this, output);
  864. value.name = this._nodeKey++;
  865. this.setNode(value);
  866. return value;
  867. }
  868. createArgument(argument) {
  869. const name = argument.name;
  870. if (!this._arguments.has(name)) {
  871. this._arguments.set(name, new view.Argument(this, argument));
  872. }
  873. return this._arguments.get(name);
  874. }
  875. createEdge(from, to) {
  876. const value = new view.Edge(from, to);
  877. return value;
  878. }
  879. build(document, originElement) {
  880. for (const argument of this._arguments.values()) {
  881. argument.build();
  882. }
  883. super.build(document, originElement);
  884. }
  885. };
  886. view.Node = class extends grapher.Node {
  887. constructor(context, value) {
  888. super();
  889. this.context = context;
  890. this.value = value;
  891. view.Node.counter = view.Node.counter || 0;
  892. this.id = 'node-' + (value.name ? 'name-' + value.name : 'id-' + (view.Node.counter++).toString());
  893. this._add(this.value);
  894. }
  895. get class() {
  896. return 'graph-node';
  897. }
  898. _add(node) {
  899. const header = this.header();
  900. const styles = [ 'node-item-type' ];
  901. const metadata = node.metadata;
  902. const category = metadata && metadata.category ? metadata.category : '';
  903. if (category) {
  904. styles.push('node-item-type-' + category.toLowerCase());
  905. }
  906. const type = node.type;
  907. if (typeof type !== 'string' || !type.split) { // #416
  908. const format = this.context.view.model && this.context.view.model.format ? this.context.view.model.format : '?';
  909. throw new view.Error("Unknown node type '" + JSON.stringify(type) + "' in format '" + format + "'.");
  910. }
  911. const content = this.context.view.showNames && (node.name || node.location) ? (node.name || node.location) : type.split('.').pop();
  912. const tooltip = this.context.view.showNames && (node.name || node.location) ? type : (node.name || node.location);
  913. header.add(null, styles, content, tooltip, () => {
  914. this.context.view.showNodeProperties(node, null);
  915. });
  916. if (node.function) {
  917. header.add(null, [ 'node-item-function' ], '+', null, () => {
  918. // debugger;
  919. });
  920. }
  921. const initializers = [];
  922. let hiddenInitializers = false;
  923. if (this.context.view.showInitializers) {
  924. for (const input of node.inputs) {
  925. if (input.visible && input.arguments.length === 1 && input.arguments[0].initializer != null) {
  926. initializers.push(input);
  927. }
  928. if ((!input.visible || input.arguments.length > 1) &&
  929. input.arguments.some((argument) => argument.initializer != null)) {
  930. hiddenInitializers = true;
  931. }
  932. }
  933. }
  934. let sortedAttributes = [];
  935. const attributes = node.attributes || [];
  936. if (this.context.view.showAttributes) {
  937. sortedAttributes = attributes.filter((attribute) => attribute.visible).slice();
  938. }
  939. sortedAttributes.sort((a, b) => {
  940. const au = a.name.toUpperCase();
  941. const bu = b.name.toUpperCase();
  942. return (au < bu) ? -1 : (au > bu) ? 1 : 0;
  943. });
  944. if (initializers.length > 0 || hiddenInitializers || sortedAttributes.length > 0) {
  945. const block = this.list();
  946. block.handler = () => {
  947. this.context.view.showNodeProperties(node);
  948. };
  949. for (const initializer of initializers) {
  950. const argument = initializer.arguments[0];
  951. const type = argument.type;
  952. let shape = '';
  953. let separator = '';
  954. if (type && type.shape && type.shape.dimensions && Array.isArray(type.shape.dimensions)) {
  955. shape = '\u3008' + type.shape.dimensions.map((d) => d ? d : '?').join('\u00D7') + '\u3009';
  956. if (type.shape.dimensions.length === 0 && argument.initializer && !argument.initializer.state) {
  957. try {
  958. shape = argument.initializer.toString();
  959. if (shape && shape.length > 10) {
  960. shape = shape.substring(0, 10) + '\u2026';
  961. }
  962. separator = ' = ';
  963. }
  964. catch (err) {
  965. let type = '?';
  966. try {
  967. type = argument.initializer.type.toString();
  968. }
  969. catch (error) {
  970. // continue regardless of error
  971. }
  972. const format = this.context.view.model && this.context.view.model.format ? this.context.view.model.format : '?';
  973. throw new view.Error("Failed to render tensor of type '" + type + "' in format '" + format + "' (" + err.message + ").");
  974. }
  975. }
  976. }
  977. block.add(argument.name ? 'initializer-' + argument.name : '', initializer.name, shape, type ? type.toString() : '', separator);
  978. }
  979. if (hiddenInitializers) {
  980. block.add(null, '\u3008' + '\u2026' + '\u3009', '', null, '');
  981. }
  982. for (const attribute of sortedAttributes) {
  983. if (attribute.visible) {
  984. let value = sidebar.NodeSidebar.formatAttributeValue(attribute.value, attribute.type);
  985. if (value && value.length > 25) {
  986. value = value.substring(0, 25) + '\u2026';
  987. }
  988. block.add(null, attribute.name, value, attribute.type, ' = ');
  989. }
  990. }
  991. }
  992. if (Array.isArray(node.chain) && node.chain.length > 0) {
  993. for (const innerNode of node.chain) {
  994. this._add(innerNode);
  995. }
  996. }
  997. if (node.inner) {
  998. this._add(node.inner);
  999. }
  1000. }
  1001. };
  1002. view.Input = class extends grapher.Node {
  1003. constructor(context, value) {
  1004. super();
  1005. this.context = context;
  1006. this.value = value;
  1007. view.Input.counter = view.Input.counter || 0;
  1008. const types = value.arguments.map((argument) => argument.type || '').join('\n');
  1009. let name = value.name || '';
  1010. if (name.length > 16) {
  1011. name = name.split('/').pop();
  1012. }
  1013. const header = this.header();
  1014. header.add(null, [ 'graph-item-input' ], name, types, () => this.context.view.showModelProperties());
  1015. this.id = 'input-' + (name ? 'name-' + name : 'id-' + (view.Input.counter++).toString());
  1016. }
  1017. get class() {
  1018. return 'graph-input';
  1019. }
  1020. };
  1021. view.Output = class extends grapher.Node {
  1022. constructor(context, value) {
  1023. super();
  1024. this.context = context;
  1025. this.value = value;
  1026. const types = value.arguments.map((argument) => argument.type || '').join('\n');
  1027. let name = value.name || '';
  1028. if (name.length > 16) {
  1029. name = name.split('/').pop();
  1030. }
  1031. const header = this.header();
  1032. header.add(null, [ 'graph-item-output' ], name, types, () => this.context.view.showModelProperties());
  1033. }
  1034. };
  1035. view.Argument = class {
  1036. constructor(context, argument) {
  1037. this.context = context;
  1038. this._argument = argument;
  1039. }
  1040. from(node) {
  1041. this._from = node;
  1042. }
  1043. to(node) {
  1044. this._to = this._to || [];
  1045. this._to.push(node);
  1046. }
  1047. build() {
  1048. this._edges = this._edges || [];
  1049. if (this._from && this._to) {
  1050. for (const to of this._to) {
  1051. let text = '';
  1052. const type = this._argument.type;
  1053. if (type && type.shape && type.shape.dimensions && type.shape.dimensions.length > 0) {
  1054. text = type.shape.dimensions.map((dimension) => dimension || '?').join('\u00D7');
  1055. }
  1056. if (this.context.view.showNames) {
  1057. text = this._argument.name.split('\n').shift(); // custom argument id
  1058. }
  1059. const edge = this.context.createEdge(this._from, to);
  1060. edge.v = this._from.name;
  1061. edge.w = to.name;
  1062. edge.label = text;
  1063. edge.id = 'edge-' + this._argument.name;
  1064. if (this._argument.controlDependency) {
  1065. edge.class = 'edge-path-control-dependency';
  1066. }
  1067. this.context.setEdge(edge);
  1068. this._edges.push(edge);
  1069. }
  1070. }
  1071. }
  1072. };
  1073. view.Edge = class extends grapher.Edge {
  1074. constructor(from, to) {
  1075. super(from, to);
  1076. }
  1077. };
  1078. view.ModelContext = class {
  1079. constructor(context, formats) {
  1080. this._context = context;
  1081. this._tags = new Map();
  1082. this._content = new Map();
  1083. this._formats = formats || new Map();
  1084. }
  1085. get identifier() {
  1086. return this._context.identifier;
  1087. }
  1088. get stream() {
  1089. return this._context.stream;
  1090. }
  1091. request(file, encoding, base) {
  1092. return this._context.request(file, encoding, base);
  1093. }
  1094. require(id) {
  1095. return this._context.require(id);
  1096. }
  1097. exception(error, fatal) {
  1098. this._context.exception(error, fatal);
  1099. }
  1100. entries(format) {
  1101. return this._formats.get(format) || new Map();
  1102. }
  1103. open(type) {
  1104. if (!this._content.has(type)) {
  1105. this._content.set(type, undefined);
  1106. let reset = false;
  1107. switch (type) {
  1108. case 'json': {
  1109. try {
  1110. reset = true;
  1111. const buffer = this.stream.peek();
  1112. const reader = json.TextReader.create(buffer);
  1113. const obj = reader.read();
  1114. this._content.set(type, obj);
  1115. }
  1116. catch (err) {
  1117. // continue regardless of error
  1118. }
  1119. break;
  1120. }
  1121. case 'pkl': {
  1122. try {
  1123. if (this.stream.length > 2) {
  1124. const stream = this.stream.peek(1)[0] === 0x78 ? zip.Archive.open(this.stream).entries.values().next().value : this.stream;
  1125. const match = (stream) => {
  1126. const head = stream.peek(2);
  1127. if (head[0] === 0x80 && head[1] < 7) {
  1128. return true;
  1129. }
  1130. stream.seek(-1);
  1131. const tail = stream.peek(1);
  1132. stream.seek(0);
  1133. if (tail[0] === 0x2e) {
  1134. return true;
  1135. }
  1136. return false;
  1137. };
  1138. if (match(stream)) {
  1139. reset = true;
  1140. const unpickler = new python.Unpickler(stream);
  1141. const execution = new python.Execution(null, (error, fatal) => {
  1142. const message = error && error.message ? error.message : error.toString();
  1143. this.exception(new view.Error(message.replace(/\.$/, '') + " in '" + this.identifier + "'."), fatal);
  1144. });
  1145. const obj = unpickler.load((name, args) => execution.invoke(name, args));
  1146. this._content.set(type, obj);
  1147. }
  1148. }
  1149. }
  1150. catch (err) {
  1151. // continue regardless of error
  1152. }
  1153. break;
  1154. }
  1155. }
  1156. if (reset) {
  1157. this.stream.seek(0);
  1158. }
  1159. }
  1160. return this._content.get(type);
  1161. }
  1162. tags(type) {
  1163. let tags = this._tags.get(type);
  1164. if (!tags) {
  1165. tags = new Map();
  1166. let reset = false;
  1167. const signatures = [
  1168. // Reject PyTorch models
  1169. [ 0x80, undefined, 0x8a, 0x0a, 0x6c, 0xfc, 0x9c, 0x46, 0xf9, 0x20, 0x6a, 0xa8, 0x50, 0x19 ],
  1170. // Reject TorchScript models
  1171. [ 0x50, 0x4b ]
  1172. ];
  1173. const stream = this.stream;
  1174. if (stream) {
  1175. if (!signatures.some((signature) => signature.length <= stream.length && stream.peek(signature.length).every((value, index) => signature[index] === undefined || signature[index] === value))) {
  1176. try {
  1177. switch (type) {
  1178. case 'pbtxt': {
  1179. reset = true;
  1180. const buffer = stream.peek();
  1181. const decoder = base.TextDecoder.create(buffer);
  1182. let count = 0;
  1183. for (let i = 0; i < 0x100; i++) {
  1184. const c = decoder.decode();
  1185. switch (c) {
  1186. case '\n': case '\r': case '\t': case '\0': break;
  1187. case undefined: i = 0x100; break;
  1188. default: count += c < ' ' ? 1 : 0; break;
  1189. }
  1190. }
  1191. if (count < 4) {
  1192. const buffer = stream.peek();
  1193. const reader = protobuf.TextReader.create(buffer);
  1194. reader.start(false);
  1195. while (!reader.end(false)) {
  1196. const tag = reader.tag();
  1197. tags.set(tag, true);
  1198. if (reader.token() === '{') {
  1199. reader.start();
  1200. while (!reader.end()) {
  1201. const subtag = reader.tag();
  1202. tags.set(tag + '.' + subtag, true);
  1203. reader.skip();
  1204. reader.match(',');
  1205. }
  1206. }
  1207. else {
  1208. reader.skip();
  1209. }
  1210. }
  1211. }
  1212. break;
  1213. }
  1214. case 'pb': {
  1215. reset = true;
  1216. const buffer = stream.peek();
  1217. const reader = protobuf.Reader.create(buffer);
  1218. const length = reader.length;
  1219. while (reader.position < length) {
  1220. const tag = reader.uint32();
  1221. const number = tag >>> 3;
  1222. const type = tag & 7;
  1223. if (type > 5 || number === 0) {
  1224. tags = new Map();
  1225. break;
  1226. }
  1227. tags.set(number, type);
  1228. try {
  1229. reader.skipType(type);
  1230. }
  1231. catch (err) {
  1232. tags = new Map();
  1233. break;
  1234. }
  1235. }
  1236. break;
  1237. }
  1238. }
  1239. }
  1240. catch (error) {
  1241. tags = new Map();
  1242. }
  1243. }
  1244. }
  1245. if (reset) {
  1246. this.stream.seek(0);
  1247. }
  1248. this._tags.set(type, tags);
  1249. }
  1250. return tags;
  1251. }
  1252. };
  1253. view.ArchiveContext = class {
  1254. constructor(host, entries, rootFolder, identifier, stream) {
  1255. this._host = host;
  1256. this._entries = new Map();
  1257. if (entries) {
  1258. for (const entry of entries) {
  1259. if (entry[0].startsWith(rootFolder)) {
  1260. const name = entry[0].substring(rootFolder.length);
  1261. this._entries.set(name, entry[1]);
  1262. }
  1263. }
  1264. }
  1265. this._identifier = identifier.substring(rootFolder.length);
  1266. this._stream = stream;
  1267. }
  1268. get identifier() {
  1269. return this._identifier;
  1270. }
  1271. get stream() {
  1272. return this._stream;
  1273. }
  1274. request(file, encoding, base) {
  1275. if (base === undefined) {
  1276. const stream = this._entries.get(file);
  1277. if (!stream) {
  1278. return Promise.reject(new Error('File not found.'));
  1279. }
  1280. if (encoding) {
  1281. const decoder = new TextDecoder(encoding);
  1282. const buffer = stream.peek();
  1283. const value = decoder.decode(buffer);
  1284. return Promise.resolve(value);
  1285. }
  1286. return Promise.resolve(stream);
  1287. }
  1288. return this._host.request(file, encoding, base);
  1289. }
  1290. require(id) {
  1291. return this._host.require(id);
  1292. }
  1293. exception(error, fatal) {
  1294. this._host.exception(error, fatal);
  1295. }
  1296. };
  1297. view.ArchiveError = class extends Error {
  1298. constructor(message) {
  1299. super(message);
  1300. this.name = 'Error loading archive.';
  1301. }
  1302. };
  1303. view.ModelFactoryService = class {
  1304. constructor(host) {
  1305. this._host = host;
  1306. this._extensions = [];
  1307. this.register('./pytorch', [ '.pt', '.pth', '.pt1', '.pyt', '.pkl', '.pickle', '.h5', '.t7', '.model', '.dms', '.tar', '.ckpt', '.chkpt', '.tckpt', '.bin', '.pb', '.zip', '.nn', '.torchmodel' ]);
  1308. this.register('./onnx', [ '.onnx', '.onn', '.pb', '.pbtxt', '.prototxt', '.model', '.pt', '.pth', '.pkl' ]);
  1309. this.register('./mxnet', [ '.json', '.params' ]);
  1310. this.register('./coreml', [ '.mlmodel', 'manifest.json', 'metadata.json', 'featuredescriptions.json' ]);
  1311. this.register('./caffe', [ '.caffemodel', '.pbtxt', '.prototxt', '.pt', '.txt' ]);
  1312. this.register('./caffe2', [ '.pb', '.pbtxt', '.prototxt' ]);
  1313. this.register('./torch', [ '.t7' ]);
  1314. this.register('./tflite', [ '.tflite', '.lite', '.tfl', '.bin', '.pb', '.tmfile', '.h5', '.model', '.json', '.txt' ]);
  1315. this.register('./tf', [ '.pb', '.meta', '.pbtxt', '.prototxt', '.pt', '.json', '.index', '.ckpt', '.graphdef', /.data-[0-9][0-9][0-9][0-9][0-9]-of-[0-9][0-9][0-9][0-9][0-9]$/, /^events.out.tfevents./ ]);
  1316. this.register('./mediapipe', [ '.pbtxt' ]);
  1317. this.register('./uff', [ '.uff', '.pb', '.pbtxt', '.uff.txt', '.trt', '.engine' ]);
  1318. this.register('./tensorrt', [ '.trt', '.engine', '.model', '.txt', '.uff', '.pb', '.tmfile', '.onnx', '.pth' ]);
  1319. this.register('./npz', [ '.npz', '.npy', '.pkl' ]);
  1320. this.register('./lasagne', [ '.pkl', '.pickle', '.joblib', '.model', '.pkl.z', '.joblib.z' ]);
  1321. this.register('./lightgbm', [ '.txt', '.pkl', '.model' ]);
  1322. this.register('./sklearn', [ '.pkl', '.pickle', '.joblib', '.model', '.meta', '.pb', '.pt', '.h5', '.pkl.z', '.joblib.z' ]);
  1323. this.register('./pickle', [ '.pkl', '.pickle', '.joblib', '.model', '.meta', '.pb', '.pt', '.h5', '.pkl.z', '.joblib.z' ]);
  1324. this.register('./cntk', [ '.model', '.cntk', '.cmf', '.dnn' ]);
  1325. this.register('./paddle', [ '.pdmodel', '.pdparams', '.pdiparams', '.paddle', '__model__', '.pbtxt', '.txt', '.tar', '.tar.gz', 'model', 'params' ]);
  1326. this.register('./bigdl', [ '.model', '.bigdl' ]);
  1327. this.register('./darknet', [ '.cfg', '.model', '.txt', '.weights' ]);
  1328. this.register('./weka', [ '.model' ]);
  1329. this.register('./rknn', [ '.rknn', '.onnx' ]);
  1330. this.register('./dlc', [ '.dlc' ]);
  1331. this.register('./keras', [ '.h5', '.hd5', '.hdf5', '.keras', '.json', '.cfg', '.model', '.pb', '.pth', '.weights', '.pkl', '.lite', '.tflite', '.ckpt' ]);
  1332. this.register('./armnn', [ '.armnn', '.json' ]);
  1333. this.register('./mnn', ['.mnn']);
  1334. this.register('./ncnn', [ '.param', '.bin', '.cfg.ncnn', '.weights.ncnn' ]);
  1335. this.register('./tnn', [ '.tnnproto', '.tnnmodel' ]);
  1336. this.register('./tengine', ['.tmfile']);
  1337. this.register('./mslite', [ '.ms']);
  1338. this.register('./barracuda', [ '.nn' ]);
  1339. this.register('./dnn', [ '.dnn' ]);
  1340. this.register('./xmodel', [ '.xmodel' ]);
  1341. this.register('./openvino', [ '.xml', '.bin' ]);
  1342. this.register('./flux', [ '.bson' ]);
  1343. this.register('./dl4j', [ '.zip' ]);
  1344. this.register('./mlnet', [ '.zip' ]);
  1345. this.register('./acuity', [ '.json' ]);
  1346. this.register('./imgdnn', [ '.dnn', 'params', '.json' ]);
  1347. }
  1348. register(id, extensions) {
  1349. for (const extension of extensions) {
  1350. this._extensions.push({ extension: extension, id: id });
  1351. }
  1352. }
  1353. open(context) {
  1354. return this._openSignature(context).then((context) => {
  1355. const containers = new Map();
  1356. let stream = context.stream;
  1357. const entries = context.entries;
  1358. if (!stream && entries && entries.size > 0) {
  1359. containers.set('', entries);
  1360. }
  1361. else {
  1362. const identifier = context.identifier;
  1363. try {
  1364. const archive = gzip.Archive.open(stream);
  1365. if (archive) {
  1366. const entries = archive.entries;
  1367. containers.set('gzip', entries);
  1368. if (archive.entries.size === 1) {
  1369. stream = archive.entries.values().next().value;
  1370. }
  1371. }
  1372. }
  1373. catch (error) {
  1374. const message = error && error.message ? error.message : error.toString();
  1375. throw new view.ArchiveError(message.replace(/\.$/, '') + " in '" + identifier + "'.");
  1376. }
  1377. try {
  1378. const formats = new Map([ [ 'zip', zip ], [ 'tar', tar ] ]);
  1379. for (const pair of formats) {
  1380. const format = pair[0];
  1381. const module = pair[1];
  1382. const archive = module.Archive.open(stream);
  1383. if (archive) {
  1384. containers.set(format, archive.entries);
  1385. containers.delete('gzip');
  1386. break;
  1387. }
  1388. }
  1389. }
  1390. catch (error) {
  1391. const message = error && error.message ? error.message : error.toString();
  1392. throw new view.ArchiveError(message.replace(/\.$/, '') + " in '" + identifier + "'.");
  1393. }
  1394. }
  1395. const modelContext = new view.ModelContext(context, containers);
  1396. return this._openContext(modelContext).then((model) => {
  1397. if (model) {
  1398. return model;
  1399. }
  1400. if (containers.size > 0) {
  1401. return this._openEntries(containers.values().next().value).then((context) => {
  1402. if (context) {
  1403. return this._openContext(context);
  1404. }
  1405. this._unsupported(modelContext);
  1406. });
  1407. }
  1408. this._unsupported(modelContext);
  1409. });
  1410. });
  1411. }
  1412. _unsupported(context) {
  1413. const identifier = context.identifier;
  1414. const extension = identifier.split('.').pop().toLowerCase();
  1415. for (const module of [ zip, tar, gzip ]) {
  1416. let archive = null;
  1417. try {
  1418. archive = module.Archive.open(context.stream);
  1419. }
  1420. catch (error) {
  1421. // continue regardless of error
  1422. }
  1423. if (archive) {
  1424. throw new view.Error("Archive contains no model files in '" + identifier + "'.", true);
  1425. }
  1426. }
  1427. const knownUnsupportedIdentifiers = new Set([
  1428. 'natives_blob.bin',
  1429. 'v8_context_snapshot.bin',
  1430. 'snapshot_blob.bin',
  1431. 'image_net_labels.json',
  1432. 'package.json',
  1433. 'models.json',
  1434. 'LICENSE.meta',
  1435. 'input_0.pb',
  1436. 'output_0.pb'
  1437. ]);
  1438. const skip = knownUnsupportedIdentifiers.has(identifier);
  1439. const encodings = [
  1440. {
  1441. type: 'pb',
  1442. name: 'Protocol Buffers',
  1443. formats: []
  1444. },
  1445. {
  1446. type: 'pbtxt',
  1447. name: 'Protocol Buffers text',
  1448. formats: [
  1449. { name: 'ImageNet LabelMap data', tags: [ 'entry', 'entry.target_class' ] },
  1450. { name: 'StringIntLabelMapProto data', tags: [ 'item', 'item.id', 'item.name' ] },
  1451. { name: 'caffe.LabelMap data', tags: [ 'item', 'item.name', 'item.label' ] },
  1452. { name: 'Triton Inference Server configuration', tags: [ 'name', 'platform', 'input', 'output' ] },
  1453. { name: 'TensorFlow OpList data', tags: [ 'op', 'op.name', 'op.input_arg' ] },
  1454. { name: 'vitis.ai.proto.DpuModelParamList data', tags: [ 'model', 'model.name', 'model.kernel' ] },
  1455. { name: 'object_detection.protos.DetectionModel data', tags: [ 'model', 'model.ssd' ] },
  1456. { name: 'object_detection.protos.DetectionModel data', tags: [ 'model', 'model.faster_rcnn' ] }
  1457. ]
  1458. },
  1459. {
  1460. type: 'json',
  1461. name: 'JSON',
  1462. formats: [
  1463. { name: 'Netron metadata', tags: [ '[].name', '[].schema' ] },
  1464. { name: 'Netron metadata', tags: [ '[].name', '[].attributes' ] },
  1465. { name: 'Darkflow metadata', tags: [ 'net', 'type', 'model' ] },
  1466. { name: 'keras-yolo2 configuration', tags: [ 'model', 'train', 'valid' ] },
  1467. { name: 'Vulkan SwiftShader ICD manifest', tags: [ 'file_format_version', 'ICD' ] },
  1468. { name: 'DeepLearningExamples configuration', tags: [ 'attention_probs_dropout_prob', 'hidden_act', 'hidden_dropout_prob', 'hidden_size', ] },
  1469. { name: 'NuGet assets', tags: [ 'version', 'targets', 'packageFolders' ] },
  1470. { name: 'NuGet data', tags: [ 'format', 'restore', 'projects' ] },
  1471. { name: 'NPM package', tags: [ 'name', 'version', 'dependencies' ] }
  1472. ]
  1473. }
  1474. ];
  1475. for (const encoding of encodings) {
  1476. const tags = context.tags(encoding.type);
  1477. if (tags.size > 0) {
  1478. for (const format of encoding.formats) {
  1479. if (format.tags.every((tag) => tags.has(tag))) {
  1480. throw new view.Error('Invalid file content. File contains ' + format.name + '.', true);
  1481. }
  1482. }
  1483. const entries = [];
  1484. entries.push(...Array.from(tags).filter((pair) => pair[0].toString().indexOf('.') === -1));
  1485. entries.push(...Array.from(tags).filter((pair) => pair[0].toString().indexOf('.') !== -1));
  1486. const content = entries.map((pair) => pair[1] === true ? pair[0] : pair[0] + ':' + JSON.stringify(pair[1])).join(',');
  1487. throw new view.Error("Unsupported " + encoding.name + " content '" + (content.length > 64 ? content.substring(0, 100) + '...' : content) + "' for extension '." + extension + "' in '" + identifier + "'.", !skip);
  1488. }
  1489. const obj = context.open(encoding.type);
  1490. if (obj) {
  1491. const match = (obj, tag) => {
  1492. if (tag.startsWith('[].')) {
  1493. tag = tag.substring(3);
  1494. return (Array.isArray(obj) && obj.some((item) => Object.prototype.hasOwnProperty.call(item, tag)));
  1495. }
  1496. return Object.prototype.hasOwnProperty.call(obj, tag);
  1497. };
  1498. for (const format of encoding.formats) {
  1499. if (format.tags.every((tag) => match(obj, tag))) {
  1500. throw new view.Error('Invalid file content. File contains ' + format.name + '.', true);
  1501. }
  1502. }
  1503. const content = JSON.stringify(obj).substring(0, 100).replace(/\s/, '').substr(0, 48) + '...';
  1504. throw new view.Error("Unsupported " + encoding.name + " content '" + (content.length > 64 ? content.substring(0, 100) + '...' : content) + "' for extension '." + extension + "' in '" + identifier + "'.", !skip);
  1505. }
  1506. }
  1507. const stream = context.stream;
  1508. stream.seek(0);
  1509. const buffer = stream.peek(Math.min(16, stream.length));
  1510. const bytes = Array.from(buffer).map((c) => (c < 16 ? '0' : '') + c.toString(16)).join('');
  1511. const content = stream.length > 268435456 ? '(' + bytes + ') [' + stream.length.toString() + ']': '(' + bytes + ')';
  1512. throw new view.Error("Unsupported file content " + content + " for extension '." + extension + "' in '" + identifier + "'.", !skip);
  1513. }
  1514. _openContext(context) {
  1515. const modules = this._filter(context).filter((module) => module && module.length > 0);
  1516. const errors = [];
  1517. let match = false;
  1518. const nextModule = () => {
  1519. if (modules.length > 0) {
  1520. const id = modules.shift();
  1521. return this._host.require(id).then((module) => {
  1522. if (!module.ModelFactory) {
  1523. throw new view.Error("Failed to load module '" + id + "'.");
  1524. }
  1525. const modelFactory = new module.ModelFactory();
  1526. if (!modelFactory.match(context)) {
  1527. return nextModule();
  1528. }
  1529. match = true;
  1530. return modelFactory.open(context).then((model) => {
  1531. return model;
  1532. }).catch((error) => {
  1533. const text = " in '" + context.identifier + "'.";
  1534. if (error && !error.message.endsWith(text) && (error.context === undefined || error.context === true)) {
  1535. error.message = error.message.replace(/\.$/, '') + text;
  1536. }
  1537. errors.push(error);
  1538. return nextModule();
  1539. });
  1540. });
  1541. }
  1542. else {
  1543. if (match) {
  1544. if (errors.length === 1) {
  1545. const error = errors[0];
  1546. return Promise.reject(error);
  1547. }
  1548. return Promise.reject(new view.Error(errors.map((err) => err.message).join('\n')));
  1549. }
  1550. return Promise.resolve(null);
  1551. }
  1552. };
  1553. return nextModule();
  1554. }
  1555. _openEntries(entries) {
  1556. try {
  1557. const rootFolder = (files) => {
  1558. const map = files.map((file) => file.split('/').slice(0, -1));
  1559. const at = index => list => list[index];
  1560. const rotate = list => list.length === 0 ? [] : list[0].map((item, index) => list.map(at(index)));
  1561. const equals = list => list.every((item) => item === list[0]);
  1562. const folder = rotate(map).filter(equals).map(at(0)).join('/');
  1563. return folder.length === 0 ? folder : folder + '/';
  1564. };
  1565. const filter = (queue) => {
  1566. let matches = [];
  1567. const nextEntry = () => {
  1568. if (queue.length > 0) {
  1569. const entry = queue.shift();
  1570. const context = new view.ModelContext(new view.ArchiveContext(this._host, null, folder, entry.name, entry.stream));
  1571. let modules = this._filter(context);
  1572. const nextModule = () => {
  1573. if (modules.length > 0) {
  1574. const id = modules.shift();
  1575. return this._host.require(id).then((module) => {
  1576. if (!module.ModelFactory) {
  1577. throw new view.ArchiveError("Failed to load module '" + id + "'.", null);
  1578. }
  1579. const factory = new module.ModelFactory();
  1580. if (factory.match(context)) {
  1581. matches.push(entry);
  1582. modules = [];
  1583. }
  1584. return nextModule();
  1585. });
  1586. }
  1587. else {
  1588. return nextEntry();
  1589. }
  1590. };
  1591. return nextModule();
  1592. }
  1593. else {
  1594. if (matches.length === 0) {
  1595. return Promise.resolve(null);
  1596. }
  1597. // MXNet
  1598. if (matches.length === 2 &&
  1599. matches.some((e) => e.name.toLowerCase().endsWith('.params')) &&
  1600. matches.some((e) => e.name.toLowerCase().endsWith('-symbol.json'))) {
  1601. matches = matches.filter((e) => e.name.toLowerCase().endsWith('.params'));
  1602. }
  1603. // TensorFlow.js
  1604. if (matches.length > 0 &&
  1605. matches.some((e) => e.name.toLowerCase().endsWith('.bin')) &&
  1606. matches.some((e) => e.name.toLowerCase().endsWith('.json'))) {
  1607. matches = matches.filter((e) => e.name.toLowerCase().endsWith('.json'));
  1608. }
  1609. // Paddle
  1610. if (matches.length > 0 &&
  1611. matches.some((e) => e.name.toLowerCase().endsWith('.pdmodel')) &&
  1612. matches.some((e) => e.name.toLowerCase().endsWith('.pdiparams'))) {
  1613. matches = matches.filter((e) => e.name.toLowerCase().endsWith('.pdmodel'));
  1614. }
  1615. if (matches.length > 0 &&
  1616. matches.some((e) => e.name.split('/').pop().toLowerCase() === 'model') &&
  1617. matches.some((e) => e.name.split('/').pop().toLowerCase() === 'params')) {
  1618. matches = matches.filter((e) => e.name.toLowerCase().split('/').pop().toLowerCase() === 'model');
  1619. }
  1620. // TensorFlow Bundle
  1621. if (matches.length > 1 &&
  1622. matches.some((e) => e.name.toLowerCase().endsWith('.data-00000-of-00001'))) {
  1623. matches = matches.filter((e) => !e.name.toLowerCase().endsWith('.data-00000-of-00001'));
  1624. }
  1625. // TensorFlow SavedModel
  1626. if (matches.length === 2 &&
  1627. matches.some((e) => e.name.toLowerCase().split('/').pop() === 'keras_metadata.pb')) {
  1628. matches = matches.filter((e) => e.name.toLowerCase().split('/').pop() !== 'keras_metadata.pb');
  1629. }
  1630. if (matches.length > 1) {
  1631. return Promise.reject(new view.ArchiveError('Archive contains multiple model files.'));
  1632. }
  1633. const match = matches.shift();
  1634. return Promise.resolve(new view.ModelContext(new view.ArchiveContext(this._host, entries, folder, match.name, match.stream)));
  1635. }
  1636. };
  1637. return nextEntry();
  1638. };
  1639. const list = Array.from(entries).map((entry) => {
  1640. return { name: entry[0], stream: entry[1] };
  1641. });
  1642. const files = list.filter((entry) => {
  1643. if (entry.name.endsWith('/')) {
  1644. return false;
  1645. }
  1646. if (entry.name.split('/').pop().startsWith('.')) {
  1647. return false;
  1648. }
  1649. if (!entry.name.startsWith('./') && entry.name.startsWith('.')) {
  1650. return false;
  1651. }
  1652. return true;
  1653. });
  1654. const folder = rootFolder(files.map((entry) => entry.name));
  1655. const queue = files.slice(0).filter((entry) => entry.name.substring(folder.length).indexOf('/') < 0);
  1656. return filter(queue).then((context) => {
  1657. if (context) {
  1658. return Promise.resolve(context);
  1659. }
  1660. const queue = files.slice(0).filter((entry) => entry.name.substring(folder.length).indexOf('/') >= 0);
  1661. return filter(queue);
  1662. });
  1663. }
  1664. catch (error) {
  1665. return Promise.reject(new view.ArchiveError(error.message));
  1666. }
  1667. }
  1668. accept(identifier) {
  1669. const extension = identifier.indexOf('.') === -1 ? '' : identifier.split('.').pop().toLowerCase();
  1670. identifier = identifier.toLowerCase().split('/').pop();
  1671. for (const entry of this._extensions) {
  1672. if ((typeof entry.extension === 'string' && identifier.endsWith(entry.extension)) ||
  1673. (entry.extension instanceof RegExp && entry.extension.exec(identifier))) {
  1674. this._host.event('File', 'Accept', extension, 1);
  1675. return true;
  1676. }
  1677. }
  1678. if (identifier.endsWith('.zip') ||
  1679. identifier.endsWith('.tar') ||
  1680. identifier.endsWith('.tar.gz') ||
  1681. identifier.endsWith('.tgz') ||
  1682. identifier.endsWith('.gz') ||
  1683. identifier.endsWith('.mar') ||
  1684. identifier.endsWith('.model') ||
  1685. identifier.endsWith('.mlpackage')) {
  1686. this._host.event('File', 'Accept', extension, 1);
  1687. return true;
  1688. }
  1689. this._host.event('File', 'Reject', extension, 1);
  1690. return false;
  1691. }
  1692. _filter(context) {
  1693. const identifier = context.identifier.toLowerCase().split('/').pop();
  1694. const list = this._extensions.filter((entry) =>
  1695. (typeof entry.extension === 'string' && identifier.endsWith(entry.extension)) ||
  1696. (entry.extension instanceof RegExp && entry.extension.exec(identifier)));
  1697. return Array.from(new Set(list.map((entry) => entry.id)));
  1698. }
  1699. _openSignature(context) {
  1700. const stream = context.stream;
  1701. if (stream) {
  1702. let empty = true;
  1703. let position = 0;
  1704. while (empty && position < stream.length) {
  1705. const buffer = stream.read(Math.min(4096, stream.length - position));
  1706. position += buffer.length;
  1707. if (!buffer.every((value) => value === 0x00)) {
  1708. empty = false;
  1709. break;
  1710. }
  1711. }
  1712. stream.seek(0);
  1713. if (empty) {
  1714. return Promise.reject(new view.Error('File has no content.', true));
  1715. }
  1716. /* eslint-disable no-control-regex */
  1717. const entries = [
  1718. { name: 'ELF executable', value: /^\x7FELF/ },
  1719. { name: 'PNG image', value: /^\x89PNG/ },
  1720. { name: 'Git LFS header', value: /^version https:\/\/git-lfs.github.com/ },
  1721. { name: 'Git LFS header', value: /^\s*oid sha256:/ },
  1722. { name: 'HTML markup', value: /^\s*<html>/ },
  1723. { name: 'HTML markup', value: /^\s*<!doctype\s*html>/ },
  1724. { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*html>/ },
  1725. { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*HTML>/ },
  1726. { name: 'HTML markup', value: /^\s*<!DOCTYPE\s*HTML\s+(PUBLIC|SYSTEM)?/ },
  1727. { name: 'Unity metadata', value: /^fileFormatVersion:/ },
  1728. { name: 'Python source code', value: /^\s*import[ ]+(os|sys|types|torch|argparse|onnx|numpy|tensorflow)(,|;|\s)/ },
  1729. { name: 'Python source code', value: /^\s*import[ ]+([a-z])+[ ]+as[ ]+/ },
  1730. { name: 'Python source code', value: /^\s*from[ ]+(torch)[ ]+import[ ]+/ },
  1731. { name: 'TSD header', value: /^%TSD-Header-###%/ },
  1732. { name: 'AppleDouble data', value: /^\x00\x05\x16\x07/ },
  1733. { name: 'TensorFlow Hub module', value: /^\x08\x03$/, identifier: 'tfhub_module.pb' }
  1734. ];
  1735. /* eslint-enable no-control-regex */
  1736. const buffer = stream.peek(Math.min(4096, stream.length));
  1737. const text = String.fromCharCode.apply(null, buffer);
  1738. for (const entry of entries) {
  1739. if (text.match(entry.value) && (!entry.identifier || entry.identifier === context.identifier)) {
  1740. return Promise.reject(new view.Error('Invalid file content. File contains ' + entry.name + '.', true));
  1741. }
  1742. }
  1743. }
  1744. return Promise.resolve(context);
  1745. }
  1746. };
  1747. view.Error = class extends Error {
  1748. constructor(message, telemetry) {
  1749. super(message);
  1750. this.name = 'Error loading model.';
  1751. this.telemetry = telemetry;
  1752. this.stack = undefined;
  1753. }
  1754. };
  1755. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  1756. module.exports.View = view.View;
  1757. module.exports.ModelFactoryService = view.ModelFactoryService;
  1758. }