TransWikia.com

Keras Predictions as a list of file names for each class

Data Science Asked on May 8, 2021

I’ve created a model using Keras, I have trained it with a training and validation set, and have used a test set filled with random number of images for each class. My test set consists of one folder with 300 images, each of the filenames for each image has the class in the name(e.g dog1.tif, dog2.tif, cat1.tif, horse5.tif). I’d like to double check which images were predicted to each class by looking checking the filenames. How can I get a list of filenames for each of the predicted class?

I first load my saved model

new_model = tf.keras.models.load_model('model')

Define my test set

test_batches = train_datagen.flow_from_directory('test_images',target_size=(224,224),batch_size=10,classes = None,class_mode= None)

Make a prediction

predictions = new_model.predict(test_batches, steps=30, verbose=0)

Then, count find the number of predictions for each class (0 is dog, 1 is cat, 2 is horse)

import collections

collections.Counter(np.argmax(predictions, axis = 1)

Again, Ideally I would like to get a list of files names for each class

or if possible,

Sort the predicted images into a a folder corresponding to the class

Question is cross posted on stack overflow

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP