一、网络结构
模型实现结果为:
二、遇到的问题及感想
- 在模型网络和全连接层之间,需要将张量进行展平。具体为,在
nn.Sequential()
之中加上nn.Flatten()
。 - 之前在白板复现AlexNet的时候,直接把网络结构描述出来了,代码如下:
self.conv=nn.Sequential(
nn.Conv2d(1, 96, 11, 4),
nn.ReLU(),
nn.MaxPool2d(3, 2),
nn.Conv2d(96, 256, 5, 1, 2),
nn.ReLU(),
nn.MaxPool2d(3,2),
nn.Conv2d(256, 384, 3, 1, 1),
nn.ReLU(),
nn.Conv2d(384, 384, 3, 1, 1),
nn.ReLU(),
nn.Conv2d(384,256, 3, 1, 1),
nn.ReLU(),
nn.MaxPool2d(3, 2),
)
然而,如果把VGG16也这样平铺直叙的描述,就过于繁琐了,因此可以用for循环进行描述,比较简洁:
conv_params = ((2, 1, 64), (2, 64, 128), (3, 128, 256), (3, 256,512), (3, 512, 512))
# VGG子模块
def vgg_block(num_convs, in_channels, out_channels):
block = []
for i in range(num_convs):
if i == 0:
block.append(nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, padding=1))
block.append(nn.ReLU())
elif i == num_convs-1:
block.append(nn.Conv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, padding=1))
block.append(nn.ReLU())
block.append(nn.MaxPool2d(kernel_size=2, stride=2, padding=0))
else:
block.append(nn.Conv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, padding=1))
block.append(nn.ReLU())
return nn.Sequential(*block)
# VGG16整体网络
def VGG16(conv_params):
net = nn.Sequential()
# 模型网络
for i, (num_convs, in_channels, out_channels) in enumerate(conv_params):
net.add_module("vgg_block_" + str(i+1), vgg_block(num_convs, in_channels, out_channels))
# 全连接层
net.add_module("fully_connected", nn.Sequential(
nn.Flatten(),
nn.Linear(512 * 7 * 7, 4096),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(4096, 4096),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(4096, 10),
))
return net