keras.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  1. var keras = keras || {};
  2. var json = json || require('./json');
  3. var python = python || require('./python');
  4. keras.ModelFactory = class {
  5. match(context) {
  6. const stream = context.stream;
  7. const signature = [ 0x89, 0x48, 0x44, 0x46, 0x0D, 0x0A, 0x1A, 0x0A ];
  8. if (stream && stream.length > signature.length && stream.peek(signature.length).every((value, index) => value === signature[index])) {
  9. return 'keras.h5';
  10. }
  11. if (context.open('json')) {
  12. const obj = context.open('json');
  13. if (obj.mxnet_version || (obj.nodes && obj.arg_nodes && obj.heads)) {
  14. return undefined;
  15. }
  16. if (obj.modelTopology && (obj.format === 'layers-model' || obj.modelTopology.class_name || obj.modelTopology.model_config)) {
  17. return 'keras.json.tfjs';
  18. }
  19. if (obj.model_config || (obj.class_name && obj.config)) {
  20. return 'keras.json';
  21. }
  22. if (Array.isArray(obj) && obj.every((item) => item.weights && item.paths)) {
  23. return 'keras.json.tfjs.weights';
  24. }
  25. if (obj.tfjsVersion) {
  26. return 'keras.json.tfjs.metadata';
  27. }
  28. }
  29. if (context.open('pkl')) {
  30. const obj = context.open('pkl');
  31. if (obj.__class__ && obj.__class__.__module__ === 'keras.engine.sequential' && obj.__class__.__name__ === 'Sequential') {
  32. return 'keras.pickle';
  33. }
  34. }
  35. return undefined;
  36. }
  37. open(context, match) {
  38. const openModel = (format, producer, backend, config, weights) => {
  39. return context.metadata('keras-metadata.json').then((metadata) => {
  40. return new keras.Model(metadata, format, producer, backend, config, weights);
  41. });
  42. };
  43. const openShards = (manifests, shards) => {
  44. const weights = new keras.Weights();
  45. 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 ] ]);
  46. for (const manifest of manifests) {
  47. let buffer = null;
  48. if (Array.isArray(manifest.paths) && manifest.paths.length > 0 && manifest.paths.every((path) => shards.has(path))) {
  49. const list = manifest.paths.map((path) => shards.get(path));
  50. const size = list.reduce((a, b) => a + b.length, 0);
  51. buffer = new Uint8Array(size);
  52. let offset = 0;
  53. for (const item of list) {
  54. buffer.set(item, offset);
  55. offset += item.length;
  56. }
  57. }
  58. let offset = 0;
  59. for (const weight of manifest.weights) {
  60. const dtype = weight.quantization && weight.quantization.dtype ? weight.quantization.dtype : weight.dtype;
  61. if (!dtype_size_map.has(dtype)) {
  62. throw new keras.Error("Unsupported weight data type size '" + dtype + "'.");
  63. }
  64. const itemsize = dtype_size_map.get(dtype);
  65. const size = weight.shape.reduce((a, b) => a * b, 1);
  66. const length = itemsize * size;
  67. const data = buffer ? buffer.slice(offset, offset + length) : null;
  68. weights.add(weight.identifier, new keras.Tensor(weight.name, weight.shape, dtype, weight.quantization, true, data));
  69. offset += length;
  70. }
  71. }
  72. return Promise.resolve(weights);
  73. };
  74. const openManifests = (manifests) => {
  75. const shards = new Map();
  76. for (const manifest of manifests) {
  77. for (const path of manifest.paths) {
  78. if (!shards.has(path)) {
  79. shards.set(path, context.request(path, null));
  80. }
  81. }
  82. }
  83. const promises = shards.values();
  84. return Promise.all(promises).then((streams) => {
  85. for (const key of shards.keys()) {
  86. shards.set(key, streams.shift().peek());
  87. }
  88. return openShards(manifests, shards);
  89. }).catch(() => {
  90. shards.clear();
  91. return openShards(manifests, shards);
  92. });
  93. };
  94. const openModelJson = (context, obj) => {
  95. const modelTopology = obj.modelTopology;
  96. const backend = modelTopology.backend || '';
  97. const format = 'TensorFlow.js ' + (obj.format ? obj.format : 'Keras' + (modelTopology.keras_version ? (' v' + modelTopology.keras_version) : ''));
  98. const producer = obj.convertedBy || obj.generatedBy || '';
  99. const manifests = obj.weightsManifest;
  100. for (const manifest of manifests) {
  101. for (const weight of manifest.weights) {
  102. weight.identifier = '';
  103. }
  104. }
  105. const model_config = modelTopology.model_config ? modelTopology.model_config : modelTopology;
  106. return openManifests(manifests).then((weights) => {
  107. return openModel(format, producer, backend, model_config, weights);
  108. });
  109. };
  110. const stream = context.stream;
  111. switch (match) {
  112. case 'keras.h5': {
  113. return context.require('./hdf5').then((hdf5) => {
  114. const find_root_group = (file) => {
  115. const root_group = file.rootGroup;
  116. const kerasmodel = root_group.group('model/kerasmodel');
  117. if (kerasmodel && kerasmodel.attributes.has('model_config')) {
  118. return kerasmodel;
  119. }
  120. return root_group;
  121. };
  122. const read_model_config = (group) => {
  123. if (group.attributes.has('model_config')) {
  124. const buffer = group.attributes.get('model_config');
  125. const reader = json.TextReader.open(buffer);
  126. if (reader) {
  127. return reader.read();
  128. }
  129. }
  130. return null;
  131. };
  132. const load_attributes_from_hdf5_group = (group, name) => {
  133. if (group.attributes.has(name)) {
  134. return group.attributes.get(name);
  135. }
  136. if (group.attributes.has(name + '0')) {
  137. let index = 0;
  138. let value = [];
  139. while (group.attributes.has(name + index.toString())) {
  140. const chunk = group.attributes.get(name + index.toString());
  141. value = value.concat(chunk);
  142. index++;
  143. }
  144. return value;
  145. }
  146. return null;
  147. };
  148. const weights = new keras.Weights();
  149. const file = hdf5.File.open(stream);
  150. const root_group = find_root_group(file);
  151. const model_config = read_model_config(root_group);
  152. if (model_config) {
  153. const backend = root_group.attributes.get('backend') || '';
  154. const version = root_group.attributes.get('keras_version') || '';
  155. const format = 'Keras' + (version ? ' v' + version : '');
  156. const model_weights_group = root_group.group('model_weights');
  157. if (model_weights_group) {
  158. const layer_names = load_attributes_from_hdf5_group(model_weights_group, 'layer_names');
  159. for (const layer_name of layer_names) {
  160. const layer_weights = model_weights_group.group(layer_name);
  161. if (layer_weights) {
  162. const weight_names = load_attributes_from_hdf5_group(layer_weights, 'weight_names');
  163. if (Array.isArray(weight_names) && weight_names.length > 0) {
  164. for (const weight_name of weight_names) {
  165. const weight = layer_weights.group(weight_name);
  166. if (weight && weight.value) {
  167. const variable = weight.value;
  168. const tensor = new keras.Tensor(weight_name, variable.shape, variable.type, null, variable.littleEndian, variable.data);
  169. weights.add(layer_name, tensor);
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. if (!model_config) {
  177. throw new keras.Error("'model_config' is not present.");
  178. }
  179. if (!model_config.class_name) {
  180. throw new keras.Error("'class_name' is not present.");
  181. }
  182. return openModel(format, '', backend, model_config, weights);
  183. }
  184. const layer_names = load_attributes_from_hdf5_group(root_group, 'layer_names');
  185. if (layer_names && Array.isArray(layer_names)) {
  186. const version = root_group.attributes.get('keras_version') || '';
  187. const format = 'Keras Weights' + (version ? ' v' + version : '');
  188. const backend = root_group.attributes.get('backend') || '';
  189. for (const layer_name of layer_names) {
  190. const layer_weights = root_group.group(layer_name);
  191. if (layer_weights) {
  192. const weight_names = load_attributes_from_hdf5_group(layer_weights, 'weight_names');
  193. if (Array.isArray(weight_names) && weight_names.length > 0) {
  194. for (const weight_name of weight_names) {
  195. const weight = layer_weights.group(weight_name);
  196. if (weight && weight.value) {
  197. const variable = weight.value;
  198. const components = weight_name.split('/');
  199. components.pop();
  200. const name = (components.length == 0 || components[0] !== layer_name) ? [ layer_name ].concat(components).join('/') : components.join('/');
  201. const tensor = new keras.Tensor(weight_name, variable.shape, variable.type, null, variable.littleEndian, variable.data);
  202. weights.add(name, tensor);
  203. }
  204. }
  205. }
  206. }
  207. }
  208. return openModel(format, '', backend, null, weights);
  209. }
  210. const rootKeys = new Set(root_group.attributes.keys());
  211. rootKeys.delete('nb_layers');
  212. if (rootKeys.size > 0 || root_group.value !== null) {
  213. throw new keras.Error('File format is not HDF5 Weights');
  214. }
  215. const format = 'HDF5 Weights';
  216. let weights_group = root_group;
  217. if (root_group.attributes.size === 0 && root_group.value === null && root_group.groups.size == 1) {
  218. const group = root_group.groups.values().next().value;
  219. if (group.attributes.size === 0 && group.value === null) {
  220. weights_group = group;
  221. }
  222. }
  223. const tensorKeys = new Set([ 'name', 'shape', 'quantization' ]);
  224. const groups = Array.from(weights_group.groups.values());
  225. if (groups.every((group) => group.attributes.size === 0 && group.groups.length == 0 && group.value !== null)) {
  226. for (const group of groups) {
  227. const variable = group.value;
  228. const tensor = new keras.Tensor(group.name, variable.shape, variable.type, null, variable.littleEndian, variable.type === 'string' ? variable.value : variable.data);
  229. weights.add('', tensor);
  230. }
  231. return openModel(format, '', '', null, weights);
  232. }
  233. if (groups.every((group) => group.value === null && Array.from(group.attributes.keys()).filter((key) => !tensorKeys.has(key)).length === 0 && Array.from(group.groups.values()).every((variable) => Object.keys(variable.attributes).length === 0 && variable.value !== null))) {
  234. for (const group of groups) {
  235. const moduleName = group.attributes.has('name') ? group.attributes.get('name') : group.name;
  236. for (const variableGroup of group.groups.values()) {
  237. if (variableGroup.attributes.size !== 0 || variableGroup.groups.size !== 0) {
  238. throw new keras.Error('Variable format is not HDF5 Weights');
  239. }
  240. const variable = variableGroup.value;
  241. if (!variable) {
  242. throw new keras.Error('Variable value is not HDF5 Weights');
  243. }
  244. const name = moduleName ? [ moduleName, variableGroup.name ].join('/') : moduleName.name;
  245. const tensor = new keras.Tensor(name, variable.shape, variable.type, null, variable.littleEndian, variable.type === 'string' ? variable.value : variable.data);
  246. weights.add(moduleName, tensor);
  247. }
  248. }
  249. return openModel(format, '', '', null, weights);
  250. }
  251. const walk = function(group) {
  252. if (group.attributes.size === 0 && group.value === null && group.groups.size > 0) {
  253. for (const subGroup of group.groups.values()) {
  254. walk(subGroup);
  255. }
  256. return;
  257. }
  258. const subKeys = new Set([ 'index', 'need_grad' ]);
  259. const attribtues = Array.from(group.attributes.keys());
  260. const match = attribtues.filter((key) => !subKeys.has(key)).length === 0;
  261. if (match && group.value !== null && group.groups.size === 0) {
  262. const variable = group.value;
  263. const variableName = group.path;
  264. let moduleName = variableName;
  265. const parts = variableName.split('/');
  266. if (parts.length > 1) {
  267. parts.pop();
  268. moduleName = parts.join('/');
  269. }
  270. const tensor = new keras.Tensor(variableName, variable.shape, variable.type, null, variable.littleEndian, variable.type === 'string' ? variable.value : variable.data);
  271. weights.add(moduleName, tensor);
  272. return;
  273. }
  274. throw new keras.Error('Module group format is not HDF5 Weights');
  275. };
  276. walk(weights_group);
  277. return openModel(format, '', '', null, weights);
  278. });
  279. }
  280. case 'keras.json': {
  281. const obj = context.open('json');
  282. const format = 'Keras' + (obj.keras_version ? ' v' + obj.keras_version : '');
  283. const backend = obj.backend || '';
  284. const config = obj.model_config ? obj.model_config : obj;
  285. const weights = new keras.Weights();
  286. return openModel(format, '', backend, config, weights);
  287. }
  288. case 'keras.json.tfjs': {
  289. const obj = context.open('json');
  290. return openModelJson(context, obj);
  291. }
  292. case 'keras.json.tfjs.weights': {
  293. const obj = context.open('json');
  294. const manifests = [];
  295. const format = 'TensorFlow.js Weights';
  296. manifests.push(...obj);
  297. for (const manifest of manifests) {
  298. for (const weight of manifest.weights) {
  299. const parts = weight.name.split('/');
  300. parts.pop();
  301. weight.identifier = parts.join('/');
  302. }
  303. }
  304. return openManifests(manifests).then((weights) => {
  305. return openModel(format, '', '', null, weights);
  306. });
  307. }
  308. case 'keras.json.tfjs.metadata': {
  309. return context.request('model.json').then((buffer) => {
  310. const reader = json.TextReader.open(buffer);
  311. const obj = reader.read();
  312. return openModelJson(context, obj);
  313. });
  314. }
  315. case 'keras.pickle': {
  316. const execution = new python.Execution(null);
  317. const obj = context.open('pkl');
  318. const decoder = new TextDecoder('utf-8');
  319. const format = 'Keras Pickle' + (obj.keras_version ? ' v' + decoder.decode(obj.keras_version) : '');
  320. const backend = obj.backend ? decoder.decode(obj.backend) : '';
  321. const reader = json.TextReader.open(obj.model_config);
  322. const model_config = reader.read();
  323. const weights = new keras.Weights();
  324. const model_weights_group = obj.model_weights;
  325. if (model_weights_group) {
  326. const layer_names = model_weights_group.layer_names.map((buffer) => decoder.decode(buffer));
  327. for (const layer_name of layer_names) {
  328. const layer_weights = model_weights_group[layer_name];
  329. if (layer_weights) {
  330. const weight_names = layer_weights.weight_names.map((buffer) => decoder.decode(buffer));
  331. if (Array.isArray(weight_names) && weight_names.length > 0) {
  332. for (const weight_name of weight_names) {
  333. const buffer = layer_weights[weight_name];
  334. const unpickler = python.Unpickler.open(buffer, execution);
  335. const variable = unpickler.load();
  336. const tensor = new keras.Tensor(weight_name, variable.shape, variable.dtype.__name__, null, true, variable.data);
  337. weights.add(layer_name, tensor);
  338. }
  339. }
  340. }
  341. }
  342. }
  343. return openModel(format, '', backend, model_config, weights);
  344. }
  345. default: {
  346. throw new keras.Error("Unsupported Keras format '" + match + "'.");
  347. }
  348. }
  349. }
  350. };
  351. keras.Model = class {
  352. constructor(metadata, format, producer, backend, config, weights) {
  353. this._format = format;
  354. this._backend = backend;
  355. this._producer = producer;
  356. metadata = new keras.GraphMetadata(metadata);
  357. this._graphs = [ new keras.Graph(metadata, config, weights) ];
  358. }
  359. get name() {
  360. return null;
  361. }
  362. get description() {
  363. return null;
  364. }
  365. get format() {
  366. return this._format;
  367. }
  368. get producer() {
  369. return this._producer;
  370. }
  371. get runtime() {
  372. return this._backend;
  373. }
  374. get graphs() {
  375. return this._graphs;
  376. }
  377. };
  378. keras.Graph = class {
  379. constructor(metadata, config, weights, group) {
  380. this._metadata = metadata;
  381. this._inputs = [];
  382. this._outputs = [];
  383. this._nodes = [];
  384. group = group || '';
  385. const loadNode = (layer, inputs, outputs, weights, group) => {
  386. layer = Object.assign({}, layer);
  387. layer.inputs = inputs;
  388. layer.outputs = outputs;
  389. return new keras.Node(this._metadata, layer, group, weights);
  390. };
  391. const getInputType = (layer) => {
  392. if (layer && layer.config) {
  393. let dataType = '?';
  394. let shape = [];
  395. const config = layer.config;
  396. if (config.dtype) {
  397. dataType = config.dtype;
  398. delete config.dtype;
  399. }
  400. if (config.batch_input_shape) {
  401. shape = config.batch_input_shape.map(s => s == null ? '?' : s);
  402. delete config.batch_input_shape;
  403. }
  404. return new keras.TensorType(dataType, new keras.TensorShape(shape));
  405. }
  406. return null;
  407. };
  408. if (config) {
  409. this._name = config.name || (config.config && config.config.name ? config.config.name : '');
  410. const is_connection = (item) => {
  411. return Array.isArray(item) && (item.length === 3 || item.length === 4) && typeof item[0] === 'string' && typeof item[1] === 'number' && typeof item[2] === 'number';
  412. };
  413. switch (config.class_name) {
  414. case 'AllCNN':
  415. case 'Sequential': {
  416. config = config.config;
  417. const inputs = null;
  418. const outputs = null;
  419. const inputName = 'input';
  420. let inputType = null;
  421. let argument = inputName;
  422. let index = 0;
  423. const layers = config.layers ? config.layers : config;
  424. for (const layer of layers) {
  425. let name = index.toString();
  426. let nodeInputs = [ { name: argument } ];
  427. if (index == 0) {
  428. if (inputs && inputs.length > 0) {
  429. nodeInputs = [ inputs[0] ];
  430. }
  431. else {
  432. inputType = getInputType(layer);
  433. }
  434. }
  435. index++;
  436. if (layer.config && layer.config.name) {
  437. name = layer.config.name;
  438. }
  439. argument = name;
  440. let nodeOutputs = [ argument ];
  441. if (index == layers.length) {
  442. if (outputs && outputs.length > 0) {
  443. nodeOutputs = [ outputs[0] ];
  444. argument = null;
  445. }
  446. }
  447. this.nodes.push(loadNode(layer, nodeInputs, nodeOutputs, weights, group));
  448. }
  449. if (!inputs) {
  450. this._inputs.push(new keras.Parameter(inputName, true, [ new keras.Argument(inputName, inputType, null) ]));
  451. }
  452. if (argument) {
  453. this._outputs.push(new keras.Parameter(argument, true, [ new keras.Argument(argument, null, null) ]));
  454. }
  455. break;
  456. }
  457. case 'Functional':
  458. case 'Model': {
  459. config = config.config;
  460. const nodes = new Map();
  461. if (config.layers) {
  462. for (const layer of config.layers) {
  463. layer.inputs = [];
  464. layer.outputs = [];
  465. layer.args = {};
  466. if (layer.name && !nodes.has(layer.name)) {
  467. nodes.set(layer.name, layer);
  468. }
  469. }
  470. const read_connection = (input_data) => {
  471. let name = input_data[0];
  472. const node = nodes.get(name);
  473. if (node) {
  474. // const node_index = input_data[1];
  475. const tensor_index = input_data[2];
  476. if (tensor_index !== 0) {
  477. name += ':' + tensor_index.toString();
  478. }
  479. while (tensor_index >= node.outputs.length) {
  480. node.outputs.push('');
  481. }
  482. node.outputs[tensor_index] = name;
  483. }
  484. return { name: name };
  485. };
  486. const read_value = (input_data) => {
  487. const array_size = (value) => {
  488. if (value.every((item) => Array.isArray(item) && item.length > 2 && item[0] === '_CONSTANT_VALUE' && item[1] === -1)) {
  489. for (let i = 0; i < value.length; i++) {
  490. value[i] = value[i][2];
  491. }
  492. }
  493. else if (value.every((item) => Array.isArray(item))) {
  494. const dims = value.map((item) => array_size(item));
  495. const dim = dims[0];
  496. for (let i = 1; i < dims.length; i++) {
  497. if (dim.length === dims[i].length) {
  498. if (!dims[i].every((value, i) => value ===dim[i])) {
  499. throw new python.Error('Invalid array shape.');
  500. }
  501. }
  502. }
  503. return [ value.length ].concat(dim);
  504. }
  505. return [ value.length ];
  506. };
  507. if (Array.isArray(input_data)) {
  508. const shape = array_size(input_data);
  509. return { shape: shape, value: input_data };
  510. }
  511. return { value: input_data };
  512. };
  513. for (const layer of config.layers) {
  514. if (layer.inbound_nodes) {
  515. for (const inbound_node of layer.inbound_nodes) {
  516. if (is_connection(inbound_node)) {
  517. layer.inputs.push(read_connection(inbound_node));
  518. const args = inbound_node[3] || {};
  519. layer.args = {};
  520. for (const entry of Object.entries(args)) {
  521. const key = entry[0];
  522. const value = entry[1];
  523. layer.args[key] = is_connection(value) ? read_connection(value) : read_value(value);
  524. }
  525. }
  526. else if (Array.isArray(inbound_node)) {
  527. for (const input_data of inbound_node) {
  528. if (is_connection(input_data)) {
  529. layer.inputs.push(read_connection(input_data));
  530. }
  531. else if (Array.isArray(input_data) && input_data.every((item) => is_connection(item))) {
  532. for (const input of input_data) {
  533. layer.inputs.push(read_connection(input));
  534. }
  535. }
  536. else if (Array.isArray(input_data)) {
  537. layer.inputs.push(read_value(input_data));
  538. }
  539. else {
  540. throw new keras.Error("Invalid inbound connection '" + JSON.stringify(input_data) + "'.");
  541. }
  542. }
  543. }
  544. else {
  545. throw new keras.Error("Invalid inbound node '" + JSON.stringify(inbound_node) + "'.");
  546. }
  547. }
  548. }
  549. }
  550. }
  551. const input_layers = is_connection(config.input_layers) ? [ config.input_layers ] : config.input_layers;
  552. if (input_layers) {
  553. for (let i = 0; i < input_layers.length; i++) {
  554. const input_layer = input_layers[i];
  555. const name = input_layer[0];
  556. let type = null;
  557. const node = nodes.get(name);
  558. if (node && node.class_name == 'InputLayer') {
  559. type = getInputType(node);
  560. nodes.delete(name);
  561. }
  562. const argument = new keras.Argument(name, type, null);
  563. const parameter = new keras.Parameter(name, true, [ argument ]);
  564. this._inputs.push(parameter);
  565. }
  566. }
  567. const output_layers = is_connection(config.output_layers) ? [ config.output_layers ] : config.output_layers;
  568. if (output_layers) {
  569. for (let j = 0; j < output_layers.length; j++) {
  570. const output_layer = output_layers[j];
  571. let outputName = output_layer[0];
  572. const outputNode = nodes.get(outputName);
  573. if (outputNode) {
  574. const outputIndex = output_layer[2];
  575. if (outputIndex != 0) {
  576. outputName += ':' + outputIndex.toString();
  577. }
  578. while (outputIndex >= outputNode.outputs.length) {
  579. outputNode.outputs.push('');
  580. }
  581. outputNode.outputs[outputIndex] = outputName;
  582. }
  583. const argument = new keras.Argument(outputName, null, null);
  584. const parameter = new keras.Parameter(outputName, true, [ argument ]);
  585. this._outputs.push(parameter);
  586. }
  587. }
  588. if (config.layers) {
  589. for (const layer of config.layers) {
  590. if (nodes.has(layer.name)) {
  591. this._nodes.push(loadNode(layer, layer.inputs, layer.outputs, weights, group));
  592. }
  593. }
  594. }
  595. break;
  596. }
  597. default:
  598. throw new keras.Error('\'' + config.class_name + '\' is not supported.');
  599. }
  600. }
  601. else if (weights) {
  602. for (const name of weights.keys()) {
  603. if (weights.get('', name).length <= 6) {
  604. const layer = { class_name: 'Weights', config: { name: name } };
  605. const node = new keras.Node(metadata, layer, '', weights);
  606. this._nodes.push(node);
  607. }
  608. }
  609. }
  610. }
  611. get name() {
  612. return this._name;
  613. }
  614. get inputs() {
  615. return this._inputs;
  616. }
  617. get outputs() {
  618. return this._outputs;
  619. }
  620. get nodes() {
  621. return this._nodes;
  622. }
  623. };
  624. keras.Parameter = class {
  625. constructor(name, visible, args) {
  626. this._name = name;
  627. this._visible = visible;
  628. this._arguments = args;
  629. }
  630. get name() {
  631. return this._name;
  632. }
  633. get visible() {
  634. return this._visible;
  635. }
  636. get arguments() {
  637. return this._arguments;
  638. }
  639. };
  640. keras.Argument = class {
  641. constructor(name, type, initializer) {
  642. if (typeof name !== 'string') {
  643. throw new keras.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
  644. }
  645. this._name= name;
  646. this._type = type || null;
  647. this._initializer = initializer || null;
  648. }
  649. get name() {
  650. return this._name;
  651. }
  652. get type() {
  653. if (this._initializer) {
  654. return this._initializer.type;
  655. }
  656. return this._type;
  657. }
  658. get quantization() {
  659. if (this._initializer) {
  660. return this._initializer.quantization;
  661. }
  662. return null;
  663. }
  664. get initializer() {
  665. return this._initializer;
  666. }
  667. };
  668. keras.Node = class {
  669. constructor(metadata, layer, group, weights) {
  670. const config = layer.config || {};
  671. const args = layer.args || {};
  672. let inputs = layer.inputs || [];
  673. let outputs = layer.outputs || [];
  674. const name = config && config.name ? config.name : '';
  675. this._group = group || '';
  676. this._name = (this._group ? this._group + '/' : '') + name;
  677. this._inputs = [];
  678. this._outputs = [];
  679. this._attributes = [];
  680. this._chain = [];
  681. let names = [ name ];
  682. let type = layer.class_name;
  683. let model = false;
  684. switch (type) {
  685. case 'Model':
  686. case 'Functional':
  687. case 'Sequential': {
  688. const name = layer.name || (layer.config ? layer.config.name : '');
  689. this._type = new keras.Graph(metadata, layer, weights, (group ? group + '/' : '') + name);
  690. model = true;
  691. if (config) {
  692. delete config.layers;
  693. delete config.input_layers;
  694. delete config.output_layers;
  695. }
  696. this._inputs = [ new keras.Parameter('inputs', true, inputs.map((input) => new keras.Argument(input.name, null, null))) ];
  697. this._outputs = [ new keras.Parameter('outputs', true, outputs.map((name) => new keras.Argument(name, null, null))) ];
  698. inputs = [];
  699. outputs = [];
  700. break;
  701. }
  702. case 'Bidirectional':
  703. case 'TimeDistributed': {
  704. if (config && config.layer) {
  705. const inner = config.layer;
  706. delete config.layer;
  707. this._inner = new keras.Node(metadata, inner, null, null);
  708. if (type == 'Bidirectional' && inner.config.name) {
  709. names = [ name + '/forward_' + inner.config.name, name + '/backward_' + inner.config.name ];
  710. if (!group) {
  711. group = name;
  712. }
  713. }
  714. }
  715. this._type = metadata.type(type) || { name: type };
  716. break;
  717. }
  718. case 'TFOpLambda': {
  719. if (config && config.function) {
  720. type = config.function;
  721. delete config.function;
  722. }
  723. this._type = metadata.type(type) || { name: type };
  724. break;
  725. }
  726. default: {
  727. this._type = metadata.type(type) || { name: type };
  728. break;
  729. }
  730. }
  731. const initializers = {};
  732. if (weights && !model) {
  733. for (const name of names) {
  734. let tensors = weights.get(group, name);
  735. if (tensors.length > 0) {
  736. for (const initializer of tensors) {
  737. inputs.push({ name: initializer.name });
  738. initializers[initializer.name] = initializer;
  739. }
  740. }
  741. else {
  742. tensors = weights.get('', name);
  743. for (const initializer of tensors) {
  744. inputs.push({ name: initializer.name });
  745. initializers[initializer.name] = initializer;
  746. }
  747. }
  748. }
  749. }
  750. if (config && !Array.isArray(config)) {
  751. for (const entry of Object.entries(config)) {
  752. const name = entry[0];
  753. const value = entry[1];
  754. if (name === 'activation' && value !== 'linear') {
  755. if (typeof value === 'string') {
  756. const set = new Map([ [ 'elu', 'ELU' ], [ 'exponential', 'Exponential' ], [ 'hard_sigmoid', 'HardSigmoid' ], [ 'linear', 'Linear' ], [ 'relu', 'ReLU' ], [ 'selu', 'SELU' ], [ 'softmax', 'Softmax'], [ 'sigmoid', 'Sigmoid' ], [ 'softplus', 'SoftPlus' ], [ 'softsign', 'SoftSign' ], [ 'tanh', 'TanH' ] ]);
  757. const type = set.has(value) ? set.get(value) : value;
  758. this.chain.push(new keras.Node(metadata, { class_name: type }, null, null));
  759. }
  760. else if (value && typeof value.class_name === 'string' && value.config) {
  761. const type = value.class_name;
  762. if (!metadata.type(type)) {
  763. metadata.add(type, { name: type, category: 'Activation' });
  764. }
  765. this.chain.push(new keras.Node(metadata, value, null, null));
  766. }
  767. }
  768. if (name !== 'name') {
  769. const attribute = new keras.Attribute(metadata.attribute(type, name), name, value);
  770. this._attributes.push(attribute);
  771. }
  772. }
  773. }
  774. const innerType = this.inner ? this.inner.type : null;
  775. const innerSchema = innerType ? metadata.type(innerType) : null;
  776. let inputIndex = 0;
  777. while (inputs.length > 0) {
  778. let list = false;
  779. let inputName = null;
  780. let visible = true;
  781. if (!innerSchema || inputIndex == 0) {
  782. if (this._type && this._type.inputs && inputIndex < this._type.inputs.length) {
  783. const input = this._type.inputs[inputIndex];
  784. inputName = input.name;
  785. if (type === 'BatchNormalization' && inputName === 'gamma' && config.scale === false) {
  786. inputIndex++;
  787. continue;
  788. }
  789. visible = input.visible == false ? false : true;
  790. if (this._type.inputs[inputIndex].list) {
  791. list = true;
  792. }
  793. }
  794. }
  795. else {
  796. switch (type) {
  797. case 'Bidirectional': {
  798. let innerIndex = inputIndex;
  799. if (innerSchema && innerSchema.inputs) {
  800. if (innerIndex < innerSchema.inputs.length) {
  801. inputName = 'forward_' + innerSchema.inputs[innerIndex].name;
  802. }
  803. else {
  804. innerIndex = innerIndex - innerSchema.inputs.length + 1;
  805. if (innerIndex < innerSchema.inputs.length) {
  806. inputName = 'backward_' + innerSchema.inputs[innerIndex].name;
  807. }
  808. }
  809. }
  810. visible = false;
  811. break;
  812. }
  813. case 'TimeDistributed':
  814. if (innerSchema && innerSchema.inputs && inputIndex < innerSchema.inputs.length) {
  815. inputName = innerSchema.inputs[inputIndex].name;
  816. }
  817. break;
  818. default:
  819. break;
  820. }
  821. }
  822. const input = !list ? [ inputs.shift() ] : inputs.splice(0, inputs.length);
  823. const inputArguments = input.map((input) => {
  824. if (input.name) {
  825. return new keras.Argument(input.name, null, initializers[input.name]);
  826. }
  827. if (input.value) {
  828. const tensor = new keras.Tensor('', input.shape, config.dtype || '?', null, undefined, input.value);
  829. return new keras.Argument('', null, tensor);
  830. }
  831. throw new keras.Error("Invalid argument '" + JSON.stringify(input.name) + "'.");
  832. });
  833. if (!inputName && inputArguments.length == 1 && inputArguments[0].initializer && inputArguments[0].initializer.name) {
  834. if (names.length === 1 && names[0] === '') {
  835. inputName = inputArguments[0].initializer.name;
  836. }
  837. else {
  838. const parts = inputArguments[0].initializer.name.split('/').pop().split(':').shift().split('_');
  839. const inputName1 = parts.pop();
  840. const inputName2 = parts.length > 0 ? [ parts.pop(), inputName1 ].join('_') : '';
  841. const inputNames = new Set([ 'recurrent_kernel', 'running_mean', 'running_std', 'moving_mean', 'moving_variance', 'depthwise_filter', 'pointwise_filter' ]);
  842. inputName = inputNames.has(inputName2) ? inputName2 : inputName1;
  843. }
  844. }
  845. this._inputs.push(new keras.Parameter(inputName || inputIndex.toString(), visible, inputArguments));
  846. inputIndex++;
  847. }
  848. for (let i = 0; i < outputs.length; i++) {
  849. const output = outputs[i];
  850. const outputName = (this._type && this._type.outputs && i < this._type.outputs.length && this._type.outputs[i] && this._type.outputs[i].name) ? this._type.outputs[i].name : i.toString();
  851. const parameter = new keras.Parameter(outputName, true, [ new keras.Argument(output, null, null) ]);
  852. this._outputs.push(parameter);
  853. }
  854. const inputTypes = new Map((this._type.inputs || []).map((input) => [ input.name, input.type ]));
  855. for (const entry of Object.entries(args)) {
  856. const name = entry[0];
  857. const value = entry[1];
  858. if (name !== 'name') {
  859. if (value.name || (inputTypes.has(name) && inputTypes.get(name) === 'Tensor' && value)) {
  860. if (value.name) {
  861. const argument = new keras.Argument(value.name, null, null);
  862. const parameter = new keras.Parameter(name, true, [ argument ]);
  863. this._inputs.push(parameter);
  864. }
  865. else {
  866. const tensor = new keras.Tensor('', value.shape, config.dtype || '?', null, undefined, value.value);
  867. const argument = new keras.Argument('', null, tensor);
  868. const parameter = new keras.Parameter(name, true, [ argument ]);
  869. this._inputs.push(parameter);
  870. }
  871. }
  872. else {
  873. const attribute = new keras.Attribute(metadata.attribute(type, name), name, value);
  874. this._attributes.push(attribute);
  875. }
  876. }
  877. }
  878. if (typeof this.type.name !== 'string' || !this.type.name.split) { // #416
  879. throw new keras.Error("Unsupported node type '" + JSON.stringify(this.type.name) + "'.");
  880. }
  881. }
  882. get type() {
  883. return this._type;
  884. }
  885. get name() {
  886. return this._name;
  887. }
  888. get inputs() {
  889. return this._inputs;
  890. }
  891. get outputs() {
  892. return this._outputs;
  893. }
  894. get attributes() {
  895. return this._attributes;
  896. }
  897. get chain() {
  898. return this._chain;
  899. }
  900. get inner() {
  901. return this._inner;
  902. }
  903. };
  904. keras.Attribute = class {
  905. constructor(metadata, name, value) {
  906. this._name = name;
  907. this._value = value;
  908. if (value && typeof value == 'object' && value.class_name && value.config) {
  909. this._value = keras.Attribute._convert(value);
  910. }
  911. switch (name) {
  912. case 'trainable':
  913. this._type = 'boolean';
  914. this._visible = false;
  915. break;
  916. case 'dtype':
  917. this._visible = false;
  918. break;
  919. default: {
  920. if (metadata) {
  921. if (metadata.type) {
  922. this._type = metadata.type;
  923. }
  924. if (Object.prototype.hasOwnProperty.call(metadata, 'visible')) {
  925. this._visible = metadata.visible;
  926. }
  927. else if (metadata.default !== undefined) {
  928. if (Array.isArray(value)) {
  929. if (Array.isArray(metadata.default)) {
  930. this._visible = value.length !== metadata.default || !this.value.every((item, index) => item == metadata.default[index]);
  931. }
  932. else {
  933. this._visible = !this.value.every((item) => item == metadata.default);
  934. }
  935. }
  936. else {
  937. this._visible = this.value !== metadata.default;
  938. }
  939. }
  940. }
  941. break;
  942. }
  943. }
  944. }
  945. get name() {
  946. return this._name;
  947. }
  948. get type() {
  949. return this._type;
  950. }
  951. get value() {
  952. return this._value;
  953. }
  954. get visible() {
  955. return this._visible == false ? false : true;
  956. }
  957. static _convert(value) {
  958. if (Array.isArray(value) || value !== Object(value)) {
  959. return value;
  960. }
  961. const obj = {};
  962. if (value.class_name) {
  963. obj.__type__ = value.class_name;
  964. }
  965. if (value.config) {
  966. for (const entry of Object.entries(value.config)) {
  967. const key = entry[0];
  968. const value = entry[1];
  969. obj[key] = keras.Attribute._convert(value);
  970. }
  971. }
  972. return obj;
  973. }
  974. };
  975. keras.Tensor = class {
  976. constructor(name, shape, type, quantization, littleEndian, data) {
  977. this._name = name;
  978. this._type = new keras.TensorType(type, new keras.TensorShape(shape));
  979. this._quantization = quantization;
  980. this._littleEndian = littleEndian;
  981. this._data = data;
  982. }
  983. get kind() {
  984. return 'Weights';
  985. }
  986. get name() {
  987. return this._name;
  988. }
  989. get type() {
  990. return this._type;
  991. }
  992. get quantization() {
  993. if (this._quantization && (this._quantization.scale !== 0 || this._quantization.min !== 0)) {
  994. const scale = this._quantization.scale || 0;
  995. const min = this._quantization.min || 0;
  996. return scale.toString() + ' * ' + (min == 0 ? 'q' : ('(q - ' + min.toString() + ')'));
  997. }
  998. return null;
  999. }
  1000. get state() {
  1001. if (Array.isArray(this._data)) {
  1002. return '';
  1003. }
  1004. return this._context().state;
  1005. }
  1006. get value() {
  1007. if (Array.isArray(this._data)) {
  1008. return this._data;
  1009. }
  1010. const context = this._context();
  1011. if (context.state) {
  1012. return null;
  1013. }
  1014. context.limit = Number.MAX_SAFE_INTEGER;
  1015. return this._decode(context, 0);
  1016. }
  1017. toString() {
  1018. if (Array.isArray(this._data)) {
  1019. return keras.Tensor._stringify(this._data, '', ' ');
  1020. }
  1021. const context = this._context();
  1022. if (context.state) {
  1023. return '';
  1024. }
  1025. context.limit = 10000;
  1026. const value = this._decode(context, 0);
  1027. return keras.Tensor._stringify(value, '', ' ');
  1028. }
  1029. _context() {
  1030. const context = {};
  1031. context.index = 0;
  1032. context.count = 0;
  1033. context.state = null;
  1034. if (!this._data) {
  1035. context.state = 'Tensor data is empty.';
  1036. return context;
  1037. }
  1038. try {
  1039. context.data = this._data instanceof Uint8Array ? this._data : this._data.peek();
  1040. }
  1041. catch (err) {
  1042. context.state = err.message;
  1043. return context;
  1044. }
  1045. switch (this._type.dataType) {
  1046. case 'boolean':
  1047. case 'float16':
  1048. case 'float32':
  1049. case 'float64':
  1050. case 'uint8':
  1051. case 'int8':
  1052. case 'int32':
  1053. case 'int64': {
  1054. context.dataType = this._type.dataType;
  1055. context.view = new DataView(context.data.buffer, context.data.byteOffset, context.data.byteLength);
  1056. context.littleEndian = this._littleEndian;
  1057. break;
  1058. }
  1059. case 'string':
  1060. context.dataType = this._type.dataType;
  1061. break;
  1062. default:
  1063. context.state = "Tensor data type '" + this._type.dataType + "' is not supported.";
  1064. break;
  1065. }
  1066. context.shape = this._type.shape.dimensions;
  1067. return context;
  1068. }
  1069. _decode(context, dimension) {
  1070. const shape = context.shape.length !== 0 ? context.shape : [ 1 ];
  1071. const results = [];
  1072. const size = shape[dimension];
  1073. const littleEndian = context.littleEndian;
  1074. if (dimension == shape.length - 1) {
  1075. for (let i = 0; i < size; i++) {
  1076. if (context.count > context.limit) {
  1077. results.push(null);
  1078. return results;
  1079. }
  1080. switch (context.dataType) {
  1081. case 'float16':
  1082. results.push(context.view.getFloat16(context.index, littleEndian));
  1083. context.index += 2;
  1084. break;
  1085. case 'float32':
  1086. results.push(context.view.getFloat32(context.index, littleEndian));
  1087. context.index += 4;
  1088. break;
  1089. case 'float64':
  1090. results.push(context.view.getFloat64(context.index, littleEndian));
  1091. context.index += 8;
  1092. break;
  1093. case 'boolean':
  1094. results.push(context.view.getInt8(context.index) !== 0);
  1095. context.index += 1;
  1096. break;
  1097. case 'uint8':
  1098. results.push(context.view.getUint8(context.index));
  1099. context.index += 1;
  1100. break;
  1101. case 'int8':
  1102. results.push(context.view.getInt8(context.index));
  1103. context.index += 1;
  1104. break;
  1105. case 'int32':
  1106. results.push(context.view.getInt32(context.index, littleEndian));
  1107. context.index += 4;
  1108. break;
  1109. case 'int64':
  1110. results.push(context.view.getInt64(context.index, littleEndian));
  1111. context.index += 8;
  1112. break;
  1113. case 'string':
  1114. results.push(context.view[context.index]);
  1115. context.index++;
  1116. break;
  1117. default:
  1118. throw new keras.Error("Unsupported tensor data type '" + context.dataType + "'.");
  1119. }
  1120. context.count++;
  1121. }
  1122. }
  1123. else {
  1124. for (let j = 0; j < size; j++) {
  1125. if (context.count > context.limit) {
  1126. results.push(null);
  1127. return results;
  1128. }
  1129. results.push(this._decode(context, dimension + 1));
  1130. }
  1131. }
  1132. if (context.shape.length == 0) {
  1133. return results[0];
  1134. }
  1135. return results;
  1136. }
  1137. static _stringify(value, indentation, indent) {
  1138. if (Array.isArray(value)) {
  1139. const result = [];
  1140. result.push(indentation + '[');
  1141. const items = value.map((item) => keras.Tensor._stringify(item, indentation + indent, indent));
  1142. if (items.length > 0) {
  1143. result.push(items.join(',\n'));
  1144. }
  1145. result.push(indentation + ']');
  1146. return result.join('\n');
  1147. }
  1148. if (value === null) {
  1149. return indentation + '...';
  1150. }
  1151. if (typeof value == 'string') {
  1152. return indentation + '"' + value + '"';
  1153. }
  1154. if (value == Infinity) {
  1155. return indentation + 'Infinity';
  1156. }
  1157. if (value == -Infinity) {
  1158. return indentation + '-Infinity';
  1159. }
  1160. if (isNaN(value)) {
  1161. return indentation + 'NaN';
  1162. }
  1163. return indentation + value.toString();
  1164. }
  1165. };
  1166. keras.TensorType = class {
  1167. constructor(dataType, shape) {
  1168. this._dataType = dataType;
  1169. this._shape = shape;
  1170. }
  1171. get dataType() {
  1172. return this._dataType;
  1173. }
  1174. get shape() {
  1175. return this._shape;
  1176. }
  1177. toString() {
  1178. return this._dataType + this._shape.toString();
  1179. }
  1180. };
  1181. keras.TensorShape = class {
  1182. constructor(dimensions) {
  1183. this._dimensions = dimensions;
  1184. }
  1185. get dimensions() {
  1186. return this._dimensions;
  1187. }
  1188. toString() {
  1189. return this._dimensions ? ('[' + this._dimensions.map((dimension) => dimension.toString()).join(',') + ']') : '';
  1190. }
  1191. };
  1192. keras.GraphMetadata = class {
  1193. constructor(metadata) {
  1194. this._metadata = metadata;
  1195. this._types = new Map();
  1196. }
  1197. type(name) {
  1198. if (this._types.has(name)) {
  1199. return this._types.get(name);
  1200. }
  1201. return this._metadata.type(name);
  1202. }
  1203. attribute(type, name) {
  1204. return this._metadata.attribute(type, name);
  1205. }
  1206. add(type, metadata) {
  1207. this._types.set(type, metadata);
  1208. }
  1209. };
  1210. keras.Weights = class {
  1211. constructor() {
  1212. this._map = new Map();
  1213. }
  1214. add(layer_name, tensor) {
  1215. if (!this._map.has(layer_name)) {
  1216. this._map.set(layer_name, []);
  1217. }
  1218. this._map.get(layer_name).push(tensor);
  1219. }
  1220. get(group, name) {
  1221. if (group) {
  1222. const list = this._map.get(group.split('/').shift());
  1223. if (list) {
  1224. const match1 = list.filter((tensor) => tensor.name.startsWith(name + '/'));
  1225. if (match1.length > 0) {
  1226. return match1;
  1227. }
  1228. const match2 = list.filter((tensor) => tensor.name.startsWith(group + '/' + name + '/'));
  1229. if (match2.length > 0) {
  1230. return match2;
  1231. }
  1232. }
  1233. }
  1234. else {
  1235. const match1 = this._map.get(name);
  1236. if (match1 && match1.length > 0) {
  1237. return match1;
  1238. }
  1239. const match2 = this._map.get('');
  1240. if (match2 && match2.length > 0) {
  1241. const match3 = match2.filter((tensor) => tensor.name.startsWith((group ? group + '/' : '') + name + '/'));
  1242. if (match3.length > 0) {
  1243. return match3;
  1244. }
  1245. }
  1246. }
  1247. return [];
  1248. }
  1249. keys() {
  1250. return this._map.keys();
  1251. }
  1252. };
  1253. keras.Error = class extends Error {
  1254. constructor(message) {
  1255. super(message);
  1256. this.name = 'Error loading Keras model.';
  1257. }
  1258. };
  1259. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  1260. module.exports.ModelFactory = keras.ModelFactory;
  1261. }