mingxin zhao
mingxin zhao

Reputation: 101

How can I view weights in a .tflite file?

I get the pre-trained .pb file of MobileNet and find it's not quantized while the fully quantized model should be converted into .tflite format. Since I'm not familiar with tools for mobile app developing, how can I get the fully quantized weights of MobileNet from .tflite file. More precisely, how can I extract quantized parameters and view its numerical values ?

Upvotes: 10

Views: 20339

Answers (4)

Sandeep Vivek
Sandeep Vivek

Reputation: 41

You can view it using Netron app macOS: Download the .dmg file or run brew install netron

Linux: Download the .AppImage file or run snap install netron

Windows: Download the .exe installer or run winget install netron

Browser: Start the browser version.

Python Server: Run pip install netron and netron [FILE] or netron.start('[FILE]').

Upvotes: 0

Samuel Tap
Samuel Tap

Reputation: 91

Using TensorFlow 2.0, you can extract the weights and some information regarding the tensor (shape, dtype, name, quantization) with the following script - inspired from TensorFlow documentation

import tensorflow as tf
import h5py


# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="v3-large_224_1.0_uint8.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()


# get details for each layer
all_layers_details = interpreter.get_tensor_details() 


f = h5py.File("mobilenet_v3_weights_infos.hdf5", "w")   

for layer in all_layers_details:
     # to create a group in an hdf5 file
     grp = f.create_group(str(layer['index']))

     # to store layer's metadata in group's metadata
     grp.attrs["name"] = layer['name']
     grp.attrs["shape"] = layer['shape']
     # grp.attrs["dtype"] = all_layers_details[i]['dtype']
     grp.attrs["quantization"] = layer['quantization']

     # to store the weights in a dataset
     grp.create_dataset("weights", data=interpreter.get_tensor(layer['index']))


 f.close()

Upvotes: 9

Jay Norwood
Jay Norwood

Reputation: 146

The Netron model viewer has nice view and export of data, as well as a nice network diagram view. https://github.com/lutzroeder/netron

Upvotes: 11

Jia Guo
Jia Guo

Reputation: 81

I'm also in the process of studying how TFLite works. What I found may not be the best approach and I would appreciate any expert opinions. Here's what I found so far using flatbuffer python API.

First you'll need to compile the schema with flatbuffer. The output will be a folder called tflite.

flatc --python tensorflow/contrib/lite/schema/schema.fbs

Then you can load the model and get the tensor you want. Tensor has a method called Buffer() which is, according to the schema,

An index that refers to the buffers table at the root of the model.

So it points you to the location of the data.

from tflite import Model
buf = open('/path/to/mode.tflite', 'rb').read()
model = Model.Model.GetRootAsModel(buf, 0)
subgraph = model.Subgraphs(0)
# Check tensor.Name() to find the tensor_idx you want
tensor = subgraph.Tensors(tensor_idx) 
buffer_idx = tensor.Buffer()
buffer = model.Buffers(buffer_idx)

After that you'll be able to read the data by calling buffer.Data()

Reference: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/schema/schema.fbs https://github.com/google/flatbuffers/tree/master/samples

Upvotes: 8

Related Questions