python - 无法从 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 函数,我得到了同样的错误。如何解决这个问题?
解决方案
如果您使用的是 Linux 或 OSX,您可以使用Google Cloud Storage FUSE,这将允许您在本地挂载您的存储桶并像使用任何其他文件系统一样使用它。按照安装指南,然后将存储桶安装在系统上的某个位置,即:
mkdir /mnt/buckets
gcsfuse gs://stereo-train /mnt/buckets
然后,您应该能够在代码中使用挂载点的路径,并从 Keras 的存储桶中加载内容。
推荐阅读
- python - 如何将参数从一个 gpflow 模型转移到另一个模型以获得相似的结果?
- android - 为什么 React-Native 无法在与 Expo 的 Android 连接上启动项目?
- javascript - 带有角度的keyPress的单元测试
- plot - 如何将 y 标签集中在 Julia 的 Makie 中的堆叠子图上?
- android - MVVM 架构。从 Repository 进行 API 调用的最佳实践,没有泄漏并且不对多个 viewModel 使用相同的实时数据
- google-analytics - 有没有办法使用 utm 参数跟踪一个链接上的多个测试?
- amazon-web-services - AWS Cognito 管理员用户邀请与 DynamoDB 用户的关系
- reactjs - 使用反应复制到剪贴板
- python - 在 tensorflow 中创建顶部“k”元素掩码的更有效方法
- c# - 使用 #syncfusion 将 word doc 转换为 .ConvertToPDF 时出现问题