使用imagenet进行错误分类

2024-06-17 12:10:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用VGG16和imagenet权重来预测图像。但不幸的是,这一预测是错误的。 我不知道我哪里出错了

代码:

from keras.preprocessing import image as image_util 
from keras.applications.imagenet_utils import preprocess_input
from keras.applications.imagenet_utils import decode_predictions
from keras.applications import VGG16
import numpy as np 
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required= True,help ="path of the image")
args = vars(ap.parse_args())

# orig = cv2.imread(args["image"]) #Opencv function to load a image

image = image_util.load_img(args["image"],target_size=(224,224))
image = image_util.img_to_array(image)

#print("!!!!!.....!!!!")
print(image.shape)

image = np.expand_dims(image,axis=0) #(224,224,3) --> (1,224,224,3)
#print("!!!!!.....!!!!")
print(image.shape)
image = preprocess_input(image)

#Loading the model 
model = VGG16(weights="imagenet")
pred = model.predict(image)
#print("111!!!!!.....!!!!")
#print(pred)
p = decode_predictions(pred)
#print("222!!!!!.....!!!!")
#print(p)

for (i,(imagenetID,label,prob)) in enumerate(p[0]):
    print("{}. {}: {:.2f}%".format(i+1, label, prob*100))


orig = cv2.imread(args["image"]) #Opencv function to load a image
(imagenetID,label,prob) = p[0][0]
cv2.putText(orig, "Label:{},{:.2f}%".format(label,prob*100),(10,30),cv2.FONT_HERSHEY_SIMPLEX,0.8,(0,255,0),2)
cv2.imshow("classification",orig)
cv2.waitKey(0) 

输出:

输出必须是一个苹果。但是我有史密斯奶奶

Output predicted image


Tags: fromimageimportutilargscv2labelkeras