博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
手动搭建VGG16模型(pytorch)
阅读量:100 次
发布时间:2019-02-26

本文共 5838 字,大约阅读时间需要 19 分钟。

文章目录

一、VGG16模型结构

在这里插入图片描述

二、代码示例

import torch.nn as nnfrom torchvision import models#vgg16 = models.vgg16_bn(pretrained=False)class VGG16(nn.Module):    def __init__(self):        super(VGG16,self).__init__()        self.features = nn.Sequential(            nn.Conv2d(3,64,kernel_size=3,stride=1,padding=1),            # inplace=True,节省空间            nn.ReLU(inplace=True),            nn.Conv2d(64,64,kernel_size=3,stride=1,padding=1),            nn.ReLU(inplace=True),            # ceil_mode=False,向下取整            nn.MaxPool2d(kernel_size=2,stride=2,padding=0,dilation=1,ceil_mode=False),            nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1),            nn.ReLU(inplace=True),            nn.Conv2d(128,128,kernel_size=3,stride=1,padding=1),            nn.ReLU(inplace=True),            nn.MaxPool2d(kernel_size=2,stride=2,padding=0,dilation=1,ceil_mode=False),            nn.Conv2d(128,256,kernel_size=3,stride=1,padding=1),            nn.ReLU(inplace=True),            nn.Conv2d(256,256,kernel_size=3,stride=1,padding=1),            nn.ReLU(inplace=True),            nn.Conv2d(256,256,kernel_size=3,stride=1,padding=1),            nn.ReLU(inplace=True),            nn.MaxPool2d(kernel_size=2,stride=2,padding=0,dilation=1,ceil_mode=False),            nn.Conv2d(256,512,kernel_size=3,stride=1,padding=1),            nn.ReLU(inplace=True),            nn.Conv2d(512,512,kernel_size=3,stride=1,padding=1),            nn.ReLU(inplace=True),            nn.Conv2d(512,512,kernel_size=3,stride=1,padding=1),            nn.ReLU(inplace=True),            nn.MaxPool2d(kernel_size=2,stride=2,padding=0,dilation=1,ceil_mode=False),            nn.Conv2d(512,512,kernel_size=3,stride=1,padding=1),            nn.ReLU(inplace=True),            nn.Conv2d(512,512,kernel_size=3,stride=1,padding=1),            nn.ReLU(inplace=True),            nn.Conv2d(512,512,kernel_size=3,stride=1,padding=1),            nn.ReLU(inplace=True),            nn.MaxPool2d(kernel_size=2,stride=2,padding=0,dilation=1,ceil_mode=False)        )        self.avgpool = nn.AdaptiveAvgPool2d(output_size=7)        self.classifier = nn.Sequential(            nn.Linear(in_features=25088,out_features=4096,bias=True),            nn.ReLU(inplace=True),            nn.Dropout(p=0.5,inplace=False),            nn.Linear(in_features=4096,out_features=4096,bias=True),            nn.ReLU(inplace=True),            nn.Dropout(p=0.5,inplace=False),            nn.Linear(in_features=4096,out_features=100,bias=True)        )    def forward(self,out):        in_size = out.size(0)        out = self.features(out)        out = self.avgpool(out)        out = out.view(in_size,-1)#拉平        out = self.classifier(out)        return outif __name__ == '__main__':    from torchsummary import summary    vgg16 = VGG16()    summary(vgg16,(3,244,244))

效果:

----------------------------------------------------------------        Layer (type)               Output Shape         Param #================================================================            Conv2d-1         [-1, 64, 244, 244]           1,792              ReLU-2         [-1, 64, 244, 244]               0            Conv2d-3         [-1, 64, 244, 244]          36,928              ReLU-4         [-1, 64, 244, 244]               0         MaxPool2d-5         [-1, 64, 122, 122]               0            Conv2d-6        [-1, 128, 122, 122]          73,856              ReLU-7        [-1, 128, 122, 122]               0            Conv2d-8        [-1, 128, 122, 122]         147,584              ReLU-9        [-1, 128, 122, 122]               0        MaxPool2d-10          [-1, 128, 61, 61]               0           Conv2d-11          [-1, 256, 61, 61]         295,168             ReLU-12          [-1, 256, 61, 61]               0           Conv2d-13          [-1, 256, 61, 61]         590,080             ReLU-14          [-1, 256, 61, 61]               0           Conv2d-15          [-1, 256, 61, 61]         590,080             ReLU-16          [-1, 256, 61, 61]               0        MaxPool2d-17          [-1, 256, 30, 30]               0           Conv2d-18          [-1, 512, 30, 30]       1,180,160             ReLU-19          [-1, 512, 30, 30]               0           Conv2d-20          [-1, 512, 30, 30]       2,359,808             ReLU-21          [-1, 512, 30, 30]               0           Conv2d-22          [-1, 512, 30, 30]       2,359,808             ReLU-23          [-1, 512, 30, 30]               0        MaxPool2d-24          [-1, 512, 15, 15]               0           Conv2d-25          [-1, 512, 15, 15]       2,359,808             ReLU-26          [-1, 512, 15, 15]               0           Conv2d-27          [-1, 512, 15, 15]       2,359,808             ReLU-28          [-1, 512, 15, 15]               0           Conv2d-29          [-1, 512, 15, 15]       2,359,808             ReLU-30          [-1, 512, 15, 15]               0        MaxPool2d-31            [-1, 512, 7, 7]               0AdaptiveAvgPool2d-32            [-1, 512, 7, 7]               0           Linear-33                 [-1, 4096]     102,764,544             ReLU-34                 [-1, 4096]               0          Dropout-35                 [-1, 4096]               0           Linear-36                 [-1, 4096]      16,781,312             ReLU-37                 [-1, 4096]               0          Dropout-38                 [-1, 4096]               0           Linear-39                  [-1, 100]         409,700================================================================Total params: 134,670,244Trainable params: 134,670,244Non-trainable params: 0----------------------------------------------------------------Input size (MB): 0.68Forward/backward pass size (MB): 258.50Params size (MB): 513.73Estimated Total Size (MB): 772.91----------------------------------------------------------------

三、参考链接

转载地址:http://syju.baihongyu.com/

你可能感兴趣的文章