如何将 tensorflow 事件从 VM 实例内的 Docker 容器写入 google 云存储

How to write tensorflow events to google cloud storage from Docker container inside VM instance

我在 Google Compute Engine 上创建了一个 VM 实例。上传我的项目并构建我的图像后,我 运行 进入我的容器并授权使用我的服务帐户访问 Google Cloud Platform:

gcloud auth activate-service-account test@xxx.iam.gserviceaccount.com --key-file=mykey.json

这样我就可以访问我的 google 云存储(我在我的存储桶上使用 gsutil cp 进行了测试并且它有效)。现在我尝试像这样执行一个 tensorflow python 脚本:

python object_detection/model_main_tf2.py \
  --pipeline_config_path=/raccoon/config.config \
  --model_dir=gs://my-bucket/ \
  --num_train_steps=10

指定为 model_dir 我的存储桶,以便将检查点和事件存储在那里(以便从我的笔记本电脑上使用 tensorboard 监控训练的进度)。

问题是我从 tensorflow 得到以下权限错误:

tensorflow.python.framework.errors_impl.PermissionDeniedError: 
Error executing an HTTP request: HTTP response code 
403 with body '{
  "error": {
    "code": 403,
    "message": "Insufficient Permission",
    "errors": [
      {
        "message": "Insufficient Permission",
        "domain": "global",
        "reason": "insufficientPermissions"
      }
    ]
  }
}
'
when initiating an upload to gs://my-bucket/train/events.out.tfevents.1601998426.266
1f74c3966.450.2928.v2
        Failed to flush 1 events to gs://my-bucket/train/events.out.tfevents.1601998426.2661f
74c3966.450.2928.v2
        Flushing first event.
        Could not initialize events writer. [Op:CreateSummaryFileWriter]

train 目录存在于我的存储桶中,正如我之前所说,以下命令正在运行:

gsutil cp test.txt gs://my-bucket/train/.

我是不是漏掉了什么?

验证 gcloud 只是确保未来的 gcloud 命令得到验证。您的脚本(可能)不使用 gcloud,因此未通过身份验证。

相反,如果您在 JSON 文件中有服务帐户凭据,则可以通过 GOOGLE_APPLICATION_CREDENTIALS 环境变量指定它,以使 TensorFlow 能够通过 read/write 到 GCS gs:// 网址。