博客
关于我
手动搭建VGG16模型(pytorch)
阅读量:102 次
发布时间: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/

你可能感兴趣的文章
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>