用例:我正在尝试在Google App Engine中加载经过预训练的Keras模型(.h5文件)。 App Engine具有Python运行时3.7。
问题:我尝试使用load_model Keras函数。不幸的是,load_model函数确实需要一个'file_path',我无法从Google App Engine文件浏览器加载模型。此外,Google Cloud Storage似乎不是一种选择,因为它无法识别为文件路径。
问题:如何将预训练的模型(例如.h5)加载到Google App Engine中(而不先保存在本地)?也许有一种方法可以将我没有想到的将model.h5从Google Storage加载到Google App Engine中,例如通过使用其他函数(tf.keras.models.load_model()除外)或其他格式?
我只想阅读模型以进行预测。不需要编写或训练模型。
您必须先下载文件,然后再使用它,无法使用Cloud Storage路径访问对象。 documentation中有一个有关如何下载对象的示例:
from google.cloud import storage
def download_blob(bucket_name, source_blob_name, destination_file_name):
"""Downloads a blob from the bucket."""
# bucket_name = "your-bucket-name"
# source_blob_name = "storage-object-name"
# destination_file_name = "local/path/to/file"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)
print(
"Blob {} downloaded to {}.".format(
source_blob_name, destination_file_name
)
)
然后将文件写入/tmp
temporary folder,这是App Engine中唯一可用的文件。但是您必须考虑到,一旦删除了使用该文件的实例,该文件也将被删除。