我想使用带有人脸的数据集创建一个图像分类程序,但我得到这个 ValueError: Input 0 of layer sequences_8 is incompatible with the layer:: expected min_ndim=4, found ndim=2。收到完整形状:[无,1]。
我的 CNN 源代码(不完整)
X = pickle.load(open("X.pickle", "rb"))
X = np.array(X)
y = pickle.load(open("y.pickle", "rb"))
y = np.array(y)
# normalizing data (a pixel goes from 0 to 255)
X = X/255.0
# Building the model
model = Sequential()
# 3 convolutional layers
model.add(Conv2D(32, (3, 3), input_shape = X.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
return_sequences=True
model.add(Conv2D(64, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
测试程序
def prepare(file):
IMG_SIZE = 80
img_array = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
model = tf.keras.models.load_model("CNN.model")
image = "test.jpg" #your image path
prediction = model.predict([image])
prediction = list(prediction[0])
print(CATEGORIES[prediction.index(max(prediction))])