如何在不写入文件系统的情况下从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)