简体   繁体   English

无法 output 图像识别结果

[英]Cannot output image recognition result

My attempt to use CIFAR 10. The result is to output a coefficient that matches the recognized image我尝试使用 CIFAR 10。结果是 output 一个与识别图像匹配的系数

import numpy as np
    from keras.utils import np_utils
    from keras.models import model_from_json
    from keras.preprocessing import image
    from keras.optimizers import SGD
    import matplotlib.pyplot as plt
    %matplotlib inline

json_file = open("cifar10_model.json", "r")
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("cifar10_model.h5")

loaded_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

classes=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

img_path = 'cat.img'
img = image.load_img(img_path, target_size=(32, 32))
plt.imshow(img)
plt.show()

x = image.img_to_array(img)
x /= 255
x = np.expand_dims(x, axis=0)

prediction = loaded_model.predicr(x)
prediction - np_utils.categorical_probac_to_classes(prediction)
print(classes[prediction[0]])

And i have error我有错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-47-d30513382e4e> in <module>
      1 prediction = loaded_model.predict(x)
      2 prediction = np_utils.to_categorical(prediction)
----> 3 print(classes[prediction[0]])

TypeError: only integer scalar arrays can be converted to a scalar index

How fix it?怎么修?

The latest Python version is installed Backend ThesorFlow安装最新Python版本后端ThesorFlow

Change last few lines as follows.更改最后几行如下。 This will use argmax to find max probability of all classes.这将使用argmax来查找所有类的最大概率。

prediction = loaded_model.predict(x)
prediction_label = tf.argmax(prediction[0]).numpy()
#prediction = np_utils.categorical_probac_to_classes(prediction)
print(classes[prediction_label])

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

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