首页 > 解决方案 > 将 tensorflow/keras 模型转换为 tensorflow lite 模型时的问题

问题描述

在尝试tensorflow转换keras modeltensorflow lite.

这是我的设置:

model = models.Sequential()
model.add(layers.Dense(128, activation='relu', input_shape=(1,)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(17, activation='softmax'))
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
...
model.save('Foo.h5')
...
converter = tf.contrib.lite.TFLiteConverter.from_keras_model_file("Foo.h5")
tflite_model = converter.convert()
open("Foo_converted.tflite", "wb").write(tflite_model)

这是输出:

INFO:tensorflow:Froze 6 variables.
INFO:tensorflow:Converted 6 variables to const ops.

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-6-f0613acb22f5> in <module>()
      2 
      3 converter = tf.contrib.lite.TFLiteConverter.from_keras_model_file("Foo.h5")
----> 4 tflite_model = converter.convert()
      5 open("converted_model.tflite", "wb").write(tflite_model)

c:\users\dev\appdata\local\programs\python\python36\lib\site-packages\tensorflow\contrib\lite\python\lite.py in convert(self)
    451           input_tensors=self._input_tensors,
    452           output_tensors=self._output_tensors,
--> 453           **converter_kwargs)
    454     else:
    455       # Graphs without valid tensors cannot be loaded into tf.Session since they

c:\users\dev\appdata\local\programs\python\python36\lib\site-packages\tensorflow\contrib\lite\python\convert.py in toco_convert_impl(input_data, input_tensors, output_tensors, *args, **kwargs)
    340   data = toco_convert_protos(model_flags.SerializeToString(),
    341                              toco_flags.SerializeToString(),
--> 342                              input_data.SerializeToString())
    343   return data
    344 

c:\users\dev\appdata\local\programs\python\python36\lib\site-packages\tensorflow\contrib\lite\python\convert.py in toco_convert_protos(model_flags_str, toco_flags_str, input_data_str)
    133     else:
    134       raise RuntimeError("TOCO failed see console for info.\n%s\n%s\n" %
--> 135                          (stdout, stderr))
    136 
    137 

RuntimeError: TOCO failed see console for info.
b'Traceback (most recent call last):\r\n  File "c:\\users\\dev\\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\\dev\\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\\dev\\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\\dev\\appdata\\local\\programs\\python\\python36\\lib\\runpy.py", line 85, in _run_code\r\n    exec(code, run_globals)\r\n  File "C:\\Users\\Dev\\AppData\\Local\\Programs\\Python\\Python36\\Scripts\\toco_from_protos.exe\\__main__.py", line 5, in <module>\r\n  File "c:\\users\\dev\\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\\dev\\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\\dev\\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

如果有人可以解释为什么转换失败,那就太好了。

标签: pythontensorflowkerastensorflow-lite

解决方案


TfliteConverter 和 TocoConverter 似乎在除 Mac 之外的所有操作系统上都存在很大问题。您仍然可以使用以下步骤将模型转换为 TensorFlow Lite:

创建一个新的 Google Colab 笔记本

编写代码来转换模型。导入 TFLiteConverter 和其他东西。

将模型上传到笔记本中。

运行笔记本。

下载生成的 TFLite 文件。

你可以使用 这个笔记本


推荐阅读