请帮助。我在colab上运行训练代码时遇到此错误 想要进行多标签分类(7个不同的输出标签)
ValueError: Error when checking target: expected dense_9 to have shape (7,) but got array with shape (1,)
我的代码如下:
with open("data/fer2013/fer2013.csv") as f:
x_train = np.array(x_train, 'float32')
y_train = np.array(y_train, 'float32')
x_test = np.array(x_test, 'float32')
y_test = np.array(y_test, 'float32')
x_train /= 255 #normalize inputs between [0, 1]
x_test /= 255
x_train = x_train.reshape(x_train.shape[0], 48, 48, 1)
x_train = x_train.astype('float32')
x_test = x_test.reshape(x_test.shape[0], 48, 48, 1)
x_test = x_test.astype('float32')
model.add(BatchNormalization(input_shape=(48,48,1)))
#1st convolution layer
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2, 2)))
#2nd convolution layer
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2, 2)))
#3rd convolution layer
model.add(Conv2D(256, (2, 2), activation='relu'))
model.add(Conv2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2, 2)))
#4th convolution layer
model.add(Conv2D(512, (2, 2), activation='relu'))
model.add(Conv2D(512, (2, 2), activation='relu'))
model.add(MaxPooling2D(pool_size=(1,1), strides=(2, 2)))
model.add(Flatten())
#fully connected neural networks
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(7, activation='softmax'))
## Start the training
#model.fit(x_train, y_train, epochs=epochs, validation_split=0.0, shuffle=True) #train for all trainset
model.fit_generator(train_generator, steps_per_epoch=batch_size, epochs=epochs, validation_data=(x_test,y_test)) #train for randomly selected one
score = model.evaluate(x_test, y_test, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], score[1]*100))
##########################------------------------############################
# serialize model to JSON
model_json = model.to_json()
with open("model/model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model/weights.h5")
print("Saved model to disk")
############################---------------------###############################
想进行多标签分类(7个不同的输出标签) 我期待一个json文件model.json 但我遇到错误
ValueError: Error when checking target: expected dense_9 to have shape (7,) but got array with shape (1,)