首页 > 解决方案 > 无法从 TensorFlow 或 Keras 中的 Google Cloud Storage 存储桶加载图像

问题描述

我在 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 函数,我得到了同样的错误。如何解决这个问题?

标签: pythontensorflowkerasgoogle-cloud-platform

解决方案


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

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

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


推荐阅读