2023-02-16 19:05:43 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
export default function parse_summary(text) {
|
|
|
|
const lines = text.trim().split(/\r?\n/);
|
2023-02-18 02:04:40 +00:00
|
|
|
|
|
|
|
const has_connected_to = lines[2].search("Connected to") > -1;
|
|
|
|
|
2023-02-16 19:05:43 +00:00
|
|
|
const layers_raw = lines.slice(
|
|
|
|
lines.findIndex(line => line.startsWith("====")) + 1,
|
|
|
|
lines.findLastIndex(line => line.startsWith("===="))
|
|
|
|
);
|
|
|
|
const layers = [];
|
|
|
|
|
2023-02-18 02:04:40 +00:00
|
|
|
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;
|
2023-02-16 19:05:43 +00:00
|
|
|
let acc = [];
|
|
|
|
for (const line of layers_raw) {
|
|
|
|
if(line.trim().length == 0) {
|
|
|
|
if(acc.length === 0) continue;
|
2023-02-18 02:04:40 +00:00
|
|
|
|
|
|
|
console.log(`DEBUG:params`, acc.map(layer_line => layer_line.substring(sep_param_hash, sep_connected_to).trim()));
|
2023-02-16 19:05:43 +00:00
|
|
|
// Handle parsed item
|
|
|
|
const result = {
|
2023-02-18 02:04:40 +00:00
|
|
|
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("")),
|
2023-02-16 19:05:43 +00:00
|
|
|
};
|
2023-02-18 02:04:40 +00:00
|
|
|
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 ];
|
|
|
|
}
|
2023-02-16 19:05:43 +00:00
|
|
|
|
|
|
|
result.type = result.name_raw.match(/ \(([^)]+)\)/)[1];
|
|
|
|
result.name = result.name_raw.split(/\s+/)[0];
|
|
|
|
|
2023-02-18 02:04:40 +00:00
|
|
|
layer_prev = result;
|
2023-02-16 19:05:43 +00:00
|
|
|
layers.push(result);
|
|
|
|
|
|
|
|
acc.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.push(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
const edges = [];
|
|
|
|
|
|
|
|
for(const layer of layers) {
|
2023-02-18 02:04:40 +00:00
|
|
|
if(!(layer.connected_to instanceof Array)) {
|
|
|
|
console.info(`[edge detect] layer.connected_to is not an instance of Array:`, layer);
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-16 19:05:43 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|