如何从 Google Cloud Storage 存储桶加载保存在 joblib 文件中的模型

Soh*_*ard 5 python google-cloud-storage scikit-learn joblib

我想加载一个从 Google Cloud Storage 存储桶保存为joblib文件的模型。当它在本地路径时,我们可以按如下方式加载它(考虑到model_file是系统中的完整路径):

loaded_model = joblib.load(model_file)
Run Code Online (Sandbox Code Playgroud)

我们如何使用 Google Cloud Storage 完成相同的任务?

小智 11

对于任何在谷歌上搜索答案的人。除了显而易见的之外,还有两个选项,使用 Google AI 平台进行模型托管(和在线预测)。

选项 1 是像这样使用 TemporaryFile:

from google.cloud import storage
from sklearn.externals import joblib
from tempfile import TemporaryFile

storage_client = storage.Client()
bucket_name=<bucket name>
model_bucket='model.joblib'

bucket = storage_client.get_bucket(bucket_name)
#select bucket file
blob = bucket.blob(model_bucket)
with TemporaryFile() as temp_file:
    #download blob into temp file
    blob.download_to_file(temp_file)
    temp_file.seek(0)
    #load into joblib
    model=joblib.load(temp_file)
#use the model
model.predict(...)

Run Code Online (Sandbox Code Playgroud)

选项 2 是像这样使用 BytesIO:

from google.cloud import storage
from sklearn.externals import joblib
from io import BytesIO

storage_client = storage.Client()
bucket_name=<bucket name>
model_bucket='model.joblib'

bucket = storage_client.get_bucket(bucket_name)
#select bucket file
blob = bucket.blob(model_bucket)
#download blob into an in-memory file object
model_file = BytesIO()
blob.download_to_file(model_file)
#load into joblib
model=joblib.load(model_local)
Run Code Online (Sandbox Code Playgroud)

  • 这个答案的解决方案比公认的解决方案要好得多。 (2认同)

Iñi*_*igo 0

我认为这是不可能的,至少是直接的。我考虑过一种解决方法,但可能没有你想要的那么有效。

通过使用 Google Cloud Storage 客户端库[1],您可以首先下载模型文件,加载它,然后在程序结束时删除它。当然,这意味着您每次运行代码时都需要下载该文件。这是一个片段:

from google.cloud import storage
from sklearn.externals import joblib

storage_client = storage.Client()
bucket_name=<bucket name>
model_bucket='model.joblib'
model_local='local.joblib'

bucket = storage_client.get_bucket(bucket_name)
#select bucket file
blob = bucket.blob(model_bucket)
#download that file and name it 'local.joblib'
blob.download_to_filename(model_local)
#load that file from local file
job=joblib.load(model_local)
Run Code Online (Sandbox Code Playgroud)