【问题标题】:How to load Tensorflow frozen graph model from Google bucket?如何从 Google 存储桶加载 Tensorflow 冻结图模型?
【发布时间】:2020-02-14 11:35:33
【问题描述】:

当我们想在本地使用 TensorFlow 加载模型时,我们这样做:

path_to _frozen = model_path + '/frozen_inference_graph.pb'
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.io.gfile.GFile(path_to _frozen, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')

我们如何使用谷歌云功能将存储模型加载到谷歌存储桶上?

【问题讨论】:

  • 本地加载时,本地路径是什么?类似/home/user?
  • 是的,类似的。顺便说一句,我的意思是如果有像“tf.MetaGraphDef”这样的另一个函数,或者我需要使用元或索引检查点。

标签: tensorflow google-cloud-functions google-cloud-storage


【解决方案1】:
def download_blob(bucket_name, source_blob_name, destination_file_name):
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)

def 处理程序(请求): download_blob(BUCKET_NAME,'redbull/output_inference_graph.pb/frozen_inference_graph.pb','/tmp/frozen_inference_graph.pb') 打印(“好的”) detection_graph = tf.Graph() 使用 detection_graph.as_default(): od_graph_def = tf.GraphDef() 使用 tf.io.gfile.GFile('/tmp/frozen_inference_graph.pb', 'rb') 作为fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='')

【讨论】:

    【解决方案2】:

    您可以将您的 pb 文件存储在存储中。

    然后,在您的函数中,将其下载到local writable directory /tmp。请记住,这个目录是“在内存中”的。这意味着分配给您的函数的内存必须明确定义以处理您的应用程序内存占用和您的模型下载文件

    用这样的东西替换你的第一行。

    # Be sure that your function service account as access to the storage bucket    
    storage_client = storage.Client()
    bucket = storage_client.get_bucket('<bucket_name>')
    blob = bucket.blob('<path/to>/frozen_inference_graph.pb')
    
    # Download locally your pb file
    path_to_frozen = '/tmp/frozen_inference_graph.pb'
    blob.download_to_filename(path_to_frozen)
    

    【讨论】:

    • 我试图加载frozen_graph.pb,但是当我想测试函数时,我得到这个错误:错误:函数崩溃。详细信息:预期的二进制或 unicode 字符串,得到无
    • 您检查文件是否下载好?像文件内容的打印或类似的东西?我还在示例中执行了拼写错误(path_to 和 _frozen 之间的空格)。我在示例中更正了它,请确保这不是您的问题。
    • 是的,我测试并下载了模型,但后来我尝试像这样使用它: detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf .io.gfile.GFile(blob.download_to_filename(PATH_TO_FROZEN_GRAPH), 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') ,这表明该错误
    • 您没有错误行来定位问题?
    • 我刚刚发现了问题,我将函数调整为:def handler(request): download_blob(BUCKET_NAME,'redbull/output_inference_graph.pb/frozen_inference_graph.pb','/tmp/frozen_inference_graph. pb') print("okay") detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.io.gfile.GFile('/tmp/frozen_inference_graph.pb', 'rb ') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') 现在它正在工作
    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 2021-08-14
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多