繁体   English   中英

如何从 VGG16 获得预测

[英]How to get the predictions from VGG16

我正在使用以下 function:

def labelObjectFromImage(image_path, directory_filename):
    scale = 100
    img = cv2.imread(image_path+directory_filename)
    new_height = int(img.shape[0] * scale / 100)
    new_width = int(img.shape[1] * scale / 100)
    while (new_height > 224 or new_width > 244):
        scale -= 1
        new_height = int(img.shape[0] * scale / 100)
        new_width = int(img.shape[1] * scale / 100)

    channels = img.shape[2]
    img = np.array(load_img(image_path+directory_filename, target_size=(new_height, new_width)))
    model = VGG16(weights="imagenet", include_top = False, input_shape = (new_height, new_width, channels))
    img = img_to_array(img)
    img = tf.image.resize(img, (new_height, new_width))
    img = tf.reshape(img, (1, new_height, new_width, channels))
    img = preprocess_input(img)
    yhat = model.predict(img)
    classes = np.argmax(yhat, axis = 1)

我现在上课怎么办? 我打印了类,它是一个 0 的多维数组。

Tensorflow Keras VGG16工作示例代码

from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.applications.vgg16 import decode_predictions

model = VGG16()
image = load_img('img.jpeg', target_size=(224, 224))
image = img_to_array(image)
# reshape data for the model
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
pred = model.predict(image)
# convert the probabilities to class labels
label = decode_predictions(pred)
# retrieve the most likely result, e.g. highest probability
label = label[0][0]
# print the classification
print('%s (%.2f%%)' % (label[1], label[2]*100))
image = preprocess_input(image)

Output

hamster (22.84%)

暂无
暂无

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

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