My question is really simple, how to find the filename associated with a prediction in Keras? That is, if I have a set of 100 test samples named and I get a numpy array which contains the estimated class probabilities, how do I map the filenames to the probabilities?

import cv2
import os
import glob 

def load_test():
    X_test = []
    y_test = []
    file_list = glob.glob('*.png')
    for test_image in file_list:
        img = cv2.imread(test_image,1)
   return X_test,y_test

if __name__ == '__main__':
   X_test = np.array(X_test, dtype = np.uint8)
   X_test = X_test.reshape(X_test.shape[0],3,100,100)
   X_test = X_test.astype('float32')
   X_test /= 255
  • $\begingroup$ I guess these are image files loaded by some utility? Could you clarify how you are loading the data to be tested? Edit the question and show just the code you have written to load and test the images. $\endgroup$ Commented Dec 7, 2016 at 18:12
  • $\begingroup$ Is the prediction in the same order as the the output of glob? $\endgroup$ Commented Dec 8, 2016 at 17:27
  • 1
    $\begingroup$ @Raghuram Yes it is. $\endgroup$ Commented Dec 8, 2016 at 18:57

1 Answer 1


The order of the files that populate file_list, is the same order X_test appears in, by row.

So just match the indices to correlate filename with prediction.

X_test[0] ~ prediction[0] ~ file_list[0]


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.