machine-learning - 如何从jupyter笔记本中的Google存储桶加载mat文件

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

我正在尝试在约 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/

相关文章:

python - mlxtendplot_decision_regions 模型适合 Pandas DataFrame?

python - 尝试理解 model.predict 的输出

machine-learning - 如何可视化决策树?

python - 使用networkx.algorithms.approximation.steinertree.steiner_tree提取斯坦纳树时出错

node.js - 无法使用 google-cloud-node 设置 Cache-Control max-age header

google-cloud-storage - 删除对象名称为 "."

python - 替换嵌套循环

python - IPython 显示不带引号的字符串

command-line-interface - 如何从命令行启动 jupyter notebook 以在当前目录中运行,而无需编辑配置文件或传递硬路径?

google-app-engine - 如何使用GAE/SE go112将大文件上传到Google云端存储