第7章畳み込みニューラルネットワーク
7.3.1. Padding7.3.1 パディング
As described above, one tricky issue when applying convolutional layers is that we tend to lose pixels on the perimeter of our image. Consider Fig. 7.3.1 that depicts the pixel utilization as a function of the convolution kernel size and the position within the image. The pixels in the corners are hardly used at all.
英語のヒント
上で述べたように、畳み込み層を当てはめるときの厄介な問題の一つは、画像の周辺の画素を失いがちなことである。畳み込みカーネルの大きさと画像の中の位置の関数として、画素の利用度を描いた図7.3.1を考えよう。隅の画素はほとんどまったく使われない。
Fig. 7.3.1図7.3.1
Pixel utilization for convolutions of size , , and respectively.
英語のヒント
大きさがそれぞれ、、の畳み込みでの画素の利用度。
Since we typically use small kernels, for any given convolution we might only lose a few pixels but this can add up as we apply many successive convolutional layers. One straightforward solution to this problem is to add extra pixels of filler around the boundary of our input image, thus increasing the effective size of the image. Typically, we set the values of the extra pixels to zero. In Fig. 7.3.2, we pad a input, increasing its size to . The corresponding output then increases to a matrix. The shaded portions are the first output element as well as the input and kernel tensor elements used for the output computation: .
英語のヒント
ふつうは小さなカーネルを使うので、一つの畳み込みでは数画素を失うだけかもしれないが、畳み込み層を何層も続けて当てはめると、それが積み重なりうる。この問題への素直な解決策の一つは、入力画像の境界の周りに詰め物の画素を余分に加え、画像の実質的な大きさを増やすことである。ふつう、余分な画素の値は0にする。図7.3.2では、の入力を埋めて、その大きさをに増やしている。すると、対応する出力はの行列に増える。網かけの部分は、最初の出力要素と、その出力の計算に使われる入力とカーネルのテンソルの要素である。。
Fig. 7.3.2図7.3.2
Two-dimensional cross-correlation with padding.
英語のヒント
パディングを伴う2次元の相互相関。
In general, if we add a total of rows of padding (roughly half on top and half on bottom) and a total of columns of padding (roughly half on the left and half on the right), the output shape will be
英語のヒント
一般に、合計行のパディング(おおよそ半分を上に、半分を下に)と、合計列のパディング(おおよそ半分を左に、半分を右に)を加えると、出力の形状は次のようになる。
(7.3.1)
This means that the height and width of the output will increase by and , respectively.
英語のヒント
つまり、出力の高さと幅は、それぞれとだけ増える。
In many cases, we will want to set and to give the input and output the same height and width. This will make it easier to predict the output shape of each layer when constructing the network. Assuming that is odd here, we will pad rows on both sides of the height. If is even, one possibility is to pad rows on the top of the input and rows on the bottom. We will pad both sides of the width in the same way.
英語のヒント
多くの場合、入力と出力の高さと幅を同じにするため、とに設定したくなる。こうすると、ネットワークを組み立てるときに、各層の出力の形状を予測しやすくなる。ここでが奇数だとすると、高さの両側に行ずつパディングする。が偶数なら、一つのやり方は、入力の上に行、下に行パディングすることである。幅の両側も同じようにパディングする。
CNNs commonly use convolution kernels with odd height and width values, such as 1, 3, 5, or 7. Choosing odd kernel sizes has the benefit that we can preserve the dimensionality while padding with the same number of rows on top and bottom, and the same number of columns on left and right.
英語のヒント
CNNでは、1、3、5、7のように、高さと幅が奇数の畳み込みカーネルがよく使われる。カーネルの大きさを奇数に選ぶと、上下に同じ行数、左右に同じ列数をパディングしながら、次元を保てるという利点がある。
Moreover, this practice of using odd kernels and padding to precisely preserve dimensionality offers a clerical benefit. For any two-dimensional tensor
X, when the kernel’s size is odd and the number of padding rows and columns on all sides are the same, thereby producing an output with the same height and width as the input, we know that the output Y[i, j] is calculated by cross-correlation of the input and convolution kernel with the window centered on X[i, j].英語のヒント
さらに、奇数のカーネルとパディングを使って次元を正確に保つというこのやり方には、事務的な利点もある。任意の2次元テンソル
Xについて、カーネルの大きさが奇数で、すべての側のパディングの行数と列数が同じであり、その結果、入力と同じ高さと幅の出力が得られるとき、出力Y[i, j]は、X[i, j]を中心とする窓での入力と畳み込みカーネルの相互相関によって計算されることがわかる。In the following example, we create a two-dimensional convolutional layer with a height and width of 3 and apply 1 pixel of padding on all sides. Given an input with a height and width of 8, we find that the height and width of the output is also 8.
英語のヒント
次の例では、高さも幅も3の2次元の畳み込み層を作り、すべての側に1画素のパディングを当てはめる。高さも幅も8の入力が与えられたとき、出力の高さと幅も8になることがわかる。
# We define a helper function to calculate convolutions. It initializes the
# convolutional layer weights and performs corresponding dimensionality
# elevations and reductions on the input and output
def comp_conv2d(conv2d, X):
# (1, 1) indicates that batch size and the number of channels are both 1
X = X.reshape((1, 1) + X.shape)
Y = conv2d(X)
# Strip the first two dimensions: examples and channels
return Y.reshape(Y.shape[2:])
# 1 row and column is padded on either side, so a total of 2 rows or columns
# are added
conv2d = nn.LazyConv2d(1, kernel_size=3, padding=1)
X = torch.rand(size=(8, 8))
comp_conv2d(conv2d, X).shapetorch.Size([8, 8])When the height and width of the convolution kernel are different, we can make the output and input have the same height and width by setting different padding numbers for height and width.
英語のヒント
畳み込みカーネルの高さと幅が異なるときは、高さと幅に異なるパディングの数を設定することで、出力と入力の高さと幅を同じにできる。
# We use a convolution kernel with height 5 and width 3. The padding on either
# side of the height and width are 2 and 1, respectively
conv2d = nn.LazyConv2d(1, kernel_size=(5, 3), padding=(2, 1))
comp_conv2d(conv2d, X).shapetorch.Size([8, 8])