将Numpy阵列加载到TensorFlow输入管道中

发布于 2025-01-23 17:38:51 字数 1473 浏览 3 评论 0原文

因此,我正在遵循一个教程,以制作图像的数据加载器( 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薯片软お妹 2025-01-30 17:38:51

@André的评论使我朝着正确的方向发展。下面的代码有效。


def process_image(file_path):
    label = get_label(file_path)
    label = np.uint8(label)

    img = np.load(file_path)
    img = tf.convert_to_tensor(img/255, dtype=tf.float32) 

    return img , label 

train_ds = images_ds.map(lambda item: tf.numpy_function(
          process_image, [item], (tf.float32, tf.uint8))) 

The comment of @André put me in the right direction. The code below works.


def process_image(file_path):
    label = get_label(file_path)
    label = np.uint8(label)

    img = np.load(file_path)
    img = tf.convert_to_tensor(img/255, dtype=tf.float32) 

    return img , label 

train_ds = images_ds.map(lambda item: tf.numpy_function(
          process_image, [item], (tf.float32, tf.uint8))) 

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文