from torch import nn class Vgg16Net(nn.Module): def __init__(self): super(Vgg16Net, self).__init__() # 第一层,2个卷积层和一个最大池化层 self.layer1 = nn.Sequential( # 输入3通道,卷积核3*3,输出64通道(如32*32*3的样本图片,(32+2*1-3)/1+1=32,输出32*32*64) nn.Conv2d(3, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(inplace=True), # 输入64通道,卷积核3*3,输出64通道(输入32*32*64,卷积3*3*64*64,输出32*32*64) nn.Conv2d(64, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(inplace=True), # 输入32*32*64,输出16*16*64 nn.MaxPool2d(kernel_size=2, stride=2) ) # 第二层,2个卷积层和一个最大池化层 self.layer2 = nn.Sequential( # 输入64通道,卷积核3*3,输出128通道(输入16*16*64,卷积3*3*64*128,输出16*16*128) nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(inplace=True), # 输入128通道,卷积核3*3,输出128通道(输入16*16*128,卷积3*3*128*128,输出16*16*128) nn.Conv2d(128, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(inplace=True), # 输入16*16*128,输出8*8*128 nn.MaxPool2d(kernel_size=2, stride=2) ) # 第三层,3个卷积层和一个最大池化层 self.layer3 = nn.Sequential( # 输入128通道,卷积核3*3,输出256通道(输入8*8*128,卷积3*3*128*256,输出8*8*256) nn.Conv2d(128, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(inplace=True), # 输入256通道,卷积核3*3,输出256通道(输入8*8*256,卷积3*3*256*256,输出8*8*256) nn.Conv2d(256, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(inplace=True), # 输入256通道,卷积核3*3,输出256通道(输入8*8*256,卷积3*3*256*256,输出8*8*256) nn.Conv2d(256, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(inplace=True), # 输入8*8*256,输出4*4*256 nn.MaxPool2d(kernel_size=2, stride=2) ) # 第四层,3个卷积层和1个最大池化层 self.layer4 = nn.Sequential( # 输入256通道,卷积3*3,输出512通道(输入4*4*256,卷积3*3*256*512,输出4*4*512) nn.Conv2d(256, 512, 3, padding=1), nn.BatchNorm2d(512), nn.ReLU(inplace=True), # 输入512通道,卷积3*3,输出512通道(输入4*4*512,卷积3*3*512*512,输出4*4*512) nn.Conv2d(512, 512, 3, padding=1), nn.BatchNorm2d(512), nn.ReLU(inplace=True), # 输入512通道,卷积3*3,输出512通道(输入4*4*512,卷积3*3*512*512,输出4*4*512) nn.Conv2d(512, 512, 3, padding=1), nn.BatchNorm2d(512), nn.ReLU(inplace=True), # 输入4*4*512,输出2*2*512 nn.MaxPool2d(kernel_size=2, stride=2) ) # 第五层,3个卷积层和1个最大池化层 self.layer5 = nn.Sequential( # 输入512通道,卷积3*3,输出512通道(输入2*2*512,卷积3*3*512*512,输出2*2*512) nn.Conv2d(512, 512, 3, padding=1), nn.BatchNorm2d(512), nn.ReLU(inplace=True), # 输入512通道,卷积3*3,输出512通道(输入2*2*512,卷积3*3*512*512,输出2*2*512) nn.Conv2d(512, 512, 3, padding=1), nn.BatchNorm2d(512), nn.ReLU(inplace=True), # 输入512通道,卷积3*3,输出512通道(输入2*2*512,卷积3*3*512*512,输出2*2*512) nn.Conv2d(512, 512, 3, padding=1), nn.BatchNorm2d(512), nn.ReLU(inplace=True), # 输入2*2*512,输出1*1*512 nn.MaxPool2d(kernel_size=2, stride=2) ) self.conv_layer = nn.Sequential( self.layer1, self.layer2, self.layer3, self.layer4, self.layer5 ) self.fc = nn.Sequential( nn.Linear(512, 4096), nn.ReLU(inplace=True), nn.Dropout(), nn.Linear(4096, 4096), nn.ReLU(inplace=True), nn.Dropout(), nn.Linear(4096, 1000) ) def forward(self, x): x = self.conv_layer(x) x = x.view(-1, 512) x = self.fc(x) return x
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110注意:本博客写的代码网络: 输入32*32*3 ,输出1*1*512。原网络:输入224*224*3 ,输出7*7*512。
参考:https://blog.csdn.net/aa330233789/article/details/106411301?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-8.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-8.nonecase
相关知识
使用PyTorch实现对花朵的分类
pytorch实现迁移训练
ResNet残差网络在PyTorch中的实现——训练花卉分类器
搭建简单的神经网络——使用pytorch实现鸢尾花的分类
pytorch深度学习框架——实现病虫害图像分类
Pytorch实现102类鲜花分类(102 Category Flower Dataset)
Pytorch介绍与linux、windows环境下安装
pytorch 花朵的分类识别
创建虚拟环境并,创建pytorch 1.3.1
基于Python和PyTorch的小程序苹果病虫害识别教程
网址: Pytorch实现Vgg16Net https://m.huajiangbk.com/newsview742487.html
上一篇: 用word模仿手写字体 |
下一篇: 南构村花手写字体参数,nango |