【问题标题】:Validation of Keras Conv2D convolution using NumPy return different layer output使用 NumPy 验证 Keras Conv2D 卷积返回不同层输出
【发布时间】:2020-07-19 21:19:59
【问题描述】:

我正在尝试使用标准 Keras 验证我的网络构建的第一层的输出。第一层的名字是conv2d。

我构建了一个新模型只是为了得到第一层的输出,使用以下代码:

inter_layer = None
weights = None
biases = None

for layer in qmodel.layers:
    if layer.name == "conv2d":
        print("Found layer: " + layer.name)
        inter_layer = layer
        weights = layer.get_weights()[0]
        biases = layer.get_weights()[1]

inter_model = Model(qmodel.input,inter_layer.output)

inter_model.compile()

然后,我做了以下事情(img_test 是 cifar10 图像之一):

first_layer_output = inter_model.predict(img_test)

# Get the 3x3 pixel upper left patch of the 3 channels of the input image
img_test_slice = img_test[0,:3,:3,:]
# Get only the first filter of the layer
weigths_slice = weights[:,:,:,0]
# Get the bias of the first filter of the layer
bias_slice = biases[0]
# Get the 3x3 pixel upper left patch of the first channel of the output of the layer
output_slice = first_layer_output[0,:3,:3,0]

我打印了每个切片的形状,得到了正确的形状:

  • img_test_slice: (3,3,3)
  • weigths_slice: (3,3,3)
  • 输出切片:(3,3)

据我所知,如果我这样做:

partial_sum = np.multiply(img_test_slice,weigths_slice)
output_pixel = partial_sum.sum() + bias_slice

output_pixel 应该是 output_slice 的值之一(实际上是索引 [1,1] 中的值,因为该层具有 padding = 'SAME')。

但是....不是。

也许我遗漏了一些关于卷积计算如何工作的非常简单的东西,但据我所知,进行元素乘法然后对所有值求和加上偏差应该是输出像素之一层。

也许层的输出数据的排列方式与层的输入不同?

【问题讨论】:

    标签: python numpy tensorflow keras convolution


    【解决方案1】:

    问题在于 get_weights 方法的使用。

    我的模型使用的是 QKeras 层,当你使用这些层时,你不应该使用 get_weights 来获取层权重,而是执行如下操作:

    for quantizer, weight in zip(layer.get_quantizers(), layer.get_weights()):
        if quantizer:
            weight = tf.constant(weight)
            weight = tf.keras.backend.eval(quantizer(weight))
    

    如果你使用这个 for 循环提取权重,你会得到真正的量化权重,所以现在计算是正确的。

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-24
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 2019-09-28
      相关资源
      最近更新 更多