aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/nn/rewrite/core/utils/utils.py
diff options
context:
space:
mode:
authorAnnie Tallund <annie.tallund@arm.com>2023-03-13 17:00:31 +0100
committerBenjamin Klimczak <benjamin.klimczak@arm.com>2023-10-11 15:41:48 +0100
commitf0b8ed75fed9dc69ab1f6313339f9f7e38bfc725 (patch)
treebc353fad664040b44915b5cf7ae807894b0b87e8 /src/mlia/nn/rewrite/core/utils/utils.py
parentb236127b9a18ec2668271c6b5baafa6a7c1dde51 (diff)
downloadmlia-f0b8ed75fed9dc69ab1f6313339f9f7e38bfc725.tar.gz
MLIA-845 Migrate rewrite code
- Add required files for rewriting of TensorFlow Lite graphs - Adapt rewrite dependency paths and project name - Add license headers Change-Id: I19c5f63215fe2af2fa7d7d44af08144c6c5f911c Signed-off-by: Benjamin Klimczak <benjamin.klimczak@arm.com>
Diffstat (limited to 'src/mlia/nn/rewrite/core/utils/utils.py')
-rw-r--r--src/mlia/nn/rewrite/core/utils/utils.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/mlia/nn/rewrite/core/utils/utils.py b/src/mlia/nn/rewrite/core/utils/utils.py
new file mode 100644
index 0000000..ed6c81d
--- /dev/null
+++ b/src/mlia/nn/rewrite/core/utils/utils.py
@@ -0,0 +1,26 @@
+# SPDX-FileCopyrightText: Copyright 2023, Arm Limited and/or its affiliates.
+# SPDX-License-Identifier: Apache-2.0
+import os
+
+import flatbuffers
+from tensorflow.lite.python import schema_py_generated as schema_fb
+
+
+def load(input_tflite_file):
+ if not os.path.exists(input_tflite_file):
+ raise RuntimeError("TFLite file not found at %r\n" % input_tflite_file)
+ with open(input_tflite_file, "rb") as file_handle:
+ file_data = bytearray(file_handle.read())
+ model_obj = schema_fb.Model.GetRootAsModel(file_data, 0)
+ model = schema_fb.ModelT.InitFromObj(model_obj)
+ return model
+
+
+def save(model, output_tflite_file):
+ builder = flatbuffers.Builder(1024) # Initial size of the buffer, which
+ # will grow automatically if needed
+ model_offset = model.Pack(builder)
+ builder.Finish(model_offset, file_identifier=b"TFL3")
+ model_data = builder.Output()
+ with open(output_tflite_file, "wb") as out_file:
+ out_file.write(model_data)