-1

I've made a convolutional neural network using Python and Keras. I am testing my models on test sets with random number of images per class (1 folder containing x amount of images). I am able to get a data frame with the showing the file name for the image and the directory, and the prediction. I would like to remove the directory from the file name. It shows random 350 images/dogs1.tif and I want it to just say dogs1.tif.

  #import my model
new_model = tf.keras.models.load_model('model folder')
#upload my test data
train_datagen = ImageDataGenerator(rescale=1./255)

test_batches = train_datagen.flow_from_directory(
        'folder containing random images',
        target_size=(224, 224),
        batch_size=10,
        classes = None,
        class_mode = None,
        shuffle = False)

#my prediction
predictions = new_model.predict(test_batches, steps=35, verbose=0)

#rounding my predctions
rounded_predictions = np.argmax(predictions, axis = -1)

#converting one hot encoded labels to categorical labels 
labels =["dog","cat","horse"]
names = [0,1,2]
labels_name = dict(zip(names, labels))

#joining them together
labels_name = dict((v,k) for k,v in labels_name.items())
predictions = [labels[k] for k in rounded_predictions]

#getting files names for the images
filenames= test_batches.filenames

#creating the dataframe
results=pd.DataFrame({"file":filenames,"pr":predictions})
cdr
  • 21
  • 1
  • 7

1 Answers1

0
#getting files names for the images
filenames= test_batches.filenames

filename_extr=[]
for i in filenames:
  filename_extr.append(os.path.basename(i))

#creating the dataframe
results=pd.DataFrame({"file":filename_extr,"pr":predictions})

Should do it. (This (using a for loop) is just one way to do it. For sure there are plenty other ways.)

Stat Tistician
  • 813
  • 5
  • 17
  • 45