我正在尝试打印所有已知类及其概率值。第一个值是概率最高的类别。

到目前为止,这是我的代码:

from keras.applications.vgg16 import VGG16
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

model = VGG16()

print(model.summary())


# load an image from file
image = load_img('./pictures/door.jpg', target_size=(224, 224))
image = img_to_array(image)  #output Numpy-array

#4-dimensional: samples, rows, columns, and channels.
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 = model.predict(image)


# convert the probabilities to class labels
label = decode_predictions(yhat)
# retrieve the most likely result, e.g. highest probability
for i in range(0,5):
    label = label[i][i]
    print('%s (%.2f%%)' % (label[1], label[2] * 100))


我收到以下错误:

Traceback (most recent call last):
  File path, line 38, in <module>
    print('%s (%.2f%%)' % (label[1], label[2] * 100))
IndexError: string index out of range


您知道如何处理吗?
在此先感谢^^

最佳答案

您的代码中有错误。试试看:

labels = decode_predictions(yhat)[0]
# retrieve the most likely result, e.g. highest probability
for i in range(0,5):
    label = labels[i]
    #print('%s (%.2f%%)' % (label[1], label[2] * 100))
    print('%s (%.2f%%)' % (label[1], float(label[2]) * 100))

关于python - 在keras中的vgg中打印所有分支机构类。 IndexError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47451919/

10-11 07:23