尝试使用tensorflow
时,遇到了将keras model
转换为tensorflow 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
如果有人可以解释为什么转换失败,那就太好了。
答案 0 :(得分:1)
在除Mac之外的所有操作系统上,TfliteConverter和TocoConverter似乎都存在很大问题。您仍然可以使用以下步骤将模型转换为TensorFlow Lite:
创建一个新的Google Colab笔记本
编写代码以转换模型。导入TFLiteConverter和其他内容。
在笔记本中上传模型。
运行笔记本电脑。
下载生成的TFLite文件。
您可以使用this notebook。
答案 1 :(得分:0)
这看起来像https://github.com/tensorflow/tensorflow/issues/22617,Windows版本中缺少_tensorflow_wrap_toco库。我建议您尝试安装最新的Tensorflow版本。