tensorflow - 如何将文件名数据集映射到文件内容数据集
问题描述
例如,我有一个 tensorflow 数据集,其中每个元素都是 atf.string
Tensor
代表图像文件的文件名。现在我想将此文件名数据集映射到图像内容张量数据集。
我写了这样的代码,但它不起作用,因为 map 函数不能急切地执行。(引发错误说张量类型没有名为 numpy 的属性。)
def parseline(line):
filename = line.numpy()
image = some_library.open_image(filename).to_numpy()
return image
dataset = dataset.map(parseline)
解决方案
基本上,可以通过以下方式完成:
path = 'path_to_images'
files = [os.path.join(path, i) for i in os.listdir(path)] # If you need to create a list of filenames, because tf functions require tensors
def parse_image(filename):
file = tf.io.read_file(filename) # this will work only with filename as tensor
image = tf.image.decode_image(f)
return img
dataset = tf.data.Dataset.from_tensor_slices(files)
dataset = dataset.map(parse_image).batch(1)
如果您处于渴望模式,只需遍历数据集
for i in dataset:
print(i)
如果没有,您将需要一个迭代器
iterator = dataset.make_one_shot_iterator()
with tf.Session as sess:
sess.run(iterator.get_next())
推荐阅读
- angular - 在 Docker 上构建 Node/NPM Angular 映像时出错
- prolog - 序言 | 访问列表中的元素
- javascript - 使触摸滑块的图像像超链接一样重定向到特定页面
- html - 有没有办法在 Chrome 浏览器开发者工具上检查文本是否转义?
- python - 在 Python 中保存斜率系数
- assembly - RISC-V 汇编语言程序将华氏温度转换为摄氏温度
- javascript - Javascript 代码仅在 Chrome 开发者工具打开时在 Chrome 中有效
- android - 编写一个使用 KeyEvent 将文本插入文本字段的 Android 系统应用程序。但扩展的 unicode 字符不可打印
- python - 我如何在 tkinter 中创建带有背景的圆形按钮
- wordpress - 在以前的多站点 WordPress 安装中无法查看、更改、添加或删除插件或主题。我应该怎么办?