如何使用keras.preprocessing从Google云端存储加载图片

问题描述 投票:2回答:2

我正在编写机器学习代码,可以在本地或在云中进行培训。我正在使用keras.preprocessing来加载图像,它在引擎盖下使用PIL。它适用于本地文件,但可以理解的是不了解Google云端存储路径,例如“gs:// ...”。

from keras.preprocessing import image image.load_img("gs://myapp-some-bucket/123.png")

给出了这个错误:

.../lib/python2.7/site-packages/keras/preprocessing/image.py", line 320, in load_img img = pil_image.open(path) File .../lib/python2.7/site-packages/PIL/Image.py", line 2530, in open fp = builtins.open(filename, "rb") IOError: [Errno 2] No such file or directory: 'gs://myapp-some-bucket/123.png'

这样做的正确方法是什么?我最终需要一个图像文件夹作为单个numpy阵列(图像解码和灰度)。

keras google-cloud-storage google-cloud-ml
2个回答
2
投票

找到了解了GCS的keras.preprocessing.image.load_img的替代品。我还包含了更多的代码来读取整个文件夹,并将文件夹中的每个图像转换为一个numpy数组进行训练......

import os import tensorflow as tf from tensorflow.python.platform import gfile filelist = gfile.ListDirectory("gs://myapp-some-bucket") sess = tf.Session() with sess.as_default(): x = np.array([np.array(tf.image.decode_png(tf.read_file(os.path.join(train_files_dir, filename))).eval()) for filename in filelist])


0
投票

加载图片:

image_path = 'gs://xxxxxxx.jpg'
image = tf.read_file(image_path)
image = tf.image.decode_jpeg(image)
image_array = sess.run(image)

保存图片:

job_dir = 'gs://xxxxxxxx'
image = tf.image.encode_jpeg(image_array)
file_name = 'xxx.jpg'
write_op = tf.write_file(os.path.join(job_dir, file_name), image)
sess.run(write_op)
© www.soinside.com 2019 - 2024. All rights reserved.