当前位置:   article > 正文

.pb文件转换为tflite文件遇到问题汇总_toco failed. see console for info.

toco failed. see console for info.

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(亲测,有效)

  1. conda install tensorflow=1.13.1
  2. 或则:
  3. pip install tensorflow==1.13.1

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

以及后面还跟了一长串:

  1. 2022-01-05 19:57:38.516029: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  2. 2022-01-05 19:57:38.516305: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
  3. 2022-01-05 19:57:38.516487: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  4. 2022-01-05 19:57:38.516752: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
  5. 2022-01-05 19:57:38.516918: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  6. 2022-01-05 19:57:38.517146: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
  7. 2022-01-05 19:57:38.517302: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  8. 2022-01-05 19:57:38.517586: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
  9. 2022-01-05 19:57:38.517748: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  10. 2022-01-05 19:57:38.517955: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
  11. 2022-01-05 19:57:38.518097: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  12. 2022-01-05 19:57:38.518394: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
  13. 2022-01-05 19:57:38.518559: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  14. 2022-01-05 19:57:38.518778: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
  15. 2022-01-05 19:57:38.518974: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  16. 2022-01-05 19:57:38.519626: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
  17. 2022-01-05 19:57:38.519798: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  18. 2022-01-05 19:57:38.520088: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
  19. 2022-01-05 19:57:38.520229: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  20. 2022-01-05 19:57:38.520475: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
  21. 2022-01-05 19:57:38.520631: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  22. 2022-01-05 19:57:38.520882: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
  23. 2022-01-05 19:57:38.521045: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  24. 2022-01-05 19:57:38.521346: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff
  25. 2022-01-05 19:57:38.521524: I tensorflow/lite/toco/import_tensorflow.cc:1373] Unable to determine output type for op: ListDiff
  26. 2022-01-05 19:57:38.521781: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: ListDiff

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

converter.post_training_quantize = True

问题解决!

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

  1. import tensorflow as tf
  2. # 把pb文件路径改成自己的pb文件路径即可
  3. path="E:/LX/tensorflow/yolov3_coco_v3.pb" #pb文件位置和文件名
  4. # 如果是不知道自己的模型的输入输出节点,建议用tensorboard做可视化查看计算图,计算图里有输入输出的节点名称
  5. inputs=["input/input_data"]
  6. #模型文件的输入节点名称
  7. outputs=["pred_sbbox/concat_2", "pred_mbbox/concat_2", "pred_lbbox/concat_2",
  8. "pred_multi_scale/concat"] #模型文件的输出节点名称
  9. input_tensor = {inputs[0]:[1,416,416,3]}
  10. # 转换pb模型到tflite模型
  11. converter = tf.lite.TFLiteConverter.from_frozen_graph(path, inputs, outputs,input_tensor)
  12. converter.post_training_quantize = True
  13. converter.allow_custom_ops=True
  14. tflite_model = converter.convert()
  15. # model_tflite.tflite这里改成自己想要保存tflite模型的地址即可
  16. open("E:/LX/tensorflow/model_tflite.tflite", "wb").write(tflite_model)

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

  1. Using TensorFlow backend.
  2. 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'.
  3. _np_qint8 = np.dtype([("qint8", np.int8, 1)])
  4. 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'.
  5. _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
  6. 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'.
  7. _np_qint16 = np.dtype([("qint16", np.int16, 1)])
  8. 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'.
  9. _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
  10. 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'.
  11. _np_qint32 = np.dtype([("qint32", np.int32, 1)])
  12. 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'.
  13. np_resource = np.dtype([("resource", np.ubyte, 1)])
  14. 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'.
  15. _np_qint8 = np.dtype([("qint8", np.int8, 1)])
  16. 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'.
  17. _np_quint8 = np.dtype([("quint8", np.uint8, 1)])

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

  1. pip uninstall numpy
  2. pip install numpy==1.16.4

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

1

  1. import tensorflow as tf
  2. # 把pb文件路径改成自己的pb文件路径即可
  3. path="E:/LX/tensorflow/yolov3_coco_v3.pb" #pb文件位置和文件名
  4. # 如果是不知道自己的模型的输入输出节点,建议用tensorboard做可视化查看计算图,计算图里有输入输出的节点名称
  5. inputs=["input/input_data"]
  6. #模型文件的输入节点名称
  7. outputs=["pred_sbbox/concat_2", "pred_mbbox/concat_2", "pred_lbbox/concat_2",
  8. "pred_multi_scale/concat"] #模型文件的输出节点名称
  9. input_tensor = {inputs[0]:[1,416,416,3]}
  10. # 转换pb模型到tflite模型
  11. converter = tf.lite.TFLiteConverter.from_frozen_graph(path, inputs, outputs,input_tensor)
  12. converter.post_training_quantize = True
  13. converter.allow_custom_ops=True
  14. tflite_model = converter.convert()
  15. # model_tflite.tflite这里改成自己想要保存tflite模型的地址即可
  16. open("E:/LX/tensorflow/model_tflite.tflite", "wb").write(tflite_model)

2、

  1. import tensorflow as tf
  2. import os
  3. os.environ["CUDA_VISIBLE_DEVICES"] = "0"
  4. config = tf.ConfigProto()
  5. config.gpu_options.allow_growth = True
  6. graph_def_file = "E:/LX/tensorflow/yolov3_coco_v3.pb"
  7. input_names = ["input/input_data"]
  8. output_names = ["pred_sbbox/concat_2", "pred_mbbox/concat_2", "pred_lbbox/concat_2",
  9. "pred_multi_scale/concat"]
  10. input_tensor = {input_names[0]:[1,416,416,3]}
  11. #uint8 quant
  12. converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, input_names, output_names, input_tensor)
  13. converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,tf.lite.OpsSet.SELECT_TF_OPS]
  14. converter.allow_custom_ops=True
  15. converter.inference_type = tf.uint8 #tf.lite.constants.QUANTIZED_UINT8
  16. input_arrays = converter.get_input_arrays()
  17. converter.quantized_input_stats = {input_arrays[0]: (127.5, 127.5)} # mean, std_dev
  18. converter.default_ranges_stats = (0, 255)
  19. tflite_uint8_model = converter.convert()
  20. open("uint8.tflite", "wb").write(tflite_uint8_model)

3、

  1. import tensorflow as tf
  2. in_path = r'E:/LX/tensorflow/yolov3_coco_v3.pb'
  3. out_path = r'E:/LX/tensorflow\output_graph.tflite'
  4. input_tensor_name = ['input/input_data']
  5. input_tensor_shape = {'input/input_data': [1, 416, 416, 3]}
  6. class_tensor_name = ["pred_sbbox/concat_2", "pred_mbbox/concat_2", "pred_lbbox/concat_2",
  7. "pred_multi_scale/concat"]
  8. convertr = tf.lite.TFLiteConverter.from_frozen_graph(in_path, input_arrays=input_tensor_name
  9. , output_arrays=class_tensor_name
  10. , input_shapes=input_tensor_shape)
  11. # convertr=tf.lite.TFLiteConverter.from_saved_model(saved_model_dir=in_path,input_arrays=[input_tensor_name],output_arrays=[class_tensor_name])
  12. tflite_model = convertr.convert()
  13. with open(out_path, 'wb') as f:
  14. f.write(tflite_model)

.h转换为tflite:

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/154306
推荐阅读
相关标签
  

闽ICP备14008679号