如何读取目录中的两个文件夹并使用 flow_from_directory 将它们合并到一个标签下?

How do I read two folders in a directory and combine them under one label using flow_from_directory?

Tensorflow/Keras

我想将图像分类为“圆形”、“方形”或“三角形”。我有一个包含 6 个文件夹的目录,每个形状都有一个单独的“阴影”或“无阴影”文件夹。如何将它们合并为一类?例如:带阴影和不带阴影的圆圈将使用 flow_from_directory 指定标签“0”。然后我会将其输入我的 CNN 模型并让它 运行.

感谢您的帮助!

classes in flow_from_directory 需要匹配子目录名称。

示例:

shapes
├── circle
│   ├── shared
│   └── unshared
├── square
│   ├── shared
│   └── unshared
└── triangle
    ├── shared
    └── unshared
import pathlib
# Get project root depending on your project structure.
PROJECT_ROOT = pathlib.Path().cwd().parent
SHAPES = PROJECT_ROOT / "shapes"

train_gen = ImageDataGenerator(
).flow_from_directory(
    directory=SHAPES, # the path to the 'shapes' directory.
    target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),
    classes=["circle", "square", "triangle"],
    batch_size=8,
    class_mode="categorical",
)

输出:

Found 12 images belonging to 3 classes.