2

I am training a fruit classification model. As of right now my classes are: ['Fresh Apples', 'Fresh Bananas', 'Fresh Oranges']

I am using train, validation, and test generators using ImageDataGenerator and flow_from_directory. I have trained the model and am now wanting to feed the test generator into the model to see the performance of the model. Right now I only have 2 images in the test generator. I have the following code to make predictions:

    predictions = tuned_model.predict(test_generator)
    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)
        )
    )

And am getting the following as a result:

    This image most likely belongs to Fresh Apples with a 46.19 percent confidence.

Yes it is low accuracy, I have only trained for 10 epochs lol. BUT, is there a way I can see which image is being tested? Or a way to know if this prediction is the correct prediction?

EDIT:

Including code of generators...

generator = ImageDataGenerator(
    rotation_range=45,
    rescale=1./255,
    horizontal_flip=True,
    vertical_flip=True,
    validation_split=.2
)

train_generator = generator.flow_from_directory(
    train_path,
    target_size=(im_height, im_width),
    batch_size = batch_size,
    subset='training'
)

validation_generator = generator.flow_from_directory(
    train_path,
    target_size=(im_height, im_width),
    batch_size=batch_size,
    subset='validation'
)

test_generator = generator.flow_from_directory(
    test_path,
    target_size= (im_height, im_width),
    batch_size= batch_size,
)

In terms of my class labels, as of right now I simply hard coded them

class_names = ['Fresh Apples', 'Fresh Bananas', 'Fresh Bananas']

I know I should probably import os and create labels based on file structure, but I will do this later unless I absolutely need to.

  • Please include more code that defines your test set labels and whether you've included them into the test generator. It'll be easier to help that way, thanks! – TC Arlen Sep 08 '21 at 21:36
  • @TCArlen I updated with code for my train, val, and test generators – LeGOATJames23 Sep 09 '21 at 17:47

1 Answers1

3

I assume when you created the test generator you set shuffle=False in flow_from_directory. Then use

files=test_generator.filenames
class_dict=test_generator.class_indices # a dictionary of the form class name: class index
rev_dict={}
for key, value in class_dict.items()
    rev_dict[value]=key   # dictionary of the form class index: class name

files is a list of filename in the order the files were presented for prediction. Then do

predictions = tuned_model.predict(test_generator)

Then iterate through the predictions

for i, p in enumerate(predictions)
    index=np.argmax(p)
    klass=rev_dict[index]    
    prob=p[index]
    print('for file ', files[i], ' predicted class is ', klass,' with probability ',prob)

Of course you could also display the image as well

Gerry P
  • 7,662
  • 3
  • 10
  • 20