来自特拉字节AI文章:https://telabytes.com/article/preview?id=20
from keras.preprocessing import image def plot_bar(predictions): types = [pred[1] for pred in predictions] probs = [pred[2] for pred in predictions] plt.barh(np.arange(len(probs)), probs) _ = plt.yticks(np.arange(len(predictions)), types) plt.show() def load_img(img_path): img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) return x
12345678910111213141516import matplotlib from keras.applications.inception_v3 import InceptionV3, preprocess_input, decode_predictions def predicts_inceptionV3(img_path): x = load_img(img_path) x = preprocess_input(x) # 加载InceptionV3预训练模型 # 如果本地不存在该模型,就下载 model = InceptionV3(weights='imagenet') # 预测狗狗品种 preds = model.predict(x) # 对预测的值解码,只显示前5个最大的值 predictions = decode_predictions(preds, top=5)[0] # 1.绘直方图显示 plot_bar(predictions) # 2.绘制原始图和预测概率图 # 创建一个绘图对象 fig, ax = plt.subplots() # 设置绘图的总容器大小 fig.set_size_inches(5, 5) # 取出预测的品种名称和它对应的概率值 fig_title = "".join(["{}: {:.5f}%n".format(pred[1], pred[2]*100) for pred in predictions]) # 设置在图像旁边的预测注解文字 ax.text(1.01, 0.7, fig_title, horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes) # 读取图片的数值内容 img = matplotlib.image.imread(img_path) # 在Axes对象上显示图像 ax.imshow(img)
123456789101112131415161718192021222324252627282930313233343536输出如下图片
如果是预测这个人的了?
来自特拉字节:https://telabytes.com/article/preview?id=20