tensorflow: Conversion from pb to tflite fails

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow installed from (source or binary): binary through pip
  • TensorFlow version (use command below): tf-nightly and tf 1.11 (updated today)
  • Python version: 3.6.6
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version: CPU only
  • GPU model and memory:
  • Exact command to reproduce:

Describe the problem

I have a trained model and exportet it to a frozen pb file. After that, I optimized this frozen graph for inference. Now I want to convert this graph to a tflite model to use on Android. The code I used to convert it is:

import tensorflow as tf

graph_def_file = "graph_optimized.pb"
input_arrays = ["Placeholder"]
output_arrays = ["output"]

converter = tf.contrib.lite.TocoConverter.from_frozen_graph(
    graph_def_file, input_arrays, output_arrays, input_shapes={"Placeholder" : [1, 227, 227, 3]})
tflite_model = converter.convert()
open("save_path/converted_model.tflite", "wb").write(tflite_model)

This fails with an error. I tried it both with my existing tensorflow installation and with a clean installation in a new virtualenv with tf-nightly like it was suggested in issue #22617 . Still the same errors happen

Source code / logs

WARNING:tensorflow:From C:/Users/User/Documents/Test_export/export.py:8: TocoConverter.from_frozen_graph (from tensorflow.contrib.lite.python.lite) is deprecated and will be removed in a future version.
Instructions for updating:
Use `lite.TFLiteConverter.from_frozen_graph` instead.
2018-10-11 11:57:49.338944: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
  File "C:/Users/User/Documents/Test_export/export.py", line 9, in <module>
    tflite_model = converter.convert()
  File "C:\Users\User\venv-tf-nightly\lib\site-packages\tensorflow\contrib\lite\python\lite.py", line 453, in convert
    **converter_kwargs)
  File "C:\Users\User\venv-tf-nightly\lib\site-packages\tensorflow\contrib\lite\python\convert.py", line 348, in toco_convert_impl
    input_data.SerializeToString())
  File "C:\Users\User\venv-tf-nightly\lib\site-packages\tensorflow\contrib\lite\python\convert.py", line 135, in toco_convert_protos
    (stdout, stderr))
RuntimeError: TOCO failed see console for info.
b'Traceback (most recent call last):\r\n  File "c:\\users\\user\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\tensorflow\\contrib\\lite\\toco\\python\\tensorflow_wrap_toco.py", line 18, in swig_import_helper\r\n    fp, pathname, description = imp.find_module(\'_tensorflow_wrap_toco\', [dirname(__file__)])\r\n  File "c:\\users\\user\\appdata\\local\\programs\\python\\python36\\lib\\imp.py", line 297, in find_module\r\n    raise ImportError(_ERR_MSG.format(name), name=name)\r\nImportError: No module named \'_tensorflow_wrap_toco\'\r\n\r\nDuring handling of the above exception, another exception occurred:\r\n\r\nTraceback (most recent call last):\r\n  File "c:\\users\\user\\appdata\\local\\programs\\python\\python36\\lib\\runpy.py", line 193, in _run_module_as_main\r\n    "__main__", mod_spec)\r\n  File "c:\\users\\user\\appdata\\local\\programs\\python\\python36\\lib\\runpy.py", line 85, in _run_code\r\n    exec(code, run_globals)\r\n  File "C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python36\\Scripts\\toco_from_protos.exe\\__main__.py", line 5, in <module>\r\n  File "c:\\users\\user\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\tensorflow\\contrib\\lite\\toco\\python\\toco_from_protos.py", line 22, in <module>\r\n    from tensorflow.contrib.lite.toco.python import tensorflow_wrap_toco\r\n  File "c:\\users\\user\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\tensorflow\\contrib\\lite\\toco\\python\\tensorflow_wrap_toco.py", line 28, in <module>\r\n    _tensorflow_wrap_toco = swig_import_helper()\r\n  File "c:\\users\\user\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\tensorflow\\contrib\\lite\\toco\\python\\tensorflow_wrap_toco.py", line 20, in swig_import_helper\r\n    import _tensorflow_wrap_toco\r\nModuleNotFoundError: No module named \'_tensorflow_wrap_toco\'\r\n'
None


Process finished with exit code 1

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (5 by maintainers)

Most upvoted comments

Same problem here. I got *.pb by this cmd: python3 export_tflite_ssd_graph.py --pipeline_config_path=training/ssd_mobilenet_v1_coco.config --trained_checkpoint_prefix=RESULTS/model.ckpt-191 --output_directory=inference_graph and I want to get *.tflite file by this cmd: tflite_convert --output_file=./ssd_mobilenet_v1_coco.tflite --graph_def_file=./tflite_graph.pb --input_shape=0,0,0,3 --input_array=image_tensor --output_array=detection_boxes,detection_scores,detection_classes,num_detections but get this error: Traceback (most recent call last): File "/home/baniel/MyCode/tfenv/tensorflow1.5-withpy3/bin/tflite_convert", line 11, in <module> sys.exit(main()) File "/home/baniel/MyCode/tfenv/tensorflow1.5-withpy3/lib/python3.5/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 401, in main app.run(main=run_main, argv=sys.argv[:1]) File "/home/baniel/MyCode/tfenv/tensorflow1.5-withpy3/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 125, in run _sys.exit(main(argv)) File "/home/baniel/MyCode/tfenv/tensorflow1.5-withpy3/lib/python3.5/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 397, in run_main _convert_model(tflite_flags) File "/home/baniel/MyCode/tfenv/tensorflow1.5-withpy3/lib/python3.5/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 159, in _convert_model output_data = converter.convert() File "/home/baniel/MyCode/tfenv/tensorflow1.5-withpy3/lib/python3.5/site-packages/tensorflow/contrib/lite/python/lite.py", line 445, in convert **converter_kwargs) File "/home/baniel/MyCode/tfenv/tensorflow1.5-withpy3/lib/python3.5/site-packages/tensorflow/contrib/lite/python/convert.py", line 277, in toco_convert_graph_def input_data.SerializeToString()) File "/home/baniel/MyCode/tfenv/tensorflow1.5-withpy3/lib/python3.5/site-packages/tensorflow/contrib/lite/python/convert.py", line 109, in toco_convert_protos (stdout, stderr)) RuntimeError: TOCO failed see console for info. b'2018-10-23 18:43:22.707313: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1055] Converting unsupported operation: TFLite_Detection_PostProcess\n2018-10-23 18:43:22.707397: F tensorflow/contrib/lite/toco/tooling_util.cc:617] Check failed: dim >= 1 (0 vs. 1)\nAborted\n' None Anyone could help me?

The last iteration of tf-nightly indeed fixes my problem! Interestingly running my code from inside PyCharm results in the same error as before although I’m using the same interpreter of my virtual environment. However, running the script from the terminal works.