0

我正在训练一个图像分类器来从 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
_________________________________________________________________
4

1 回答 1

1

由于大量的训练文件,没有测试集。

虽然这不是你的问题,但我觉得应该解决这个问题:我知道有些团体采用了非常不同的方法并且没有保留有效/测试集,但这对我来说听起来非常不正统和不确定。就个人而言,我很少看到没有测试测试或某种训练后验证的文章。

测试集很有用,因为它们是您的模型适用于看不见的样本的经验证据。使用它们+统计显着性/置信度测试,您可以根据统计断言您的模型不仅是一个非常复杂的 if-else 机器。

底线是,如果您是该地区的新手,则不应遵守此类冒险策略。

关于你的问题:什么y_train.shape?我相信您使用的是稀疏标签 ( [1., 0., 1., 2., 3., ..., 4.]) 而不是一次性编码的标签:

[[0. 1.]
 [1. 0.]
 [0. 1.]
 ...
 [1. 0.]]

哪里argmax(x, axis=1)给你真正的班级索引。

如何解决:

替换loss="categorical_crossentropy"sparse_categorical_crossentropy。这是高效的内存,但要求每个样本与单个标签相关联。

或者

y_train在将其传递给之前进行一次热编码ImageDataGenerator#flow

from keras.utils import to_categorical

y_train = to_categorical(y_train)
train_datagen = ImageDataGenerator(...)
train_generator = train_datagen.flow(x_train, y_train, ...)
...

这更消耗内存,但允许单个样本与多个标签相关联:

[[1. 1. 0.]
 [0. 0. 1.]
 ...
 [1. 0. 1.]]
于 2018-05-01T00:57:52.703 回答