从文件路径中删除目录

我使用 Python 和 Keras 制作了一个卷积神经网络。我在测试集上测试我的模型,每个类的图像数量是随机的(1 个文件夹包含 x 数量的图像)。我能够获得一个数据框,其中显示图像和目录的文件名以及预测。我想从文件名中删除目录。它随机显示 350 张图像/dogs1.tif,我希望它只显示 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})


慕森卡
浏览 85回答 1
1回答

12345678_0001

#getting files names for the imagesfilenames= test_batches.filenamesfilename_extr=[]for i in filenames:  filename_extr.append(os.path.basename(i))#creating the dataframeresults=pd.DataFrame({"file":filename_extr,"pr":predictions})应该做的。(这(使用 for 循环)只是一种方法。当然还有很多其他方法。)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python