首页 > 解决方案 > 如何将 pb 文件转换为 tf lite?

问题描述

我正在使用 Inception v3 进行图像分类,我重新训练了生成 2 个文件“retrained_graph.pb”和“retrained_lables”的模型。

在android中使用它我想将它转换为tflite文件所以我使用了这段代码

import tensorflow as tf
from tensorflow import lite
converter = lite.TFLiteConverter.from_frozen_graph(
   'retrained_graph.pb' ,'DecodeJpeg/contents', 'final_result')

tflite_model = converter.convert()
open("test.tflite", "wb").write(tflite_model)

我收到此错误

回溯(最后一次调用):
文件“tfconvert.py”,第 4 行,在“retrained_graph.pb”、“DecodeJpeg/contents”、“final_result”)
文件“C:\Users\Thakkar\AppData\Local\Programs\ Python\Python36\lib\site-packages\tensorflow_core\lite\python\lite.py”,第 705 行,在 from_frozen_graph sess.graph,input_arrays)文件“C:\Users\Thakkar\AppData\Local\Programs\Python\Python36 \lib\site-packages\tensorflow_core\lite\python\util.py",第 122 行,在 get_tensors_from_tensor_names",".join(invalid_tensors))) ValueError: Invalid tensors 'D,e,c,o,d,e, J,p,e,g,/,c,o,n,t,e,n,t,s' 被发现。

标签: pythontensorflow

解决方案


您需要修复此处给出的 TFLite 转换器代码

converter = lite.TFLiteConverter.from_frozen_graph(
   graph_def_file='retrained_graph.pb' ,
   input_arrays=['DecodeJpeg/contents'],
   output_arrays=['final_result']
)

推荐阅读