python - 在 TensorFlow 中加载导出的模型

标签 python machine-learning tensorflow

我有一个在 GPU 机器上训练的 TensorFLow 模型。接下来,我需要导出它并部署在仅 CPU 的生产计算机上。

我已按照MNIST export example中所述使用了导出。 。 Saver 对象已在上面初始化。

with graph.as_default():
    saver = tf.train.Saver(tf.all_variables(), sharded=True)

...


export_path =  'resnet34_rmsprop_wd1e-1/saves/'
print('Exporting trained model to %s' % export_path)
init_op = tf.group(tf.initialize_all_tables(), name='init_op')
model_exporter = exporter.Exporter(saver)
model_exporter.init(sess.graph.as_graph_def(),
                            init_op=init_op,
                            default_graph_signature=exporter.classification_signature(input_tensor=inference_images,
                                                                                      classes_tensor=inference_class,
                                                                                      scores_tensor=inference_predictions),
                            named_graph_signatures={'inputs': exporter.generic_signature({'images': inference_images}),
                                                    'outputs': exporter.generic_signature({'class': inference_class, 'predictions': inference_predictions})})
model_exporter.export(export_path, tf.constant(1), sess)
print('Done exporting!')

接下来,我尝试加载保存的模型:

new_saver = tf.train.import_meta_graph('assets/saved_model/export.meta')
new_saver.restore(sess, 'assets/saved_model/export')

我得到的是:

Traceback (most recent call last):
File "script_test_classifier.py", line 4, in <module>
...
line 33, in __initialize_session__
new_saver = tf.train.import_meta_graph('assets/saved_model/export.meta')
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1711, in import_meta_graph
read_meta_graph_file(meta_graph_or_file), clear_devices)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1598, in _import_meta_graph_def
input_graph_def, name="", producer_op_list=producer_op_list)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/importer.py", line 258, in import_graph_def
op_def = op_dict[node.op]
KeyError: u'SaveV2'

错误的原因是什么以及如何修复?

此外,也许还有其他方法可以将 TensorFlow 模型导入 Python?

最佳答案

最好使用 TensorFlow 服务来加载导出的模型:https://tensorflow.github.io/serving/serving_basic

此错误表示在注册的操作中找不到“SaveV2”。所以您可以尝试将TensorFlow升级到最新版本。

关于python - 在 TensorFlow 中加载导出的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40616430/

相关文章:

Python:如何将初始质心放在k-means中的特定数据点上?

java - Tensorflow Java API - 复杂的例子

python - 是否可以直接使用 pymongo 对子文档的 mongodb 数组进行排序?

machine-learning - 如何更新反卷积层的权重?

python - 如何通过 python setuptools 执行脚本或函数?

python-3.x - 如何安装并运行openpose?

python - 在 centos 5 上安装 Tensorflow

python - 如何使用 TensorBoard 可视化具有自定义模型子类的 keras 模型?

python - 从视频文件中提取的音频太大

python - 在 python 2.5 中打印有关异常的信息?