1
$\begingroup$

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 = []
    os.chdir(testing_path)
    file_list = glob.glob('*.png')
    for test_image in file_list:
        img = cv2.imread(test_image,1)
        X_test.append(img)
        y_test.append(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
$\endgroup$
  • $\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$ – Neil Slater Dec 7 '16 at 18:12
  • $\begingroup$ Is the prediction in the same order as the the output of glob? $\endgroup$ – Raghuram Dec 8 '16 at 17:27
  • 1
    $\begingroup$ @Raghuram Yes it is. $\endgroup$ – Armen Aghajanyan Dec 8 '16 at 18:57
2
$\begingroup$

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]

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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