• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# TFLite Serialization Tool
2
3**NOTE:** This tool is intended for advanced users only, and should be used with
4care.
5
6The (C++) serialization library generates and writes a TFLite flatbuffer given
7an `Interpreter` or `Subgraph`. Example use-cases include authoring models with
8the `Interpreter` API, or updating models on-device (by modifying `tensor.data`
9for relevant tensors).
10
11## Serialization
12
13### Writing flatbuffer to file
14
15To write a TFLite model from an `Interpreter` (see `lite/interpreter.h`):
16`std::unique_ptr<tflite::Interpreter> interpreter; // ...build/modify
17interpreter... tflite::ModelWriter writer(interpreter.get()); std::string
18filename = "/tmp/model.tflite"; writer.Write(filename);`
19
20Note that the above API does not support custom I/O tensors or custom ops yet.
21However, it does support model with Control Flow.
22
23To generate/write a flatbuffer for a particular `Subgraph` (see
24`lite/core/subgraph.h`) you can use `SubgraphWriter`.
25
26```
27std::unique_ptr<tflite::Interpreter> interpreter;
28// ...build/modify interpreter...
29// The number of subgraphs can be obtained by:
30// const int num_subgraphs = interpreter_->subgraphs_size();
31// Note that 0 <= subgraph_index < num_subgraphs
32tflite::SubgraphWriter writer(&interpreter->subgraph(subgraph_index));
33std::string filename = "/tmp/model.tflite";
34writer.Write(filename);
35```
36
37`SubgraphWriter` supports custom ops and/or custom I/O tensors.
38
39### Generating flatbuffer in-memory
40
41Both `ModelWriter` and `SubgraphWriter` support a `GetBuffer` method to return
42the generated flatbuffer in-memory:
43
44```
45std::unique_ptr<uint8_t[]> output_buffer;
46size_t output_buffer_size;
47tflite::ModelWriter writer(interpreter.get());
48writer.GetBuffer(&output_buffer, &output_buffer_size);
49```
50
51## De-serialization
52
53The flatbuffers written as above can be de-serialized just like any other TFLite
54model, for eg:
55
56```
57std::unique_ptr<FlatBufferModel> model =
58    FlatBufferModel::BuildFromFile(filename);
59tflite::ops::builtin::BuiltinOpResolver resolver;
60InterpreterBuilder builder(*model, resolver);
61std::unique_ptr<Interpreter> new_interpreter;
62builder(&new_interpreter);
63```
64