vgg11/13/16/19-pytorch实现
import torch
import torch.nn as nn
cfg = {
'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'B': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'C': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'D': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}
class VGG(nn.Module):
def __init__(self, feature, num_class=10):
super().__init__()
self.feature = feature
self.classifier = nn.Sequential(
nn.Linear(7 * 7 * 512, 4096),
# nn.Linear(512, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, num_class)
)
def forward(self, x):
output = self.feature(x)
output = output.view(output.size()[0], -1)
output = self.classifier(output)
return output
def make_layers(cfg, batch_norm=False):
layers = []
input_channel = 3
for l in cfg:
if l == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
continue
layers += [nn.Conv2d(input_channel, l, kernel_size=3, padding=1)] # stride默认为1,即保持图像尺寸不变
if batch_norm == True:
layers += [nn.BatchNorm2d(l)]
layers += [nn.ReLU(inplace=True)]
input_channel = l
return nn.Sequential(*layers) # *list能提取列表中的元素
def vgg11_bn():
return VGG(make_layers(cfg['A'], batch_norm=True))
def vgg13_bn():
return VGG(make_layers(cfg['B'], batch_norm=True))
def vgg16_bn():
return VGG(make_layers(cfg['C'], batch_norm=True))
def vgg19_bn():
return VGG(make_layers(cfg['D'], batch_norm=True))
if __name__ == '__main__':
# l = [1, 2, 3]
# print(*l)
from torchsummary import summary
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = vgg16_bn().to(device)
summary(model, (3, 224, 224))
Output
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 224, 224] 1,792
BatchNorm2d-2 [-1, 64, 224, 224] 128
ReLU-3 [-1, 64, 224, 224] 0
Conv2d-4 [-1, 64, 224, 224] 36,928
BatchNorm2d-5 [-1, 64, 224, 224] 128
ReLU-6 [-1, 64, 224, 224] 0
MaxPool2d-7 [-1, 64, 112, 112] 0
Conv2d-8 [-1, 128, 112, 112] 73,856
BatchNorm2d-9 [-1, 128, 112, 112] 256
ReLU-10 [-1, 128, 112, 112] 0
Conv2d-11 [-1, 128, 112, 112] 147,584
BatchNorm2d-12 [-1, 128, 112, 112] 256
ReLU-13 [-1, 128, 112, 112] 0
MaxPool2d-14 [-1, 128, 56, 56] 0
Conv2d-15 [-1, 256, 56, 56] 295,168
BatchNorm2d-16 [-1, 256, 56, 56] 512
ReLU-17 [-1, 256, 56, 56] 0
Conv2d-18 [-1, 256, 56, 56] 590,080
BatchNorm2d-19 [-1, 256, 56, 56] 512
ReLU-20 [-1, 256, 56, 56] 0
Conv2d-21 [-1, 256, 56, 56] 590,080
BatchNorm2d-22 [-1, 256, 56, 56] 512
ReLU-23 [-1, 256, 56, 56] 0
MaxPool2d-24 [-1, 256, 28, 28] 0
Conv2d-25 [-1, 512, 28, 28] 1,180,160
BatchNorm2d-26 [-1, 512, 28, 28] 1,024
ReLU-27 [-1, 512, 28, 28] 0
Conv2d-28 [-1, 512, 28, 28] 2,359,808
BatchNorm2d-29 [-1, 512, 28, 28] 1,024
ReLU-30 [-1, 512, 28, 28] 0
Conv2d-31 [-1, 512, 28, 28] 2,359,808
BatchNorm2d-32 [-1, 512, 28, 28] 1,024
ReLU-33 [-1, 512, 28, 28] 0
MaxPool2d-34 [-1, 512, 14, 14] 0
Conv2d-35 [-1, 512, 14, 14] 2,359,808
BatchNorm2d-36 [-1, 512, 14, 14] 1,024
ReLU-37 [-1, 512, 14, 14] 0
Conv2d-38 [-1, 512, 14, 14] 2,359,808
BatchNorm2d-39 [-1, 512, 14, 14] 1,024
ReLU-40 [-1, 512, 14, 14] 0
Conv2d-41 [-1, 512, 14, 14] 2,359,808
BatchNorm2d-42 [-1, 512, 14, 14] 1,024
ReLU-43 [-1, 512, 14, 14] 0
MaxPool2d-44 [-1, 512, 7, 7] 0
Linear-45 [-1, 4096] 102,764,544
ReLU-46 [-1, 4096] 0
Dropout-47 [-1, 4096] 0
Linear-48 [-1, 4096] 16,781,312
ReLU-49 [-1, 4096] 0
Dropout-50 [-1, 4096] 0
Linear-51 [-1, 10] 40,970
================================================================
Total params: 134,309,962
Trainable params: 134,309,962
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 321.94
Params size (MB): 512.35
Estimated Total Size (MB): 834.87
----------------------------------------------------------------
Process finished with exit code 0