Please follow below sample code snippets to resolve your query.(I have used 20 images dataset of two classes)
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
PATH ="/train/" #path to your train directory which contains 8 subfolders
train_datagen = ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(PATH, target_size=(50, 50), batch_size=5, class_mode='binary') #class_mode='categorical'
Output:
Found 20 images belonging to 2 classes.
After defining and compiling the model, fit this dataset to the model.
num_classes = 2 # you can define num_classes = 8
model = tf.keras.Sequential([
tf.keras.layers.Rescaling(1./255),
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(num_classes)
])
model.compile(optimizer='adam',loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])
model.fit(train_generator,steps_per_epoch=2,epochs=5) # you can define steps_per_epoch=(number of images/batch_size),epochs
Please refer this link for more detials.