tf.js 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414
  1. /* jshint esversion: 6 */
  2. // Experimental
  3. var tf = tf || {};
  4. var base = base || require('./base');
  5. var json = json || require('./json');
  6. var protobuf = protobuf || require('./protobuf');
  7. tf.ModelFactory = class {
  8. match(context) {
  9. return this._format(context).length > 0;
  10. }
  11. open(context) {
  12. return context.require('./tf-proto').then(() => {
  13. tf.proto = protobuf.get('tf');
  14. const openModel = (saved_model, format, producer, bundle) => {
  15. return tf.Metadata.open(context).then((metadata) => {
  16. return new tf.Model(metadata, saved_model, format, producer, bundle);
  17. });
  18. };
  19. const openSavedModel = (saved_model, format, producer) => {
  20. if (saved_model.meta_graphs.length === 1 &&
  21. saved_model.meta_graphs[0].object_graph_def &&
  22. saved_model.meta_graphs[0].object_graph_def.nodes &&
  23. saved_model.meta_graphs[0].object_graph_def.nodes.length > 0) {
  24. const identifier = 'variables/variables.index';
  25. return context.request(identifier, null).then((stream) => {
  26. return tf.TensorBundle.open(stream, identifier, context).then((bundle) => {
  27. return openModel(saved_model, format, producer, bundle);
  28. });
  29. }).catch(() => {
  30. return openModel(saved_model, format, producer, null);
  31. });
  32. }
  33. if (saved_model && saved_model.meta_graphs && saved_model.meta_graphs.length > 0 &&
  34. saved_model.meta_graphs[0].meta_info_def &&
  35. Object.prototype.hasOwnProperty.call(saved_model.meta_graphs[0].meta_info_def, 'tensorflow_version')) {
  36. producer = 'TensorFlow v' + saved_model.meta_graphs[0].meta_info_def.tensorflow_version;
  37. }
  38. return openModel(saved_model, format, producer, null);
  39. };
  40. const openBundle = (context, stream, identifier) => {
  41. return tf.TensorBundle.open(stream, identifier, context).then((bundle) => {
  42. return openModel(null, 'TensorFlow Tensor Bundle v' + bundle.format.toString(), null, bundle);
  43. }).catch((error) => {
  44. context.exception(error, false);
  45. const message = error && error.message ? error.message : error.toString();
  46. throw new tf.Error(message.replace(/\.$/, '') + " in '" + identifier + "'.");
  47. });
  48. };
  49. const openData = (context) => {
  50. const identifier = context.identifier;
  51. const base = identifier.split('.');
  52. base.pop();
  53. const file = base.join('.') + '.index';
  54. return context.request(file, null).then((stream) => {
  55. return open(stream, file, context);
  56. }).catch((/* error */) => {
  57. const file = base.join('.') + '.ckpt';
  58. return context.request(file, null).then((stream) => {
  59. open(stream, file, context);
  60. });
  61. });
  62. };
  63. const openEventFile = () => {
  64. let format = 'TensorFlow Event File';
  65. let producer = null;
  66. const stream = context.stream;
  67. const eventFileReader = tf.EventFileReader.open(stream);
  68. const saved_model = new tf.proto.tensorflow.SavedModel();
  69. for (;;) {
  70. const event = eventFileReader.read();
  71. if (!event) {
  72. break;
  73. }
  74. switch (event.what) {
  75. case 'file_version': {
  76. const formats = new Map([
  77. [ 'brain.Event:1', 'TensorFlow Event File v1' ],
  78. [ 'brain.Event:2', 'TensorFlow Event File v2' ]
  79. ]);
  80. if (!formats.has(event.file_version)) {
  81. throw new tf.Error("Unknown event file version '" + event.file_version + "'.");
  82. }
  83. format = formats.get(event.file_version);
  84. break;
  85. }
  86. case 'graph_def': {
  87. const buffer = event.graph_def;
  88. const reader = protobuf.BinaryReader.open(buffer);
  89. const graph_def = tf.proto.tensorflow.GraphDef.decode(reader);
  90. const meta_graph = new tf.proto.tensorflow.MetaGraphDef();
  91. meta_graph.meta_info_def = new tf.proto.tensorflow.MetaGraphDef.MetaInfoDef();
  92. meta_graph.meta_info_def.any_info = event.wall_time.toString();
  93. meta_graph.graph_def = graph_def;
  94. saved_model.meta_graphs.push(meta_graph);
  95. break;
  96. }
  97. }
  98. }
  99. if (saved_model.meta_graphs.every((meta_graph) => meta_graph.graph_def.node.every((node) => node.op.startsWith('aten::') || node.op.startsWith('prim::') || node.op === 'IO Node'))) {
  100. producer = 'PyTorch';
  101. const openPyTorchMetadata = (context, saved_model) => {
  102. return context.request('pytorch-metadata.json', 'utf-8', null).then((data) => {
  103. const metadata = new Map();
  104. for (const item of JSON.parse(data)) {
  105. const index = item.name.indexOf(':');
  106. const key = (index !== -1) ? item.name.substring(0, index) : item.name;
  107. const name = key.replace(/^torch\./, 'aten::');
  108. if (!metadata.has(name)) {
  109. metadata.set(name, []);
  110. }
  111. metadata.get(name).push(item);
  112. }
  113. for (const meta_graph of saved_model.meta_graphs) {
  114. for (const node of meta_graph.graph_def.node) {
  115. node.__metadata__ = Array.from(metadata.get(node.op) || []);
  116. }
  117. }
  118. return saved_model;
  119. }).catch(() => {
  120. return saved_model;
  121. });
  122. };
  123. return openPyTorchMetadata(context, saved_model).then((saved_model) => {
  124. return openModel(saved_model, format, producer, null);
  125. });
  126. }
  127. return openSavedModel(saved_model, format, producer);
  128. };
  129. const openJson = (context) => {
  130. try {
  131. const obj = context.open('json');
  132. const format = 'TensorFlow.js ' + (obj.format || 'graph-model');
  133. const producer = obj.convertedBy || obj.generatedBy || '';
  134. const meta_graph = new tf.proto.tensorflow.MetaGraphDef();
  135. meta_graph.graph_def = tf.JsonReader.decodeGraphDef(obj.modelTopology);
  136. const saved_model = new tf.proto.tensorflow.SavedModel();
  137. saved_model.meta_graphs.push(meta_graph);
  138. const nodes = new Map();
  139. for (const node of meta_graph.graph_def.node) {
  140. node.input = node.input || [];
  141. if (node.op === 'Const') {
  142. nodes.set(node.name, node);
  143. }
  144. }
  145. const shards = new Map();
  146. const manifests = Array.isArray(obj.weightsManifest) ? obj.weightsManifest : [];
  147. for (const manifest of manifests) {
  148. for (const path of manifest.paths) {
  149. if (!shards.has(path)) {
  150. shards.set(path, context.request(path, null));
  151. }
  152. }
  153. }
  154. const openShards = (shards) => {
  155. const dtype_size_map = new Map([ [ 'float16', 2 ], [ 'float32', 4 ], [ 'float64', 8 ], [ 'int8', 1 ], [ 'int16', 2 ], [ 'int32', 4 ], [ 'int64', 8 ], [ 'uint8', 1 ], [ 'uint16', 2 ], [ 'uint32', 4 ], [ 'uint64', 8 ], [ 'bool', 1 ] ]);
  156. for (const manifest of manifests) {
  157. let buffer = null;
  158. if (Array.isArray(manifest.paths) && manifest.paths.length > 0 && manifest.paths.every((path) => shards.has(path))) {
  159. const list = manifest.paths.map((path) => shards.get(path));
  160. const size = list.reduce((a, b) => a + b.length, 0);
  161. buffer = new Uint8Array(size);
  162. let offset = 0;
  163. for (const item of list) {
  164. buffer.set(item, offset);
  165. offset += item.length;
  166. }
  167. }
  168. let offset = 0;
  169. for (const weight of manifest.weights) {
  170. const dtype = weight.quantization && weight.quantization.dtype ? weight.quantization.dtype : weight.dtype;
  171. if (!dtype_size_map.has(dtype)) {
  172. throw new tf.Error("Unknown weight data type size '" + dtype + "'.");
  173. }
  174. const itemsize = dtype_size_map.get(dtype);
  175. const size = weight.shape.reduce((a, b) => a * b, 1);
  176. const length = itemsize * size;
  177. const tensor_content = buffer ? buffer.slice(offset, offset + length) : null;
  178. offset += length;
  179. if (nodes.has(weight.name)) {
  180. const node = nodes.get(weight.name);
  181. node.attr.value.tensor.dtype = tf.Utility.dataTypeKey(dtype);
  182. node.attr.value.tensor.tensor_content = tensor_content;
  183. }
  184. }
  185. }
  186. return openSavedModel(saved_model, format, producer, null);
  187. };
  188. return Promise.all(shards.values()).then((streams) => {
  189. for (const key of shards.keys()) {
  190. shards.set(key, streams.shift().peek());
  191. }
  192. return openShards(shards);
  193. }).catch(() => {
  194. shards.clear();
  195. return openShards(shards);
  196. });
  197. }
  198. catch (error) {
  199. throw new tf.Error('File text format is not TensorFlow.js graph-model (' + error.message + ').');
  200. }
  201. };
  202. const openTextGraphDef = (context) => {
  203. try {
  204. const stream = context.stream;
  205. const reader = protobuf.TextReader.open(stream);
  206. const graph_def = tf.proto.tensorflow.GraphDef.decodeText(reader);
  207. const meta_graph = new tf.proto.tensorflow.MetaGraphDef();
  208. meta_graph.graph_def = graph_def;
  209. const saved_model = new tf.proto.tensorflow.SavedModel();
  210. saved_model.meta_graphs.push(meta_graph);
  211. const format = 'TensorFlow Graph';
  212. return openSavedModel(saved_model, format, null);
  213. }
  214. catch (error) {
  215. const message = error && error.message ? error.message : error.toString();
  216. throw new tf.Error('File text format is not tensorflow.GraphDef (' + message.replace(/\.$/, '') + ').');
  217. }
  218. };
  219. const openTextMetaGraphDef = (context) => {
  220. try {
  221. const stream = context.stream;
  222. const reader = protobuf.TextReader.open(stream);
  223. const meta_graph = tf.proto.tensorflow.MetaGraphDef.decodeText(reader);
  224. const saved_model = new tf.proto.tensorflow.SavedModel();
  225. saved_model.meta_graphs.push(meta_graph);
  226. const format = 'TensorFlow MetaGraph';
  227. return openSavedModel(saved_model, format, null);
  228. }
  229. catch (error) {
  230. throw new tf.Error('File text format is not tensorflow.MetaGraphDef (' + error.message + ').');
  231. }
  232. };
  233. const openTextSavedModel = (context) => {
  234. try {
  235. const stream = context.stream;
  236. const reader = protobuf.TextReader.open(stream);
  237. const saved_model = tf.proto.tensorflow.SavedModel.decodeText(reader);
  238. let format = 'TensorFlow Saved Model';
  239. if (saved_model && Object.prototype.hasOwnProperty.call(saved_model, 'saved_model_schema_version')) {
  240. format = format + ' v' + saved_model.saved_model_schema_version.toString();
  241. }
  242. return openSavedModel(saved_model, format, null);
  243. }
  244. catch (error) {
  245. throw new tf.Error('File text format is not tensorflow.SavedModel (' + error.message + ').');
  246. }
  247. };
  248. const openBinaryProto = (stream, identifier) => {
  249. let saved_model = null;
  250. let format = null;
  251. const extension = identifier.split('.').pop().toLowerCase();
  252. try {
  253. if (identifier.endsWith('saved_model.pb')) {
  254. const reader = protobuf.BinaryReader.open(stream);
  255. saved_model = tf.proto.tensorflow.SavedModel.decode(reader);
  256. format = 'TensorFlow Saved Model';
  257. if (saved_model && Object.prototype.hasOwnProperty.call(saved_model, 'saved_model_schema_version')) {
  258. format = format + ' v' + saved_model.saved_model_schema_version.toString();
  259. }
  260. }
  261. }
  262. catch (error) {
  263. const signature = [ 0x08, 0x01, 0x12 ];
  264. if (signature.length < stream.length && stream.peek(3).every((value, index) => value === signature[index])) {
  265. const message = error && error.message ? error.message : error.toString();
  266. throw new tf.Error('File format is not tensorflow.SavedModel (' + message.replace(/\.$/, '') + ').');
  267. }
  268. }
  269. try {
  270. if (!saved_model && extension == 'meta') {
  271. const reader = protobuf.BinaryReader.open(stream);
  272. const meta_graph = tf.proto.tensorflow.MetaGraphDef.decode(reader);
  273. saved_model = new tf.proto.tensorflow.SavedModel();
  274. saved_model.meta_graphs.push(meta_graph);
  275. format = 'TensorFlow MetaGraph';
  276. }
  277. }
  278. catch (error) {
  279. const message = error && error.message ? error.message : error.toString();
  280. throw new tf.Error('File format is not tensorflow.MetaGraphDef (' + message.replace(/\.$/, '') + ').');
  281. }
  282. try {
  283. if (!saved_model) {
  284. const reader = protobuf.BinaryReader.open(stream);
  285. const graph_def = tf.proto.tensorflow.GraphDef.decode(reader);
  286. const meta_graph = new tf.proto.tensorflow.MetaGraphDef();
  287. meta_graph.graph_def = graph_def;
  288. saved_model = new tf.proto.tensorflow.SavedModel();
  289. saved_model.meta_graphs.push(meta_graph);
  290. format = 'TensorFlow Graph';
  291. }
  292. }
  293. catch (error) {
  294. const message = error && error.message ? error.message : error.toString();
  295. throw new tf.Error('File format is not tensorflow.GraphDef (' + message.replace(/\.$/, '') + ').');
  296. }
  297. return openSavedModel(saved_model, format, null);
  298. };
  299. const openSavedMetadata = (context) => {
  300. /*
  301. const stream = context.stream;
  302. const reader = protobuf.BinaryReader.open(stream);
  303. const saved_metadata = tf.proto.third_party.tensorflow.python.keras.protobuf.SavedMetadata.decode(reader);
  304. debugger;
  305. */
  306. const identifier = 'saved_model.pb';
  307. return context.request(identifier, null).then((stream) => {
  308. return openBinaryProto(stream, identifier);
  309. });
  310. };
  311. switch (this._format(context)) {
  312. case 'tf.bundle':
  313. return openBundle(context, context.stream, context.identifier);
  314. case 'tf.data':
  315. return openData(context);
  316. case 'tf.events':
  317. return openEventFile(context);
  318. case 'tf.json':
  319. return openJson(context);
  320. case 'tf.pbtxt.GraphDef':
  321. return openTextGraphDef(context);
  322. case 'tf.pbtxt.MetaGraphDef':
  323. return openTextMetaGraphDef(context);
  324. case 'tf.pbtxt.SavedModel':
  325. return openTextSavedModel(context);
  326. case 'tf.pb':
  327. return openBinaryProto(context.stream, context.identifier);
  328. case 'tf.pb.keras.SavedMetadata':
  329. return openSavedMetadata(context);
  330. default:
  331. throw new tf.Error('Unknown format.');
  332. }
  333. });
  334. }
  335. _format(context) {
  336. const identifier = context.identifier;
  337. const extension = identifier.split('.').pop().toLowerCase();
  338. if (extension === 'meta') {
  339. const tags = context.tags('pb');
  340. if (tags.size !== 0) {
  341. return 'tf.pb';
  342. }
  343. }
  344. if (extension === 'pbtxt' || extension === 'prototxt' || extension === 'pt') {
  345. if (identifier.endsWith('predict_net.pbtxt') || identifier.endsWith('predict_net.prototxt') ||
  346. identifier.endsWith('init_net.pbtxt') || identifier.endsWith('init_net.prototxt')) {
  347. return '';
  348. }
  349. const tags = context.tags('pbtxt');
  350. if (['input_stream', 'output_stream', 'input_side_packet', 'output_side_packet'].some((key) => tags.has(key) || tags.has('node.' + key))) {
  351. return '';
  352. }
  353. if (tags.has('saved_model_schema_version') || tags.has('meta_graphs')) {
  354. return 'tf.pbtxt.SavedModel';
  355. }
  356. if (tags.has('graph_def')) {
  357. return 'tf.pbtxt.MetaGraphDef';
  358. }
  359. if (tags.has('node')) {
  360. return 'tf.pbtxt.GraphDef';
  361. }
  362. }
  363. if (extension === 'pb' || extension === 'pbtxt' || extension === 'prototxt' || extension === 'graphdef') {
  364. if (identifier.endsWith('predict_net.pb') || identifier.endsWith('init_net.pb')) {
  365. return '';
  366. }
  367. if (identifier == 'tfhub_module.pb') {
  368. const stream = context.stream;
  369. const signature = [ 0x08, 0x03 ];
  370. if (signature.length === stream.length && stream.peek(signature.length).every((value, index) => value === signature[index])) {
  371. return '';
  372. }
  373. }
  374. const tags = context.tags('pb');
  375. if (tags.size > 0) {
  376. if (!Array.from(tags).some((pair) => pair[0] >= 5 || pair[1] === 5)) {
  377. if (tags.size === 1 && tags.get(1) === 2) {
  378. const tags = context.tags('pb+');
  379. const match = (tags, schema) => {
  380. for (const pair of schema) {
  381. const key = pair[0];
  382. const inner = pair[1];
  383. if (!tags.has(key)) {
  384. continue;
  385. }
  386. else if (inner === false) {
  387. return false;
  388. }
  389. if (Array.isArray(inner)) {
  390. const value = tags.get(key);
  391. if (!(value instanceof Map) || !match(value, inner)) {
  392. return false;
  393. }
  394. }
  395. else if (inner !== tags.get(key)) {
  396. return false;
  397. }
  398. }
  399. return true;
  400. };
  401. // mediapipe.BoxDetectorIndex
  402. if (match(tags, [[1,[[1,[[1,[[1,5],[2,5],[3,5],[4,5],[6,0],[7,5],[8,5],[10,5],[11,0],[12,0]]],[2,5],[3,[]]]],[2,false],[3,false],[4,false],[5,false]]],[2,false],[3,false]] )) {
  403. return '';
  404. }
  405. // third_party.tensorflow.python.keras.protobuf.SavedMetadata
  406. if (match(tags, [[1,[[1,[[1,0],[2,0]]],[2,0],[3,2],[4,2],[5,2]]]])) {
  407. return 'tf.pb.keras.SavedMetadata';
  408. }
  409. }
  410. if (tags.get(1) !== 2) {
  411. return 'tf.pb';
  412. }
  413. const decode = (buffer, value) => {
  414. const reader = protobuf.BinaryReader.open(buffer);
  415. const length = reader.length;
  416. while (reader.position < length) {
  417. const tag = reader.uint32();
  418. const number = tag >>> 3;
  419. const type = tag & 7;
  420. if (value === number) {
  421. return type === 2 ? reader.bytes() : null;
  422. }
  423. else {
  424. reader.skipType(type);
  425. }
  426. }
  427. return null;
  428. };
  429. const stream = context.stream;
  430. const buffer = stream.peek();
  431. const nodeBuffer = decode(buffer, 1);
  432. if (nodeBuffer) {
  433. const nameBuffer = decode(nodeBuffer, 1);
  434. if (nameBuffer) {
  435. const decoder = new TextDecoder('utf-8');
  436. const name = decoder.decode(nameBuffer);
  437. if (Array.from(name).filter((c) => c <= ' ').length < 256) {
  438. return 'tf.pb';
  439. }
  440. }
  441. }
  442. }
  443. }
  444. else {
  445. const tags = context.tags('pbtxt');
  446. if (['input_stream', 'output_stream', 'input_side_packet', 'output_side_packet'].some((key) => tags.has(key) || tags.has('node.' + key))) {
  447. return false;
  448. }
  449. if (tags.has('node')) {
  450. return 'tf.pbtxt.GraphDef';
  451. }
  452. if (tags.has('graph_def')) {
  453. return 'tf.pbtxt.MetaGraphDef';
  454. }
  455. if (tags.has('saved_model_schema_version') || tags.has('meta_graphs')) {
  456. return 'tf.pbtxt.SavedModel';
  457. }
  458. }
  459. }
  460. if (extension === 'json') {
  461. const obj = context.open('json');
  462. if (obj && obj.modelTopology && (obj.format === 'graph-model' || Array.isArray(obj.modelTopology.node))) {
  463. return 'tf.json';
  464. }
  465. }
  466. if (extension === 'index' || extension === 'ckpt') {
  467. const stream = context.stream;
  468. if (stream.length > 8) {
  469. stream.seek(-8);
  470. const buffer = stream.read(8);
  471. stream.seek(0);
  472. const signature = [ 0x57, 0xfb, 0x80, 0x8b, 0x24, 0x75, 0x47, 0xdb ];
  473. if (buffer.every((value, index) => value === signature[index])) {
  474. return 'tf.bundle';
  475. }
  476. }
  477. }
  478. if (/.data-[0-9][0-9][0-9][0-9][0-9]-of-[0-9][0-9][0-9][0-9][0-9]$/.exec(identifier)) {
  479. return 'tf.data';
  480. }
  481. if (/^events.out.tfevents./.exec(identifier)) {
  482. const stream = context.stream;
  483. if (tf.EventFileReader.open(stream)) {
  484. return 'tf.events';
  485. }
  486. }
  487. return '';
  488. }
  489. };
  490. tf.Model = class {
  491. constructor(metadata, model, format, producer, bundle) {
  492. this._format = format;
  493. this._producer = producer || '';
  494. this._graphs = [];
  495. if (model) {
  496. for (let i = 0; i < model.meta_graphs.length; i++) {
  497. const meta_graph = model.meta_graphs[i];
  498. const name = (meta_graph.meta_info_def && meta_graph.meta_info_def.any_info) ? meta_graph.meta_info_def.any_info.toString() : ((model.meta_graphs.length > 1) ? i.toString() : '-');
  499. const graph = new tf.Graph(metadata, meta_graph, name, bundle);
  500. this._graphs.push(graph);
  501. }
  502. }
  503. else {
  504. const graph = new tf.Graph(metadata, null, '', bundle);
  505. this._graphs.push(graph);
  506. }
  507. }
  508. get format() {
  509. return this._format;
  510. }
  511. get producer() {
  512. return this._producer;
  513. }
  514. get description() {
  515. return null;
  516. }
  517. get graphs() {
  518. return this._graphs;
  519. }
  520. };
  521. tf.Graph = class {
  522. constructor(metadata, meta_graph, name, bundle) {
  523. this._name = name;
  524. this._inputs = [];
  525. this._outputs = [];
  526. this._nodes = [];
  527. this._version = null;
  528. if (meta_graph && meta_graph.graph_def) {
  529. const graph = meta_graph.graph_def;
  530. if (graph.versions) {
  531. this._version = 'v' + graph.versions.producer.toString();
  532. }
  533. else if (graph.version) {
  534. this._version = graph.version;
  535. }
  536. else if (meta_graph.meta_info_def && meta_graph.meta_info_def.tensorflow_version) {
  537. this._version = meta_graph.meta_info_def.tensorflow_version;
  538. }
  539. if (meta_graph.meta_info_def && meta_graph.meta_info_def.tags) {
  540. this._tags = meta_graph.meta_info_def.tags.join(', ');
  541. }
  542. metadata = new tf.GraphMetadata(metadata, graph.library);
  543. const nodes = graph.node || [];
  544. const context = tf.Utility.createGraph(metadata, nodes);
  545. this._nodes = context.nodes;
  546. this._inputs = context.inputs;
  547. this._outputs = context.outputs;
  548. }
  549. else if (bundle) {
  550. const nodeNames = [];
  551. const nodeMap = new Map();
  552. for (const tensor of bundle.tensors) {
  553. const parts = tensor.name.split('/');
  554. if (bundle.format === 2) {
  555. if (tensor.name === '_CHECKPOINTABLE_OBJECT_GRAPH' ||
  556. tensor.name.startsWith('optimizer/') ||
  557. tensor.name.startsWith('keras_api/metrics/') ||
  558. tensor.name.endsWith('/ExponentialMovingAverage') ||
  559. tensor.name.indexOf('.OPTIMIZER_SLOT') !== -1) {
  560. continue;
  561. }
  562. if (tensor.name.endsWith('/.ATTRIBUTES/VARIABLE_VALUE')) {
  563. parts.pop();
  564. parts.pop();
  565. }
  566. }
  567. const tensorName = parts.pop();
  568. const nodeName = parts.join('/');
  569. if (!nodeMap.has(nodeName)) {
  570. nodeNames.push(nodeName);
  571. nodeMap.set(nodeName, []);
  572. }
  573. nodeMap.get(nodeName).push({ name: tensorName, value: tensor });
  574. }
  575. const namespaces = new Set();
  576. for (const name of nodeNames) {
  577. this._nodes.push(new tf.Node(metadata, namespaces, null, 'Node', name, null, nodeMap.get(name)));
  578. }
  579. }
  580. }
  581. get name() {
  582. return this._name;
  583. }
  584. get version() {
  585. return this._version;
  586. }
  587. get tags() {
  588. return this._tags;
  589. }
  590. get groups() {
  591. return false;
  592. // TODO return true;
  593. }
  594. get inputs() {
  595. return this._inputs;
  596. }
  597. get outputs() {
  598. return this._outputs;
  599. }
  600. get nodes() {
  601. return this._nodes;
  602. }
  603. get metadata() {
  604. return this._metadata;
  605. }
  606. };
  607. tf.Parameter = class {
  608. constructor(name, args) {
  609. this._name = name;
  610. this._arguments = args;
  611. }
  612. get name() {
  613. return this._name;
  614. }
  615. get visible() {
  616. return true;
  617. }
  618. get arguments() {
  619. return this._arguments;
  620. }
  621. };
  622. tf.Argument = class {
  623. constructor(name, type, initializer) {
  624. if (typeof name !== 'string') {
  625. throw new tf.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
  626. }
  627. this._name = name;
  628. this._type = type || null;
  629. this._initializer = initializer || null;
  630. }
  631. get name() {
  632. return this._name;
  633. }
  634. get type() {
  635. if (this._initializer) {
  636. return this._initializer.type;
  637. }
  638. return this._type;
  639. }
  640. get initializer() {
  641. return this._initializer;
  642. }
  643. };
  644. tf.Function = class {
  645. constructor(metadata, func) {
  646. this._name = func.signature.name;
  647. this._version = null;
  648. this._tags = null;
  649. this._inputs = [];
  650. this._outputs = [];
  651. this._nodes = [];
  652. const input_arg = func.signature.input_arg;
  653. const output_arg = func.signature.output_arg;
  654. const ret = func.ret;
  655. if (input_arg) {
  656. for (const input of input_arg) {
  657. const argument = new tf.Argument(input.name, new tf.TensorType(input.type, null), null);
  658. this._inputs.push(new tf.Parameter(input.name, [ argument ]));
  659. }
  660. }
  661. const output_arg_map = new Map();
  662. if (output_arg) {
  663. const ret_map = new Map();
  664. for (const key of Object.keys(ret)) {
  665. const value = func.ret[key];
  666. const split = value.split(':', 2);
  667. ret_map.set(key, split[0]);
  668. }
  669. for (const output of output_arg) {
  670. const name = ret_map.get(output.name);
  671. this._outputs.push(new tf.Parameter(output.name, [
  672. new tf.Argument(name, new tf.TensorType(output.type, null), null)
  673. ]));
  674. output_arg_map.set(name, output.name);
  675. }
  676. }
  677. const nodes = func.node_def || [];
  678. const context = tf.Utility.createGraph(metadata, nodes, output_arg_map);
  679. this._nodes = context.nodes;
  680. this._inputs = this._inputs.concat(context.inputs);
  681. this._outputs = this._outputs.concat(context.outputs);
  682. }
  683. get type() {
  684. return 'function';
  685. }
  686. get name() {
  687. return this._name;
  688. }
  689. get version() {
  690. return this._version;
  691. }
  692. get tags() {
  693. return this._tags;
  694. }
  695. get groups() {
  696. return false;
  697. // TODO return true;
  698. }
  699. get inputs() {
  700. return this._inputs;
  701. }
  702. get outputs() {
  703. return this._outputs;
  704. }
  705. get nodes() {
  706. return this._nodes;
  707. }
  708. };
  709. tf.Node = class {
  710. constructor(metadata, namespaces, node, op, name, initializers, tensors) {
  711. this._type = Object.assign({}, node && node.metadata ? node.metadata : metadata.type(op) || { name: op });
  712. this._type.identifier = this._type.name;
  713. this._type.name = op;
  714. this._name = name;
  715. this._attributes = [];
  716. this._inputs = [];
  717. this._outputs = [];
  718. this._group = '';
  719. if (namespaces.has(name)) {
  720. this._group = name;
  721. }
  722. else {
  723. const lastIndex = name.lastIndexOf('/');
  724. if (lastIndex != -1) {
  725. const namespace = name.substring(0, lastIndex);
  726. if (namespaces.has(namespace)) {
  727. this._group = namespace;
  728. }
  729. }
  730. }
  731. if (node) {
  732. if (node.device !== undefined) {
  733. this._device = node.device;
  734. }
  735. if (node.attr) {
  736. this._attributes = Object.keys(node.attr).map((name) => {
  737. const value = node.attr[name];
  738. return new tf.Attribute(metadata, op, name, value);
  739. });
  740. }
  741. let inputIndex = 0;
  742. const inputs = node.input.filter((input) => !input.name.startsWith('^'));
  743. if (this._type && this._type.inputs) {
  744. for (const input of this._type.inputs) {
  745. let inputCount = 1;
  746. if (input.numberAttr) {
  747. const inputNumber = node.attr[input.numberAttr];
  748. if (inputNumber && inputNumber.i) {
  749. inputCount = inputNumber.i;
  750. }
  751. }
  752. else if (input.typeListAttr) {
  753. const inputTypeListAttr = node.attr[input.typeListAttr];
  754. if (inputTypeListAttr && inputTypeListAttr.list && inputTypeListAttr.list.type) {
  755. inputCount = inputTypeListAttr.list.type.length;
  756. }
  757. }
  758. const inputArguments = inputs.slice(inputIndex, inputIndex + inputCount).map((input) => {
  759. return initializers.has(input.name) ? initializers.get(input.name) : new tf.Argument(input.name, null, null);
  760. });
  761. this._inputs.push(new tf.Parameter(input.name, inputArguments));
  762. inputIndex += inputCount;
  763. }
  764. }
  765. this._inputs.push(...inputs.slice(inputIndex).map((input, index) => {
  766. return new tf.Parameter(input.label ? input.label : (inputIndex + index).toString(), [
  767. initializers.has(input.name) ? initializers.get(input.name) : new tf.Argument(input.name, null, null)
  768. ]);
  769. }));
  770. let outputIndex = 0;
  771. const outputs = node.output;
  772. if (this._type && this._type.outputs) {
  773. for (const output of this._type.outputs) {
  774. let outputCount = 1;
  775. if (output.numberAttr) {
  776. const outputNumber = node.attr[output.numberAttr];
  777. if (outputNumber && outputNumber.i) {
  778. outputCount = outputNumber.i;
  779. }
  780. }
  781. else if (output.typeListAttr) {
  782. const outputTypeListAttr = node.attr[output.typeListAttr];
  783. if (outputTypeListAttr && outputTypeListAttr.list && outputTypeListAttr.list.type) {
  784. outputCount = outputTypeListAttr.list.type.length;
  785. }
  786. }
  787. const outputArguments = outputs.slice(outputIndex, outputIndex + outputCount).map((output) => {
  788. return new tf.Argument(output.name ? output.name : '-', null, null);
  789. });
  790. this._outputs.push(new tf.Parameter(output.name, outputArguments));
  791. outputIndex += outputCount;
  792. }
  793. }
  794. this._outputs.push(...outputs.slice(outputIndex).map((output, index) => {
  795. return new tf.Parameter((outputIndex + index).toString(), [
  796. new tf.Argument(output.name ? output.name : '-', null, null)
  797. ]);
  798. }));
  799. this._controlDependencies = node.controlDependencies.map((input) => input.name);
  800. }
  801. else if (tensors) {
  802. for (const tensor of tensors) {
  803. this._inputs.push(new tf.Parameter(tensor.name, [
  804. new tf.Argument(tensor.value.name, null, tensor.value)
  805. ]));
  806. }
  807. }
  808. }
  809. get type() {
  810. return this._type;
  811. }
  812. get name() {
  813. return this._name;
  814. }
  815. get device() {
  816. return this._device || null;
  817. }
  818. get group() {
  819. return this._group;
  820. }
  821. get description() {
  822. return '';
  823. }
  824. get inputs() {
  825. return this._inputs;
  826. }
  827. get outputs() {
  828. return this._outputs;
  829. }
  830. get controlDependencies() {
  831. return this._controlDependencies;
  832. }
  833. get attributes() {
  834. return this._attributes;
  835. }
  836. };
  837. tf.Attribute = class {
  838. constructor(metadata, op, name, value) {
  839. this._name = name;
  840. this._value = null;
  841. this._type = null;
  842. const schema = value && value.metadata ? value.metadata : metadata.attribute(op, name);
  843. const visible = metadata.visible(op, name);
  844. if (Object.prototype.hasOwnProperty.call(value, 'tensor')) {
  845. this._type = 'tensor';
  846. this._value = new tf.Tensor(value.tensor);
  847. }
  848. else if (schema && schema.type) {
  849. this._type = schema.type;
  850. }
  851. switch (value.value) {
  852. case 'type':
  853. this._type = 'type';
  854. this._value = tf.Utility.dataType(value.type);
  855. break;
  856. case 'i':
  857. this._value = value.i;
  858. break;
  859. case 'f':
  860. this._value = value.f;
  861. break;
  862. case 'b':
  863. this._value = value.b;
  864. break;
  865. case 'shape':
  866. this._type = 'shape';
  867. this._value = new tf.TensorShape(value.shape);
  868. break;
  869. case 's':
  870. this._value = tf.Utility.decodeText(value.s);
  871. break;
  872. case 'func': {
  873. this._type = 'function';
  874. this._value = metadata.type(value.func.name);
  875. break;
  876. }
  877. case 'list': {
  878. const list = value.list;
  879. if (list.s && list.s.length > 0) {
  880. this._value = list.s.map((s) => tf.Utility.decodeText(s));
  881. }
  882. else if (list.i && list.i.length > 0) {
  883. this._value = list.i;
  884. }
  885. else if (list.f && list.f.length > 0) {
  886. this._value = list.f;
  887. }
  888. else if (list.type && list.type.length > 0) {
  889. this._type = 'type[]';
  890. this._value = list.type.map((type) => tf.Utility.dataType(type));
  891. }
  892. else if (list.shape && list.shape.length > 0) {
  893. this._type = 'shape[]';
  894. this._value = list.shape.map((shape) => new tf.TensorShape(shape));
  895. }
  896. else if (list.func && list.func.length > 0) {
  897. this._type = 'function[]';
  898. this._value = list.func.map((func) => metadata.type(func.name));
  899. }
  900. else {
  901. this._value = [];
  902. }
  903. break;
  904. }
  905. }
  906. if (schema) {
  907. if (Object.prototype.hasOwnProperty.call(schema, 'visible') && !schema.visible) {
  908. this._visible = false;
  909. }
  910. else if (Object.prototype.hasOwnProperty.call(schema, 'default')) {
  911. const equals = (value, defaultValue) => {
  912. if (!Array.isArray(defaultValue) && defaultValue === Object(defaultValue)) {
  913. switch (defaultValue.type) {
  914. case 'type':
  915. defaultValue = tf.Utility.dataType(defaultValue.value);
  916. break;
  917. case 'shape':
  918. case 'tensor':
  919. defaultValue = defaultValue.value;
  920. break;
  921. default:
  922. throw new tf.Error(JSON.stringify(defaultValue));
  923. }
  924. }
  925. switch (typeof value) {
  926. case 'boolean':
  927. case 'number':
  928. case 'string':
  929. return value === defaultValue;
  930. }
  931. if (value instanceof base.Int64 || value instanceof base.Uint64) {
  932. return value.toNumber() === defaultValue;
  933. }
  934. return false;
  935. };
  936. const value = this._value;
  937. const defaultValue = schema.default;
  938. if (Array.isArray(value) && Array.isArray(defaultValue)) {
  939. if (value.length === defaultValue.length && value.every((item, index) => equals(item, defaultValue[index]))) {
  940. this._visible = false;
  941. }
  942. }
  943. else {
  944. if (equals(value, defaultValue)) {
  945. this._visible = false;
  946. }
  947. }
  948. }
  949. }
  950. if (name == '_output_shapes') {
  951. this._visible = false;
  952. this._type = 'shape[]';
  953. }
  954. if (name == '_class') {
  955. this._visible = false;
  956. }
  957. if (visible === false) {
  958. this._visible = false;
  959. }
  960. }
  961. get name() {
  962. return this._name;
  963. }
  964. get type() {
  965. return this._type;
  966. }
  967. get value() {
  968. return this._value;
  969. }
  970. get visible() {
  971. return this._visible == false ? false : true;
  972. }
  973. };
  974. tf.Tensor = class {
  975. constructor(tensor, name, kind) {
  976. this._name = name;
  977. this._kind = kind || null;
  978. if (tensor) {
  979. this._type = new tf.TensorType(tensor.dtype, tensor.tensor_shape || tensor.tensorShape);
  980. this._tensor = tensor;
  981. if (Object.prototype.hasOwnProperty.call(tensor, 'tensor_content')) {
  982. this._buffer = tensor.tensor_content;
  983. }
  984. else {
  985. const DataType = tf.proto.tensorflow.DataType;
  986. switch (tensor.dtype) {
  987. case DataType.DT_FLOAT:
  988. this._data = tensor.float_val || null;
  989. break;
  990. case DataType.DT_DOUBLE:
  991. this._data = tensor.double_val || null;
  992. break;
  993. case DataType.DT_INT8:
  994. case DataType.DT_UINT8:
  995. case DataType.DT_INT32:
  996. this._data = tensor.int_val || null;
  997. break;
  998. case DataType.DT_UINT32:
  999. this._data = tensor.uint32_val || null;
  1000. break;
  1001. case DataType.DT_INT64:
  1002. this._data = tensor.int64_val || null;
  1003. break;
  1004. case DataType.DT_UINT64:
  1005. this._data = tensor.uint64_val || null;
  1006. break;
  1007. case DataType.DT_BOOL:
  1008. this._data = tensor.bool_val || null;
  1009. break;
  1010. case DataType.DT_STRING:
  1011. this._data = tensor.string_val || null;
  1012. break;
  1013. }
  1014. }
  1015. }
  1016. else {
  1017. this._type = new tf.TensorType('?', null);
  1018. this._tensor = null;
  1019. }
  1020. }
  1021. get name() {
  1022. return this._name;
  1023. }
  1024. get type() {
  1025. return this._type;
  1026. }
  1027. get kind() {
  1028. return this._kind;
  1029. }
  1030. set kind(value) {
  1031. this._kind = value;
  1032. }
  1033. get state() {
  1034. return this._context().state;
  1035. }
  1036. get value() {
  1037. const context = this._context();
  1038. if (context.state) {
  1039. return null;
  1040. }
  1041. context.limit = Number.MAX_SAFE_INTEGER;
  1042. return this._decode(context, 0);
  1043. }
  1044. toString() {
  1045. const context = this._context();
  1046. if (context.state) {
  1047. return '';
  1048. }
  1049. context.limit = 10000;
  1050. const value = this._decode(context, 0);
  1051. return tf.Tensor._stringify(value, '', ' ');
  1052. }
  1053. _context() {
  1054. const context = {};
  1055. context.state = null;
  1056. context.index = 0;
  1057. context.count = 0;
  1058. context.size = 1;
  1059. if (!this._tensor) {
  1060. context.state = 'Tensor has content.';
  1061. return context;
  1062. }
  1063. if (!this._tensor.dtype) {
  1064. context.state = 'Tensor has no data type.';
  1065. return context;
  1066. }
  1067. const shape = this._tensor.tensor_shape || this._tensor.tensorShape;
  1068. if (!shape || !shape.dim) {
  1069. context.state = 'Tensor has no dimensions.';
  1070. return context;
  1071. }
  1072. for (const dim of shape.dim) {
  1073. context.size = context.size * (dim.size ? dim.size : 0);
  1074. }
  1075. if (this._buffer) {
  1076. const DataType = tf.proto.tensorflow.DataType;
  1077. switch (this._tensor.dtype) {
  1078. case DataType.DT_FLOAT:
  1079. case DataType.DT_DOUBLE:
  1080. case DataType.DT_QINT8:
  1081. case DataType.DT_QUINT8:
  1082. case DataType.DT_INT8:
  1083. case DataType.DT_UINT8:
  1084. case DataType.DT_INT16:
  1085. case DataType.DT_UINT16:
  1086. case DataType.DT_INT32:
  1087. case DataType.DT_UINT32:
  1088. case DataType.DT_INT64:
  1089. case DataType.DT_UINT64:
  1090. context.rawData = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength);
  1091. break;
  1092. }
  1093. }
  1094. else if (this._data) {
  1095. if (this._data.length == context.size) {
  1096. context.data = this._data;
  1097. }
  1098. else if (this._data.length === 1) {
  1099. context.data = new Array(context.size).fill(this._data[0]);
  1100. }
  1101. else {
  1102. context.state = "Tensor has no data.";
  1103. return context;
  1104. }
  1105. }
  1106. else {
  1107. context.state = "Tensor has no data.";
  1108. return context;
  1109. }
  1110. if (!context.data && !context.rawData) {
  1111. context.state = "Tensor data type '" + this.type.dataType + "' is not implemented.";
  1112. return context;
  1113. }
  1114. context.shape = shape.dim.map((dim) => dim.size);
  1115. return context;
  1116. }
  1117. _decode(context, dimension) {
  1118. let shape = context.shape;
  1119. if (shape.length == 0) {
  1120. shape = [ 1 ];
  1121. }
  1122. const results = [];
  1123. const size = shape[dimension];
  1124. if (dimension == shape.length - 1) {
  1125. for (let i = 0; i < size; i++) {
  1126. if (context.count > context.limit) {
  1127. results.push('...');
  1128. return results;
  1129. }
  1130. if (context.data) {
  1131. const value = context.data[context.index++];
  1132. results.push((this._tensor.dtype == tf.proto.tensorflow.DataType.DT_STRING) ? tf.Utility.decodeText(value) : value);
  1133. context.count++;
  1134. }
  1135. else {
  1136. if (context.rawData) {
  1137. switch (this._tensor.dtype) {
  1138. case tf.proto.tensorflow.DataType.DT_FLOAT:
  1139. results.push(context.rawData.getFloat32(context.index, true));
  1140. context.index += 4;
  1141. context.count++;
  1142. break;
  1143. case tf.proto.tensorflow.DataType.DT_DOUBLE:
  1144. results.push(context.rawData.getFloat64(context.index, true));
  1145. context.index += 8;
  1146. context.count++;
  1147. break;
  1148. case tf.proto.tensorflow.DataType.DT_INT8:
  1149. results.push(context.rawData.getInt8(context.index));
  1150. context.index += 1;
  1151. context.count++;
  1152. break;
  1153. case tf.proto.tensorflow.DataType.DT_UINT8:
  1154. results.push(context.rawData.getUint8(context.index));
  1155. context.index += 1;
  1156. context.count++;
  1157. break;
  1158. case tf.proto.tensorflow.DataType.DT_INT16:
  1159. results.push(context.rawData.getInt16(context.index));
  1160. context.index += 2;
  1161. context.count++;
  1162. break;
  1163. case tf.proto.tensorflow.DataType.DT_UINT16:
  1164. results.push(context.rawData.getUint16(context.index));
  1165. context.index += 2;
  1166. context.count++;
  1167. break;
  1168. case tf.proto.tensorflow.DataType.DT_INT32:
  1169. results.push(context.rawData.getInt32(context.index, true));
  1170. context.index += 4;
  1171. context.count++;
  1172. break;
  1173. case tf.proto.tensorflow.DataType.DT_UINT32:
  1174. results.push(context.rawData.getUint32(context.index, true));
  1175. context.index += 4;
  1176. context.count++;
  1177. break;
  1178. case tf.proto.tensorflow.DataType.DT_INT64:
  1179. results.push(context.rawData.getInt64(context.index, true));
  1180. context.index += 8;
  1181. context.count++;
  1182. break;
  1183. case tf.proto.tensorflow.DataType.DT_UINT64:
  1184. results.push(context.rawData.getUint64(context.index, true));
  1185. context.index += 8;
  1186. context.count++;
  1187. break;
  1188. case tf.proto.tensorflow.DataType.DT_QINT8:
  1189. results.push(context.rawData.getInt8(context.index, true));
  1190. context.index += 1;
  1191. context.count++;
  1192. break;
  1193. case tf.proto.tensorflow.DataType.DT_QUINT8:
  1194. results.push(context.rawData.getUint8(context.index, true));
  1195. context.index += 1;
  1196. context.count++;
  1197. break;
  1198. default:
  1199. throw new tf.Error("Unsupported data type '" + this._tensor.dtype + "'.");
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. else {
  1206. for (let j = 0; j < size; j++) {
  1207. if (context.count > context.limit) {
  1208. results.push('...');
  1209. return results;
  1210. }
  1211. results.push(this._decode(context, dimension + 1, shape));
  1212. }
  1213. }
  1214. if (context.shape.length == 0) {
  1215. return results[0];
  1216. }
  1217. return results;
  1218. }
  1219. static _stringify(value, indentation, indent) {
  1220. if (Array.isArray(value)) {
  1221. const result = [];
  1222. result.push(indentation + '[');
  1223. const items = value.map((item) => tf.Tensor._stringify(item, indentation + indent, indent));
  1224. if (items.length > 0) {
  1225. result.push(items.join(',\n'));
  1226. }
  1227. result.push(indentation + ']');
  1228. return result.join('\n');
  1229. }
  1230. if (typeof value == 'string') {
  1231. return indentation + value;
  1232. }
  1233. if (value == Infinity) {
  1234. return indentation + 'Infinity';
  1235. }
  1236. if (value == -Infinity) {
  1237. return indentation + '-Infinity';
  1238. }
  1239. if (isNaN(value)) {
  1240. return indentation + 'NaN';
  1241. }
  1242. return indentation + value.toString();
  1243. }
  1244. };
  1245. tf.TensorType = class {
  1246. constructor(dtype, shape) {
  1247. this._dtype = dtype;
  1248. this._shape = new tf.TensorShape(shape);
  1249. }
  1250. get dataType() {
  1251. return this._dtype ? tf.Utility.dataType(this._dtype) : '?';
  1252. }
  1253. get shape() {
  1254. return this._shape;
  1255. }
  1256. toString() {
  1257. return this.dataType + this._shape.toString();
  1258. }
  1259. };
  1260. tf.TensorShape = class {
  1261. constructor(shape) {
  1262. this._shape = shape;
  1263. }
  1264. get dimensions() {
  1265. if (this._shape && this._shape.dim) {
  1266. if (this._shape.unknown_rank) {
  1267. return null;
  1268. }
  1269. if (this._shape.dim.length == 0) {
  1270. return [];
  1271. }
  1272. if (this._shape.dim.length == 1 && !this._shape.dim[0].size) {
  1273. return [ 0 ];
  1274. }
  1275. return this._shape.dim.map((dim) => (dim.size && dim.size != -1) ? dim.size : '?');
  1276. }
  1277. return null;
  1278. }
  1279. toString() {
  1280. if (this._shape && this._shape.dim) {
  1281. if (this._shape.unknown_rank) {
  1282. return '[-]';
  1283. }
  1284. if (this._shape.dim.length == 0) {
  1285. return '';
  1286. }
  1287. if (this._shape.dim.length == 1 && !this._shape.dim[0].size) {
  1288. return '[0]';
  1289. }
  1290. return '[' + this._shape.dim.map((dim) => (dim.size && dim.size != -1) ? dim.size.toString() : '?').join(',') + ']';
  1291. }
  1292. return '?';
  1293. }
  1294. };
  1295. tf.TensorBundle = class {
  1296. static open(stream, identifier, context) {
  1297. const format = !identifier.toLowerCase().endsWith('.index') ? 1 : 2;
  1298. const table = new tf.TensorBundle.Table(stream);
  1299. if (!table.entries.has('')) {
  1300. throw new tf.Error('Bundle header not available.');
  1301. }
  1302. if (format === 1) {
  1303. return Promise.resolve(new tf.TensorBundle(format, table.entries, []));
  1304. }
  1305. const buffer = table.entries.get('');
  1306. const reader = protobuf.BinaryReader.open(buffer);
  1307. const header = tf.proto.tensorflow.BundleHeaderProto.decode(reader);
  1308. const numShards = header.num_shards;
  1309. const promises = [];
  1310. for (let i = 0; i < numShards; i++) {
  1311. const shardIndex = ('0000' + i).slice(-5);
  1312. const shardCount = ('0000' + numShards).slice(-5);
  1313. const filename = identifier.split('.');
  1314. filename.pop();
  1315. const basename = filename.join('.');
  1316. const name = basename + '.data-' + shardIndex + '-of-' + shardCount;
  1317. promises.push(context.request(name, null));
  1318. }
  1319. return Promise.all(promises).then((streams) => {
  1320. return new tf.TensorBundle(format, table.entries, streams);
  1321. }).catch((error) => {
  1322. context.exception(error, false);
  1323. return new tf.TensorBundle(format, table.entries, null);
  1324. });
  1325. }
  1326. constructor(format, entries, streams) {
  1327. this._format = format;
  1328. this._tensors = [];
  1329. switch (format) {
  1330. case 1: {
  1331. const buffer = entries.get('');
  1332. const reader = protobuf.BinaryReader.open(buffer);
  1333. const header = tf.proto.tensorflow.SavedTensorSlices.decode(reader);
  1334. const data = new Map();
  1335. for (const pair of entries) {
  1336. if (pair[0] !== '' && pair[0] !== 'global_step') {
  1337. const buffer = pair[1];
  1338. const reader = protobuf.BinaryReader.open(buffer);
  1339. const slices = tf.proto.tensorflow.SavedTensorSlices.decode(reader);
  1340. const name = slices.data.name;
  1341. const tensor = slices.data.data;
  1342. if (!data.has(name)) {
  1343. if (tensor.tensor_content && tensor.tensor_content.length > 0) {
  1344. data.set(name, { key: 'tensor_content', value: tensor.tensor_content });
  1345. }
  1346. else {
  1347. const keys = Object.keys(tensor).filter((key) => key.endsWith('_val') && tensor[key] && tensor[key].length > 0);
  1348. data.set(name, keys.length == 1 ? { key: keys[0], value: tensor[keys[0]] } : null);
  1349. }
  1350. }
  1351. else {
  1352. const item = data.get(name);
  1353. if (item !== null) {
  1354. if (tensor[item.key] && tensor[item.key].length > 0) {
  1355. item.value = item.value.concat(tensor[item.key]);
  1356. }
  1357. else {
  1358. data.set(name, null);
  1359. }
  1360. }
  1361. }
  1362. }
  1363. }
  1364. for (const meta of header.meta.tensor) {
  1365. if (meta.name !== 'global_step') {
  1366. const tensor = new tf.proto.tensorflow.TensorProto();
  1367. tensor.dtype = meta.type;
  1368. tensor.tensor_shape = meta.shape;
  1369. const item = data.get(meta.name);
  1370. if (item) {
  1371. tensor[item.key] = item.value;
  1372. }
  1373. this._tensors.push(new tf.Tensor(tensor, meta.name, null));
  1374. }
  1375. }
  1376. break;
  1377. }
  1378. case 2: {
  1379. entries.forEach((buffer, name) => {
  1380. if (name !== '') {
  1381. const reader = protobuf.BinaryReader.open(buffer);
  1382. const entry = tf.proto.tensorflow.BundleEntryProto.decode(reader);
  1383. const tensor = new tf.proto.tensorflow.TensorProto();
  1384. tensor.dtype = entry.dtype;
  1385. tensor.tensor_shape = entry.shape;
  1386. const offset = Number.isInteger(entry.offset) ? entry.offset : entry.offset.toNumber();
  1387. const size = Number.isInteger(entry.size) ? entry.size : entry.size.toNumber();
  1388. if (streams) {
  1389. const stream = streams[entry.shard_id];
  1390. stream.seek(offset);
  1391. tensor.tensor_content = stream.peek(size);
  1392. }
  1393. this._tensors.push(new tf.Tensor(tensor, name, null));
  1394. }
  1395. });
  1396. break;
  1397. }
  1398. }
  1399. }
  1400. get format() {
  1401. return this._format;
  1402. }
  1403. get tensors() {
  1404. return this._tensors;
  1405. }
  1406. };
  1407. tf.TensorBundle.Table = class {
  1408. constructor(stream) {
  1409. // https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/lib/io/table.cc
  1410. this.entries = new Map();
  1411. if (stream.length <= 54) {
  1412. throw new tf.Error('Invalid index file size.');
  1413. }
  1414. stream.seek(-48);
  1415. const buffer = stream.peek(48);
  1416. const reader = new tf.BinaryReader(buffer);
  1417. reader.seek(-8);
  1418. const signature = [ 0x57, 0xfb, 0x80, 0x8b, 0x24, 0x75, 0x47, 0xdb ];
  1419. if (!reader.read(8).every((value, index) => value === signature[index])) {
  1420. throw new tf.Error('Invalid table signature.');
  1421. }
  1422. reader.seek(-48); // kEncodedLength
  1423. reader.varint64(); // metaindex offset
  1424. reader.varint64(); // metaindex size
  1425. const indexOffset = reader.varint64();
  1426. const indexSize = reader.varint64();
  1427. const indexBlock = new tf.TensorBundle.Table.Block(stream, indexOffset, indexSize);
  1428. for (const entry of indexBlock.entries) {
  1429. const valueReader = new tf.BinaryReader(entry[1]);
  1430. const offset = valueReader.varint64();
  1431. const size = valueReader.varint64();
  1432. const block = new tf.TensorBundle.Table.Block(stream, offset, size);
  1433. for (const pair of block.entries) {
  1434. this.entries.set(pair[0], pair[1]);
  1435. }
  1436. }
  1437. stream.seek(0);
  1438. }
  1439. };
  1440. tf.TensorBundle.Table.Block = class {
  1441. constructor(stream, offset, size) {
  1442. // https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/lib/io/block.cc
  1443. this.entries = new Map();
  1444. stream.seek(offset);
  1445. const buffer = stream.read(size); // blockContents
  1446. const compression = stream.byte();
  1447. stream.skip(4); // crc32
  1448. let reader = new tf.BinaryReader(buffer);
  1449. switch (compression) {
  1450. case 0: // kNoCompression
  1451. break;
  1452. case 1: // kSnappyCompression
  1453. reader = new tf.BinaryReader(reader.unsnappy());
  1454. break;
  1455. default:
  1456. throw new tf.Error("Unsupported block compression '" + compression + "'.");
  1457. }
  1458. reader.seek(-4);
  1459. const numRestarts = reader.int32();
  1460. reader.seek(-4 - (4 * numRestarts));
  1461. const restartOffsets = [];
  1462. for (let i = 0; i < numRestarts; i++) {
  1463. restartOffsets.push(reader.int32());
  1464. }
  1465. const textDecoder = new TextDecoder();
  1466. for (let i = 0; i < numRestarts; i++) {
  1467. reader.seek(restartOffsets[i]);
  1468. let key = '';
  1469. while (reader.position < reader.length) {
  1470. const sharedSize = reader.varint32(); // index shared size
  1471. const nonSharedSize = reader.varint32(); // index non shared size
  1472. const valueSize = reader.varint32();
  1473. if (sharedSize === 0 && nonSharedSize === 0 && valueSize === 0) {
  1474. break;
  1475. }
  1476. key = key.substring(0, sharedSize);
  1477. key = key + textDecoder.decode(reader.read(nonSharedSize));
  1478. const value = reader.read(valueSize);
  1479. this.entries.set(key, value);
  1480. }
  1481. }
  1482. }
  1483. };
  1484. tf.BinaryReader = class {
  1485. constructor(buffer) {
  1486. this._buffer = buffer;
  1487. this._position = 0;
  1488. this._length = this._buffer.length;
  1489. this._dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  1490. }
  1491. get position() {
  1492. return this._position;
  1493. }
  1494. get length() {
  1495. return this._length;
  1496. }
  1497. seek(position) {
  1498. this._position = position >= 0 ? position : this._length + position;
  1499. if (this._position > this._length) {
  1500. throw new tf.Error('Expected ' + (this._position - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.');
  1501. }
  1502. }
  1503. skip(offset) {
  1504. this._position += offset;
  1505. if (this._position > this._length) {
  1506. throw new tf.Error('Expected ' + (this._position - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.');
  1507. }
  1508. }
  1509. read(size) {
  1510. const position = this._position;
  1511. this.skip(size);
  1512. return this._buffer.subarray(position, this._position);
  1513. }
  1514. byte() {
  1515. const position = this._position;
  1516. this.skip(1);
  1517. return this._dataView.getUint8(position);
  1518. }
  1519. uint16() {
  1520. const position = this._position;
  1521. this.skip(2);
  1522. return this._dataView.getUint16(position, true);
  1523. }
  1524. int32() {
  1525. const position = this._position;
  1526. this.skip(4);
  1527. return this._dataView.getInt32(position, true);
  1528. }
  1529. uint32() {
  1530. const position = this._position;
  1531. this.skip(4);
  1532. return this._dataView.getUint32(position, true);
  1533. }
  1534. uint64() {
  1535. const position = this._position;
  1536. this.skip(4);
  1537. return this._dataView.getUint64(position, true);
  1538. }
  1539. varint32() {
  1540. return this.varint64();
  1541. }
  1542. varint64() {
  1543. let result = 0;
  1544. for (let shift = 0; shift <= 63; shift += 7) {
  1545. const byte = this.byte();
  1546. if (byte & 128) {
  1547. result |= (byte & 127) << shift;
  1548. }
  1549. else {
  1550. result |= byte << shift;
  1551. break;
  1552. }
  1553. }
  1554. return result;
  1555. }
  1556. unsnappy() {
  1557. const data = new Uint8Array(this.varint64());
  1558. const mask = [0, 0xff, 0xffff, 0xffffff, 0xffffffff];
  1559. let position = 0;
  1560. while (this._position < this._length) {
  1561. let length = 0;
  1562. const c = this.byte();
  1563. switch (c & 0x03) {
  1564. case 0: {
  1565. length = (c >>> 2) + 1;
  1566. if (length > 60) {
  1567. const short = length - 60;
  1568. length = (this.uint32() & mask[short]) + 1;
  1569. this._position += short - 4;
  1570. }
  1571. data.set(this.read(length), position);
  1572. break;
  1573. }
  1574. case 1: {
  1575. length = ((c >>> 2) & 0x07) + 4;
  1576. const offset = this.byte() + ((c >>> 5) << 8);
  1577. data.set(data.subarray(position - offset, position - offset + length), position);
  1578. break;
  1579. }
  1580. case 2: {
  1581. length = (c >>> 2) + 1;
  1582. const offset = this.uint16();
  1583. data.set(data.subarray(position - offset, position - offset + length), position);
  1584. break;
  1585. }
  1586. case 3: {
  1587. length = (c >>> 2) + 1;
  1588. const offset = this.uint32();
  1589. data.set(data.subarray(position - offset, position - offset + length), position);
  1590. break;
  1591. }
  1592. }
  1593. position += length;
  1594. }
  1595. return data;
  1596. }
  1597. };
  1598. tf.EventFileReader = class {
  1599. static open(stream) {
  1600. if (stream.length < 16) {
  1601. return null;
  1602. }
  1603. const masked_crc32c = (bytes) => {
  1604. const poly = 0x82f63b78;
  1605. let crc = 0xffffffff;
  1606. for (let n = 0; n < bytes.length; n++) {
  1607. crc ^= bytes[n];
  1608. crc = crc & 1 ? (crc >>> 1) ^ poly : crc >>> 1;
  1609. crc = crc & 1 ? (crc >>> 1) ^ poly : crc >>> 1;
  1610. crc = crc & 1 ? (crc >>> 1) ^ poly : crc >>> 1;
  1611. crc = crc & 1 ? (crc >>> 1) ^ poly : crc >>> 1;
  1612. crc = crc & 1 ? (crc >>> 1) ^ poly : crc >>> 1;
  1613. crc = crc & 1 ? (crc >>> 1) ^ poly : crc >>> 1;
  1614. crc = crc & 1 ? (crc >>> 1) ^ poly : crc >>> 1;
  1615. crc = crc & 1 ? (crc >>> 1) ^ poly : crc >>> 1;
  1616. crc = crc >>> 0;
  1617. }
  1618. crc = crc ^ 0xffffffff;
  1619. crc = crc >>> 0;
  1620. crc = ((crc >> 15) | (crc << 17)) + 0xa282ead8;
  1621. crc = crc >>> 0;
  1622. return crc;
  1623. };
  1624. const buffer = stream.peek(12);
  1625. const reader = new tf.BinaryReader(buffer);
  1626. const length_bytes = reader.read(8);
  1627. const length_crc = reader.uint32();
  1628. if (masked_crc32c(length_bytes) !== length_crc) {
  1629. return null;
  1630. }
  1631. return new tf.EventFileReader(stream);
  1632. }
  1633. constructor(stream) {
  1634. this._stream = stream;
  1635. }
  1636. read() {
  1637. if (this._stream.position < this._stream.length) {
  1638. const uint64 = (stream) => {
  1639. const buffer = stream.read(8);
  1640. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  1641. return view.getUint64(0, true).toNumber();
  1642. };
  1643. const length = uint64(this._stream);
  1644. this._stream.skip(4); // masked crc of length
  1645. const buffer = this._stream.read(length);
  1646. const reader = protobuf.BinaryReader.open(buffer);
  1647. const event = tf.proto.tensorflow.Event.decode(reader);
  1648. this._stream.skip(4); // masked crc of data
  1649. return event;
  1650. }
  1651. }
  1652. };
  1653. tf.GraphMetadata = class {
  1654. constructor(metadata, library) {
  1655. this._metadata = metadata;
  1656. this._functions = new Map();
  1657. this._attributes = new Map();
  1658. this._visibleCache = new Map();
  1659. if (library && Array.isArray(library.function)) {
  1660. for (const func of library.function) {
  1661. const name = func.signature.name;
  1662. if (this._functions.has(func.name)) {
  1663. throw new tf.Error("Duplicate function name '" + func.name + "'.");
  1664. }
  1665. this._functions.set(name, func);
  1666. }
  1667. }
  1668. }
  1669. type(name) {
  1670. if (this._functions.has(name)) {
  1671. const func = this._functions.get(name);
  1672. if (func instanceof tf.Function) {
  1673. return func;
  1674. }
  1675. this._functions.set(name, new tf.Function(this, func));
  1676. return this._functions.get(name);
  1677. }
  1678. return this._metadata.type(name);
  1679. }
  1680. attribute(type, name) {
  1681. const key = type + '::' + name;
  1682. if (!this._attributes.has(key)) {
  1683. const schema = this.type(type);
  1684. if (schema && schema.attributes) {
  1685. for (const attribute of schema.attributes) {
  1686. const key = type + '::' + attribute.name;
  1687. this._attributes.set(key, attribute);
  1688. }
  1689. }
  1690. }
  1691. return this._attributes.get(key);
  1692. }
  1693. visible(type, name) {
  1694. if (!this._visibleCache.has(type)) {
  1695. const set = new Set();
  1696. const schema = this.type(type);
  1697. if (schema && schema.inputs) {
  1698. for (const input of schema.inputs) {
  1699. if (input.typeAttr) {
  1700. set.add(input.typeAttr);
  1701. }
  1702. else if (input.typeListAttr) {
  1703. set.add(input.typeListAttr);
  1704. }
  1705. if (input.numberAttr) {
  1706. set.add(input.numberAttr);
  1707. }
  1708. }
  1709. }
  1710. if (schema && schema.outputs) {
  1711. for (const output of schema.outputs) {
  1712. if (output.typeAttr) {
  1713. set.add(output.typeAttr);
  1714. }
  1715. else if (output.typeListAttr) {
  1716. set.add(output.typeListAttr);
  1717. }
  1718. if (output.numberAttr) {
  1719. set.add(output.numberAttr);
  1720. }
  1721. }
  1722. }
  1723. this._visibleCache.set(type, set);
  1724. }
  1725. return !this._visibleCache.get(type).has(name);
  1726. }
  1727. };
  1728. tf.Metadata = class {
  1729. static open(context) {
  1730. if (tf.Metadata._metadata) {
  1731. return Promise.resolve(tf.Metadata._metadata);
  1732. }
  1733. return context.request('tf-metadata.json', 'utf-8', null).then((data) => {
  1734. tf.Metadata._metadata = new tf.Metadata(data);
  1735. return tf.Metadata._metadata;
  1736. }).catch(() => {
  1737. tf.Metadata._metadata = new tf.Metadata(null);
  1738. return tf.Metadata._metadata;
  1739. });
  1740. }
  1741. constructor(data) {
  1742. this._map = new Map();
  1743. if (data) {
  1744. const metadata = JSON.parse(data);
  1745. this._map = new Map(metadata.map((item) => [ item.name, item ]));
  1746. }
  1747. }
  1748. type(operator) {
  1749. return this._map.get(operator);
  1750. }
  1751. };
  1752. tf.Utility = class {
  1753. static decodeText(value) {
  1754. if (typeof value === 'string') {
  1755. return value;
  1756. }
  1757. if (value.length === 0) {
  1758. return '';
  1759. }
  1760. tf.Utility._utf8Decoder = tf.Utility._utf8Decoder || new TextDecoder('utf-8');
  1761. return tf.Utility._utf8Decoder.decode(value);
  1762. }
  1763. static dataType(type) {
  1764. if (!tf.Utility._dataTypes) {
  1765. const dataTypes = new Map();
  1766. const DataType = tf.proto.tensorflow.DataType;
  1767. for (let key of Object.keys(DataType)) {
  1768. const value = DataType[key];
  1769. key = key.startsWith('DT_') ? key.substring(3) : key;
  1770. dataTypes.set(value, key.toLowerCase());
  1771. }
  1772. dataTypes.set(DataType.DT_HALF, 'float16');
  1773. dataTypes.set(DataType.DT_FLOAT, 'float32');
  1774. dataTypes.set(DataType.DT_DOUBLE, 'float64');
  1775. tf.Utility._dataTypes = dataTypes;
  1776. }
  1777. return tf.Utility._dataTypes.has(type) ? tf.Utility._dataTypes.get(type) : '?';
  1778. }
  1779. static dataTypeKey(type) {
  1780. if (!tf.Utility._dataTypeKeys) {
  1781. const dataTypeKeys = new Map();
  1782. const DataType = tf.proto.tensorflow.DataType;
  1783. for (let key of Object.keys(DataType)) {
  1784. const value = DataType[key];
  1785. key = key.startsWith('DT_') ? key.substring(3) : key;
  1786. dataTypeKeys.set(key.toLowerCase(), value);
  1787. }
  1788. dataTypeKeys.set('float16', DataType.DT_HALF);
  1789. dataTypeKeys.set('float32', DataType.DT_FLOAT);
  1790. dataTypeKeys.set('float64', DataType.DT_DOUBLE);
  1791. tf.Utility._dataTypeKeys = dataTypeKeys;
  1792. }
  1793. return tf.Utility._dataTypeKeys.get(type);
  1794. }
  1795. static createGraph(metadata, nodes, output_arg_map) {
  1796. const context = {};
  1797. context.inputs = [];
  1798. context.outputs = [];
  1799. context.nodes = [];
  1800. const namespaces = new Set();
  1801. const node_map = new Map();
  1802. for (const node of nodes) {
  1803. const nodeName = node.name;
  1804. node_map.set(nodeName, node);
  1805. if (node.op != 'Const') {
  1806. const index = nodeName.lastIndexOf('/');
  1807. if (index != -1) {
  1808. const namespace = nodeName.substring(0, index);
  1809. namespaces.add(namespace);
  1810. }
  1811. }
  1812. node.output = [];
  1813. }
  1814. for (const node of nodes) {
  1815. const inputs = node.input;
  1816. node.input = [];
  1817. node.controlDependencies = [];
  1818. for (const input of inputs) {
  1819. const split = input.split(':', 3);
  1820. const input_name = split[0];
  1821. const input_index = split.length == 1 ? 0 : parseInt(split[split.length - 1]);
  1822. const from_name = input_name.startsWith('^') ? input_name.substring(1) : input_name;
  1823. const from = node_map.get(from_name);
  1824. const output_name = input_index == 0 ? from_name : from_name + ':' + input_index.toString();
  1825. const input_arg = from ? { name: output_name, from: from } : { name: output_name };
  1826. if (input_name.startsWith('^')) {
  1827. node.controlDependencies.push(input_arg);
  1828. }
  1829. else {
  1830. node.input.push(input_arg);
  1831. }
  1832. if (from) {
  1833. for (let i = from.output.length; i <= input_index; i++) {
  1834. from.output.push({ name: i === 0 ? from_name : from_name + ':' + i.toString(), to: [] });
  1835. }
  1836. from.output[input_index].to.push(node);
  1837. }
  1838. }
  1839. }
  1840. if (output_arg_map) {
  1841. for (const node of nodes) {
  1842. if (output_arg_map.has(node.name)) {
  1843. node.output.push({ name: node.name, to: [] });
  1844. }
  1845. }
  1846. }
  1847. const initializers = new Map();
  1848. const map_tensor = (name, node, kind) => {
  1849. if (node && node.op === 'Const' && node.input.length === 0 && node.output.length === 1 && node.output[0].to.length === 1 && node.controlDependencies.length === 0) {
  1850. const value = node.attr.value;
  1851. if (value && Object.prototype.hasOwnProperty.call(value, 'tensor')) {
  1852. const tensor = new tf.Tensor(value.tensor, name, kind);
  1853. return new tf.Argument(name, tensor.type, tensor);
  1854. }
  1855. }
  1856. return null;
  1857. };
  1858. const map_resource = (name, node, tensor) => {
  1859. if (node && node.op === 'Placeholder' && node.input.length === 0 && node.output.length === 1 && node.output[0].to.length === 1 && node.controlDependencies.length === 0) {
  1860. const dtype = node.attr.dtype.type;
  1861. if (dtype === tf.proto.tensorflow.DataType.DT_RESOURCE) {
  1862. return new tf.Argument(name, null, tensor);
  1863. }
  1864. }
  1865. return null;
  1866. };
  1867. for (const node of node_map.values()) {
  1868. if (node.op === 'Identity' && node.input.length === 1 && node.output.length === 1 && node.output[0].to.length === 1 && node.controlDependencies.length === 0) {
  1869. const initializer = map_tensor(node.name, node.input[0].from, 'Identity Constant');
  1870. if (initializer) {
  1871. initializers.set(initializer.name, initializer);
  1872. node_map.delete(initializer.name);
  1873. node_map.delete(node.input[0].name);
  1874. }
  1875. const identity = node.input[0].from;
  1876. if (identity && identity.op === 'Identity' && identity.input.length === 1 && identity.output.length === 1 && node.output[0].to.length === 1 && node.controlDependencies.length === 0) {
  1877. const initializer = map_tensor(node.name, identity.input[0].from, 'Identity Constant');
  1878. if (initializer) {
  1879. initializers.set(initializer.name, initializer);
  1880. node_map.delete(initializer.name);
  1881. node_map.delete(initializer.name);
  1882. node_map.delete(identity.name);
  1883. node_map.delete(node.name);
  1884. }
  1885. }
  1886. }
  1887. }
  1888. for (const node of node_map.values()) {
  1889. const initializer = map_tensor(node.name, node, 'Const');
  1890. if (initializer) {
  1891. initializers.set(initializer.name, initializer);
  1892. node_map.delete(node.name);
  1893. node_map.delete(initializer.name);
  1894. }
  1895. }
  1896. for (const node of node_map.values()) {
  1897. if (node.op === 'ReadVariableOp' && node.input.length === 1 && node.output.length === 1 && node.output[0].to.length === 1 && node.controlDependencies.length === 0) {
  1898. if (node.attr && node.attr.dtype && node.attr._output_shapes && node.attr._output_shapes.list && node.attr._output_shapes.list.shape) {
  1899. const tensor = new tf.proto.tensorflow.TensorProto();
  1900. tensor.dtype = node.attr.dtype.type;
  1901. tensor.tensor_shape = node.attr._output_shapes.list.shape[0];
  1902. const name = node.name;
  1903. const initializer = map_resource(name, node.input[0].from, new tf.Tensor(tensor, name, 'Resource Variable'));
  1904. if (initializer) {
  1905. initializers.set(initializer.name, initializer);
  1906. node_map.delete(initializer.name);
  1907. node_map.delete(node.input[0].name);
  1908. }
  1909. }
  1910. }
  1911. }
  1912. const input_map = new Map();
  1913. for (const node of node_map.values()) {
  1914. if (node.op == 'Placeholder' && node.input.length === 0 && node.output.length === 1 && node.output[0].to.length === 1 && node.controlDependencies.length === 0) {
  1915. const dtype = node.attr.dtype;
  1916. const shape = node.attr.shape;
  1917. if (dtype && dtype.type && shape && shape.shape) {
  1918. const name = node.name;
  1919. const type = new tf.TensorType(dtype.type, shape.shape);
  1920. const argument = new tf.Argument(name, type, null);
  1921. input_map.set(name, new tf.Parameter(name, [ argument ]));
  1922. node_map.delete(name);
  1923. }
  1924. }
  1925. }
  1926. const updatePyTorch = (node_map) => {
  1927. for (const node of node_map.values()) {
  1928. if (node.op === 'prim::Constant' && node.input.length === 0 && node.controlDependencies.length === 0 && node.attr && Object.keys(node.attr).length === 1 && node.attr.attr && node.attr.attr.s) {
  1929. const value = tf.Utility.decodeText(node.attr.attr.s);
  1930. const match = /{\s*value\s*:\s*(.*)\s*}/.exec(value);
  1931. if (match) {
  1932. node.value = match[1].trim();
  1933. }
  1934. const empty = /{\s*}/.exec(value);
  1935. if (empty) {
  1936. node.value = null;
  1937. }
  1938. }
  1939. if (node.op === 'prim::GetAttr' && node.input.length === 1 && node.controlDependencies.length === 0 && node.attr && Object.keys(node.attr).length === 1 && node.attr.attr && node.attr.attr.s) {
  1940. const value = tf.Utility.decodeText(node.attr.attr.s);
  1941. const match = /{\s*name\s*:\s*([A-za-z0-9_]*)\s*}/.exec(value);
  1942. if (match) {
  1943. node.value = match[1].trim();
  1944. }
  1945. }
  1946. if (node.op === 'IO Node' && node.controlDependencies.length === 0) {
  1947. const shape = node.attr && node.attr._output_shapes && node.attr._output_shapes.list && node.attr._output_shapes.list.shape ? node.attr._output_shapes.list.shape[0] : null;
  1948. const type = shape ? new tf.TensorType('?', shape) : null;
  1949. if (node.input.length === 0 && node.output.length === 1) {
  1950. context.inputs.push(new tf.Parameter(node.name, [
  1951. new tf.Argument(node.output[0].name, type, null)
  1952. ]));
  1953. node_map.delete(node.name);
  1954. }
  1955. if (node.input.length === 1 && node.output.length === 0) {
  1956. context.outputs.push(new tf.Parameter(node.name, [
  1957. new tf.Argument(node.input[0].name, type, null)
  1958. ]));
  1959. node_map.delete(node.name);
  1960. }
  1961. }
  1962. if (Object.keys(node.attr).length === 2 &&
  1963. node.attr.attr && node.attr.attr.s && node.attr._output_shapes) {
  1964. const value = tf.Utility.decodeText(node.attr.attr.s);
  1965. if (/\s*/.exec(value) || /{\s*}/.exec(value)) {
  1966. node.attr = {};
  1967. delete node._output_shapes;
  1968. }
  1969. }
  1970. }
  1971. const remove_input = (input, node) => {
  1972. const from = input.from;
  1973. if (from) {
  1974. for (const output of from.output) {
  1975. output.to = output.to.filter((to) => to !== node);
  1976. }
  1977. if (from.output.every((output) => output.to.length === 0) && from.controlDependencies.length === 0) {
  1978. from.remove = true;
  1979. }
  1980. delete input.from;
  1981. }
  1982. };
  1983. for (const node of node_map.values()) {
  1984. if (node.op === 'prim::ListConstruct' && node.input.every((input) => input.from.value !== undefined) && node.controlDependencies.length === 0) {
  1985. node.value = node.input.map((input) => input.from.value);
  1986. for (const input of node.input) {
  1987. remove_input(input, node);
  1988. }
  1989. node.input = [];
  1990. }
  1991. }
  1992. for (const node of node_map.values()) {
  1993. const remove = new Set();
  1994. for (let i = 0; i < node.input.length; i++) {
  1995. const input = node.input[i];
  1996. const from = input.from;
  1997. if (from) {
  1998. if (from.op === 'prim::GetAttr' && from.input.length === 1 && from.output.length === 1 && from.controlDependencies.length === 0 && from.value !== undefined) {
  1999. remove_input(input, node);
  2000. input.label = from.value;
  2001. const tensor = new tf.Tensor(null, input.name, from.op);
  2002. const argument = new tf.Argument(input.name, null, tensor);
  2003. initializers.set(input.name, argument);
  2004. }
  2005. if (from.op === 'prim::Constant' && from.input.length === 0 && from.controlDependencies.length === 0 && from.value !== undefined) {
  2006. input.constant = from.value;
  2007. remove_input(input, node);
  2008. remove.add(input.name);
  2009. }
  2010. if (from.op === 'prim::ListConstruct' && from.output.length === 1 && from.controlDependencies.length === 0 && from.value !== undefined) {
  2011. input.list = from.value;
  2012. remove_input(input, node);
  2013. remove.add(input.name);
  2014. }
  2015. }
  2016. }
  2017. if (node.__metadata__) {
  2018. for (const metadata of node.__metadata__) {
  2019. const parameters = Array.prototype.slice.call(metadata.inputs || []).concat(Array.prototype.slice.call(metadata.attributes || []));
  2020. let match = true;
  2021. const inputs = Array.from(node.input);
  2022. if (inputs.length > parameters.length) {
  2023. match = false;
  2024. }
  2025. while (inputs.length > 0 && match) {
  2026. match = false;
  2027. const input = inputs.shift();
  2028. delete input.metadata;
  2029. const parameter = parameters.shift();
  2030. switch (parameter.type) {
  2031. case 'Tensor': {
  2032. if ((input.constant === undefined && input.list === undefined) || input.constant === null) {
  2033. input.metadata = parameter;
  2034. match = true;
  2035. }
  2036. else {
  2037. inputs.unshift(input);
  2038. match = true;
  2039. }
  2040. break;
  2041. }
  2042. case 'int64': {
  2043. const value = parseInt(input.constant);
  2044. if (input.constant !== undefined && Number.isInteger(value)) {
  2045. input.attr = new tf.proto.tensorflow.AttrValue();
  2046. input.attr.i = value;
  2047. input.attr.metadata = parameter;
  2048. match = true;
  2049. }
  2050. break;
  2051. }
  2052. case 'float32': {
  2053. const value = parseFloat(input.constant);
  2054. if (input.constant !== undefined && !isNaN(value)) {
  2055. input.attr = new tf.proto.tensorflow.AttrValue();
  2056. input.attr.f = value;
  2057. input.attr.metadata = parameter;
  2058. match = true;
  2059. }
  2060. break;
  2061. }
  2062. case 'int64[]': {
  2063. if (Array.isArray(input.list)) {
  2064. const list = input.list.map((item) => parseInt(item));
  2065. if (list.every((value) => Number.isInteger(value))) {
  2066. input.attr = new tf.proto.tensorflow.AttrValue();
  2067. input.attr.list = new tf.proto.tensorflow.ListValue();
  2068. input.attr.list.i = list;
  2069. input.attr.metadata = parameter;
  2070. match = true;
  2071. }
  2072. }
  2073. break;
  2074. }
  2075. case 'boolean': {
  2076. if (input.constant === 'false' || input.constant === '0') {
  2077. input.attr = new tf.proto.tensorflow.AttrValue();
  2078. input.attr.b = false;
  2079. input.attr.metadata = parameter;
  2080. match = true;
  2081. }
  2082. else if (input.constant === 'true' || input.constant === '1') {
  2083. input.attr = new tf.proto.tensorflow.AttrValue();
  2084. input.attr.b = true;
  2085. input.attr.metadata = parameter;
  2086. match = true;
  2087. }
  2088. break;
  2089. }
  2090. case 'Scalar': {
  2091. const value = parseInt(input.constant);
  2092. if (input.constant !== undefined && Number.isInteger(value)) {
  2093. input.attr = new tf.proto.tensorflow.AttrValue();
  2094. input.attr.i = value;
  2095. input.attr.metadata = parameter;
  2096. match = true;
  2097. }
  2098. break;
  2099. }
  2100. default:
  2101. break;
  2102. }
  2103. }
  2104. if (match) {
  2105. node.metadata = metadata;
  2106. break;
  2107. }
  2108. else {
  2109. for (const input of node.input) {
  2110. delete input.metadata;
  2111. delete input.attr;
  2112. }
  2113. }
  2114. }
  2115. }
  2116. node.input = node.input.filter((input, index) => {
  2117. if (input.attr) {
  2118. const name = input.attr.metadata ? input.attr.metadata.name : index.toString();
  2119. node.attr[name] = input.attr;
  2120. }
  2121. else if (input.constant !== undefined && input.constant !== null) {
  2122. const attr = new tf.proto.tensorflow.AttrValue();
  2123. attr.s = input.constant;
  2124. node.attr[index.toString()] = attr;
  2125. }
  2126. else if (input.list !== undefined) {
  2127. const attr = new tf.proto.tensorflow.AttrValue();
  2128. attr.list = new tf.proto.tensorflow.ListValue();
  2129. attr.list.s = input.list;
  2130. node.attr[index.toString()] = attr;
  2131. }
  2132. return !remove.has(input.name);
  2133. });
  2134. }
  2135. for (const node of node_map.values()) {
  2136. if (node.op === 'prim::GetAttr' && node.remove) {
  2137. node_map.delete(node.name);
  2138. }
  2139. if (node.op === 'prim::Constant' && node.remove) {
  2140. node_map.delete(node.name);
  2141. }
  2142. if (node.op === 'prim::ListConstruct' && node.remove) {
  2143. node_map.delete(node.name);
  2144. }
  2145. }
  2146. };
  2147. updatePyTorch(node_map);
  2148. for (const input of input_map.values()) {
  2149. context.inputs.push(input);
  2150. }
  2151. for (const node of node_map.values()) {
  2152. context.nodes.push(new tf.Node(metadata, namespaces, node, node.op, node.name, initializers, null));
  2153. }
  2154. return context;
  2155. }
  2156. };
  2157. tf.JsonReader = class {
  2158. static decodeGraphDef(json) {
  2159. const message = new tf.proto.tensorflow.GraphDef();
  2160. message.node = json.node.map((node) => tf.JsonReader.decodeNodeDef(node));
  2161. return message;
  2162. }
  2163. static decodeNodeDef(json) {
  2164. const message = new tf.proto.tensorflow.NodeDef();
  2165. message.name = json.name;
  2166. message.op = json.op;
  2167. message.input = json.input || [];
  2168. if (json.device) {
  2169. message.device = json.device;
  2170. }
  2171. message.attr = {};
  2172. if (json.attr) {
  2173. for (const key of Object.keys(json.attr)) {
  2174. message.attr[key] = tf.JsonReader.decodeAttrValue(json.attr[key]);
  2175. }
  2176. }
  2177. return message;
  2178. }
  2179. static decodeAttrValue(json) {
  2180. const message = new tf.proto.tensorflow.AttrValue();
  2181. const keys = Object.keys(json);
  2182. if (keys.length !== 1) {
  2183. throw new tf.Error("Unsupported JSON tensorflow.AttrValue '" + JSON.stringify(keys) + "'.");
  2184. }
  2185. const key = keys[0];
  2186. const value = json[key];
  2187. switch (key) {
  2188. case 'type':
  2189. message.type = tf.proto.tensorflow.DataType[value];
  2190. break;
  2191. case 'shape':
  2192. message.shape = tf.JsonReader.decodeTensorShapeProto(value);
  2193. break;
  2194. case 'tensor':
  2195. message.tensor = tf.JsonReader.decodeTensorProto(value);
  2196. break;
  2197. case 'b':
  2198. message[key] = value;
  2199. break;
  2200. case 'f':
  2201. message[key] = parseFloat(value);
  2202. break;
  2203. case 'i':
  2204. message[key] = parseInt(value, 10);
  2205. break;
  2206. case 's':
  2207. message[key] = typeof value === 'string' ? atob(value) : tf.Utility.decodeText(Uint8Array.from(value));
  2208. break;
  2209. case 'list':
  2210. message.list = tf.JsonReader.decodeAttrValueListValue(json.list);
  2211. break;
  2212. default:
  2213. throw new tf.Error("Unsupported JSON 'tensorflow.AttrValue." + key + "'.");
  2214. }
  2215. return message;
  2216. }
  2217. static decodeAttrValueListValue(json) {
  2218. const message = new tf.proto.tensorflow.AttrValue.ListValue();
  2219. const properties = Object.keys(json);
  2220. if (properties.length > 0) {
  2221. const keys = properties.filter((key) => Array.isArray(json[key]) && json[key].length > 0);
  2222. if (keys.length !== 1) {
  2223. throw new tf.Error("Unsupported JSON tensorflow.AttrValue.ListValue '" + JSON.stringify(keys) + "'.");
  2224. }
  2225. const key = keys[0];
  2226. const list = json[key];
  2227. switch (key) {
  2228. case 'i':
  2229. message[key] = list.map((value) => parseInt(value, 10));
  2230. break;
  2231. case 's':
  2232. message[key] = list.map((value) => typeof value === 'string' ? atob(value) : tf.Utility.decodeText(Uint8Array.from(value)));
  2233. break;
  2234. case 'type':
  2235. message[key] = list.map((value) => tf.proto.tensorflow.DataType[value]);
  2236. break;
  2237. default:
  2238. throw new tf.Error("Unsupported JSON 'tensorflow.AttrValue.ListValue." + key + "'.");
  2239. }
  2240. }
  2241. return message;
  2242. }
  2243. static decodeTensorProto(json) {
  2244. const message = new tf.proto.tensorflow.TensorProto();
  2245. message.dtype = tf.proto.tensorflow.DataType[json.dtype];
  2246. message.tensor_shape = tf.JsonReader.decodeTensorShapeProto(json.tensorShape);
  2247. return message;
  2248. }
  2249. static decodeTensorShapeProto(json) {
  2250. const message = new tf.proto.tensorflow.TensorShapeProto();
  2251. message.dim = (json.dim || []).map((json) => {
  2252. const message = new tf.proto.tensorflow.TensorShapeProto.Dim();
  2253. message.size = json.size;
  2254. message.name = json.name;
  2255. return message;
  2256. });
  2257. return message;
  2258. }
  2259. };
  2260. tf.Error = class extends Error {
  2261. constructor(message) {
  2262. super(message);
  2263. this.name = 'Error loading TensorFlow model.';
  2264. }
  2265. };
  2266. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  2267. module.exports.ModelFactory = tf.ModelFactory;
  2268. }