TFLite: Extracting intermediate activation tensors

Hi all,

I am trying to get access to intermediate activation data (activations of neurons in hidden layers) in a TFLite model.
tf.lite.Interpreter has the experimental_preserve_all_tensors flag, but apparently no method to actually obtain the data after or during interpretation of the model. In fact, for both tensor() and get_tensor(), the documentation explicitly states that they “cannot be used to read intermediate results”, without giving any information about a method that can.

Presumably, the existence of the flag means there must be some way to read the intermediate activations, so what is it?

Thanks and have a good one!

Suppose we have a sample tflite file as shown below,

Would it accomplish your goal if you could get the intermediate output of the various OPs or activation functions from the interpreter as shown below?

I just rewrote the TFLite binary and regenerated tflite. I did not write a single line of source code.

sudo apt-get install -y flatbuffers-compiler

wget https://raw.githubusercontent.com/tensorflow/tensorflow/v2.11.0/tensorflow/lite/schema/schema.fbs

flatc -t \
--strict-json \
--defaults-json \
-o . \
schema.fbs -- sample_float32.tflite

Add the output number to “outputs” of the generated JSON file and save it.
20230323180658

You can regenerate tflite files from JSON files with the following command.

flatc \
-o mod \
-b schema.fbs \
sample_float32.json
1 Like