Tensorflow/Keras
I want to classify images into either “Circle”, “Square” or “Triangle”. I have a directory containing 6 folders with each shape having a separate “shaded” or “unshaded” folder. How can I combine them into one category? For example: shaded and unshaded circles will be given a label “0” using flow_from_directory. I will then feed this into my CNN model and let it run.
Thanks for the help!
Advertisement
Answer
classes
in flow_from_directory
needs to match the subdirectory names.
Example:
JavaScript
x
11
11
1
shapes
2
├── circle
3
│ ├── shared
4
│ └── unshared
5
├── square
6
│ ├── shared
7
│ └── unshared
8
└── triangle
9
├── shared
10
└── unshared
11
JavaScript
1
14
14
1
import pathlib
2
# Get project root depending on your project structure.
3
PROJECT_ROOT = pathlib.Path().cwd().parent
4
SHAPES = PROJECT_ROOT / "shapes"
5
6
train_gen = ImageDataGenerator(
7
).flow_from_directory(
8
directory=SHAPES, # the path to the 'shapes' directory.
9
target_size=(IMAGE_WIDTH, IMAGE_HEIGHT),
10
classes=["circle", "square", "triangle"],
11
batch_size=8,
12
class_mode="categorical",
13
)
14
Output:
JavaScript
1
2
1
Found 12 images belonging to 3 classes.
2