我是一名新的ML程序员,正在研究代码以显示所有类的准确性。该代码仅显示最可能的类。
而且,我在结果变量上打印了一个图,以查看其中的内容,除了一个类之外,其他所有内容都为0是否正常?所有班级应该有某种权重和概率?
整个项目都是关于迁移学习的,我在keras和cifar10中使用VGG16,model_weights.h5具有从cifar10中提取的功能,并且model_structure文件是具有模型结构的JSON文件,而vgg16具有密集结构修改的图层
from keras.models import model_from_json
from pathlib import Path
from keras.preprocessing import image
import numpy as np
# These are the CIFAR10 class labels from the training data (in order from 0 to 9)
class_labels = [
"Plane",
"Car",
"Bird",
"Cat",
"Deer",
"Dog",
"Frog",
"Horse",
"Boat",
"Truck"
]
# Load the json file that contains the model's structure
f = Path("model_structure.json")
model_structure = f.read_text()
# Recreate the Keras model object from the json data
model = model_from_json(model_structure)
# Re-load the model's trained weights
model.load_weights("model_weights.h5")
# Load an image file to test, resizing it to 32x32 pixels (as required by this model)
img = image.load_img("catdog11.jpg", target_size=(32, 32))
# Convert the image to a numpy array
image_to_test = image.img_to_array(img)
# Add a fourth dimension to the image (since Keras expects a list of images, not a single image)
list_of_images = np.expand_dims(image_to_test, axis=0)
# Make a prediction using the model
results = model.predict(list_of_images)
print("what is in results?: ", results)
# Since we are only testing one image, we only need to check the first result
single_result = results[0]
# We will get a likelihood score for all 10 possible classes. Find out which class had the highest score.
most_likely_class_index = int(np.argmax(single_result))
class_likelihood = single_result[most_likely_class_index]
# Get the name of the most likely class
class_label = class_labels[most_likely_class_index]
# Print the result
print("This is image is a {} - Likelihood: {:2f}".format(class_label, class_likelihood))
此刻正在显示
Using TensorFlow backend.
2019-04-15 17:56:05.082617: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140]
Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
结果如何?:[[0。 0. 0. 0. 0. 1. 1. 0. 0. 0. 0。]]
这是一只狗的图像-可能性:1.000000
以退出代码0结束的过程
我想要获得的结果是:
CIFAR 10具有10个类别,因此当我输入图像时,它应该显示为:
青蛙:0.4%
卡车:0.002%
以此类推
答案 0 :(得分:0)
您只需要更改最后一条输出行,即可将class_likelihood
乘以百分比:
print("This is image is a {} - Likelihood: {}".format(class_label, class_likelihood*100 ) )
简而言之,您只需将class_likelihood
乘以100。
percent = class_likelihood * 100