Trying to create a model to tell if x image belongs to a class or not

Hi, I’ve been trying to create a model using tensorflow keras to see if, giving the model an image it can tell me if it belongs to a specific class or not. I’m using a dataset of images composed by different gameboys and gameboy games and I would like to end with a model that can tell me if an image is certain gameboy model or not.

For the last 7 or 8 months I’ve been searching and trying multiple model examples from youtube videos and forums, but none has worked for me yet. My last attempt uses a model that has 6 classes and tells me which of the 6 classes the test image belongs to (not accurate at all). I would like the model to just tell me if x image is y class or not, I don’t need it to know if it belong to class a, b, c or d, but I don’t know how to make a model like that.

The dataset has 6 directories (I’m using tf.keras.utils.image_dataset_from_directory because I don’t know how else to import the images with classes, so I have 6 classes) and each directory has around 65 images.

The model, after around 200-250 epochs shows between 0.8~0-9 accuracy and similar validation accuracy after each epoch, but when I use prediction and some test images the percent confidence barely reaches 36%, using this code for predictions:

predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])

print(
“This image most likely belongs to {} with a {:.2f} percent confidence.”
.format(class_names[np.argmax(score)], 100 * np.max(score))
)

I honestly don’t know what else to try, I’ve been trying to learn and understand a bit tensorflow and keras, but there’s still so much I don’t know. My goal would be to have multiple models, one model to check if an image is a gameboy color or not, another model to check if an image is a gameboy advance game or not, etc, or a single model that can tell which class an image belongs to (I would also like to know if it’s possible to have a “none of the above” class for when an image won’t match with anything in the dataset)

I’ve been trying to figure it out by myself all these months but I’ve reached a stalemate and don’t know how to go on, any help is highly appreciated. It all started as a “lets see if it works” but now I would like to make a working image classification model.

I’ll try to upload the script, I can’t see how to upload in this Create a new Topic GUI
Edit: I can’t find how to upload files so I’ll upload them to gdrive and share the link:
script in txt extension so gdrive won’t cancel the upload
In the training model process I see a lot of loop warnings. I’ve read these slow down the training process a lot and happen in tensorflow 2.9 when augmentating shuffled images and that it will probably be fixed in the future. Feel free to correct me if I’m wrong or if it can be fixed in 2.9. As for right now, the model takes about 3 minutes to complete 1 epoch (I can’t upload image due to link limit).
accuracy and predictions shown after epoch.

Let me now if I need to upload something else, like the .h5 trained model file.

Hi @Sergio,

Welcome to the TensorFlow Forum!

This objective can be achieved by using Image classification model of Supervised learning where you have to train the model based on the available image dataset and its given classes. You can predict the model on new dataset after model training.

Please have a look at this similar example for your reference.