首页 > 解决方案 > Keras 特征提取 - 预期 input_1 有 4 个维度,但得到了形状为 (1, 46) 的数组

问题描述

在提取图像特征时,我遇到了 Keras 的问题。我已经用这段代码添加了 4d 层

# Add a fourth dimension (since Keras expects a list of images)
image_array = np.expand_dims(image_array, axis=0)

但仍然给我一个错误。

这是我的实际代码:

from pathlib import Path
import numpy as np
import joblib
from keras.preprocessing import image
from keras.applications import vgg16
import os.path

# Path to folders with training data
img_db = Path("database") / "train"

images = []
labels = []

# Load all the not-dog images
for file in img_db.glob("*/*.jpg"):

    file = str(file)

    # split path with filename
    pathname, filename = os.path.split(file)
    person = pathname.split("\\")[-1]

    print("Processing file: {}".format(file))

    # Load the image from disk
    img = image.load_img(file)

    # Convert the image to a numpy array
    image_array = image.img_to_array(img)

    # Add a fourth dimension (since Keras expects a list of images)
    # image_array = np.expand_dims(image_array, axis=0)

    # Add the image to the list of images
    images.append(image_array)

    # For each 'not dog' image, the expected value should be 0
    labels.append(person)

# Create a single numpy array with all the images we loaded
x_train = np.array(images)

# Also convert the labels to a numpy array
y_train = np.array(labels)

# Normalize image data to 0-to-1 range
x_train = vgg16.preprocess_input(x_train)

input_shape = (250, 250, 3)
# Load a pre-trained neural network to use as a feature extractor
pretrained_nn = vgg16.VGG16(weights='imagenet', include_top=False, input_shape=input_shape)

# Extract features for each image (all in one pass)
features_x = pretrained_nn.predict(x_train)

# Save the array of extracted features to a file
joblib.dump(features_x, "x_train.dat")

# Save the matching array of expected values to a file
joblib.dump(y_train, "y_train.dat")

错误

回溯(最近一次通话最后):文件“C:/Users/w024029h/PycharmProjects/keras_pretrained/pretrained_vgg16.py”,第 57 行,在 features_x = pretrained_nn.predict(x_train) 文件“C:\Users\w024029h\AppData\Local \Programs\Python\Python36\lib\site-packages\keras\engine\training.py",第 1817 行,预测 check_batch_axis=False) 文件 "C:\Users\w024029h\AppData\Local\Programs\Python\Python36\ lib\site-packages\keras\engine\training.py",第 113 行,在 _standardize_input_data 'with shape ' + str(data_shape)) , 46)

标签: kerasvgg-net

解决方案


After adding an extra dimension, image_array will have a shape similar to (1, 3, 250, 250) or (1, 250, 250, 3) (depending on your backend, considering 3-channel images).

When you do images.append(image_array), it will append this 4d-array into a list of numpy arrays. In practice, this list will be a 5d array, but when you convert the list back to a numpy array, numpy does not have a way to know what is the desired shape/number of dimensions you want.

You can use np.vstack() (doc) to stack each individual 4d-array in the first axis.

Change these lines in your code:

# Create a single numpy array with all the images we loaded
x_train = np.array(images)

For:

x_train = np.vstack(images)

推荐阅读