1.LeNet

  LeNet是指LeNet-5,它是第一个成功应用于数字识别的卷积神经网络。在MNIST数据集上,可以达到99.2%的准确率。LeNet-5模型总共有7层,包括两个卷积层,两个池化层,两个全连接层和一个输出层。

import torch
import torch.nn as nn
from torch.autograd import Variable
#方形卷积核和等长的步长
m1=nn.Conv2d(16,33,3,stride=2)
#非长方形卷积核,非等长的步长和边界填充
m2=nn.Conv2d(16,33,(3,5),stride=(2,1),padding=(4,2))
#非方形卷积核,非等长的步长,边界填充和空间间隔
m3=nn.Conv2d(16,33,(3,5),stride=(2,1),padding=(4,2),dilation=(3,1))
input=Variable(torch.randn(20,16,50,100))
output=m2(input) 
####LeNet的PyTorch实现
class LeNet(nn.Module):
def __init__(self):
super(LeNet,self).__init__()
self.conv1=nn.Conv2d(3,6,5)
self.conv2=nn.Conv2d(6,16,5)
self.fc1=nn.Linear(16*5*5,120)
self.fc2=nn.Linear(120,84)
self.fc3=nn.Linear(84,10)
def forward(self,x):
out=F.relu(self.conv1(x))
out=F.max_pool2d(out,2)
out=F.relu(self.conv2(out))
out=F.max_pool2d(out,2)
#这句话一般出现在model类的forward函数中,具体位置一般都是在调用分类器之前。
#分类器是一个简单的nn.Linear()结构,输入输出都是维度为一的值,x = x.view(x.size(0), -1)
#这句话的出现就是为了将前面多维度的tensor展平成一维
#x = x.view(batchsize, -1)中batchsize指转换后有几行,
#而-1指在不告诉函数有多少列的情况下,根据原tensor数据和batchsize自动分配列数。
out=out.view(out.size(0),-1)
out=F.relu(self.fc1(out))
our=F.relu(self.fc2(out))
out=self.fc3(out)
return out

  2.AlexNet

  AlexNet具有更深的网络结构,使用层叠的卷积层,同时增加了Dropout和数据增强,并使用ReLU代替了之前的sigmoid函数,采用多GPU训练。

  AlexNet共8层,前5层为卷积层,后3层为全连接层。

#####AlexNet的PyTorch实现
class AlexNet(nn.Module):
def __init__(self,num_classes):
super(AlexNet,self).__init__()
self.features=nn.Sequential(
nn.Conv2d(3,96,kernel_size=11,stride=4,padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3,stride=2),
nn.Conv2d(96,256,kernel_size=5,padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3,stride=2),
nn.Conv2d(256,384,kernel_size=3,padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384,384,kernel_size=3,padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384,256,kernel_size=3,padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3,stride=2),
)
self.classifier=nn.Sequential(
nn.Dropout(),
nn.Linear(256*6*6,4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096,4096),
nn.ReLU(inplace=True),
nn.Linear(4096,num_classes),
)
def forward(self,x):
x=self.features()
x=x.view(x.size(0),256*6*6)
x=self.classifier(x)
return x

  3.VGGNet

  VGGNet采用了几个3*3的卷积核代替AlexNet中较大的卷积核,模型由若干卷积层和池化层堆叠而成。

####VGGNet的实现
cfg={
'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'],
}
class VGG(nn.Module):
def __init__(self,vgg_name):
super(VGG,self).__init__()
self.features=self._make_layers(cfg[vgg_name])
self.classifier=nn.Linear(512,10)
def forward(self,x):
out=self.features(x)
out=out.view(out.size(0),-1)
out=self.classifier(out)
return out
def _make_layers(self,cfg):
layers=[]
in_channels=3
for x in cfg:
if x =='M':
layers+=[nn.MaxPool2d(kernel_size=2,stride=2)]
else:
layers+=[nn.Conv2d(in_channels,x,kernal_size=3,padding=1),nn.BatchNorm2d(x),nn.ReLU(inplace=True)]
in_channels=x
layers+=[nn.AvgPool2d(kernel_size=1,stride=1)]
return nn.Sequential(*layers)

  4.GooLeNet

'''GoogLeNet with PyTorch.'''
import torch
import torch.nn as nn
import torch.nn.functional as F # 编写卷积+bn+relu模块
class BasicConv2d(nn.Module):
def __init__(self, in_channels, out_channals, **kwargs):
super(BasicConv2d, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channals, **kwargs)
self.bn = nn.BatchNorm2d(out_channals) def forward(self, x):
x = self.conv(x)
x = self.bn(x)
return F.relu(x) # 编写Inception模块
class Inception(nn.Module):
def __init__(self, in_planes,
n1x1, n3x3red, n3x3, n5x5red, n5x5, pool_planes):
super(Inception, self).__init__()
# 1x1 conv branch
self.b1 = BasicConv2d(in_planes, n1x1, kernel_size=1) # 1x1 conv -> 3x3 conv branch
self.b2_1x1_a = BasicConv2d(in_planes, n3x3red,
kernel_size=1)
self.b2_3x3_b = BasicConv2d(n3x3red, n3x3,
kernel_size=3, padding=1) # 1x1 conv -> 3x3 conv -> 3x3 conv branch
self.b3_1x1_a = BasicConv2d(in_planes, n5x5red,
kernel_size=1)
self.b3_3x3_b = BasicConv2d(n5x5red, n5x5,
kernel_size=3, padding=1)
self.b3_3x3_c = BasicConv2d(n5x5, n5x5,
kernel_size=3, padding=1) # 3x3 pool -> 1x1 conv branch
self.b4_pool = nn.MaxPool2d(3, stride=1, padding=1)
self.b4_1x1 = BasicConv2d(in_planes, pool_planes,
kernel_size=1) def forward(self, x):
y1 = self.b1(x)
y2 = self.b2_3x3_b(self.b2_1x1_a(x))
y3 = self.b3_3x3_c(self.b3_3x3_b(self.b3_1x1_a(x)))
y4 = self.b4_1x1(self.b4_pool(x))
# y的维度为[batch_size, out_channels, C_out,L_out]
# 合并不同卷积下的特征图
return torch.cat([y1, y2, y3, y4], 1) class GoogLeNet(nn.Module):
def __init__(self):
super(GoogLeNet, self).__init__()
self.pre_layers = BasicConv2d(3, 192,
kernel_size=3, padding=1) self.a3 = Inception(192, 64, 96, 128, 16, 32, 32)
self.b3 = Inception(256, 128, 128, 192, 32, 96, 64) self.maxpool = nn.MaxPool2d(3, stride=2, padding=1) self.a4 = Inception(480, 192, 96, 208, 16, 48, 64)
self.b4 = Inception(512, 160, 112, 224, 24, 64, 64)
self.c4 = Inception(512, 128, 128, 256, 24, 64, 64)
self.d4 = Inception(512, 112, 144, 288, 32, 64, 64)
self.e4 = Inception(528, 256, 160, 320, 32, 128, 128) self.a5 = Inception(832, 256, 160, 320, 32, 128, 128)
self.b5 = Inception(832, 384, 192, 384, 48, 128, 128) self.avgpool = nn.AvgPool2d(8, stride=1)
self.linear = nn.Linear(1024, 10) def forward(self, x):
out = self.pre_layers(x)
out = self.a3(out)
out = self.b3(out)
out = self.maxpool(out)
out = self.a4(out)
out = self.b4(out)
out = self.c4(out)
out = self.d4(out)
out = self.e4(out)
out = self.maxpool(out)
out = self.a5(out)
out = self.b5(out)
out = self.avgpool(out)
out = out.view(out.size(0), -1)
out = self.linear(out)
return out def test():
net = GoogLeNet()
x = torch.randn(1,3,32,32)
y = net(x)
print(y.size()) test()

  

经典的卷积神经网络及其Pytorch代码实现的更多相关文章

  1. 五大经典卷积神经网络介绍:LeNet / AlexNet / GoogLeNet / VGGNet/ ResNet

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! LeNet / AlexNet / GoogLeNet / VGG ...

  2. 卷积神经网络CNN的原理(三)---代码解析

    卷积神经网络在几个主流的神经网络开源架构上面都有实现,我这里不是想实现一个自己的架构,主要是通过分析一个最简单的卷积神经网络实现代码,来达到进一步的加深理解卷积神经网络的目的. 笔者在github上找 ...

  3. 卷积神经网络的初步理解LeNet-5(转)

    深度神经网路已经在语音识别,图像识别等领域取得前所未有的成功.本人在多年之前也曾接触过神经网络.本系列文章主要记录自己对深度神经网络的一些学习心得. 第二篇,讲讲经典的卷积神经网络.我不打算详细描述卷 ...

  4. TensorFlow学习笔记(四)图像识别与卷积神经网络

    一.卷积神经网络简介 卷积神经网络(Convolutional Neural Network,CNN)是一种前馈神经网络,它的人工神经元可以响应一部分覆盖范围内的周围单元,对于大型图像处理有出色表现. ...

  5. 数据挖掘、目标检测中的cnn和cn---卷积网络和卷积神经网络

    content 概述 文字识别系统LeNet-5 简化的LeNet-5系统 卷积神经网络的实现问题 深度神经网路已经在语音识别,图像识别等领域取得前所未有的成功.本人在多年之前也曾接触过神经网络.本系 ...

  6. 卷积神经网络(CNN)新手指南 1

    http://blog.csdn.net/real_myth/article/details/52273930 卷积神经网络(CNN)新手指南 2016-07-29 18:22 Blake 1条评论 ...

  7. 浅谈卷积神经网络及matlab实现

    前言,好久不见,大家有没有想我啊.哈哈.今天我们来随便说说卷积神经网络. 1卷积神经网络的优点 卷积神经网络进行图像分类是深度学习关于图像处理的一个应用,卷积神经网络的优点是能够直接与图像像素进行卷积 ...

  8. 卷积神经网络(CNN)在语音识别中的应用

    前言 总结目前语音识别的发展现状,dnn.rnn/lstm和cnn算是语音识别中几个比较主流的方向.2012年,微软邓力和俞栋老师将前馈神经网络FFDNN(Feed Forward Deep Neur ...

  9. SIGAI深度学习第八集 卷积神经网络2

    讲授Lenet.Alexnet.VGGNet.GoogLeNet等经典的卷积神经网络.Inception模块.小尺度卷积核.1x1卷积核.使用反卷积实现卷积层可视化等. 大纲: LeNet网络 Ale ...

随机推荐

  1. PHP 安装xdebug

    xdebug官网: https://xdebug.org 安装步骤如下: 使用 phpinfo() 打印出PHP相关信息, 全选, 复制 打开 xdebug 网站: https://xdebug.or ...

  2. 【洛谷P4251】[SCOI2015]小凸玩矩阵(二分+二分图匹配)

    洛谷 题意: 给出一个\(n*m\)的矩阵\(A\).现要从中选出\(n\)个数,任意两个数不能在同一行或者同一列. 现在问选出的\(n\)个数中第\(k\)大的数的最小值是多少. 思路: 显然二分一 ...

  3. Django的下载与创建。

    一.下载 (1)下载命令. 在cmd中输入下载命令: pip3 install django==1.11.11 1.11.11是该版本号. (2)pycharm中下载 直接在pycharm中下载set ...

  4. day4_7.2

    流程语句 1.if判断语句 在python中if语句可以依据判断的条件,决定执行哪个语句.其格式如下: if 条件: 代码1 else: 代码2 当满足条件1时,执行代码1,否则执行代码2.所以条件语 ...

  5. 关于字符串在ie浏览器拼接问题

    常用的字符串在ie浏览器拼接不识别的问题,建议不要使用字符串拼接,可直接用jquery添加方便快捷一些

  6. 记录错误or日记(更新中)

    前言: 从2018.8-17开始记录 本篇随笔记录做题时的小错误(大多数),考试总结(懒得总结了),做过的每个题的错误 2019.12.7 傻逼学校,给我三个小时假期给你们做题挣工资 2019.11. ...

  7. git pull --rebase的理解

    在使用git的过程中经常需要使用到git pull命令,在更新远端代码的同时如果与本地代码产生冲突了, 那么冲突的文件中就出现了需要手动合并的部分,而git pull --rebase不同的地方则是当 ...

  8. Vue 学习记录(一)-创建项目

    环境准备 node.js vue-cli 安装配置环境 1.下载node.js,使用默认配置安装 . 2.使用npm命令安装国内下载镜像(可选) cmd: npm install  -g  cnpm  ...

  9. Django-compressor压缩静态文件,逆天!!!!!

    使用django-compressor压缩混淆静态文件 使用django-compressor压缩混淆静态文件 使用django-compressor压缩混淆静态文件 django-compresso ...

  10. (三十九)golang--反序列化

    反序列化:是指将json字符串反序列化成原来的数据类型. import ( "encoding/json" "fmt" ) type monster struc ...