1.网络结构图及对应输出结果
文章图片
2.pytorch代码实现
import torch.nn as nn
from torchsummary import summary
import torchclass VGG16(nn.Module):
def __init__(self):
super(VGG16, self).__init__()
self.maxpool1 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3,stride=1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3,stride=1, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2)
)self.maxpool2 = nn.Sequential(
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)
)self.maxpool3 = nn.Sequential(
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)
)self.maxpool4 = nn.Sequential(
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)
)self.maxpool5= nn.Sequential(
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)
)self.dense = nn.Sequential(
nn.Linear(512 * 5 * 5, 4096),
nn.ReLU(),
nn.Linear(4096, 4096),
nn.ReLU(),
nn.Linear(4096, 1000)
)
def forward(self, x):
pool1=self.maxpool1(x)
pool2=self.maxpool2(pool1)
pool3=self.maxpool3(pool2)
pool4=self.maxpool4(pool3)
pool5=self.maxpool5(pool4)flat = pool5.view(pool5.size(0), -1)
class_ = self.dense(flat)
return class_
if __name__ == "__main__":
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
vgg_model=VGG16().to(device)
summary(vgg_model, (3,160, 160)) #打印网络结构
【pytorch|VGG16网络结构图及pytorch 代码实现】3.打印网络结构输出结果
文章图片
推荐阅读
- Pytorch|PyTorch学习笔记(一)(PyTorch环境安装)
- Pytorch|PyTorch学习笔记(五)(模型定义、修改、保存)
- 深度学习|PyTorch学习笔记(四)(PyTorch基础实战)
- 深度学习|Pytorch深度学习实战笔记
- 深度学习|PyTorch学习笔记(三)(PyTorch主要组成模块)
- Pytorch|PyTorch学习笔记(二)(简介与基础知识)
- 深度学习|深度学习 --- 卷积神经网络CNN(LeNet-5网络详解)
- 深度学习|YOLOv1——v5学习笔记
- 僧旅|神经网络模型LeNet-5学习