![](/img/trans.png)
[英]Converting Keras Model Weights and Architecture to TensorFlow Lite Model
[英]Issues when converting tensorflow/keras model to tensorflow lite model
在尝试使用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
如果有人可以解释转换失败的原因,那就太好了。
TfliteConverter 和 TocoConverter 在除 Mac 以外的所有操作系统上似乎都存在很大问题。 您仍然可以使用以下步骤将模型转换为 TensorFlow Lite:
创建一个新的 Google Colab 笔记本
编写代码来转换模型。 导入 TFLiteConverter 和其他东西。
上传笔记本中的模型。
运行笔记本。
下载生成的 TFLite 文件。
你可以使用这个笔记本。
这看起来像https://github.com/tensorflow/tensorflow/issues/22617 ,其中 Windows 版本中缺少 _tensorflow_wrap_toco 库。 我建议您尝试安装最新的 Tensorflow 版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.