VGGNet
前言
《Very Deep Convolutional Networks for Large-Scale Image Recognition》
原文链接.
intro:ICLR 2015
VGGNet 分为VGGNet16和VGGNet19,这两种差不多,就是层数的区别
一、网络结构
二、简单实现代码
import torch
from torch import nn
class VGG(nn.Module):
def __init__(self, in_channels, out_channels):
super(VGG, self).__init__()
# 224
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
)
# 112
self.conv2 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
)
# 56
self.conv3 = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
)
# 28
self.conv4 = nn.Sequential(
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
)
# 14
self.conv5 = nn.Sequential(
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
)
# 全连接层
self.fc = nn.Sequential(
nn.Linear(in_features=7 * 7 * 512, out_features=1 * 1 * 4096),
nn.Dropout(),
nn.Linear(in_features=4096, out_features=1 * 1 * 4096),
nn.Dropout(),
nn.Linear(in_features=4096, out_features=1 * 1 * out_channels),
nn.Softmax()
)
def forward(self, x):
x1_shape = x.shape
out = self.conv1(x)
x2_shape = out.shape
out = self.conv2(out)
x3_shape = out.shape
out = self.conv3(out)
x4_shape = out.shape
out = self.conv4(out)
x5_shape = out.shape
out = self.conv5(out)
x6_shape = out.shape
out = torch.flatten(out, 1)
out = self.fc(out)
return out
def test():
net = VGG(in_channels=3, out_channels=10)
y = net(torch.randn(4, 3, 224, 224))
#print(net.out_channels)
print(net)
test()
# 代码没啥好说的,就是照着网络结构写的