@wetron/savedmodel
v0.0.35
Published
TF SavedModel parser - extracts graph structure from keras_metadata.pb and saved_model.pb files
Maintainers
Readme
@wetron/savedmodel
TF SavedModel parser for wetron. Reads .pb files in two formats:
saved_model.pb- TensorFlow SavedModel GraphDef (raw TF ops)keras_metadata.pb- Keras layer metadata protobuf
Returns a ModelGraph IR. Graph structure only - no weight tensors are deserialized.
Install
pnpm add @wetron/savedmodelIncluded automatically when you install @wetron/core.
API
import { parseSavedModel } from "@wetron/savedmodel";
const bytes = new Uint8Array(await file.arrayBuffer());
const graph = parseSavedModel(bytes); // synchronous, returns ModelGraphThrows ParseError from @wetron/common/ir if the file is too short or has unrecognized .pb content.
Checkpoint loading
Variable weights live outside the .pb file in the TF2 variables/ checkpoint pair. Load and attach them with:
import {
loadSavedModelWeights,
loadSavedModelWeightsFromUrls,
attachCheckpointToGraph,
} from "@wetron/savedmodel";
// From local files
const loaded = await loadSavedModelWeights(indexFile, dataFile);
// From URLs (one URL per shard, in shard order)
const loaded = await loadSavedModelWeightsFromUrls(
"https://.../variables.index",
"https://.../variables.data-00000-of-00001",
);
const graphWithWeights = attachCheckpointToGraph(graph, loaded);graph.hasExternalWeights is true whenever at least one VarHandleOp is present, signalling the host app to prompt for the checkpoint files.
Format detection
The first byte determines which variant is parsed:
| First byte | Format |
| ---------- | ---------------------------------- |
| 0x0a | keras_metadata.pb (Keras layers) |
| 0x08 | saved_model.pb (TF GraphDef) |
parseModel from @wetron/core routes .pb files here automatically based on filename.
What gets parsed
keras_metadata.pb
- Layer graph from the embedded
keras_metadataJSON - Supported topologies:
Sequential,Functional InputLayerentries ->ModelGraph.inputs- Layer
class_name-> nodeopType - Layer config fields -> node
attributes
saved_model.pb
- First
MetaGraphDef->GraphDefnodes Placeholdernodes ->ModelGraph.inputs- All other ops ->
ModelGraph.nodes - Output nodes inferred as nodes whose outputs are never consumed as inputs
Notes
ModelGraph.initializersis always empty - weight data is not parsed.Constnodes (weight constants) appear as graph nodes with categoryconstant.- Control dependencies (inputs prefixed with
^) are ignored. - Port suffixes (
:0,:1) are stripped from input tensor names. - Non-fatal per-node errors are attached as
warningson the returned graph.
