tfsummaryvis/src/lib/parse_summary.mjs

78 lines
2.5 KiB
JavaScript
Raw Normal View History

"use strict";
export default function parse_summary(text) {
const lines = text.trim().split(/\r?\n/);
const has_connected_to = lines[2].search("Connected to") > -1;
const layers_raw = lines.slice(
lines.findIndex(line => line.startsWith("====")) + 1,
lines.findLastIndex(line => line.startsWith("===="))
);
const layers = [];
const sep_output_shape = lines[2].search("Output Shape");
const sep_param_hash = lines[2].search("Param #");
const sep_connected_to = has_connected_to ? lines[2].search("Connected to") : lines[2].length;
let layer_prev = null;
let acc = [];
for (const line of layers_raw) {
if(line.trim().length == 0) {
if(acc.length === 0) continue;
console.log(`DEBUG:params`, acc.map(layer_line => layer_line.substring(sep_param_hash, sep_connected_to).trim()));
// Handle parsed item
const result = {
name_raw: acc.map(layer_line => layer_line.substring(0, sep_output_shape).trim()).join(""),
output_shape: acc.map(layer_line => layer_line.substring(sep_output_shape, sep_param_hash).trim()).join(""),
params: parseInt(acc.map(layer_line => layer_line.substring(sep_param_hash, sep_connected_to).trim()).join("")),
};
if(has_connected_to) {
result.connected_to = JSON.parse(acc.map(layer_line => layer_line.substring(sep_connected_to).trim()).join("")
.replace(/'/g, '"'))
.map(connected_name => connected_name.replace(/(\[0\])+$/, ""));
}
else {
if(layer_prev !== null)
result.connected_to = [ layer_prev.name ];
}
result.type = result.name_raw.match(/ \(([^)]+)\)/)[1];
result.name = result.name_raw.split(/\s+/)[0];
layer_prev = result;
layers.push(result);
acc.length = 0;
}
acc.push(line);
}
const edges = [];
for(const layer of layers) {
if(!(layer.connected_to instanceof Array)) {
console.info(`[edge detect] layer.connected_to is not an instance of Array:`, layer);
continue;
}
for(const connection of layer.connected_to) {
edges.push({
from: connection,
to: layer.name
});
}
}
return {
title: lines[0].match(/^Model: "([^"]+)"/)[1],
params: parseInt(lines.find(line => line.search(/^Total params:/) > -1).split(": ")[1].replace(/,/g, "")),
params_trainable: parseInt(lines.find(line => line.search(/^Trainable params:/) > -1).split(": ")[1].replace(/,/g, "")),
params_nontrainable: parseInt(lines.find(line => line.search(/^Non-trainable params:/) > -1).split(": ")[1].replace(/,/g, "")),
layers,
edges
}
}