我正在尝试在约 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 文件吗?
最佳答案
我还没有发现任何从 python 中的 blob 对象
到 mat
文件的直接导入。不过,有一种解决方法可以解决该问题:创建一个临时文件并使用 loadmat
函数的路径,而不是直接导入 blob 对象并通过 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)
希望有帮助:)
关于machine-learning - 如何从jupyter笔记本中的Google存储桶加载mat文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55896822/