从 Google 云 Python 作业访问 Google 存储上的文件

Accessing files on Google Storage from a Google Cloud Python job

我正在松散地跟随 tutorial 在 Google Cloud AI Platform 上训练 TensorFlow 估计器。

我想访问包含我的训练和评估数据的目录,为此我已将我的数据文件递归复制到 Google 存储,如下所示:

gsutil cp -r data gs://name-of-my-bucket/data

这很好用,而且 gsutil ls gs://name-of-my-bucket/data 正确 returns:

gs://name-of-my-bucket/data/test.json
gs://name-of-my-bucket/data/test
gs://name-of-my-bucket/data/train

但是,从 Python 脚本调用 os.listdir(data_dir) 会引发 FileNotFoundError 对于我目前尝试过的任何 data_dir 值,包括 'data/''name-of-my-bucket/data/'。为什么?

我知道我的 Python 脚本正在从目录 /root/.local/lib/python3.7/site-packages/trainer/ /user_dir.

中执行

Python 出现问题的代码(编辑)

这是出现错误的行之前的代码,直接来自我的 Python 脚本的 __main__ 部分:

PARSER = argparse.ArgumentParser()
PARSER.add_argument('--job-dir', ...)
PARSER.add_argument('--eval-steps', ...)
PARSER.add_argument('--export-format', ...)

ARGS = PARSER.parse_args()
tf.logging.set_verbosity('INFO')
os.environ['TF_CPP_MIN_LOG_LEVEL'] = str(tf.logging.__dict__['INFO'] / 10)

HPARAMS = hparam.HParams(**ARGS.__dict__)

这是出现错误的代码行(在我上面报告的代码行之后立即调用的单独函数的第一行):

mug_dirs = [f for f in os.listdir(image_dir) if not f.startswith('.')]

日志(编辑)

我的这份工作的日志是 infos 的列表(加上与 TensorFlow 相关的 5 个弃用 warnings),然后是 错误 来自 master-replica-0 任务:

Traceback (most recent call last): File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "/usr/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/root/.local/lib/python3.7/site-packages/trainer/final_task.py", line 114, in <module> train_model(HPARAMS) File "/root/.local/lib/python3.7/site-packages/trainer/final_task.py", line 55, in train_model (train_data, train_labels) = data.create_data_with_labels("data/train/") File "/root/.local/lib/python3.7/site-packages/trainer/data.py", line 13, in create_data_with_labels mug_dirs = [f for f in os.listdir(image_dir) if not f.startswith('.')] FileNotFoundError: [Errno 2] No such file or directory: 'data/train/'

... 接着是来自同一任务的另一个 错误 (从我的 Python 命令报告非零退出状态),然后是两个 关于清理的信息,最后是来自 service 任务的 错误

The replica master 0 exited with a non-zero status of 1. Traceback (most recent call last): File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "/usr/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/root/.local/lib/python3.7/site-packages/trainer/final_task.py", line 114, in <module> train_model(HPARAMS) File "/root/.local/lib/python3.7/site-packages/trainer/final_task.py", line 55, in train_model (train_data, train_labels) = data.create_data_with_labels("data/train/") File "/root/.local/lib/python3.7/site-packages/trainer/data.py", line 13, in create_data_with_labels mug_dirs = [f for f in os.listdir(image_dir) if not f.startswith('.')] FileNotFoundError: [Errno 2] No such file or directory: 'data/train/' To find out more about why your job exited please check the logs: https://console.cloud.google.com/logs/viewer?project=1047296516162&resource=ml_job%2Fjob_id%2Fml6_run_25&advancedFilter=resource.type%3D%22ml_job%22%0Aresource.labels.job_id%3D%22ml6_run_25%22

云存储对象是一个平面命名空间,不包含在文件夹中。由于更加用户友好的体验,gsutil 和 Google Cloud Storage UI 将创建分层文件树的错觉。可以在 documentation.

上找到更多信息

现在,如果您尝试读取云存储上托管的文件对象,您可能需要使用以下 documentation to download an object to your local directory using the Cloud Storage Client Libraries. Alternatively, you may as well use the gsutil cp 命令,这将允许您在本地目录和Cloud Storage 存储桶,以及其他选项。

从本地目录中的 GCS 存储桶下载副本对象后,您将能够根据需要操作该文件。

更新 - 引用云存储对象文件 - 不要使用 os.listdir 访问 GCS 存储桶对象。

因为 Cloud Storage 是一个平面命名空间,一个 Cloud Storage 存储桶 gs://my-bucket/data/test.json 将包含一个名为 data/test.json 的对象,该对象存储在 gs://my-bucket 的根目录中。请注意,对象名称包含 / 个字符。因此,如果您想访问您存储桶中的文件 test.json,您可以查看上面的文档并使用 data/test.json 作为参考 - 文件夹的概念本身不存在。或者,如果您需要访问您的训练文件对象,您可以使用 data/train 作为参考。

您可以使用 tensorflow API 获取 GCP 目录中的所有文件。你可以参考他们的文档:https://www.tensorflow.org/api_docs/python/tf/io/gfile/glob

例如,如果你想获取你的GCP下的所有json个文件,你可以使用这个:

import tensorflow as tf

json_files = tf.io.gfile.glob("gs://name-of-my-bucket/data/"+"*.json")