.pb文件转换为tflite文件遇到问题汇总

1、AttributeError: type object 'TFLiteConverterV2' has no attribute 'from_frozen_graph'    或者 convertr = tf.lite.TFLiteConverter.from_frozen_graph(in_path, input_arrays=input_tensor_name   AttributeError: module 'tensorflow' has no attribute 'lite'

对于tenorflow版本不对,利用aconda新建一个环境,安装:tensorflow 1.13.1(亲测,有效)

conda install tensorflow=1.13.1
或则:
pip install tensorflow==1.13.1

2、tensorflow.lite.python.convert.ConverterError: TOCO failed. See console for info.

以及后面还跟了一长串:

2022-01-05 19:57:38.516029: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.516305: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
2022-01-05 19:57:38.516487: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.516752: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
2022-01-05 19:57:38.516918: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.517146: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
2022-01-05 19:57:38.517302: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.517586: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
2022-01-05 19:57:38.517748: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.517955: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
2022-01-05 19:57:38.518097: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.518394: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
2022-01-05 19:57:38.518559: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.518778: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
2022-01-05 19:57:38.518974: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.519626: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
2022-01-05 19:57:38.519798: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.520088: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
2022-01-05 19:57:38.520229: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.520475: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
2022-01-05 19:57:38.520631: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.520882: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
2022-01-05 19:57:38.521045: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.521346: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
2022-01-05 19:57:38.521524: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
2022-01-05 19:57:38.521781: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff

这个问题困扰了我很久,换了很多版本,我都不能跑通,最后在代码里面加了:

converter.post_training_quantize = True

问题解决!

最后自己转换成功代码为:

import tensorflow as tf

# 把pb文件路径改成自己的pb文件路径即可
path="E:/LX/tensorflow/yolov3_coco_v3.pb"        #pb文件位置和文件名

# 如果是不知道自己的模型的输入输出节点,建议用tensorboard做可视化查看计算图,计算图里有输入输出的节点名称
inputs=["input/input_data"]
#模型文件的输入节点名称
outputs=["pred_sbbox/concat_2", "pred_mbbox/concat_2", "pred_lbbox/concat_2",
                         "pred_multi_scale/concat"]            #模型文件的输出节点名称

input_tensor = {inputs[0]:[1,416,416,3]}
# 转换pb模型到tflite模型
converter = tf.lite.TFLiteConverter.from_frozen_graph(path, inputs, outputs,input_tensor)
converter.post_training_quantize = True
converter.allow_custom_ops=True
tflite_model = converter.convert()
# model_tflite.tflite这里改成自己想要保存tflite模型的地址即可
open("E:/LX/tensorflow/model_tflite.tflite", "wb").write(tflite_model)

3 出现一下提示,不是错误,但是让人看着烦:

Using TensorFlow backend.
D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
D:\tools\_virtualenv_dir\myproject_2_quchumasaike\env2_py36_quchumasaike\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])

numpy版本不对,重装numpy版本!

pip uninstall numpy
pip install numpy==1.16.4

最后稍微提一下,网络上:pb文件转tflite文件代码汇总:

1

import tensorflow as tf

# 把pb文件路径改成自己的pb文件路径即可
path="E:/LX/tensorflow/yolov3_coco_v3.pb"        #pb文件位置和文件名

# 如果是不知道自己的模型的输入输出节点,建议用tensorboard做可视化查看计算图,计算图里有输入输出的节点名称
inputs=["input/input_data"]
#模型文件的输入节点名称
outputs=["pred_sbbox/concat_2", "pred_mbbox/concat_2", "pred_lbbox/concat_2",
                         "pred_multi_scale/concat"]            #模型文件的输出节点名称

input_tensor = {inputs[0]:[1,416,416,3]}
# 转换pb模型到tflite模型
converter = tf.lite.TFLiteConverter.from_frozen_graph(path, inputs, outputs,input_tensor)
converter.post_training_quantize = True
converter.allow_custom_ops=True
tflite_model = converter.convert()
# model_tflite.tflite这里改成自己想要保存tflite模型的地址即可
open("E:/LX/tensorflow/model_tflite.tflite", "wb").write(tflite_model)

2、

import tensorflow as tf
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
config = tf.ConfigProto()
config.gpu_options.allow_growth = True

graph_def_file = "E:/LX/tensorflow/yolov3_coco_v3.pb"

input_names = ["input/input_data"]
output_names = ["pred_sbbox/concat_2", "pred_mbbox/concat_2", "pred_lbbox/concat_2",
                         "pred_multi_scale/concat"]
input_tensor = {input_names[0]:[1,416,416,3]}


#uint8 quant
converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, input_names, output_names, input_tensor)
converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,tf.lite.OpsSet.SELECT_TF_OPS]
converter.allow_custom_ops=True

converter.inference_type = tf.uint8    #tf.lite.constants.QUANTIZED_UINT8
input_arrays = converter.get_input_arrays()
converter.quantized_input_stats = {input_arrays[0]: (127.5, 127.5)} # mean, std_dev
converter.default_ranges_stats = (0, 255)

tflite_uint8_model = converter.convert()
open("uint8.tflite", "wb").write(tflite_uint8_model)

3、

import tensorflow as tf

in_path = r'E:/LX/tensorflow/yolov3_coco_v3.pb'
out_path = r'E:/LX/tensorflow\output_graph.tflite'

input_tensor_name = ['input/input_data']
input_tensor_shape = {'input/input_data': [1, 416, 416, 3]}

class_tensor_name = ["pred_sbbox/concat_2", "pred_mbbox/concat_2", "pred_lbbox/concat_2",
                          "pred_multi_scale/concat"]

convertr = tf.lite.TFLiteConverter.from_frozen_graph(in_path, input_arrays=input_tensor_name
                                                     , output_arrays=class_tensor_name
                                                     , input_shapes=input_tensor_shape)

# convertr=tf.lite.TFLiteConverter.from_saved_model(saved_model_dir=in_path,input_arrays=[input_tensor_name],output_arrays=[class_tensor_name])
tflite_model = convertr.convert()

with open(out_path, 'wb') as f:
    f.write(tflite_model)

.h转换为tflite:

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
converter =  tf.compat.v1.lite.TFLiteConverter.from_keras_model_file('F:\yolo\yolo.h5')
tflite_model = converter.convert()
open("yolov3.tflite", "wb").write(tflite_model)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值