Fully Convolutional Networks(FCN)
图片均来自论文:Fully Convolutional Networks for Semantic Segmentation
上图简单解释:
若image为:224×224的图片,通过pool1后,为112×112,通过pool2后,为56×6,通过pool3后,为28×28,通过pool4后,为14×14,通过pool5后,为7×7
FCN-32s:7 × 7 直接32倍上采样变成 224 × 224
FCN-16s:7×7 两倍上采样变成 14 × 14, 和pool4后的14 ×14相加,16倍上采样变成224×224
FCN-8s: FCN-16s 上采样前矩阵 进行2倍上采样变成28 × 28,和 pool3后的 28 × 28相加,8倍上采样为224 × 224
FCN-VGG16
下采样的工作利用VGG16来完成
先利用pytorch简单的实现一下
class VGG(torch.nn.Module):
def __init__(self, n_class=10):
super().__init__()
# stage = 1
self.conv1_1 = torch.nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1)
self.relu1_1 = torch.nn.ReLU(inplace=True)
self.conv1_2 = torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1)
self.relu1_2 = torch.nn.ReLU(inplace=True)
self.pool1 = torch.nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)
# stage = 2
self.conv2_1 = torch.nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1)
self.relu2_1 = torch.nn.ReLU(inplace=True)
self.conv2_2 = torch.nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1)
self.relu2_2 = torch.nn.ReLU(inplace=True)
self.pool2 = torch.nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)
# stage = 3
self.conv3_1 = torch.nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1)
self.relu3_1 = torch.nn.ReLU(inplace=True)
self.conv3_2 = torch.nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)
self.relu3_2 = torch.nn.ReLU(inplace=True)
self.conv3_3 = torch.nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)
self.relu3_3 = torch.nn.ReLU(inplace=True)
self.pool3 = torch.nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)
# stage = 4
self.conv4_1 = torch.nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1)
self.relu4_1 = torch.nn.ReLU(inplace=True)
self.conv4_2 = torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)
self.relu4_2 = torch.nn.ReLU(inplace=True)
self.conv4_3 = torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)
self.relu4_3 = torch.nn.ReLU(inplace=True)
self.pool4 = torch.nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)
# stage = 5
self.conv5_1 = torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)
self.relu5_1 = torch.nn.ReLU(inplace=True)
self.conv5_2 = torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)
self.relu5_2 = torch.nn.ReLU(inplace=True)
self.conv5_3 = torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)
self.relu5_3 = torch.nn.ReLU(inplace=True)
self.pool5 = torch.nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)
## fully connection。这里利用了全卷积替代了全连接层
self.fc_6 = torch.nn.Conv2d(in_channels=512, out_channels=4096, kernel_size=7)
self.fc_7 = torch.nn.Conv2d(in_channels=4096, out_channels=4096, kernel_size=1)
self.fc_8 = torch.nn.Conv2d(in_channels=4096, out_channels=1000, kernel_size=1)
def forward(self, x):
print(x.shape)
h = self.pool1(self.conv1_2(self.conv1_1(x)))
print(h.shape)
h = self.pool1(self.conv2_2(self.conv2_1(h)))
print(h.shape)
h = self.pool1(self.conv3_2(self.conv3_1(h)))
print(h.shape)
h = self.pool1(self.conv4_2(self.conv4_1(h)))
print(h.shape)
h = self.pool1(self.conv5_2(self.conv5_1(h)))
print(h.shape)
h = self.fc_8(self.fc_7(self.fc_6(h)))
print(h.shape)
return h
做一个简单的测验
h = torch.randn(1,3,224,224)
model = VGG()
其结果就如上图所示一致
torch.Size([1, 3, 224, 224])
torch.Size([1, 64, 112, 112])
torch.Size([1, 128, 56, 56])
torch.Size([1, 256, 28, 28])
torch.Size([1, 512, 14, 14])
torch.Size([1, 512, 7, 7])
torch.Size([1, 1000, 1, 1])