为了计算矩阵在经过上述卷积层和池化层后的维度变化,我们需要逐层解析每一个操作。我们首先设置初始参数:矩阵的初始维度是 (1, 64, 128, 128),其中:
- 批次大小 (batch size) = 1
- 输入通道数 (input channels) = 64
- 高度 (height) = 128
- 宽度 (width) = 128
给定:
nf (初试通道数) = 1
output_nc (最大通道数) = 512
假设 use_bias=True。
逐步解析如下操作:
n_downsampling = 5
for i in range(n_downsampling): # add downsampling layers
mult = 2**i
inc = min(nf * mult, output_nc)
ouc = min(nf * mult * 2, output_nc)
model += [
nn.Conv2d(inc, ouc, kernel_size=3, stride=2, padding=1, bias=use_bias),
norm_layer(nf * mult * 2),
nn.ReLU(True),
]
让我们逐层解析:
第一层
-
( i = 0 ), ( mult = 2^0 = 1 )
-
( inc = \min(nf \times mult, output_nc) = \min(1 \times 1, 512) = 1 )
-
( ouc = \min(nf \times mult \times 2, output_nc) = \min(1 \times 2, 512) = 2 )
经过 ( \text{Conv2D}(1 \rightarrow 2, \text{kernel size}=3, \text{stride}=2, \text{padding}=1) ):
-
输出通道数:2
-
高度/宽度的变化:如果输入尺寸是 ( \text{H} = 128 ), ( \text{W} = 128 ), 经过 Conv2D 层后有:
[
\text{Output Height (O_H)} = \text{Output Width (O_W)} = \left\lfloor \frac{{\text{input size} + 2 \times \text{padding} - \text{kernel size}}}{\text{stride}} + 1 \right\rfloor
]
代入数值:
[
O_H = O_W = \left\lfloor \frac{{128 + 2 \times 1 - 3}}{2} + 1 \right\rfloor = \left\lfloor \frac{128}{2} \right\rfloor = 64
]
因此输出维度是 ( (1, 2, 64, 64) )。
第二层
-
( i = 1 ), ( mult = 2^1 = 2 )
-
( inc = \min(nf \times mult, output_nc) = \min(1 \times 2, 512) = 2 )
-
( ouc = \min(nf \times mult \times 2, output_nc) = \min(1 \times 4, 512) = 4 )
经过 ( \text{Conv2D}(2 \rightarrow 4, \text{kernel size}=3, \text{stride}=2, \text{padding}=1) ):
类似的计算方式:
[
O_H = O_W = \left\lfloor \frac{64 + 2 - 3}{2} + 1 \right\rfloor = \left\lfloor \frac{63}{2} + 1 \right\rfloor = 32
]
因此输出维度是 ( (1, 4, 32, 32) )。
第三层
-
( i = 2 ), ( mult = 2^2 = 4 )
-
( inc = \min(nf \times mult, output_nc) = \min(1 \times 4, 512) = 4 )
-
( ouc = \min(nf \times mult \times 2, output_nc) = \min(1 \times 8, 512) = 8 )
经过 ( \text{Conv2D}(4 \rightarrow 8, \text{kernel size}=3, \text{stride}=2, \text{padding}=1) ):
类似的计算方式:
[
O_H = O_W = \left\lfloor \frac{32 + 2 - 3}{2} + 1 \right\rfloor = \left\lfloor \frac{31}{2} + 1\right\rfloor = 16
]
因此输出维度是 ( (1, 8, 16, 16) )。
第四层
-
( i = 3 ), ( mult = 2^3 = 8 )
-
( inc = \min(nf \times mult, output_nc) = \min(1 \times 8, 512) = 8 )
-
( ouc = \min(nf \times mult \times 2, output_nc) = \min(1 \times 16, 512) = 16 )
经过 ( \text{Conv2D}(8 \rightarrow 16, \text{kernel size}=3, \text{stride}=2, \text{padding}=1) ):
类似的计算方式:
[
O_H = O_W = \left\lfloor \frac{16 + 2 - 3}{2} + 1 \right\rfloor = \left\lfloor \frac{15}{2} + 1 \right\rfloor = 8
]
因此输出维度是 ( (1, 16, 8, 8) )。
第五层
-
( i = 4 ), ( mult = 2^4 = 16 )
-
( inc = \min(nf \times mult, output_nc) = \min(1 \times 16, 512) = 16 )
-
( ouc = \min(nf \times mult \times 2, output_nc) = \min(1 \times 32, 512) = 32 )
经过 ( \text{Conv2D}(16 \rightarrow 32, \text{kernel size}=3, \text{stride}=2, \text{padding}=1) ):
类似的计算方式:
[
O_H = O_W = \left\lfloor \frac{8 + 2 - 3}{2} + 1 \right\rfloor = \left\lfloor \frac{7}{2} + 1 \right\rfloor = 4
]
因此输出维度变成 ( (1, 32, 4, 4) )。
所以,经过上述操作后,最终输出的维度是 ( (1, 32, 4, 4) )。