gpt4 book ai didi

tensorflow - 从 tf.Tensor 中包含的路径加载文件

转载 作者:行者123 更新时间:2023-12-03 14:55:31 26 4
gpt4 key购买 nike

我使用 tf.keras 构建了一个简单的模型和 tf.data.Dataset用于高效加载,因为数据集有几 GB 大。
图片在tiff格式,因此需要直接加载为 numpy.array .
我确实有一个标签和文件路径的数据集,并且想要映射一个用于加载该数据集的函数。因此,我必须以某种方式从张量中获取 python 字符串表示。
我尝试使用通常的 tf.Tensor.eval()然后将字符加入一个完整的字符串,但得到错误:ValueError: Cannot evaluate tensor using 评估(): No default session is registered.这确实有意义,因为在执行 keras 模型之前没有 session
然后我试着把 tf.enable_eager_execution()在数据集文件中我的 tensorflow 导入正下方(并从 .eval() 更改为 .numpy() ),但出现错误:AttributeError: 'Tensor' object has not attribute 'numpy'暗示tf.enable_eager_execution()不工作
基本上我正在尝试读取以下张量 a 中包含的字符串:

path = tf.decode_raw(path, tf.uint8)
path = ''.join(map(chr, path.eval())) # with session
path = ''.join(map(chr, path.numpy())) # with eager execution

image = PIL.Image.open(path)
image = numpy.array(image)
当在单个文件中“原型(prototype)设计”而没有其余部分时,两者都可以正常工作,但在具有例如我的模型在 model.pydataset.py 中的数据集但有 tf.enable_eager_execution()同时

最佳答案

这个问题很老了,但因为它可能对某人有用,这将是我的方法:
您可以使用 tf.io.read_file 读取文件,然后将其转换为带有 tf.image.decode_png 的图像如果您的图像是png。有几种其他格式可用。
tf_filepath 张量中包含的 RGB 图像的路径

image = tf.io.read_file(tf_filepath)
image = tf.image.decode_png(image, channels=3, dtype=tf.uint8)

关于tensorflow - 从 tf.Tensor 中包含的路径加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58085880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com