3

我正在尝试在 python 中实现二维卷积。我有一组尺寸的输入图像(m、64、64、3),其中 m 是图像的数量。我想对高度和宽度使用过滤器大小 f=8 和 stride=8,并使用相同的填充,以便保留输入宽度和高度 (64, 64)。

使用公式[n' = floor((n-f+2*pad)/stride + 1)]并输入 n'=64, n=64, stride=8, f=8,我得到pad=224,其中大得不合理。

比如我取了m,图片数量为1080,估计是内存出错,系统崩溃了。

但是当我使用 Keras 库和以下代码时,它运行良好。

X = keras.layers.Conv2D(filters=32, kernel_size=(8, 8), strides=(8, 8), padding='same')(X)

这是我在 python 中的 Conv2D 实现:

import numpy as np

# A.shape = (1080, 64, 64, 3)
# W.shape = (8, 8, 3, 32)
# b.shape = (32,)

def conv_fwd(A, W, b, pad=0, stride=1):
    pad_A = np.pad(A, ((0, 0), (pad, pad), (pad, pad), (0, 0)), mode='constant')
    (m, w, h, nc) = A.shape
    (fw, fh, ncc, ncn) = W.shape

    if nc != ncc:
        raise Exception('Number of channels in kernel and input do not match')

    wn = int((w-fw+2*pad)/stride + 1)
    hn = int((h-fh+2*pad)/stride + 1)
    A_n = np.zeros((m, wn, hn, ncn))
    W = W.reshape(fw*fh*ncc, ncn)

    for i in range(wn):
        for j in range(hn):
            A_n[:, i, j] = pad_A[:, i*stride:i*stride+fw, j*stride:j*stride+fh].reshape(m, fw*fh*nc).dot(W) + b
    return A_n

所以我假设在 keras 中计算填充有不同的过程。我试图寻找源代码,但找不到它。它是如何工作的?

4

1 回答 1

1

In the formula, n' = floor((n-f+2*pad)/stride + 1 you have taken n' == n == 64.

That is not correct. n' is equal to n only when value of Stride is equal to 1 but here, Stride is greater than 1 (8).

That's the reason you are getting very high value for Padding.

Now, as your goal is to find the value of Padding, I have a solution/workaround (which might not be very optimized).

Initially, build the Model with Padding = Same, as shown below:

import tensorflow as tf
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(filters = 64, strides = (2,2), kernel_size = (3,3), 
input_shape = (64,64,3), padding = 'same'))
print(model.summary())

Summary of the Model with Padding = Same is shown below:

Model: "sequential_12"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_25 (Conv2D)           (None, 32, 32, 64)        1792      
=================================================================
Total params: 1,792
Trainable params: 1,792
Non-trainable params: 0

If we observe the Shape of the Image, it is reduced from (64,64) to (32,32) even though Padding == Same.

Now, build the Model with Padding = Valid, as shown below:

import tensorflow as tf
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(filters = 64, strides = (2,2), kernel_size = (3,3), 
input_shape = (64,64,3), padding = 'valid'))
print(model.summary())

Summary for the above Model is shown below:

Model: "sequential_11"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_24 (Conv2D)           (None, 31, 31, 64)        1792      
=================================================================
Total params: 1,792
Trainable params: 1,792
Non-trainable params: 0

If we observe, the Shape of the Convolutional Layer is (None,31,31,64).

Now, Padding can be obtained by the formula,

Height with SAME Padding - Height with VALID Padding

or

Width with SAME Padding - Width with VALID Padding

i.e., 32 - 31 = 1.

Padding in your case, with Input Shape = (64, 64,3), Filter Size = 8, Strides = 8 is 1 i.e.,

Input is Padded with 1 Row and 1 Column of Zeros.

于 2020-07-15T12:27:07.827 回答