You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino BLE 33运行TFLite模型遇WHERE算子版本不兼容问题求助

问题描述

在Arduino BLE 33(nRF52840)开发板上运行自制模型,通过Arduino IDE上传时出现报错:

Didn't find op for builtin opcode 'WHERE' version '1'. An older version of this builtin might be supported. Are you using an old TFLite binary with a newer model?
Failed to get registration from op code WHERE
AllocateTensors() failed
Invoke failed!

版本信息

  • tensorflow==2.8.0
  • TensorFlow Lite Micro Library:通过git clone https://github.com/tensorflow/tflite-micro-arduino-examples Arduino_TensorFlowLite获取
  • Arduino IDE 2.2.1

已尝试的解决步骤

  1. 模型转换与代码生成:将模型转为.tflite文件后,通过以下代码生成model.h和model.cc:
!echo "const unsigned char model[] = {" > /content/model.h
!cat gesture_model.tflite | xxd -i      >> /content/model.h
!echo "};"                              >> /content/model.h

import os
model_h_size = os.path.getsize("model.h")
print(f"Header file, model.h, is {model_h_size:,} bytes.")
print("\nOpen the side panel (refresh if needed). Double click model.h to download the file.")
  1. 使用AllOpsResolver:仍出现WHERE算子报错。
  2. 替换为MicroMutableOpResolver:MicroMutableOpResolver中无AddWhere()方法,注释掉该调用后报错:

MicroAllocator: Model allocation started before finishing the previously allocated model
Failed starting model allocation.
Invoke failed!
(注:模型共使用19种算子)

  1. 确认算子支持:TF Lite支持tfl.where,但TFLite Micro中无对应实现。
  2. 排查版本兼容:查到WHERE算子版本对应关系:
{{BuiltinOperator_WHERE, 1}, "1.14.0"}
{{BuiltinOperator_WHERE, 2}, "2.8.0"},

推测版本不兼容,但不知如何修改模型使用的算子版本。

解决方案

方案1:修改模型以移除WHERE算子

TFLite Micro对WHERE算子的支持不完善,优先考虑重构模型逻辑,用已支持的算子替代WHERE的功能:

  • 若用WHERE提取满足条件的索引,可改用StridedSlice+Gather组合实现类似逻辑;
  • 对于简单条件筛选,将逻辑提前到模型训练阶段或预处理环节,避免推理时使用WHERE。

方案2:适配算子版本

你的TensorFlow版本是2.8.0,对应WHERE算子版本2,但模型生成的是版本1,重新转换模型时指定兼容算子版本:

import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
# 指定支持的算子集
converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS,
    tf.lite.OpsSet.SELECT_TF_OPS
]
# 强制对齐算子版本
converter._experimental_lower_tensor_list_ops = True
tflite_model = converter.convert()

# 保存模型
with open('gesture_model.tflite', 'wb') as f:
    f.write(tflite_model)

方案3:自定义WHERE算子实现

若必须保留WHERE算子,需在TFLite Micro中添加自定义实现:

  1. 从TensorFlow源码提取WHERE算子实现,适配Micro环境;
  2. 在MicroMutableOpResolver中注册自定义WHERE算子;
  3. 重新编译TFLite Micro库并导入Arduino项目。
    此方法需要熟悉TFLite Micro底层实现,适合有经验的开发者。

方案4:对齐TFLite Micro库版本

尝试将TFLite Micro库版本与TensorFlow 2.8.0对齐:

git clone -b v2.8.0 https://github.com/tensorflow/tflite-micro-arduino-examples Arduino_TensorFlowLite

替换Arduino库目录中的Arduino_TensorFlowLite文件夹,重新编译上传。

内容的提问来源于stack exchange,提问作者Seongjae Lee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 11:42:54