【问题标题】:How to get prediction label along with percentage如何获得预测标签和百分比
【发布时间】:2025-12-01 05:50:01
【问题描述】:

我正在使用迁移学习在 Fruits360 数据集上使用 keras 重新训练 VGG16 模型。我已经训练了模型并生成了 model.h5 文件。现在,为了测试我训练的模型,我编写了一个单独的代码,如下所示,并加载了 model.h5 文件和输入图像,并使用 model.predict() 函数进行了预测。我得到了一组预测值作为输出,但我无法获得输出的标签。 预测后如何也得到标签?

from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.preprocessing import image
from keras.models import load_model
import os 

my_list = os.listdir('./fruits-360/Training')
labels = sorted(my_list)
#print(len(labels))

saved_model = load_model("output.h5")
# load an image from file
image = load_img('apple.jpg', target_size=(224, 224))
# convert the image pixels to a numpy array
image = img_to_array(image)
# reshape data for the model
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
# prepare the image for the VGG model
image = preprocess_input(image)
# predict the probability across all output classes
yhat = saved_model.predict(image)

print(yhat)

我从中得到的输出:

[[7.17755854e-02 1.64420519e-04 7.11962930e-04 1.09639345e-03
  3.65487649e-03 1.30461820e-03 1.71189080e-03 8.44106398e-05
  2.32845647e-04 2.93225341e-04 5.51751134e-09 8.36079926e-06
  2.45124284e-07 4.89534505e-05 3.62677121e-04 3.77899994e-07
  1.04390840e-09 2.77215719e-07 1.48338046e-07 1.58574392e-06
  1.85948572e-08 2.35122825e-05 1.40991315e-05 1.53142121e-09
  4.20618314e-08 9.00860164e-10 8.37871852e-08 1.38314470e-04
  2.33362043e-05 1.02217612e-07 1.56784572e-05 1.45486838e-05
  1.35744230e-07 7.53441327e-07 8.10141572e-08 9.25831589e-09
  1.17044747e-05 7.80909737e-09 1.17813433e-05 1.39052809e-05
  1.33823562e-06 8.83602411e-07 5.22362086e-07 3.12003103e-04
  3.63733534e-07 3.09960592e-06 7.83494880e-10 2.16209537e-06
  1.09540458e-07 1.00488634e-07 5.04332002e-06 3.11387431e-08
  1.43967145e-06 3.70907003e-08 9.72185060e-02 7.17791181e-07
  8.50022047e-07 1.09006250e-11 8.06401147e-07 2.94776954e-04
  1.42594319e-04 6.57663213e-06 2.22632690e-09 1.33982932e-04
  7.27764191e-03 1.76724559e-03 4.58840788e-07 2.83163081e-05
  1.27739793e-06 1.51839274e-07 6.35151446e-01 1.49872008e-04
  1.69212143e-07 6.46130411e-06 8.09798095e-09 1.33023859e-04
  3.11768084e-10 6.82332274e-03 2.72009001e-05 1.36803810e-05
  3.21909931e-04 2.18727801e-05 4.89347076e-06 1.65353231e-05
  8.18530396e-02 2.71601088e-08 3.78919160e-03 1.93472511e-06
  2.28390039e-04 9.45829204e-04 8.07484355e-08 2.39097773e-07
  3.94911304e-08 6.42228715e-10 1.27851049e-10 2.42364536e-06
  6.91388919e-08 5.50304435e-07 5.60582407e-08 6.93544493e-08
  2.04468861e-07 1.82402204e-07 1.29191315e-08 1.40132336e-03
  7.21434930e-08 1.26103216e-04 7.80344158e-02 6.98078452e-07
  6.39117275e-07 4.86231899e-09 6.67545173e-05 1.98491052e-05
  3.82679382e-08 4.00836188e-06 1.76605427e-05 5.99655250e-05
  1.41588691e-06 6.29748298e-09 1.60603679e-03 2.18801666e-04
  1.52924549e-05 2.39897645e-07 5.80409534e-08 1.40595137e-06
  4.33732907e-07 9.40148311e-06 6.87087507e-08 9.42246814e-08
  4.06775257e-07 1.12163532e-08 8.79949056e-08]]

我尝试了很多不同的选项,这些选项在其他有关相同问题的问题中得到了回答,但我无法找到任何解决方案。 谁能帮忙? 如果你愿意,我也可以提供再培训代码供参考。

提前致谢!

【问题讨论】:

  • 所以'saved_model'只提供特征提取,而你想要'saved_model'返回特征提取+最终概率?
  • 我希望它返回图像中水果/蔬菜的概率和标签。
  • 调用 predict 时 saved_model 返回什么?
  • 只是概率,如输出所示
  • 如果输出是概率,只需 np.argmax(yhat, axis=1) 为您提供预测标签

标签: python tensorflow keras prediction transfer-learning


【解决方案1】:

您可以从训练数据集中获取标签:

    class_names = your_train_ds.class_names
    labels = (v_datagen.class_indices)
    # this returns python dictionary in order of label_name:index
    
    # We need to switch this order to index:label_name, 
    # so that we can access the label name using the index as key

    labels = dict((val,ky) for ky,val in labels.items())

现在,获取 cmets 中提到的预测索引

    pred_ind = np.argmax(yhat, axis=1)
    print(labels[pred_ind])

【讨论】: