简体   繁体   English

Keras 预测 model - 如何获得第一个元素

[英]Keras prediction model - how to get the first element

Hi I am doing a small model which predicts fruits.嗨,我正在做一个预测水果的小型 model。 I have a function which runs a series of predfor various images and they output the prediction as shown below.我有一个 function 运行一系列 predfor 各种图像,他们 output 预测如下所示。

[[0. [[0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]] 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

I know the output is not an array, however I would like to know if there is a possible way to check the values at certain positions.我知道 output 不是数组,但是我想知道是否有可能的方法来检查某些位置的值。 For example, if it was an array, i would have done:例如,如果它是一个数组,我会这样做:

if prediction[0] == 1:
    print(prediction, "Apple")

However, since it's not, I have no clue how to check the values inside it.但是,既然不是,我不知道如何检查其中的值。 Is there a way I can check?有什么方法可以检查吗?

The function is this: function 是这样的:

def fruit_prediction(image_dir):
    img_list = os.listdir(image_dir)
    print(img_list)
    for fruits in img_list:
        path = os.path.join(image_dir, fruits)
        img = image.load_img(path, target_size = (150, 150))
        array = image.img_to_array(img)
        x = np.expand_dims(array, axis=0)

        vimage = np.vstack([x])
        prediction = model.predict(vimage)
        print(prediction, fruits)

Use of np.argmax() may be solve your problem使用 np.argmax() 可能会解决您的问题

here np.argmax(prediction ) will return index of highest probability.Now you have index.with the help of index you can easily determine fruit这里 np.argmax(prediction ) 将返回最高概率的索引。现在你有了索引。在索引的帮助下你可以很容易地确定水果

ex.前任。

#let your food rep index in one hot encoding like below.
fruit={0:'apple',1:'orange',2:'banana'....like that}

Now you have prediction as you calulated in above function现在您有了上面 function 中计算的预测

 index=np.argmax(prediction)
 print("fruit name",fruit[index])

Hi Guys I managed to fix this by using prediction.argmax(), like this嗨,伙计们,我设法通过使用 prediction.argmax() 解决了这个问题,就像这样

if prediction.argmax() == 0:
    print(np.around(prediction, 3), "\n(File: ", fruit, ") Prediction: Apple\n")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM