如何将TensorFlow模型文件graph.pb转为TensorFlow Lite?执行命令报错求助
解决TensorFlow PB模型转TFLite的Bazel命令错误
我来帮你搞定这个模型转换的问题,先分析下你遇到的两个关键错误:
错误原因拆解
WARNING: Config values are not defined in any .rc file: opt:Windows环境下Bazel默认没配置opt这个优化编译选项,它不是转换模型的必需参数,反而会触发配置警告。ERROR: No targets found to run:你的命令有两处问题:一是TensorFlow新版本里contrib/lite模块已经被迁移到tensorflow/lite目录下,旧路径无效;二是Windows下不需要用双反斜杠\\转义路径,这会导致Bazel无法识别目标位置。
解决方案一:修正Bazel命令
如果你坚持用Bazel方式转换,先确保你克隆的TensorFlow源码是最新版,然后用这个修正后的命令:
bazel run //tensorflow/lite/toco:toco -- ^ --input_file=optimized_graph.pb ^ --output_file=abc.tflite ^ --input_format=TENSORFLOW_GRAPHDEF ^ --output_format=TFLITE ^ --inference_type=FLOAT ^ --input_shape=1,128,128,3 ^ --input_array=input ^ --output_array=final_result
- 去掉了
--config=opt避免配置错误,之后如果需要优化编译,先配置好Windows下Bazel的编译环境(比如安装MSVC并设置环境变量)再添加该选项。 - 把TOCO工具的目标路径修正为
//tensorflow/lite/toco:toco,适配新版本TensorFlow的目录结构。 - 用Windows CMD的换行符
^拆分长命令(如果用PowerShell可以直接换行)。
解决方案二:使用tflite_convert工具(更推荐)
其实完全没必要用Bazel编译,TensorFlow安装后自带tflite_convert工具,操作更简单:
如果你已经通过pip install tensorflow安装了TensorFlow,直接在命令行运行:
CMD环境:
tflite_convert ^ --input_file=optimized_graph.pb ^ --output_file=abc.tflite ^ --input_format=TENSORFLOW_GRAPHDEF ^ --output_format=TFLITE ^ --inference_type=FLOAT ^ --input_shape=1,128,128,3 ^ --input_array=input ^ --output_array=final_result
PowerShell/Linux环境:
tflite_convert \ --input_file=optimized_graph.pb \ --output_file=abc.tflite \ --input_format=TENSORFLOW_GRAPHDEF \ --output_format=TFLITE \ --inference_type=FLOAT \ --input_shape=1,128,128,3 \ --input_array=input \ --output_array=final_result
这个工具直接封装了TOCO的功能,不用编译TensorFlow源码,适合快速完成模型转换。
内容的提问来源于stack exchange,提问作者Yasir Raza
相关产品推荐
相关产品推荐

