我需要优化旧版 tensorflow 模型,我正在关注这篇博客文章:https : //medium.com/google-cloud/optimizing-tensorflow-models-for-serving-959080e9ddbf(我首先尝试了 tensorflow lite转换器没有成功)。
博客文章中的步骤有效,除了最后一个,将优化的 Graph 转换回 SavedModel 格式。经过一些实验,我最终得到了这个功能:
def convert_graph_def_to_saved_model(export_dir, graph_filepath):
if tf.gfile.Exists(export_dir):
tf.gfile.DeleteRecursively(export_dir)
graph_def = get_graph_def_from_file(graph_filepath)
with tf.Session(graph=tf.Graph()) as session:
tf.import_graph_def(graph_def, name='')
inputs = {name_map['{}:0'.format(node.name)]:
session.graph.get_tensor_by_name(
'{}:0'.format(node.name))
for node in graph_def.node if node.op=='Placeholder'}
outputs = {'class_ids': session.graph.get_tensor_by_name(
'head/predictions/class_ids:0')}
signature = tf.saved_model.signature_def_utils.predict_signature_def(
inputs=inputs, outputs=outputs)
session.run([tf.global_variables_initializer(), tf.tables_initializer()])
b = tf.saved_model.builder.SavedModelBuilder(export_dir)
b.add_meta_graph_and_variables(session, [tag_constants.SERVING], signature_def_map={'predict':signature},
legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op'))
b.save()
print('Optimized graph converted to SavedModel!')
这将正确创建 SavedModel 文件。
然后进行预测,遗留代码使用
self.predictor = tensorflow.contrib.predictor.from_saved_model(model_path. signature_def_key="predict"
predictions = self.predictor(feed_dict)
其中 feed_dict 包含数据。
在这一点上,但是我收到以下错误:
in predict(self, df, feed_dict, single_feed_dict)
218 raise Exception('You must provide a df or a feed_dict to predict')
219
--> 220 predictions = self.predictor(feed_dict)
221
222 return predictions
venv_py3.7/lib/python3.7/site-packages/tensorflow_core/contrib/predictor/predictor.py in __call__(self, input_dict)
75 if value is not None:
76 feed_dict[self.feed_tensors[key]] = value
---> 77 return self._session.run(fetches=self.fetch_tensors, feed_dict=feed_dict)
venv_py3.7/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
954 try:
955 result = self._run(None, fetches, feed_dict, options_ptr,
--> 956 run_metadata_ptr)
957 if run_metadata:
958 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
venv_py3.7/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1178 if final_fetches or final_targets or (handle and feed_dict_tensor):
1179 results = self._do_run(handle, final_targets, final_fetches,
-> 1180 feed_dict_tensor, options, run_metadata)
1181 else:
1182 results = []
venv_py3.7/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1357 if handle is None:
1358 return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1359 run_metadata)
1360 else:
1361 return self._do_call(_prun_fn, handle, feeds, fetches)
venv_py3.7/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args)
1382 '\nsession_config.graph_options.rewrite_options.'
1383 'disable_meta_optimizer = True')
-> 1384 raise type(e)(node_def, op, message)
1385
1386 def _extend_graph(self):
FailedPreconditionError: Table not initialized.
我使用 python 3.7 和 tensorflow 1.15。
有人可以帮忙吗?