下载flatbuffer:https://github.com/google/flatbuffers
解压flatbuffer,进入其根目录
cd flatbuffers
mkdir build
cd build
cmake ..
make -j4
sudo make install
flatc --version
#输出flatbuffer版本:flatc version 1.11.0
将/tensorlow/lite/schema/schema.fbs文件拷贝到要转换的.tflite文件所在目录下,执行以下命令可以将.tflite转换成json格式的文件,例如:将mobilenetv1转换成json文件格式:
flatc -t schema.fbs -- mobilenet_v1_1.0_224.tflite
TFlite的JSON文件结构如下图所示:
operator_codes:以列表的行驶存储该网络结构用的layer种类;例如mobilenet v1使用了AVERAGE_POOL_2D,CONV_2D,DEPTHWISE_CONV_2D,SOFTMAX,SQUEEZE这几种layer种类。
operator_codes: [ { builtin_code: "AVERAGE_POOL_2D" }, { builtin_code: "CONV_2D" }, { builtin_code: "DEPTHWISE_CONV_2D" }, { builtin_code: "SOFTMAX" }, { builtin_code: "SQUEEZE" } ],
subgraphs:为每一层的具体信息,具体包括:
1)tensors.包含input、weight、bias的shape信息、量化参数以及在buffer数据区的offset值;
2)inputs: 整个网络的输入对应的tensors索引;
3)outputs: 整个网络的输出对应的tensors索引;
4)operators:网络结构所需要的相关参数;
buffers: 存放weight、bias等权重信息。
下面仿照生成的mobilenet.json文件写一个只包含两个算子的模型,权重信息等手动写入,然后将该json文件生成为.tflite文件。
test.json文件:
{
version: 3,
operator_codes: [
{
builtin_code: "AVERAGE_POOL_2D"
},
{
builtin_code: "CONV_2D"
}
],
subgraphs: [
{
tensors: [
{
shape: [
1,
3,
3,
3
],
buffer: 1,
name: "Conv2d_0/weights",
quantization: {
}
},
{
shape: [
1
],
buffer: 2,
name: "Conv2d_0/Conv2D_bias",
quantization: {
}
},
{
shape: [
1,
2,
2,
1
],
buffer: 3,
name: "Conv2d_1/weights",
quantization: {
}
},
{
shape: [
1
],
buffer: 4,
name: "Conv2d_1/Conv2D_bias",
quantization: {
}
},
{
shape: [
1,
8,
8,
3
],
buffer: 0,
name: "input",
quantization: {
}
}
],
inputs: [
0
],
outputs: [
3
],
operators: [
{
opcode_index: 1,
inputs: [
0,
1,
2
],
outputs: [
1
],
builtin_options_type: "Conv2DOptions",
builtin_options: {
stride_w: 1,
stride_h: 1,
}
},
],
}
],
description: "TOCO Converted.",
buffers :[
{
},
{
data: [
1,2,1,
1,1,1,
1,1,2,
1,2,1,
1,1,1,
1,1,2,
1,2,1,
1,1,1,
1,1,2,
]
},
{
data: [
3,1,1,2
]
},
{
data: [
1,1,2,2,
1,1,2,2,
1,1,2,2,
1,1,2,2
]
},
{
data: [
3,2,1,2
]
},
]
}
转换命令:
flatc -b schema.fbs test.json
如果转换成功,将在当前路径下生成test.tflite文件。
欢迎关注亦梦云烟的微信公众号: 亦梦智能计算