VGG16网络结构图及pytorch 代码实现

1.网络结构图及对应输出结果
VGG16网络结构图及pytorch 代码实现_第1张图片

2.pytorch代码实现

import torch.nn as nn
from torchsummary import summary
import torch

class 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)) #打印网络结构

3.打印网络结构输出结果
VGG16网络结构图及pytorch 代码实现_第2张图片

你可能感兴趣的:(pytorch,pytorch,神经网络,深度学习)