如何从jupyter笔记本中的谷歌存储桶加载mat文件

问题描述 投票:1回答:2

我正在尝试在~16gb的图像数据上训练模型。我需要从我的云存储桶导入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')

有谁知道如何从云存储桶加载mat文件?

machine-learning jupyter-notebook google-cloud-storage mat
2个回答
0
投票

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

为了重现这个场景,我跟着Google Cloud Storage python example(将mat file上传到一个桶)。以下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)

希望能帮助到你 :)


-1
投票

此代码描述了将对象上载到存储桶。我添加了网址,您可以在其中找到更多信息:

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

© www.soinside.com 2019 - 2024. All rights reserved.