【问题标题】:Unable to load images from a Google Cloud Storage bucket in TensorFlow or Keras无法从 TensorFlow 或 Keras 中的 Google Cloud Storage 存储桶加载图像
【发布时间】:2020-12-06 02:50:04
【问题描述】:

我在 Google Cloud Storage 上有一个存储桶,其中包含用于 TensorFlow 模型训练的图像。我正在使用tensorflow_cloud 加载存储在名为stereo-train 的存储桶中的图像,并且包含图像的目录的完整URL 是:

gs://stereo-train/data_scene_flow/training/dat

但是在tf.keras.preprocessing.image_dataset_from_directory函数中使用这个路径,我在谷歌云控制台的日志中得到错误:

FileNotFoundError: [Errno 2] No such file or directory: 'gs://stereo-train/data_scene_flow/training/dat'

如何解决这个问题?

代码:

GCP_BUCKET = "stereo-train"

kitti_dir = os.path.join("gs://", GCP_BUCKET, "data_scene_flow")
kitti_training_dir = os.path.join(kitti_dir, "training", "dat")

ds = tf.keras.preprocessing.image_dataset_from_directory(kitti_training_dir, image_size=(375,1242), batch_size=batch_size, shuffle=False, label_mode=None)


即使我使用以下内容,它也不起作用:


filenames = np.sort(np.asarray(os.listdir(kitti_train))).tolist()
# Make a Dataset of image tensors by reading and decoding the files.
ds = list(map(lambda x: tf.io.decode_image(tf.io.read_file(kitti_train + x)), filenames))

tf.io.read_file 而不是 keras 函数,我得到了同样的错误。如何解决这个问题?

【问题讨论】:

  • 你不能“修复它”,只是所有的 keras 函数都不接受谷歌云 URL,只有正常的文件系统路径。
  • Keras 函数的替代方案是什么?
  • 即使我使用以下内容: filenames = np.sort(np.asarray(os.listdir(kitti_train))).tolist() # 通过读取和解码文件来制作图像张量数据集. ds = list(map(lambda x: tf.io.decode_image(tf.io.read_file(kitti_train + x)), filenames)) 我得到同样的错误。

标签: python tensorflow keras google-cloud-platform


【解决方案1】:

如果您使用的是 Linux 或 OSX,您可以使用Google Cloud Storage FUSE,这将允许您在本地挂载您的存储桶并像使用任何其他文件系统一样使用它。关注installation guide,然后将存储桶安装在系统上的某个位置,即:

mkdir /mnt/buckets
gcsfuse gs://stereo-train /mnt/buckets

那么您应该能够在代码中使用挂载点的路径,并从 Keras 的存储桶中加载内容。

【讨论】:

  • 在我的原始代码中,我将 gs:// 路径替换为本地路径,并在运行代码之前运行以下命令: gcsfuse gs://stereo-train ~/Documents/stereo-train 我得到google cloud 上不存在~/Documents/stereo-train 的错误\
  • 如何使用fuse本地挂载bucket?
  • 您使用的是 Windows,因此没有适用于您的操作系统的 FUSE 驱动程序,但可能有一些替代方案,请参见此处:serverfault.com/questions/831179/…
猜你喜欢
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
相关资源
最近更新 更多