损失没有变化,准确性保持不变。
left_input = Input((3,224,224))
right_input = Input((3,224,224))
encoded_l = encoder2(left_input)
encoded_r = encoder2(right_input)
# Getting the L1 Distance between the 2 encodings
L1_layer = L.Lambda(lambda tensor:K.abs(tensor[0] - tensor[1]))
L1_distance = L1_layer([encoded_l, encoded_r])
prediction = L.Dense(1,activation='sigmoid')(L1_distance)
siamese_net = Model(inputs=[left_input,right_input],outputs=prediction)
optimizer = Adam(0.0005, decay=2.5e-4)
siamese_net.compile(loss="binary_crossentropy",optimizer=optimizer,metrics=['accuracy'])
siamese_net.fit([Xl_train.reshape((-1,3,224,224)),Xr_train.reshape((-1,3,224,224))], to_categorical(y_train),
batch_size=32,
epochs=50,
verbose=1,
validation_data=([Xl_test.reshape((-1,3,224,224)),Xr_test.reshape((-1,3,224,224))],to_categorical(y_test)),
shuffle=True)
我只设置了正对,因此我必须通过在对中的一组成员上使用 np.roll() 来设置负对,以产生新的组合。因此,由于不平衡问题,它不能预测 1(因为正样本和负样本具有相同的对数)。我尝试过改组数据集,在最后一层使用带有 2 个节点的 softmax。我在带有 rbf 内核的 SVM 上进行了尝试。虽然准确度很差,但它仍然至少预测为 0。这是编码器的代码-
def encoder():
model=Sequential()
model.add(Convolution2D(128, (7, 7), activation='relu'))
model.add(Dropout(0.2))
model.add(Convolution2D(64, (5, 5), activation='relu'))
model.add(Dropout(0.2))
model.add(Convolution2D(64, (5, 5), activation='relu'))
model.add(Dropout(0.2))
model.add(Convolution2D(16, (5, 5), activation='relu'))
model.add(Dropout(0.2))
model.add(Convolution2D(1, (5, 5), activation='relu'))
model.add(Flatten())
model.build((None,3, 224, 224))
#print(model.summary())
return model
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 128, 218, 218) 18944
_________________________________________________________________
dropout_1 (Dropout) (None, 128, 218, 218) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 64, 214, 214) 204864
_________________________________________________________________
dropout_2 (Dropout) (None, 64, 214, 214) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 64, 210, 210) 102464
_________________________________________________________________
dropout_3 (Dropout) (None, 64, 210, 210) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 16, 206, 206) 25616
_________________________________________________________________
dropout_4 (Dropout) (None, 16, 206, 206) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 1, 202, 202) 401
_________________________________________________________________
flatten_1 (Flatten) (None, 40804) 0
=================================================================
Total params: 352,289
Trainable params: 352,289
Non-trainable params: 0
编辑 1- 我添加了- kernel_initializer='random_uniform',bias_initializer='zeros'
到所有层,以便它停止将所有内容预测为 0。训练损失现在正在减少,但验证损失和准确性没有改变。
编辑2-解决了。这是我的数据集的问题。