如何从Jupyter Notebook中的Google存储桶中加载Mat文件

时间:2019-04-29 04:08:19

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

我正在尝试在约16gb的图像数据上训练模型。我需要从我的Cloud Storage存储桶中导入let query ={ date:{ $gte:moment('2016-01-10').startOf('day').toDate(), $lte:moment('2016-01-10').endOf('day').toDate(), } } 文件。但是,由于annotations.mat需要文件路径,因此我不确定如何导入Google存储桶路径。我尝试创建垫数据的loadmat文件,但Jupyter Notebook崩溃。

当前尝试:

pickle

我想做类似的事情:

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())

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

2 个答案:

答案 0 :(得分:0)

我在python中没有发现从blob objectmat文件的任何直接导入。但是,有一种解决方法可以解决该问题:与其直接导入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 :(得分:-1)

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

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