【问题标题】:Python Beam can't pickle/dill a large Tensorflow ModelPython Beam 无法腌制/挖取大型 TensorFlow 模型
【发布时间】:2017-07-18 15:32:33
【问题描述】:

我们正在尝试在线提供图像处理模型(在 Tensorflow 中),这样我们就不必出于速度目的而对 REST 服务或 Cloud-ML/ML-Engine 模型进行外部调用。

我们不是在每次推理时都尝试加载模型,而是想测试是否可以为每个 beam.DoFn 对象实例将模型加载到内存中,这样我们就可以减少加载和服务时间为模型。

例如

    from __future__ import absolute_import
    from __future__ import division
    from __future__ import print_function

    import tensorflow as tf
    import numpy as np


    class InferenceFn(object):

      def __init__(self, model_full_path,):
        super(InferenceFn, self).__init__()
        self.model_full_path = model_full_path
        self.graph = None
        self.create_graph()


      def create_graph(self):
        if not tf.gfile.FastGFile(self.model_full_path):
          self.download_model_file()
        with tf.Graph().as_default() as graph:
          with tf.gfile.FastGFile(self.model_full_path, 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            _ = tf.import_graph_def(graph_def, name='')
        self.graph = graph

当它不是 beam.DoFn 并且只是一个常规类时,它能够在本地运行得很好,但是当它转换为 DoFn 并且我尝试使用 Cloud Dataflow 远程执行它时,作业失败,因为在序列化/酸洗期间,我想相信它试图序列化整个模型

例如 Example of Error

有没有办法规避或阻止 python/dataflow 尝试序列化模型?

【问题讨论】:

    标签: python tensorflow google-cloud-dataflow tensorflow-serving


    【解决方案1】:

    是的——将模型作为字段存储在 DoFn 上需要对其进行序列化,以便将该代码传递给每个工作人员。您应该查看以下内容:

    1. 安排让每个工人都可以使用模型文件。这在Python dependencies document 中针对数据流进行了描述。
    2. 在您的 DoFn 中实现 start_bundle 方法并让它读取文件并将其存储在本地线程中。

    这确保文件的内容不会在您的本地机器上被读取和腌制,而是让每个工作人员都可以使用该文件然后读入。

    【讨论】:

    • start_bundle 函数有效。问题在于我们试图提供的模型文件的深度。
    猜你喜欢
    • 2015-06-15
    • 1970-01-01
    • 2020-04-07
    • 2019-05-11
    • 2020-12-31
    • 2022-01-11
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多