我使用 Python 和 Keras 制作了一个卷积神经网络。我在测试集上测试我的模型,每个类的图像数量是随机的(1 个文件夹包含 x 数量的图像)。我能够获得一个数据框,其中显示图像和目录的文件名以及预测。我想从文件名中删除目录。它随机显示 350 张图像/dogs1.tif,我希望它只显示 dogs1.tif。 #import my modelnew_model = tf.keras.models.load_model('model folder')#upload my test datatrain_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 predictionpredictions = new_model.predict(test_batches, steps=35, verbose=0)#rounding my predctionsrounded_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 togetherlabels_name = dict((v,k) for k,v in labels_name.items())predictions = [labels[k] for k in rounded_predictions]#getting files names for the imagesfilenames= test_batches.filenames#creating the dataframeresults=pd.DataFrame({"file":filenames,"pr":predictions})
1 回答
12345678_0001
TA贡献1802条经验 获得超5个赞
#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})
应该做的。(这(使用 for 循环)只是一种方法。当然还有很多其他方法。)
添加回答
举报
0/150
提交
取消