其他分享
首页 > 其他分享> > VGG-19架构 pytorch实现

VGG-19架构 pytorch实现

作者:互联网

在这里插入图片描述

import torch
import torch.nn as nn
from torchinfo import summary
class VGG19(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1=nn.Conv2d(3,64,kernel_size=3,padding=1)
        self.conv2=nn.Conv2d(64,64,kernel_size=3,padding=1)
        self.pool1 = nn.MaxPool2d(kernel_size=2,stride=2)
        self.conv3=nn.Conv2d(64,128,kernel_size=3,padding=1)
        self.conv4=nn.Conv2d(128,128,kernel_size=3,padding=1)
        self.pool2=nn.MaxPool2d(kernel_size=2,stride=2)
        self.conv5 = nn.Conv2d(128, 256, kernel_size=3,padding=1)
        self.conv6 = nn.Conv2d(256, 256, kernel_size=3,padding=1)
        self.conv7 = nn.Conv2d(256, 256, kernel_size=3,padding=1)
        self.conv8 = nn.Conv2d(256, 256, kernel_size=3,padding=1)
        self.pool3=nn.MaxPool2d(kernel_size=2,stride=2)
        self.conv9 = nn.Conv2d(256, 512, kernel_size=3,padding=1)
        self.conv10 = nn.Conv2d(512, 512, kernel_size=3,padding=1)
        self.conv11= nn.Conv2d(512, 512, kernel_size=3,padding=1)
        self.conv12= nn.Conv2d(512, 512, kernel_size=3,padding=1)
        self.pool4=nn.MaxPool2d(kernel_size=2,stride=2,padding=1)
        self.conv13 = nn.Conv2d(512, 512, kernel_size=3,padding=1)
        self.conv14 = nn.Conv2d(512, 512, kernel_size=3,padding=1)
        self.conv15 = nn.Conv2d(512, 512, kernel_size=3,padding=1)
        self.conv16 = nn.Conv2d(512, 512, kernel_size=3,padding=1)
        self.pool5=nn.MaxPool2d(kernel_size=2,stride=2)
        self.fc1=nn.Linear(7*7*512,4096)
        self.fc2=nn.Linear(4096,4096)
        self.fc3=nn.Linear(4096,10)

        self.relu=nn.ReLU()
        self.softmax=nn.Softmax(dim=1)
        print("success")
    def forward(self,x):
        x = self.relu(self.conv1(x))
        x = self.relu(self.conv2(x))
        x = self.pool1(x)
        x = self.relu(self.conv3(x))
        x = self.relu(self.conv4(x))
        x = self.pool2(x)
        x = self.relu(self.conv5(x))
        x = self.relu(self.conv6(x))
        x = self.relu(self.conv7(x))
        x = self.relu(self.conv8(x))
        x = self.pool3(x)
        x = self.relu(self.conv9(x))
        x = self.relu(self.conv10(x))
        x = self.relu(self.conv11(x))
        x = self.relu(self.conv12(x))
        x = self.pool4(x)
        x = self.relu(self.conv13(x))
        x = self.relu(self.conv14(x))
        x = self.relu(self.conv15(x))
        x = self.relu(self.conv16(x))
        x = self.pool5(x)
        # x = x.view(x.size()[0], -1)
        x = x.view(-1,7*7*512)
        x = self.relu(self.fc1(x))
        x = self.relu(self.fc2(x))
        output = self.softmax(self.fc3(x))
        return output

net= VGG19()
# print(net)
data=torch.ones(size=(10,3,224,224))
net(data)
print(net(data).shape)
print(net(data))
# summary(net)

结果:

=================================================================
Layer (type:depth-idx)                   Param #
=================================================================
VGG19                                    --
├─Conv2d: 1-1                            1,792
├─Conv2d: 1-2                            36,928
├─MaxPool2d: 1-3                         --
├─Conv2d: 1-4                            73,856
├─Conv2d: 1-5                            147,584
├─MaxPool2d: 1-6                         --
├─Conv2d: 1-7                            295,168
├─Conv2d: 1-8                            590,080
├─Conv2d: 1-9                            590,080
├─Conv2d: 1-10                           590,080
├─MaxPool2d: 1-11                        --
├─Conv2d: 1-12                           1,180,160
├─Conv2d: 1-13                           2,359,808
├─Conv2d: 1-14                           2,359,808
├─Conv2d: 1-15                           2,359,808
├─MaxPool2d: 1-16                        --
├─Conv2d: 1-17                           2,359,808
├─Conv2d: 1-18                           2,359,808
├─Conv2d: 1-19                           2,359,808
├─Conv2d: 1-20                           2,359,808
├─MaxPool2d: 1-21                        --
├─Linear: 1-22                           102,764,544
├─Linear: 1-23                           16,781,312
├─Linear: 1-24                           40,970
├─ReLU: 1-25                             --
├─Softmax: 1-26                          --

标签:kernel,nn,19,self,VGG,relu,pytorch,Conv2d,size
来源: https://blog.csdn.net/m0_52295416/article/details/120926581