GSOC Log jiyeon's log

[Week 2] Have one-convolution models generated


How to get all Operations of a TFLite model

In the interpreter.py code, especially _get_ops_details() method, it returns tensor index of each layer. Below is the code of getting all Conv2D layers inside tflite model and get the input/output tensor size.

import tensorflow as tf

SAVED_MODEL_PATH = "./densenet.tflite"
interpreter = tf.lite.Interpreter(model_path=SAVED_MODEL_PATH)

ops = interpreter._get_ops_details()
for op_index, op in enumerate(ops):
    if op['op_name'] == "CONV_2D":
        for tensor_idx in op['inputs']:
            tensor = interpreter._get_tensor_details(tensor_idx)
            tensor_shape = tensor['shape']
            print(tensor['name'], '\t', tensor['shape'])

And below is the output.

Placeholder 	 [  1 224 224   3]
conv2d/kernel 	 [64  7  7  3]
conv2d/Conv2D_bias 	 [64]
----
block-0/denseblock-0-0/Relu 	 [ 1 56 56 64]
block-0/denseblock-0-0/conv2d/kernel 	 [32  3  3 64]
block-0/denseblock-0-0/conv2d/Conv2D_bias 	 [32]
----
block-0/denseblock-0-1/Relu 	 [ 1 56 56 96]
block-0/denseblock-0-1/conv2d/kernel 	 [32  3  3 96]
block-0/denseblock-0-1/conv2d/Conv2D_bias 	 [32]
----
...

1. Generate One-Conv2D TFLite Model

import tensorflow as tf
from tensorflow import keras

model = keras.models.Sequential([
    keras.layers.Conv2D(filters=32, kernel_size=3, padding='same', activation='relu', input_shape=(112, 112, 3))
])
model.compile(optimizer='Adam', loss='mean_sqared_error')

converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

with open('conv2d.tflite', 'wb') as f:
    f.write(tflite_model)

This code is generating only one-convolution tflite mode. It can easily convert from keras model to tflite model with tf.lite.TFLiteConverter.from_keras_model().

And the results looks like below.

img

2. How to know Conv2D parameters inside tflite model

There is lots of parameters of Conv2D. Like

  • padding size
  • input tensor shape
  • output tensor shape
  • stride
  • kernel size
  • dilation size

We are already able to know the input/output tensor size and kernel size using _get_tensor_details(tensor_idx) method. In One convolution block, the middle one let us know the kernel size and input tensor shape. But the problem is It’s quite hard to find out other parameters.

In netron app, it seems to use tflite-support repository. Actually I couldn’t fully understand becuase netron’s load is run on node.js code, But It makes it possible to know the attribute.

    # install tensorflow support package
    echo "tf install"
    ${python} -m pip install --quiet --upgrade wheel
    ${python} -m pip install --quiet --upgrade tf-nightly

    # clone tensorflow & tflite-support repository
    echo "tflite sync"
    [ -d "./third_party/source/tensorflow" ] ||  git clone https://github.com/tensorflow/tensorflow.git "./third_party/source/tensorflow"
    pushd "./third_party/source/tensorflow" > /dev/null
    git pull --quiet --prune
    git submodule sync --quiet
    git submodule update --quiet --init --recursive
    popd > /dev/null
    [ -d "./third_party/source/tflite-support" ] || git clone https://github.com/tensorflow/tflite-support.git "./third_party/source/tflite-support"
    pushd "./third_party/source/tflite-support" > /dev/null
    git pull --quiet --prune
    popd > /dev/null

    # Get the model's schema (maybe?)
    echo "tflite schema"
    [[ $(grep -U $'\x0D' ./source/tflite-schema.js) ]] && crlf=1
    temp1=$(mktemp -d)
    temp2=$(mktemp -d)
    node ./tools/flatc.js --text --root tflite --out ./source/tflite-schema.js ./third_party/source/tensorflow/tensorflow/lite/schema/schema.fbs # it seems what I want to know.
    node ./tools/flatc.js --root tflite --out ${temp1}/tflite-metadata-schema.js ./third_party/source/tflite-support/tensorflow_lite_support/metadata/metadata_schema.fbs
    sed "s/var \$root = flatbuffers.get('tflite');//g" < ${temp1}/tflite-metadata-schema.js >> ${temp2}/tflite-metadata-schema.js
    cat ${temp2}/tflite-metadata-schema.js >> ./source/tflite-schema.js
    rm -rf ${temp1} ${temp2}
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/tflite-schema.js ./source/tflite-schema.js
    fi

In this code, ./tools/flatc.js looks like loading tflite model and get the all of attribute of the model with tensorflow & tflite-support third party code. (.fbs file format?)

Anyway, I want to know the parameters using python code but I don’t know how, so I just asked to stackoverflow. 😂

Stackoverflow Link : https://stackoverflow.com/questions/68105252/how-can-i-know-conv2d-parameters-inside-tflite-model-using-python