【发布时间】:2021-02-16 01:05:58
【问题描述】:
我正在 pytorch 中从头开始实施 googlenet(较小版本)。架构如下:
对于下采样模块,我有以下代码:
class DownSampleModule(nn.Module):
def __init__(self, in_channel, ch3, w):
super(DownSampleModule, self).__init__()
kernel_size = 3
padding = (kernel_size-1)/2
self.branch1 = nn.Sequential(
ConvBlock(in_channel, ch3, kernel_size = 3,stride=2, padding=int(padding))
)
self.branch2 = nn.Sequential(
nn.MaxPool2d(3, stride=2, padding=0, ceil_mode=True)
)
def forward(self, x):
branch1 = self.branch1(x)
branch2 = self.branch2(x)
return torch.cat([padded_tensor, branch2], 1)
ConvBlock 来自这个模块
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, padding):
super(ConvBlock, self).__init__()
#padding = (kernel_size -1 )/2
#print(padding)
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)
self.bn = nn.BatchNorm2d(out_channels)
self.act = nn.ReLU()
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.act(x)
return x
基本上,我们正在创建两个分支:卷积模块和最大池。然后将这两个分支的输出连接到通道维度上。
但是,我有以下问题:
- 首先,我们调用
self.pool1 = DownSampleModule(in_channel=80, ch3 = 80, w=30)。两个分支的尺寸相似。它们是:
Downsample Convolution:torch.Size([1, 80, 15, 15])
Maxpool Convolution:torch.Size([1, 80, 15, 15])
- 但是,当我们调用
self.pool2 = DownSampleModule(in_channel = 144, ch3 = 96, w=15)时。尺寸不同,因此无法连接。
Downsample Convolution:torch.Size([1, 96, 8, 8])
Maxpool Convolution:torch.Size([1, 144, 7, 7])
有人知道计算正确填充的公式吗?谢谢。
在 Keras 中,您可以设置 padding="same" 或 "valid",但 pytorch 不支持。
【问题讨论】:
标签: tensorflow deep-learning computer-vision pytorch