我正在尝试使用我采集的图像数据集为Keras中的二进制分类任务训练一个简单的CNN模型。问题是,经过几个时期之后,我的精度,val_accuracy和损失不断增加。我是否以错误的方式处理数据?还是模型设置中有问题?
开始时,我使用softmax作为最终的激活函数和绝对熵,还使用了标签上的to_categorical函数。
在了解了通常导致这种情况发生的原因之后,我决定改用sigmoid和binary_crossentropy,而不要使用to_categorical。问题仍然存在,我开始怀疑是问题所在(这两个类太相似了)还是馈送图像阵列的方式。
conkeras1 = []
pics = os.listdir("/Matrices/")
# I do this for the images of both classes, just keeping it short.
for x in range(len(pics)):
img = image.load_img("Matrices/"+pics[x])
conkeras1.append(img)
conkeras = conkeras1+conkeras2
conkeras = np.array([image.img_to_array(x) for x in conkeras]).astype("float32")
conkeras = conkeras / 255 # I also tried normalizing with a z-score with no success
yecs1 = [1]*len(conkeras1)
yecs2 = [0]*len(conkeras2)
y_train = yecs1+yecs2
y_train = np.array(y_train).astype("float32")
model = Sequential([
Conv2D(64, (3, 3), input_shape=conkeras.shape[1:], padding="same", activation="relu"),
Conv2D(32, (3, 3), activation="relu", padding="same"),
Flatten(),
Dense(500, activation="relu"),
#Dense(4096, activation="relu"),
Dense(1, activation="sigmoid")
])
model.compile(loss=keras.losses.binary_crossentropy,
optimizer=keras.optimizers.Adam(lr=0.001),
metrics=['accuracy'])
history = model.fit(conkeras, y_train,
batch_size=32,
epochs=32, shuffle=True,
verbose=1,
callbacks=[tensorboard])
我得到的输出是这样:
975/975 [==============================] - 107s 110ms/step - loss: 8.0022 - acc: 0.4800
Epoch 2/32
975/975 [==============================] - 99s 101ms/step - loss: 8.1756 - acc: 0.4872
Epoch 3/32
975/975 [==============================] - 97s 100ms/step - loss: 8.1756 - acc: 0.4872
Epoch 4/32
975/975 [==============================] - 97s 99ms/step - loss: 8.1756 - acc: 0.4872
这些是过渡集和标签的形状
>>> conkeras.shape
(975, 100, 100, 3)
>>> y_train.shape
(975,)