将Numpy阵列加载到TensorFlow输入管道中
因此,我正在遵循一个教程,以制作图像的数据加载器( https://github.com/codebasics/deep-learning-keras-tf-tutorial/blob/master/44_tf_data_pipeline/tf_data_pipeline.pipeline.ipynb )。
完整的代码就是这样:
images_ds = tf.data.Dataset.list_files("path/class/*")
def get_label(file_path):
import os
parts = tf.strings.split(file_path, os.path.sep)
return parts[-2]
## How the tutorial does it
def process_image(file_path):
label = get_label(file_path)
img = tf.io.read_file(file_path)
img = tf.image.decode_jpeg(img)
return img, label
## How I want to do it
def process_image(file_path):
label = get_label(file_path)
img = np.load(file_path)
img = tf.convert_to_tensor(img)
return img, label
train_ds = images_ds.map(process_image)
在教程中,数据是.jpeg。但是,我的数据是.npy。
因此,将数据加载到以下代码不起作用:
img = tf.io.read_file(file_path)
img = tf.image.decode_jpeg(img)
我想解决此问题,但我的解决方案不起作用。
img = np.load(file_path)
img = tf.convert_to_tensor(img)
当我喂食process_image函数1实例时,它确实有效。但是,当我使用.map函数时,我会出现错误。
错误:
TypeError:预期的str,字节或OS.Pathike对象,而不是张量
与tf.image.decode_image()用于解码numpy数组和/或有人可以帮助我解决我当前的错误吗?
So I am following a tutorial for making a dataloader for images (https://github.com/codebasics/deep-learning-keras-tf-tutorial/blob/master/44_tf_data_pipeline/tf_data_pipeline.ipynb).
The full code is something like this:
images_ds = tf.data.Dataset.list_files("path/class/*")
def get_label(file_path):
import os
parts = tf.strings.split(file_path, os.path.sep)
return parts[-2]
## How the tutorial does it
def process_image(file_path):
label = get_label(file_path)
img = tf.io.read_file(file_path)
img = tf.image.decode_jpeg(img)
return img, label
## How I want to do it
def process_image(file_path):
label = get_label(file_path)
img = np.load(file_path)
img = tf.convert_to_tensor(img)
return img, label
train_ds = images_ds.map(process_image)
In the tutorial, the data is a .jpeg. However, my data is a .npy.
Therefore, loading the data with the following code does not work:
img = tf.io.read_file(file_path)
img = tf.image.decode_jpeg(img)
I want to work around this problem, but my solution does not work.
img = np.load(file_path)
img = tf.convert_to_tensor(img)
It does work when I feed the process_image function 1 instance. However, when I use the .map function, I get an error.
Error:
TypeError: expected str, bytes or os.PathLike object, not Tensor
Is there an equivalent function to tf.image.decode_image() for decoding a numpy array and/or can someone help me with my current error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@André的评论使我朝着正确的方向发展。下面的代码有效。
The comment of @André put me in the right direction. The code below works.