簡體   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