提取图像特征时,我对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:/用户/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”, _standardize_input_data中的第113行 'with shape'+ str(data_shape))ValueError:检查时出错:预期input_1具有4维,但数组的形状为(1, 46)
答案 0 :(得分:0)
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)