【问题标题】:How to load a mat file from a google storage bucket in jupyter notebook如何从 jupyter notebook 中的谷歌存储桶加载 mat 文件
【发布时间】:2019-09-17 16:36:40
【问题描述】:

我正在尝试在大约 16gb 的图像数据上训练模型。我需要从我的 Cloud Storage 存储桶中导入一个 annotations.mat 文件。但是,由于loadmat 需要文件路径,我不确定如何导入 Google Storage 存储桶路径。我尝试创建 mat 数据的 pickle 文件,但 Jupyter Notebook 崩溃。

当前尝试:

from google.cloud import storage
client = storage.Client()
bucket = client.get_bucket('bucket-id')
blob = bucket.get_blob('path/to/annotations.pkl')
# crashes here
print(blob.download_as_string())

我想做类似的事情:

import scipy.io as sio

client = storage.Client()
bucket = client.get_bucket('bucket-id')

matfile = sio.loadmat(buket_path + 'path/to/annotations.pkl')

有人知道如何从 Cloud Storage 存储桶加载 mat 文件吗?

【问题讨论】:

    标签: machine-learning jupyter-notebook google-cloud-storage mat


    【解决方案1】:

    我没有在 python 中找到从 blob objectmat 文件的任何直接导入。但是有一个解决方法可以解决这个问题:不要直接导入 blob 对象并通过loadmat 读取它,而是创建一个临时文件并使用loadmat 函数的路径。

    为了重现场景,我跟着Google Cloud Storage python example(上传了一个mat file到一个bucket)。以下 python 代码下载 blob 对象,使用loadmat 读取它,最后删除创建的文件:

    from google.cloud import storage
    import scipy.io
    
    
    bucket_name = '<BUCKET NAME>'
    mat_file_path = '<PATH>/<MAT FILENAME>'
    temp_mat_filename = 'temp.mat'
    
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(mat_file_path)
    # Download mat file to temporary mat file
    blob.download_to_filename(temp_mat_filename)
    # Get mat object from temporary mat file
    mat = scipy.io.loadmat(temp_mat_filename)
    # Remove temp_mat_filename file
    # import os
    # os.remove(temp_mat_filename)
    

    希望对你有帮助:)

    【讨论】:

      【解决方案2】:

      这段代码描述了上传对象到桶。 我添加了网址,您可以在其中找到更多信息:

      https://cloud.google.com/storage/docs/uploading-objects.

      【讨论】:

        猜你喜欢
        • 2019-02-09
        • 2019-04-13
        • 1970-01-01
        • 1970-01-01
        • 2019-04-16
        • 2021-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多