Class TfModel

java.lang.Object
ai.beestream.jitnn.tf.TfModel
All Implemented Interfaces:
AutoCloseable

public final class TfModel extends Object implements AutoCloseable
A TensorFlow model on jitNN: a SavedModel directory (TensorFlow 2 / Keras export, with its variables checkpoint) or a frozen GraphDef file. The graph is flattened (function calls inlined), its variables read from the checkpoint, and it is translated to the operators of jitnn-onnx, which runs it.
TfModel model = TfModel.load(Path.of("my_saved_model"));
Map<String, Tensor> out = model.run(Map.of("keras_tensor", Tensor.of(x, 1, 4)));
Inputs and outputs carry the signature's names. Integer inputs (token ids) are INT32 tensors; images stay NHWC, as TensorFlow takes them.
  • Method Details

    • load

      public static TfModel load(Path path) throws IOException
      Loads a SavedModel directory's serving_default signature, or a frozen GraphDef file.
      Throws:
      IOException
    • load

      public static TfModel load(Path path, String signature, boolean halfPrecision) throws IOException
      Loads signature signature of a SavedModel (ignored for a frozen GraphDef, whose inputs are its placeholders and whose outputs are the nodes nothing consumes). With halfPrecision, matrix products run on FP16 operands, as in jitnn-onnx.
      Throws:
      IOException
    • inputNames

      public List<String> inputNames()
      The model's input names, as run takes them.
    • outputNames

      public List<String> outputNames()
      The model's output names.
    • session

      public OnnxSession session()
      The jitnn-onnx session the model was translated to.
    • close

      public void close()
      Releases the model's weights (see OnnxSession.close()); the model cannot run afterwards.
      Specified by:
      close in interface AutoCloseable
    • run

      public Map<String,Tensor> run(Map<String,Tensor> inputs)
      Runs the model; outputs are lazy jitNN tensors, by signature output name.