如何在不写入文件系统的情况下从Google存储桶还原Tensorflow模型? [英] How to restore Tensorflow model from Google bucket without writing to filesystem?

查看:64
本文介绍了如何在不写入文件系统的情况下从Google存储桶还原Tensorflow模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2gb的Tensorflow模型,我想将其添加到App Engine上的Flask项目中,但是我似乎找不到任何文档说明我正在尝试做的事情.

I have a 2gb Tensorflow model that I'd like to add to a Flask project I have on App Engine but I can't seem to find any documentation stating what I'm trying to do is possible.

由于App Engine不允许写入文件系统,因此我将模型的文件存储在Google Bucket中,并尝试从那里恢复模型.这些是那里的文件:

Since App Engine doesn't allow writing to the file system, I'm storing my model's files in a Google Bucket and attempting to restore the model from there. These are the files there:

  • model.ckpt.data-00000-of-00001
  • model.ckpt.index
  • model.ckpt.meta
  • 检查点

在本地工作,我可以使用

Working locally, I can just use

with tf.Session() as sess:
    logger.info("Importing model into TF")
    saver = tf.train.import_meta_graph('model.ckpt.meta')
    saver.restore(sess, model.ckpt)

使用Flask的@before_first_request将模型加载到内存中的位置.

Where the model is loaded into memory using Flask's @before_first_request.

一旦在App Engine上,我就可以做到这一点:

Once it's on App Engine, I assumed I could to this:

blob = bucket.get_blob('blob_name')
filename = os.path.join(model_dir, blob.name)
blob.download_to_filename(filename)

然后执行相同的还原.但是App Engine不允许.

Then do the same restore. But App Engine won't allow it.

是否有一种方法可以将这些文件流式传输到Tensorflow的还原功能中,从而不必将文件写入文件系统?

Is there a way to stream these files into Tensorflow's restore functions so the files don't have to be written to the file system?

推荐答案

在Dan Cornilescu提出一些技巧并进行深入研究后,我发现Tensorflow使用名为ParseFromString的函数构建了MetaGraphDef,所以这就是我的最终结果在做:

After some tips from Dan Cornilescu and digging into it I found that Tensorflow builds the MetaGraphDef with a function called ParseFromString, so here's what I ended up doing:

from google.cloud import storage
from tensorflow import MetaGraphDef

client = storage.Client()
bucket = client.get_bucket(Config.MODEL_BUCKET)
blob = bucket.get_blob('model.ckpt.meta')
model_graph = blob.download_as_string()

mgd = MetaGraphDef()
mgd.ParseFromString(model_graph)

with tf.Session() as sess:
    saver = tf.train.import_meta_graph(mgd)

这篇关于如何在不写入文件系统的情况下从Google存储桶还原Tensorflow模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆