ValueError。输入数组的样本数应该与目标数组相同。发现1280个输入样本和320个目标样本

问题描述 投票:0回答:1
 这段代码有什么问题? faces = datasets.fetch_olivetti_faces() X_train, X_test, y_train, y_test = train_test_split(faces.data,faces.target, test_size=0.2) X_train = X_train.reshape(-1,32 ,32 ,1) X_test = X_test.reshape(-1,32 , 32 ,1) # 规范化数据 X_train = X_train. astype('float32') X_test = X_test.astype('float32') X_train = 255.0 X_test = 255.0 # 一个热点类=40 y_train = keras.utils.to_categorical(y_train, classes) y_test = keras.utils.to_categorical(y_test, classes) #用Keras建立LetNet模型 def LetNet(width, height, depth, classes):      #初始化模型 model = Sequential() #第一层,卷积和池化 model.add(Conv2D(input_shape=(width, height, depth), kernel_size=(5, 5), filters=6, strides=(1,1), activation='tanh')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))      # 第二层,卷积和池化模型.add(Conv2D(input_shape=(width, height, depth), kernel_size=(5, 5), filters=16, strides=(1,1), activation='tanh')) 模型.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))      # 完全连接层 model.add(Flatten()) model.add(Dense(120,activation = 'tanh')) model.add(Dense(84,activation = 'tanh'))      # softmax分类器 model.add(Dense(classes)) model.add(Activation("softmax")) return model LetNet_model = LetNet(32,32,1,40) LetNet_model.summary() #Strat训练 LetNet_model.compile(optimizer=Adam(lr=0. 001, beta_1=0.9, beta_2=0.999, epsilon=1e-08),loss = 'categorical_crossentropy',metrics=['accuracy']) History = LetNet_model.fit(X_train, y_train, epochs=5, batch_size=32,validation_data=(X_test, y_test))
python keras deep-learning face-recognition
1个回答
0
投票

我的计算结果是,你的输入图像的形状是64乘64,有一个通道,而不是32乘32。

你可以修改下面的行。

    X_train = X_train.reshape(-1, 64 ,64 ,1)
    X_test = X_test.reshape(-1, 64 , 64 ,1)

另外,改变你的模型输入。

LetNet_model = LetNet(64,64,1,40)
© www.soinside.com 2019 - 2024. All rights reserved.