博客
关于我
手动搭建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/

你可能感兴趣的文章
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>