我正在尝试在~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文件?
我没有发现从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)
希望能帮助到你 :)
此代码描述了将对象上载到存储桶。我添加了网址,您可以在其中找到更多信息: