プリンキピア

第14章コンピュータビジョン

14.10.1. Basic Operation14.10.1 基本の操作

Ignoring channels for now, let’s begin with the basic transposed convolution operation with stride of 1 and no padding. Suppose that we are given a nh×nw input tensor and a kh×kw kernel. Sliding the kernel window with stride of 1 for nw times in each row and nh times in each column yields a total of nhnw intermediate results. Each intermediate result is a (nh+kh−1)×(nw+kw−1) tensor that are initialized as zeros. To compute each intermediate tensor, each element in the input tensor is multiplied by the kernel so that the resulting kh×kw tensor replaces a portion in each intermediate tensor. Note that the position of the replaced portion in each intermediate tensor corresponds to the position of the element in the input tensor used for the computation. In the end, all the intermediate results are summed over to produce the output.
英語のヒント
いまはチャネルを無視して、ストライド1でパディングのない基本の転置畳み込みの操作から始めよう。nh×nwの入力テンソルとkh×kwのカーネルが与えられたとする。カーネルの窓を、ストライド1で各行にnw回、各列にnh回スライドさせると、合計nhnw個の途中結果が得られる。各途中結果は、ゼロで初期化された(nh+kh−1)×(nw+kw−1)のテンソルである。各途中のテンソルを計算するには、入力テンソルの各要素にカーネルを掛け、その結果のkh×kwのテンソルで各途中のテンソルの一部を置き換える。各途中のテンソルで置き換えられる部分の位置は、計算に使う入力テンソルの要素の位置に対応することに注意してほしい。最後に、すべての途中結果を足し合わせて出力を生成する。
As an example, Fig. 14.10.1 illustrates how transposed convolution with a 2×2 kernel is computed for a 2×2 input tensor.
英語のヒント
例として、図14.10.1は、2×2の入力テンソルについて、2×2のカーネルによる転置畳み込みがどう計算されるかを示している。
Fig. 14.10.1図14.10.1
Transposed convolution with a 2×2 kernel. The shaded portions are a portion of an intermediate tensor as well as the input and kernel tensor elements used for the computation.
英語のヒント
2×2のカーネルによる転置畳み込み。網かけの部分は、途中のテンソルの一部と、計算に使う入力とカーネルのテンソルの要素である。
We can implement this basic transposed convolution operation trans_conv for a input matrix X and a kernel matrix K.
英語のヒント
入力行列Xとカーネル行列Kについて、この基本の転置畳み込みの操作trans_convを実装できる。
def trans_conv(X, K):
    h, w = K.shape
    Y = torch.zeros((X.shape[0] + h - 1, X.shape[1] + w - 1))
    for i in range(X.shape[0]):
        for j in range(X.shape[1]):
            Y[i: i + h, j: j + w] += X[i, j] * K
    return Y
In contrast to the regular convolution (in Section 7.2) that reduces input elements via the kernel, the transposed convolution broadcasts input elements via the kernel, thereby producing an output that is larger than the input. We can construct the input tensor X and the kernel tensor K from Fig. 14.10.1 to validate the output of the above implementation of the basic two-dimensional transposed convolution operation.
英語のヒント
カーネルを通じて入力の要素を減らす通常の畳み込み(7.2節)とは対照的に、転置畳み込みはカーネルを通じて入力の要素をブロードキャストし、それによって入力より大きい出力を生み出す。図14.10.1の入力テンソルXとカーネルテンソルKを作って、上の基本の2次元転置畳み込みの操作の実装の出力を確かめられる。
X = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
K = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
trans_conv(X, K)
tensor([[ 0.,  0.,  1.],
        [ 0.,  4.,  6.],
        [ 4., 12.,  9.]])
Alternatively, when the input X and kernel K are both four-dimensional tensors, we can use high-level APIs to obtain the same results.
英語のヒント
別のやり方として、入力XとカーネルKがどちらも4次元のテンソルのときは、高水準のAPIを使って同じ結果を得られる。
X, K = X.reshape(1, 1, 2, 2), K.reshape(1, 1, 2, 2)
tconv = nn.ConvTranspose2d(1, 1, kernel_size=2, bias=False)
tconv.weight.data = K
tconv(X)
tensor([[[[ 0.,  0.,  1.],
          [ 0.,  4.,  6.],
          [ 4., 12.,  9.]]]], grad_fn=<ConvolutionBackward0>)