python - 如何使用 tf.data.Dataset 包含文件
问题描述
我正在训练人脸识别模型,因此对于Triplet Loss,我必须生成批次,使其包含来自每个标签的固定数量的图像。例如。我的意思是每次生成批次进行训练时从 3 个随机标签中获取 8 张图像,正如Github Issue中所建议的那样。
在我的数据集文件夹中,我有一个子文件夹,它被重命名为标签并包含该文件夹的图像。
在给定的问题中,提出了解决方案,
import numpy as np
import cv2
num_labels = len(path_list)
num_classes_per_batch = 3
num_images_per_class = 8
image_dirs = ["/content/drive/My Drive/smalld_processed/train/{:d}".format(i) for i in
range(num_labels)]
## Create the list of datasets creating filenames
#datasets = [tf.data.Dataset.list_files(f"{image_dir}/*.jpg" for image_dir in image_dirs)]
datasets = [tf.data.Dataset.list_files(f"{image_dir}/*.jpg") for image_dir in image_dirs]
adk = ["{}/*.jpg".format(image_dir) for image_dir in image_dirs]
print(adk)
def generator():
while True:
# Sample the labels that will compose the batch
labels = np.random.choice(range(num_labels),
num_classes_per_batch,
replace=False)
for label in labels:
for _ in range(num_images_per_class):
yield label
choice_dataset = tf.data.Dataset.from_generator(generator, tf.int64)
dataset = tf.data.experimental.choose_from_datasets(datasets, choice_dataset)
## Now you read the image content
def load_image(filename):
image = cv2.imread(filename,1)
image = dataset.map(image, num_parallel_calls=tf.data.experimental.AUTOTUNE)
image = image[...,::-1]
label = int(os.path.split(os.path.dirname(filename))[1])
image=dataset1.append()
label=dataset2.append
return image, label
dataset = dataset.map(load_image, num_parallel_calls=tf.data.experimental.AUTOTUNE)
batch_size = num_classes_per_batch * num_images_per_class
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch(None)
有了这个,我无法加载图像,它向我显示了这个错误。
SystemError: <built-in function imread> returned NULL without setting an error
您能帮我解决错误或有关如何加载图像的任何其他建议吗?提前致谢!!
解决方案
我认为在这种情况下你cv2.imread
的行为是错误的。我将首先构建一个简单的程序,它不会“即时”进行阅读,而是预加载图像以在小型数据集上进行训练。
也感觉你在滥用 dataset.map 函数。我会推荐这个关于 tf.data.Dataset 函数的教程:http://tensorexamples.com/2020/07/27/Using-the-tf.data.Dataset.html ,也许这个关于增强的,所以你可以看到您应该如何map
正确使用该功能:http: //tensorexamples.com/2020/07/28/Augmentation.html。
祝你好运!
推荐阅读
- javascript - 有没有办法使用Javascript添加错误消息以显示在网页上?
- javascript - 如何在博客文章中添加代码片段?
- html - 显示块没有响应我的锚链接
- linux - 访问 /proc/sys 中的文件时,内核中发生了什么权限检查?
- android-studio - 如何在 Flutter 中的容器内使用扩展磁贴?
- visual-studio - VS Code 中的错误 [无法写入用户设置。请打开用户设置以更正其中的错误/警告,然后重试。]
- c# - Automapper Null 依赖注入使用 [Inject] 属性
- python - 为什么嵌入列表不附加 5
- mysql - 如何将数据从一个表传输到另一个表
- apache-spark - 在pyspark中加入2个表,多个条件,左连接?