pytorch Vgg网络模型
Vgg网络
网络亮点
通过堆叠多个3x3的卷积核来代替大尺寸卷积核(减少参数)
堆叠两个3x3的卷积核代替一个5x5的卷积核,堆叠三个3x3的卷积核代替7X7卷积核
import torch.nn as nn
import torch# official pretrain weights
model_urls = {'vgg11': 'https://download.pytorch.org/models/vgg11-bbd30ac9.pth','vgg13': 'https://download.pytorch.org/models/vgg13-c768596a.pth','vgg16': 'https://download.pytorch.org/models/vgg16-397923af.pth','vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth'
}class VGG(nn.Module):def __init__(self, features, num_classes=1000, init_weights=False):super(VGG, self).__init__()self.features = featuresself.classifier = nn.Sequential(nn.Linear(512*7*7, 4096), #512 channel 7x7 图像大小nn.ReLU(True),nn.Dropout(p=0.5),nn.Linear(4096, 4096),nn.ReLU(True),nn.Dropout(p=0.5),nn.Linear(4096, num_classes))if init_weights:self._initialize_weights()def forward(self, x):# N x 3 x 224 x 224 输入图像x = self.features(x)# N x 512 x 7 x 7 展平前x = torch.flatten(x, start_dim=1)# N x 512*7*7 送入分类x = self.classifier(x)return xdef _initialize_weights(self):for m in self.modules():if isinstance(m, nn.Conv2d):# nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')nn.init.xavier_uniform_(m.weight)if m.bias is not None:nn.init.constant_(m.bias, 0)elif isinstance(m, nn.Linear):nn.init.xavier_uniform_(m.weight)# nn.init.normal_(m.weight, 0, 0.01)nn.init.constant_(m.bias, 0)def make_features(cfg: list):layers = []in_channels = 3#RGB 三通道for v in cfg:if v == "M":layers += [nn.MaxPool2d(kernel_size=2, stride=2)]else:conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)layers += [conv2d, nn.ReLU(True)]in_channels = vreturn nn.Sequential(*layers)#非关键字参数#模型深度选择
cfgs = {'vgg11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],'vgg13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],'vgg16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],'vgg19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}def vgg(model_name="vgg16", **kwargs):assert model_name in cfgs, "Warning: model number {} not in cfgs dict!".format(model_name)cfg = cfgs[model_name]model = VGG(make_features(cfg), **kwargs)return model#网络模型打印
vgg_module = vgg("vgg13")
print(vgg_module)
网络模型打印输出
VGG((features): Sequential((0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(1): ReLU(inplace=True)(2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(3): ReLU(inplace=True)(4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)(5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(6): ReLU(inplace=True)(7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(8): ReLU(inplace=True)(9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)(10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(11): ReLU(inplace=True)(12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(13): ReLU(inplace=True)(14): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)(15): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(16): ReLU(inplace=True)(17): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(18): ReLU(inplace=True)(19): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)(20): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(21): ReLU(inplace=True)(22): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(23): ReLU(inplace=True)(24): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False))(classifier): Sequential((0): Linear(in_features=25088, out_features=4096, bias=True)(1): ReLU(inplace=True)(2): Dropout(p=0.5, inplace=False)(3): Linear(in_features=4096, out_features=4096, bias=True)(4): ReLU(inplace=True)(5): Dropout(p=0.5, inplace=False)(6): Linear(in_features=4096, out_features=1000, bias=True))
)
pytorch Vgg网络模型相关推荐
- 深度学习修炼(五)——基于pytorch神经网络模型进行气温预测
文章目录 5 基于pytorch神经网络模型进行气温预测 5.1 实现前的知识补充 5.1.1 神经网络的表示 5.1.2 隐藏层 5.1.3 线性模型出错 5.1.4 在网络中加入隐藏层 5.1.5 ...
- pytorch vgg 网络
论文全名:<Very Deep Convolutional Networks for Large-Scale Image Recognition> 2014年,牛津大学计算机视觉组(Vis ...
- 基于PyTorch的卷积神经网络图像分类——猫狗大战(二):使用Pytorch定义网络模型
文章目录 1. 需要用到的库 2. 模型定义 3. 测试 基于上一篇文章 https://blog.csdn.net/linghu8812/article/details/100044971,这次介绍 ...
- 深度学习图像处理之VGG网络模型 (超级详细)
VGG介绍: 由牛津大学著名研究所VGG提出,斩获该年ImageNet竞赛中Localization Task(定位任务)第一名和Classification Task(分类任务)第二名. VGG网络 ...
- 图片分类 猫狗大战 pytorch VGG
使用pytorch实现猫狗大战 一.简介 二.理论 三.实现 1️⃣.实现准备 2️⃣.创建VGG16模型 3️⃣.训练模型 4️⃣.在验证集上测试训练的模型 5️⃣.在测试集上运行 四.总结 五.我 ...
- pytorch VGG
相对alexn 增加了层数,filter变成3*3
- pytorch——VGG网络搭建
目录
- 神经网络模型u-net 、VGG
[理论] 神经网络模型u-net .VGG 文章目录 [理论] 神经网络模型u-net .VGG 一.人脑视觉处理机理人脑视觉机理 二.U-net网络 1.定义 编码解码结构 上采样.下采样.长连接 ...
- 【Pytorch】基于卷积神经网络实现的面部表情识别
作者:何翔 学院:计算机学院 学号:04191315 班级:软件1903 转载请标注本文链接: https://blog.csdn.net/HXBest/article/details/1219812 ...
最新文章
- centos下LAMP之源码编译安装httpd
- python求最值_用Python实现最速下降法求极值的方法
- unity Android 剪贴板,Unity移动端的复制要这么写示例代码
- linux rpm 包制作详解
- Swin Transformer 升级,Swin V2:向更大容量、更高分辨率的更大模型迈进
- [你必须知道的.NET]第二十八回:说说Name这回事儿
- 计算机二级c语言第82套,计算机等级考试二级C语言模拟试题十套答案
- 从各位前辈手中搜集的经验
- ANDROID笔记:Activity之间的传值
- 20175322 周弋鸿 2018-2019-2 《Java程序设计》第八周学习总结
- 基于javaEE的网上商城购物系统
- linux服务器如何查看硬盘型号,Linux如何查看硬盘型号和缓存
- 学习cola DDD 架构遇到的问题 在start模块的pom文件添加依赖
- 【技巧】如何以“只读方式”打开PPT文稿?附两个方法
- 用计算机输入文字语音播放出来,Win10如何实现电脑语音输入?Win10使用搜狗输入法语音输入文字的方法...
- python红楼梦词频统计
- ap的ht模式_华通AP-HT-WD400AP-IN系列
- linux go设置阿里云代理
- pt, px, DPI: 关于长度单位的误解
- R语言--MCMC算法介绍以及例子
热门文章
- Jmeter5.x线程组setUp-tearDown和调度器实战
- AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
- unable to access https://github.com/....解决方法
- 计算机英语断句,英语断句方法
- 中学计算机社团活动计划,社团活动方案
- 2021年秋招面经:上海禾赛提前批(FPGA设计)
- ZOC7 for Mac(终端模拟器)
- PostgreSQL 连接(JOIN)
- android vrs技术,步步高 vivo V1/Y1 智能手机音质测评报告 VRS[vivo signal
- [题解][CF-1292C]Xenon‘s Attack on the Gangs