gpt4 book ai didi

python - 将 tf.keras.utils.image_dataset_from_directory 与标签列表一起使用

转载 作者:行者123 更新时间:2023-12-01 23:01:37 32 4
gpt4 key购买 nike

我有目录示例中对应文件数的标签列表:[1,2,3]

train_ds = tf.keras.utils.image_dataset_from_directory(
train_path,
label_mode='int',
labels = train_labels,
# validation_split=0.2,
# subset="training",
shuffle=False,
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)

我得到错误:

ValueError: Expected the lengths of `labels` to match the number of files in the target directory. len(labels) is 51033 while we found 0 files in ../input/jpeg-happywhale-128x128/train_images-128-128/train_images-128-128.

我尝试定义父目录,但在那种情况下我得到了 1 个类。

最佳答案

您的数据文件夹可能结构不正确。尝试这样的事情:

import numpy
from PIL import Image
import tensorflow as tf

samples = 10
for idx, c in enumerate(['/content/data/class1/', '/content/data/class2/']*samples):
imarray = numpy.random.rand(100,100,3) * 255
im = Image.fromarray(imarray.astype('uint8')).convert('RGB')
im.save('{}result_image{}.png'.format(c, idx))

train_labels = [0]*samples + [1]*samples
train_ds = tf.keras.utils.image_dataset_from_directory(
'/content/data',
label_mode='int',
labels = train_labels,
shuffle=False,
seed=123,
image_size=(100, 100),
batch_size=4)

for x, y in train_ds.take(1):
print(x.shape, y)
Found 20 files belonging to 2 classes.
(4, 100, 100, 3) tf.Tensor([0 0 0 0], shape=(4,), dtype=int32)

您的文件夹结构应如下所示:

├── data
│ ├── class2
│ │ ├── result_image5.png
│ │ ├── result_image9.png
│ │ ├── result_image15.png
│ │ ├── result_image13.png
│ │ ├── result_image1.png
│ │ ├── result_image3.png
│ │ ├── result_image11.png
│ │ ├── result_image19.png
│ │ ├── result_image7.png
│ │ └── result_image17.png
│ └── class1
│ ├── result_image14.png
│ ├── result_image8.png
│ ├── result_image12.png
│ ├── result_image18.png
│ ├── result_image16.png
│ ├── result_image6.png
│ ├── result_image2.png
│ ├── result_image10.png
│ ├── result_image4.png
│ └── result_image0.png

关于python - 将 tf.keras.utils.image_dataset_from_directory 与标签列表一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71704268/

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