简体   繁体   English

如何直接从云存储桶加载 tf.keras model?

[英]How to load tf.keras model directly from cloud bucket?

I try to load tf.keras model direcly from cloud bucket but I can't see easy wat to do it.我尝试直接从云存储桶加载 tf.keras model 但我看不到这样做很容易。 I would like to load whole model structure not only weights.我想加载整个 model 结构,而不仅仅是权重。

I see 3 possible directions:我看到了 3 个可能的方向:

  1. Is posssible to load keras model directly from Google cloud bucket?是否可以直接从谷歌云存储桶加载 keras model ? Command tf.keras.model.load_model('gs://my_bucket/model.h5') doesn't work命令 tf.keras.model.load_model('gs://my_bucket/model.h5') 不起作用

  2. I tried to use tensorflow.python.lib.ii.file_io but I don't know how to load this as model.我尝试使用 tensorflow.python.lib.ii.file_io 但我不知道如何将其加载为 model。

  3. I copied model to local directory by gsutil cp command but I don't know how to wait until operation will be complete.我通过 gsutil cp 命令将 model 复制到本地目录,但我不知道如何等到操作完成。 tf try to load model before download operation is complete so the errors occurs tf 尝试在下载操作完成之前加载 model 因此出现错误

I will be thankful for any sugestions.我会感谢任何建议。

Peter彼得

  1. Load the file from gs storage从 gs 存储加载文件
from tensorflow.python.lib.io import file_io
model_file = file_io.FileIO('gs://mybucket/model.h5', mode='rb')
  1. Save a temporary copy of the model locally在本地保存 model 的临时副本
temp_model_location = './temp_model.h5'
temp_model_file = open(temp_model_location, 'wb')
temp_model_file.write(model_file.read())
temp_model_file.close()
model_file.close()
  1. Load model saved locally加载本地保存的model
model = tf.keras.models.load_model(temp_model_location)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM