【问题标题】:Why does accruacy of the CNN drop when I remove a filter and its associated weights?为什么当我删除过滤器及其相关权重时 CNN 的准确性会下降?
【发布时间】:2020-05-14 07:30:03
【问题描述】:

model 架构是 Conv2D with 32 filters -> Flatten -> Dense -> Compile -> Fit

我使用删除了第一层的最后一个过滤器和该模型中相应的全连接层

w,b = model.layers[0].get_weights()
w = np.delete(w, [32], -1)
b = np.delete(b, [32], 0)

w_2,b_2 = model.layers[2].get_weights()
w_2 = w_2[:20956,:]

我使用 20956 是因为第一层的输出是 26 x 26 x 31,这是 2D 中的图像尺寸乘以通道数。

我使用以下方法创建了一个名为 model_1 的新模型:

# Input stays the same
model_1 = Sequential()

# New modified conv layer
model_1.add(Conv2D(31, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape,
                 kernel_initializer='he_normal'))

model_1.add(Flatten())
model_1.add(Dense(10, activation='softmax'))


model_1.layers[0].set_weights([w,b])
model_1.layers[2].set_weights([w_2,b_2])

model_1.compile(loss="categorical_crossentropy",
              optimizer="Adam",
              metrics=['accuracy'])

我可以通过执行 model_1.layers[0].get_weights()[0] == model.layers[0].get_weights()[0][:,:,:,:31]model_1.layers[2].get_weights()[0] == model.layers[2].get_weights()[0][:20956,:] 来确认权重相同,这将返回 True。

当我这样做时

score = model_1.evaluate(x_test_reshape, y_test)

print('Test loss:', score[0])
print('Test accuracy:', score[1])

score = model.evaluate(x_test_reshape, y_test)

print('Test loss:', score[0])
print('Test accuracy:', score[1])

准确率从 98% 下降到 10%,有什么想法吗?

【问题讨论】:

    标签: keras deep-learning conv-neural-network tensor


    【解决方案1】:

    您实际上所做的是从最后一个卷积层中删除一个通道。直觉上听起来这没什么大不了的,剩下的 31 个通道仍然会让网络表现良好。实际上,所有卷积通道在随后的密集层中都相互交互,但由于这种交互缺少一个信息通道,因此对其进行了优化,其准确性将会下降。

    另一种思考方式是将您的网络视为连续步骤的函数,将图像作为输入并以 98% 的准确率作为输出标签。删除此函数中的一小部分 (1/32) 计算将改变结果,并且可能会产生更差的结果,因为在这些计算仍然存在的情况下对函数进行了优化。您正在删除对达到高精度显然至关重要的部分功能。

    您可以通过在短时间内使用 31 个通道训练您的新模型来测试这一点。由于新模型只需要重新学习删除通道的功能,它应该很快再次达到高性能。

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 2020-11-19
      • 2021-02-06
      • 1970-01-01
      • 2018-12-24
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      相关资源
      最近更新 更多