'How to include background images in Keras multlabel image classification?

I have been trying to train a multilabel classification model and want to include images that have no label to improve the training. I am using flow_from_dataframe to load the data and it sees the empty label list as a new class. I have also tried passing the class list into this function, in this case it completely ignores the images with no label. How can I train on images with no label in Keras?

Example of the dataframe I want to use:

0                        2022-04-06-16-15-18-209181.jpg            []
1                        2022-04-11-19-14-50-087726.jpg            []
2                        2022-04-06-16-15-11-507593.jpg            []
3                        2022-04-05-18-53-55-625837.jpg            [class1]
...                                                 ...                ...
3631                     2022-04-06-14-09-19-008147.jpg  [class1, class2]
3632                     2022-04-05-18-14-48-329041.jpg  [class1, class2]

Code:

train_generator = datagen.flow_from_dataframe(
    df,
    directory=data_dir + "/images",
    x_col='filename',
    y_col='labels',
    class_mode='categorical',
    shuffle=True,
    target_size=(IMAGE_SIZE, IMAGE_SIZE),
    batch_size=BATCH_SIZE, 
    subset='training',
    seed=42)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source