Create tensorflow dataset from image local directory(从图像本地目录创建 tensorflow 数据集)
问题描述
我在本地有一个非常庞大的图像数据库,数据分布就像每个文件夹都包含一个类的图像.
I have a very huge database of images locally, with the data distribution like each folder cointains the images of one class.
我想使用 tensorflow 数据集 API 来批量获取数据,而无需将所有图像都加载到内存中.
I would like to use the tensorflow dataset API to obtain batches de data without having all the images loaded in memory.
我尝试过这样的事情:
def _parse_function(filename, label):
image_string = tf.read_file(filename, "file_reader")
image_decoded = tf.image.decode_jpeg(image_string, channels=3)
image = tf.cast(image_decoded, tf.float32)
return image, label
image_list, label_list, label_map_dict = read_data()
dataset = tf.data.Dataset.from_tensor_slices((tf.constant(image_list), tf.constant(label_list)))
dataset = dataset.shuffle(len(image_list))
dataset = dataset.repeat(epochs).batch(batch_size)
dataset = dataset.map(_parse_function)
iterator = dataset.make_one_shot_iterator()
image_list 是一个列表,其中添加了图像的路径(和名称),而 label_list 是一个列表,其中每个图像的类都以相同的顺序添加了.
image_list is a list where the path (and name) of the images have been appended and label_list is a list where the class of each image has been appended in the same order.
但是 _parse_function 不起作用,我记录的错误是:
But the _parse_function does not work, the error that I recibe is:
ValueError:形状必须为 0 级,但对于输入形状为 [?] 的file_reader"(操作:ReadFile")为 1 级.
ValueError: Shape must be rank 0 but is rank 1 for 'file_reader' (op: 'ReadFile') with input shapes: [?].
我已经用谷歌搜索了错误,但对我没有任何作用.
I have googled the error, but nothing works for me.
如果我不使用地图功能,我只是记录图像的路径(存储在 image_list 中),所以我认为我需要地图功能来读取图像,但我无法做到有效.
If I do not use the map function, I just recibe the path of the images (which are store in image_list), so I think that I need the map function to read the images, but I am not able to make it works.
提前谢谢你.
def read_data():
image_list = []
label_list = []
label_map_dict = {}
count_label = 0
for class_name in os.listdir(base_path):
class_path = os.path.join(base_path, class_name)
label_map_dict[class_name]=count_label
for image_name in os.listdir(class_path):
image_path = os.path.join(class_path, image_name)
label_list.append(count_label)
image_list.append(image_path)
count_label += 1
推荐答案
错误在这一行 dataset = dataset.repeat(epochs).batch(batch_size)
你的管道添加了batchsize作为维度输入.
The error is in this line dataset = dataset.repeat(epochs).batch(batch_size)
Your pipeline adds batchsize as a dimension to input.
您需要在这样的地图功能之后批处理您的数据集
You need to batch your dataset after map function like this
dataset = tf.data.Dataset.from_tensor_slices((tf.constant(image_list), tf.constant(label_list)))
dataset = dataset.shuffle(len(image_list))
dataset = dataset.repeat(epochs)
dataset = dataset.map(_parse_function).batch(batch_size)
这篇关于从图像本地目录创建 tensorflow 数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从图像本地目录创建 tensorflow 数据集
基础教程推荐
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01