Keras和VGG16:VGG16模型的多个图像。预测

时间:2018-10-12 22:07:36

标签: python keras vgg-net

以下是Keras文档页面的代码:

from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np

model = VGG16(weights='imagenet', include_top=False)

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

features = model.predict(x)

我正在尝试将几张图像输入model.predict(x)

这是我到目前为止所拥有的。我必须使用OpenCV读取图像,因为它们是PGM文件,很难用Pillow读取。

A1_16 = []
width = 224
height = 224
dim = (width, height)

for i ....... get all the images
    data = cv2.imread(filename,-1) 
    if statement ....
        resized = cv2.resize(data, dim, interpolation = cv2.INTER_AREA)
        resized = np.expand_dims(resized, axis=0)
        A1_16.append(resized)

A1_16 = np.asarray(A1_16)
A1_16 = preprocess_input(A1_16)

A1_16的最终形状为(624, 1, 224, 224),我应该在第3个索引中得到3,但是我似乎无法操纵数组来获得它。

from keras.applications.vgg16 import VGG16
model = VGG16(weights='imagenet', include_top=False)

from keras.applications.vgg16 import preprocess_input
A1_16_train, A1_16_test, y1_16_train, y1_16_test = model_selection.train_test_split(A1_16, y1, test_size = 0.2)

yhat = model.predict(A1_16_train)

这是由于形状不正确而出现错误的地方。

ValueError: Error when checking input: expected input_3 to have shape (224, 224, 3) but got array with shape (1, 224, 224)

有时错误是:

ValueError: Error when checking input: expected input_3 to have shape (None, None, 3) but got array with shape (1, 224, 224)

我似乎永远都不会得到3。

0 个答案:

没有答案