我正在从事以下工作:
ValueError:检查目标时出错:预期density_1的形状为(10,),但数组的形状为(5,)。
这是我的代码:
def conv_layer(x,filters,stride=1):
x = Conv2D(filters,kernel_size=3,padding="same",strides=stride)(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
return x
def SimpleNet(input_size=(224,224,3),num_classes=5):
inputs = Input(shape=input_size)
outputs = conv_layer(inputs,16,stride=2)
outputs = conv_layer(outputs,32)
outputs = conv_layer(outputs,32)
outputs = conv_layer(outputs,32,stride=2)
outputs = conv_layer(outputs,64)
outputs = conv_layer(outputs,64)
outputs = conv_layer(outputs,64,stride=2)
outputs = conv_layer(outputs,128)
outputs = conv_layer(outputs,128)
outputs = Dropout(0.5)(outputs)
outputs = GlobalAvgPool2D()(outputs)
outputs = Dense(num_classes,activation="softmax")(outputs)
return Model(inputs=inputs,outputs=outputs).
model = SimpleNet()
model.compile(loss="categorical_crossentropy",optimizer=Adam(lr=0.001),metrics=["accuracy"])
model.summary()
model_path = "my-model_{epoch:03d}.h5"
checkpoint = ModelCheckpoint(filepath=model_path, monitor="val_acc", save_best_only=True,save_weights_only=True,verbose=1)
model.fit_generator(train_data,
steps_per_epoch=int(9000/BATCH_SIZE),
epochs=100,
validation_data=test_data,
validation_steps=int(2000/BATCH_SIZE),
callbacks=[checkpoint])