如何在.tflite文件中查看权重?

问题描述 投票:0回答:2

我得到了MobileNet的预训练的.pb文件,发现它没有量化,而完全量化的模型应该转换为.tflite格式。由于我不熟悉移动应用程序开发工具,如何从.tflite文件中获取MobileNet的完全量化权重。更准确地说,我如何提取量化参数并查看其数值?

tensorflow tensorflow-lite
2个回答
2
投票

Netron模型查看器具有良好的数据视图和导出,以及良好的网络图视图。 https://github.com/lutzroeder/netron


0
投票

我也正在研究TFLite是如何工作的。我发现的可能不是最好的方法,我会感谢任何专家的意见。这是我到目前为止使用flatbuffer python API找到的内容。

首先,您需要使用flatbuffer编译模式。输出将是一个名为tflite的文件夹。

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

然后你可以加载模型并得到你想要的张量。 Tensor有一个名为Buffer()的方法,根据模式,

一个索引,它引用模型根目录下的buffers表。

因此它指向数据的位置。

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

© www.soinside.com 2019 - 2024. All rights reserved.