【问题标题】:Validation accuracy stuck at 0.50 when training CNN siamese network based on Keras MNIST example基于 Keras MNIST 示例训练 CNN 连体网络时,验证准确率停留在 0.50
【发布时间】:2019-07-14 07:42:24
【问题描述】:

在基于Keras MNIST example 训练 Siamese 卷积神经网络时,它会卡在 0.50 的验证准确度上。此外,训练和验证损失减少,而训练准确度也徘徊在 0.50 左右。

创建连体网络的代码类似这样:

input_shape = x_train.shape[2:]
print('input_shape:',input_shape)
# network definition
base_network = create_base_network(input_shape)

input_a = Input(shape=input_shape)
input_b = Input(shape=input_shape)

# because we re-use the same instance `base_network`,
# the weights of the network
# will be shared across the two branches
processed_a = base_network(input_a)
processed_b = base_network(input_b)

distance = Lambda(euclidean_distance,
                  output_shape=eucl_dist_output_shape)([processed_a, processed_b])

model = Model([input_a, input_b], distance)

create_base_network 函数创建 ResNet50 和另外两个前馈层 512 和 1024,所有层都被冻结,除了最后几个未冻结的层。

此问题已在多个平台(Stackoverflow、Github 甚至 Reddit)上多次讨论,但尚未提供明确的答案/解决方案。

【问题讨论】:

    标签: python keras conv-neural-network


    【解决方案1】:

    我已经解决这个问题几个小时了。虽然这不会影响很多人,因为这个用例非常具体,但受影响的人可以尝试关注。它对我有用。

    问题似乎与 Python 3.5 的一些低级行为有关,超出了我的理解范围。切换到 Python 2.7 成功了,网络从一开始就能够达到 70% 以上的准确率。在此之前,即使经过 10 个 epoch,准确率也停留在 50%。

    Train on 36000 samples, validate on 4000 samples
    Epoch 1/10
    36000/36000 [==============================] - 866s 24ms/step - loss: 0.2026 - accuracy: 0.7133 - val_loss: 0.2054 - val_accuracy: 0.7173
    
    Epoch 00001: val_loss improved from inf to 0.20544, saving model to model_checkpoint/siamese_40k_headache_20thFeb-1.hdf5
    Epoch 2/10
    36000/36000 [==============================] - 770s 21ms/step - loss: 0.1949 - accuracy: 0.7271 - val_loss: 0.2020 - val_accuracy: 0.7235
    
    Epoch 00002: val_loss improved from 0.20544 to 0.20204, saving model to model_checkpoint/siamese_40k_headache_20thFeb-1.hdf5
    Epoch 3/10
     7040/36000 [====>.........................] - ETA: 9:12 - loss: 0.1879 - accuracy: 0.7320
    

    此外,使用迁移学习训练 Siamese 网络时,验证准确率停留在 0.50 也可能与 Keras 的批量标准化层的实现有关。更多信息可以阅读here。手动安装这个版本的 Keras 可以修复它:

    !pip install -U --force-reinstall --no-dependencies git+https://github.com/datumbox/keras@fork/keras2.2.4
    

    这绝不是问题的直接解决方案,而是可行的解决方法。

    【讨论】:

    • 我在 google colab 中运行了相同的示例。训练集上的准确率 99.58%。我使用 python3 作为运行时。
    • @TanmayPatil 这听起来很有趣。可以分享一下 colab 笔记本吗?
    猜你喜欢
    • 2019-06-09
    • 2018-09-18
    • 2023-03-17
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2020-04-07
    • 1970-01-01
    相关资源
    最近更新 更多