我得到了MobileNet的预先训练的.pb文件,发现它没有被量化,而完全量化的模型应该被转换成.tflite格式。由于我不熟悉移动应用开发工具,如何从.tflite文件中获取MobileNet的完全量化的权重。更准确地说,如何提取量化参数并查看其数值?
发布于 2018-09-13 03:30:10
Netron模型查看器有很好的数据视图和导出,以及很好的网络图视图。https://github.com/lutzroeder/netron
发布于 2019-11-06 16:27:31
使用TensorFlow 2.0,您可以使用受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()发布于 2018-09-05 05:26:13
我也在研究TFLite是如何工作的。我发现的可能不是最好的方法,我希望得到任何专家的意见。这是我到目前为止使用flatbuffer python API所发现的情况。
首先,您需要使用flatbuffer编译模式。输出将是一个名为tflite的文件夹。
flatc --python tensorflow/contrib/lite/schema/schema.fbs
然后你可以加载模型并获得你想要的张量。Tensor有一个名为Buffer()的方法,根据该模式,
引用模型根部的缓冲器表的索引。
因此,它会将您指向数据的位置。
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)之后,您将能够通过调用buffer.Data()读取数据
参考:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/schema/schema.fbs https://github.com/google/flatbuffers/tree/master/samples
https://stackoverflow.com/questions/52111699
复制相似问题