带有Tensorflow扩展管道的TF-Lite转换器(芝加哥出租车管道示例)

时间:2019-03-26 17:22:21

标签: python tensorflow tensorflow-lite tfx

目标:TFX-> TF Lite转换器->在移动/ IoT设备上部署模型

我目前正在学习Tensorflow Extended及其Chicago Taxi Pipeline Example。 管道已完成运行(尽管有很多困难),并且 Pusher 组件已发出 Tensorflow SavedModel 文件(.pb)。

但是,这里遇到一个新问题: 通过Tensorflow Nightly / 1.13.1(同时尝试)和Python 2.7.6,我可以使用一些简单的python代码生成,保存和加载 SavedModel (用于测试实用程序的百万位数数据模型) ,例如saved_model.simple_savesaved_model.loader.load,但在将 TFX Pusher 发出的模型应用于以下模型时,我一直遇到错误。

(也许我在TFX管道上做错了什么?)

我使用的代码:

import tensorflow as tf
with tf.Session(graph=tf.Graph()) as sess:
    tf.compat.v1.saved_model.loader.load(sess, ["serve"], "/home/tigerpaws/taxi/serving_model/taxi_simple/1553187887")#"/home/tigerpaws/saved_model_example/model")
    graph=tf.get_default_graph()

错误:

KeyError                                  Traceback (most recent call last)
<ipython-input-11-a6978b82c3d2> in <module>()
      1 with tf.Session(graph=tf.Graph()) as sess:
----> 2     tf.compat.v1.saved_model.loader.load(sess, ["serve"], "/home/tigerpaws/taxi/serving_model/taxi_simple/1553187887")#"/home/tigerpaws/saved_model_example/model")
      3     graph=tf.get_default_graph()

/home/tigerpaws/anaconda/lib/python2.7/site-packages/tensorflow/python/util/deprecation.pyc in new_func(*args, **kwargs)
    322               'in a future version' if date is None else ('after %s' % date),
    323               instructions)
--> 324       return func(*args, **kwargs)
    325     return tf_decorator.make_decorator(
    326         func, new_func, 'deprecated',

/home/tigerpaws/anaconda/lib/python2.7/site-packages/tensorflow/python/saved_model/loader_impl.pyc in load(sess, tags, export_dir, import_scope, **saver_kwargs)
    267   """
    268   loader = SavedModelLoader(export_dir)
--> 269   return loader.load(sess, tags, import_scope, **saver_kwargs)
    270 
    271 

/home/tigerpaws/anaconda/lib/python2.7/site-packages/tensorflow/python/saved_model/loader_impl.pyc in load(self, sess, tags, import_scope, **saver_kwargs)
    418     with sess.graph.as_default():
    419       saver, _ = self.load_graph(sess.graph, tags, import_scope,
--> 420                                  **saver_kwargs)
    421       self.restore_variables(sess, saver, import_scope)
    422       self.run_init_ops(sess, tags, import_scope)

/home/tigerpaws/anaconda/lib/python2.7/site-packages/tensorflow/python/saved_model/loader_impl.pyc in load_graph(self, graph, tags, import_scope, **saver_kwargs)
    348     with graph.as_default():
    349       return tf_saver._import_meta_graph_with_return_elements(  # pylint: disable=protected-access
--> 350           meta_graph_def, import_scope=import_scope, **saver_kwargs)
    351 
    352   def restore_variables(self, sess, saver, import_scope=None):

/home/tigerpaws/anaconda/lib/python2.7/site-packages/tensorflow/python/training/saver.pyc in _import_meta_graph_with_return_elements(meta_graph_or_file, clear_devices, import_scope, return_elements, **kwargs)
   1455           import_scope=import_scope,
   1456           return_elements=return_elements,
-> 1457           **kwargs))
   1458 
   1459   saver = _create_saver_from_imported_meta_graph(

/home/tigerpaws/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/meta_graph.pyc in import_scoped_meta_graph_with_return_elements(meta_graph_or_file, clear_devices, graph, import_scope, input_map, unbound_inputs_col_name, restore_collections_predicate, return_elements)
    804         input_map=input_map,
    805         producer_op_list=producer_op_list,
--> 806         return_elements=return_elements)
    807 
    808     # Restores all the other collections.

/home/tigerpaws/anaconda/lib/python2.7/site-packages/tensorflow/python/util/deprecation.pyc in new_func(*args, **kwargs)
    505                 'in a future version' if date is None else ('after %s' % date),
    506                 instructions)
--> 507       return func(*args, **kwargs)
    508 
    509     doc = _add_deprecated_arg_notice_to_docstring(

/home/tigerpaws/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/importer.pyc in import_graph_def(graph_def, input_map, return_elements, name, op_dict, producer_op_list)
    397   if producer_op_list is not None:
    398     # TODO(skyewm): make a copy of graph_def so we're not mutating the argument?
--> 399     _RemoveDefaultAttrs(op_dict, producer_op_list, graph_def)
    400 
    401   graph = ops.get_default_graph()

/home/tigerpaws/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/importer.pyc in _RemoveDefaultAttrs(op_dict, producer_op_list, graph_def)
    157     # Remove any default attr values that aren't in op_def.
    158     if node.op in producer_op_dict:
--> 159       op_def = op_dict[node.op]
    160       producer_op_def = producer_op_dict[node.op]
    161       # We make a copy of node.attr to iterate through since we may modify

KeyError: u'BucketizeWithInputBoundaries'

还有另一种尝试,我尝试将 SavedModel 转换为 GraphDef(冻结图),因此我可以再次尝试使用该转换器。 转换需要一个output_node_names,我不知道。 我也找不到在代码中保存模型的位置(所以也许我可以在某处发现输出节点名称)。

关于问题或替代方法的任何想法?预先感谢。

编辑:有人可以帮助创建标签吗?我的声誉尚未达到1500,但是这个问题实际上是关于tfx / tensorflow-extended

1 个答案:

答案 0 :(得分:0)

抱歉造成的混乱;该问题实际上是由读取SavedModel文件引起的。

在SavedModel中,存在BucketizeWithInputBoundaries中未定义的操作op_dict

这仍然在Google的TODO列表中,在他们的两个脚本中进行了注释。

HereHere。 (Github链接):

# TODO(jyzhao): BucketizeWithInputBoundaries error without this.

导入指定的脚本后,此问题已解决。

from tensorflow.contrib.boosted_trees.python.ops import quantile_ops  # pylint: disable=unused-import