且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不写入文件系统的情况下从Google存储桶还原Tensorflow模型?

更新时间:2023-09-21 08:04:40

在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)