我尝试在 ESP32 上运行一些机器学习,并且我想使用 Tensorflow lite micro。但我真的不明白,他们是如何构建层的。这是如何训练人物检测模型的示例: 人员检测模型训练
很清楚,但最后他们说:
MobileNet v1 是由 14 个深度可分离卷积层组成的堆栈,其中有一个平均池,然后是一个全连接层,最后是一个 softmax。
如果我检查示例代码,他们在其中构建 tf lite 微模型,它只有 3 行:
static tflite::MicroMutableOpResolver<3> micro_op_resolver;
micro_op_resolver.AddAveragePool2D();
micro_op_resolver.AddConv2D();
micro_op_resolver.AddDepthwiseConv2D();
有平均池和深度层,但是 Conv2D 层来自哪里?并且仅呈现 1 个深度层,但在文档中,模型中有 14 个深度层。
所以问题是,训练模型和我应该在tensoflow lite micro中构建的模型之间有什么关系吗?如果有,我该如何确定如何构建。这就是问题,如果没有关系,我需要以什么方式建立模型。
他们没有显式构建模型,他们依赖于包含架构的模型文件(source):
model = tflite::GetModel(g_person_detect_model_data);
其中
g_person_detect_model_data.cc
是使用以下命令从 tflite 模型(包含架构)生成的(请参阅自述文件中的转换为 c 源文件):
# Install xxd if it is not available
!apt-get -qq install xxd
# Save the file as a C source file
!xxd -i vww_96_grayscale_quantized.tflite > person_detect_model_data.cc
因此您共享的代码不会构建模型。您看到的是,出于性能原因,他们显式添加了模型所需的操作,而不是依赖于更复杂的
tflite::AllOpsResolver
。
您共享的代码上方的评论表明了这一点:
// Pull in only the operation implementations we need.
// This relies on a complete list of all the ops needed by this graph.
// An easier approach is to just use the AllOpsResolver, but this will
// incur some penalty in code space for op implementations that are not
// needed by this graph.
//
// tflite::AllOpsResolver resolver;
// NOLINTNEXTLINE(runtime-global-variables)
static tflite::MicroMutableOpResolver<5> micro_op_resolver;
micro_op_resolver.AddAveragePool2D();
micro_op_resolver.AddConv2D();
micro_op_resolver.AddDepthwiseConv2D();
micro_op_resolver.AddReshape();
micro_op_resolver.AddSoftmax();