首页 > 解决方案 > 关于连体CNN的准确性

问题描述

# We have 2 inputs, 1 for each picture
left_input = Input(img_size)
right_input = Input(img_size)

# We will use 2 instances of 1 network for this task
convnet = MobileNetV2(weights='imagenet', include_top=False, input_shape=img_size,input_tensor=None)
convnet.trainable=True
x=convnet.output
x=tf.keras.layers.GlobalAveragePooling2D()(x)
x=Dense(320,activation='relu')(x)
x=Dropout(0.2)(x)
preds = Dense(101, activation='sigmoid')(x) # Apply sigmoid
convnet = Model(inputs=convnet.input, outputs=preds)

# Connect each 'leg' of the network to each input
# Remember, they have the same weights
encoded_l = convnet(left_input)
encoded_r = convnet(right_input)


# Getting the L1 Distance between the 2 encodings
L1_layer = Lambda(lambda tensor:K.abs(tensor[0] - tensor[1]))

# Add the distance function to the network
L1_distance = L1_layer([encoded_l, encoded_r])

prediction = Dense(1,activation='sigmoid')(L1_distance)
siamese_net = Model(inputs=[left_input,right_input],outputs=prediction)

optimizer = Adam(lr, decay=2.5e-4)
#//TODO: get layerwise learning rates and momentum annealing scheme described in paperworking
siamese_net.compile(loss=keras.losses.binary_crossentropy,optimizer=optimizer,metrics=['accuracy'])

siamese_net.summary()

训练结果如下

纪元 1/10 126/126 [===============================] - 169s 1s/步 - 损失:0.5683 - 准确度: 0.6840 - val_loss: 0.4644 - val_accuracy: 0.8044 Epoch 2/10 126/126 [=============================] - 163s 1s/step - loss: 0.2032 - accuracy: 0.9795 - val_loss: 0.2117 - val_accuracy: 0.9681 Epoch 3/10 126/126 [====================== ========] - 163s 1s/步 - 损失:0.1110 - 准确度:0.9925 - val_loss:0.1448 - val_accuracy:0.9840 Epoch 4/10 126/126 [============ ==================] - 164s 1s/步 - 损失:0.0844 - 准确度:0.9950 - val_loss:0.1384 - val_accuracy:0.9820 Epoch 5/10 126/126 [== ============================] - 163s 1s/step - loss: 0.0634 - accuracy: 0.9990 - val_loss: 0.0829 - val_accuracy: 1.0000时代 6/10 126/126 [===============================] - 165s 1s/步 - 损失:0.0526 - 准确度:0.9995 - val_loss:0.0729 - val_accuracy:1。0000 纪元 7/10 126/126 [===============================] - 164s 1s/步 - 损失:0.0465 -准确度:0.9995 - val_loss:0.0641 - val_accuracy:1.0000 Epoch 8/10 126/126 [=============================] - 163s 1s/步 - 损失:0.0463 - 准确度:0.9985 - val_loss:0.0595 - val_accuracy:1.0000

当我比较两个不同的图像时,该模型的预测精度很高。此外,它对同一类图像的预测非常好。但是当我将 Image1 与 image1 本身进行比较时,它预测它们仅以 0.5 的概率相似。在其他情况下,如果我将 image1 与 image2 进行比较,那么它以 0.8 的概率正确预测。(这里 image1 和 image2 属于同一类)

当我比较单个图像时,它预测正确,我尝试了不同的替代方案,但没有锻炼。我可以知道可能是什么错误吗?

标签: pythontensorflowkeras

解决方案


The L1 distance between two equal vectors is always zero.

When you pass the same image, the encodings generated are equal (encoded_l is equal to encoded_r). Hence, the input to your final sigmoid layer is a zero vector.

And, sigmoid(0) = 0.5.

enter image description here

This is the reason providing identical inputs to your model gives 0.5 as the output.


推荐阅读