首页 > 解决方案 > 用于 sparkfun 边缘微控制器的 Tensorflow lite,未找到内置操作码“CONV_2D”版本 2 和 3 的操作

问题描述

我正在尝试将我的 TFlite 模型与 sparkfun edge (Apollo 3) 微控制器一起使用来识别来自 Piano App 的简单曲调,例如 (do re mi fa ....) 我将以下 python 代码与 tensorflow v2 一起使用

from tensorflow.keras import layers......

model = tf.keras.Sequential([
layers.InputLayer(input_shape=(2048,)),
layers.Reshape(target_shape=(1,2048,1)),
layers.Conv2D(8,(1,8),strides=(1,8)), 
layers.MaxPool2D((1,2),strides=(1,2)), 
layers.Conv2D(8,(1,8),strides=(1,8)), 
layers.MaxPool2D((1,2),strides=(1,2)),
layers.Reshape(target_shape=(64,)) ,
layers.Dense(8,activation="softmax")  
])
model.build(input_shape=(2048,))
model.compile(optimizer="adam",loss="binary_crossentropy")

我训练模型并使用以下代码对其进行转换

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS]
model_tflite = converter.convert()
open('model.tflite', "wb").write(model_tflite)

!apt-get  install xxd
!xxd -i 'model.tflite' > 'model.cc'
!cat 'model.cc'

我从微控制器得到以下调试输出:


setup_NN invoked ///
Model TFLITE_SCHEMA_VERSION OK ///
interpreter OK ///
AllocateTensors OK ///
________________

in_dim= 2 ///
input_type = kTfLiteFloat32 ///
in_size[0]= 1 ///
in_size[1]= 2048 ///
________________

out_dim= 2 ///
output_type = kTfLiteFloat32 ///
out_size[0]= 1 ///
out_size[1]= 8 ///
________________

Setup OK

Loop funktion inoked

**Didn't find op for builtin opcode 'CONV_2D' version '2'**
Invoke failed
___________________

当我改变

converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS]

to 
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
with adding a 
representative_dataset function 

我得到错误:没有找到内置操作码'CONV_2D'版本' 3 '的操作

我认为sparkfun edge MC 接受 float32 作为输入,我是否必须尝试使用​​ tensorflow.compat.v1 insted of v2?你能指出哪里错了,或者只是给我看一个工作的例子?

可以说像 sine_model 这样只有完全连接层 + 激活的另一个模型对我来说很好(在 float32 中),但我真的需要 conv 层,我知道使用 Conv1D 更好,但我在某处读到它是不支持

标签: tensorflowneural-networkmicrocontrollertensorflow-litesparkfun

解决方案


推荐阅读