从keras加载带有image_dataset_from_directory()的图像



图像文件夹具有以下结构:

train/
...batch0/
......file00.jpg
......file01.jpg
...batch1/
......file10.jpg
......file11.jpg

目录batch0batch1的名称不是标签,标签在一个单独的文件中。问题是将这些图像加载到数据集中。函数image_dataset_from_directory('/batch0')image_dataset_from_directory('/batch1')不起作用。

错误:

ValueError: Expected the lengths of labels to match the number of files in the target directory. len(labels) is 2 while we found 0 files in ../train/batch0/. 

看起来这应该在tf夜间版本中得到修复。错误消息已更新为,

如果您希望获得一个只包含图像(没有标签(的数据集,通过CCD_ 5。

传递label_mode=None而不是labels=None

train/
...batch0/
......file00.jpg
......file01.jpg
...batch1/
......file10.jpg
......file11.jpg
import pathlib
data_dir = pathlib.Path('/content/train/')
import tensorflow as tf
batch_size = 16
img_height = 180
img_width = 180
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
label_mode=None,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)

输出

Found 4 files belonging to 2 classes.
Using 3 files for training.

相关内容

最新更新