我正在训练一个图像分类器来从 Kaggle 集中区分猫和狗。
这是我的相关代码:
FINAL_ACTIVATION = "softmax"
OPTIMIZER = keras.optimizers.Adamax()
STRIDES = (2, 2)
DROPOUT = 0.5
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape = INPUT_SHAPE))
model.add(keras.layers.ELU())
model.add(MaxPooling2D(pool_size = (2, 2), strides = STRIDES))
model.add(Conv2D(64, (3, 3)))
model.add(keras.layers.ELU())
model.add(MaxPooling2D(pool_size = (2, 2), strides = STRIDES))
model.add(Conv2D(128, (3, 3)))
model.add(keras.layers.ELU())
model.add(MaxPooling2D(pool_size = (2, 2), strides = STRIDES))
model.add(Conv2D(128, (3, 3)))
model.add(keras.layers.ELU())
model.add(MaxPooling2D(pool_size = (2, 2), strides = STRIDES))
model.add(Flatten())
model.add(Dense(100))
model.add(keras.layers.ELU())
model.add(Dropout(DROPOUT))
model.add(Dense(50))
model.add(keras.layers.ELU())
model.add(Dropout(DROPOUT))
model.add(Dense(2))
model.add(Activation(FINAL_ACTIVATION))
model.compile(
loss="categorical_crossentropy",
optimizer = OPTIMIZER,
metrics = ["accuracy"]
)
train_datagen = ImageDataGenerator(
rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True
)
train_generator = train_datagen.flow(
x_train,
y_train,
batch_size = BATCH_SIZE
)
model.summary()
model.fit_generator(
train_generator,
steps_per_epoch = x_train.shape[0] // BATCH_SIZE,
epochs = EPOCHS
)
由于大量的训练文件,没有测试集。但是,当我尝试训练此网络时,我收到错误“ValueError:检查目标时出错:预期激活_1 具有形状 (2,) 但得到的数组具有形状 (1,)。” 我在这里做错了什么?
卷积神经网络对我来说仍然有点黑魔法,所以我可能在这里犯了一些初学者的错误。我想我可能是。
这是我的模型:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 98, 98, 32) 896
_________________________________________________________________
elu_1 (ELU) (None, 98, 98, 32) 0
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 49, 49, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 47, 47, 64) 18496
_________________________________________________________________
elu_2 (ELU) (None, 47, 47, 64) 0
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 23, 23, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 21, 21, 128) 73856
_________________________________________________________________
elu_3 (ELU) (None, 21, 21, 128) 0
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 10, 10, 128) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 8, 8, 128) 147584
_________________________________________________________________
elu_4 (ELU) (None, 8, 8, 128) 0
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 4, 4, 128) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 2048) 0
_________________________________________________________________
dense_1 (Dense) (None, 100) 204900
_________________________________________________________________
elu_5 (ELU) (None, 100) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 100) 0
_________________________________________________________________
dense_2 (Dense) (None, 50) 5050
_________________________________________________________________
elu_6 (ELU) (None, 50) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 50) 0
_________________________________________________________________
dense_3 (Dense) (None, 2) 102
_________________________________________________________________
activation_1 (Activation) (None, 2) 0
=================================================================
Total params: 450,884
Trainable params: 450,884
Non-trainable params: 0
_________________________________________________________________