VGGNet

VGGNet



前言

《Very Deep Convolutional Networks for Large-Scale Image Recognition》
原文链接.
intro:ICLR 2015
VGGNet 分为VGGNet16和VGGNet19,这两种差不多,就是层数的区别

一、网络结构

在这里插入图片描述

在这里插入图片描述

二、简单实现代码

import torch
from torch import nn



class VGG(nn.Module):

    def __init__(self, in_channels, out_channels):
        super(VGG, self).__init__()
        # 224
        self.conv1 = nn.Sequential(
            nn.Conv2d(in_channels, 64, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
        )
        # 112
        self.conv2 = nn.Sequential(
            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
        )
        # 56
        self.conv3 = nn.Sequential(
            nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
        )
        # 28
        self.conv4 = nn.Sequential(
            nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
        )
        # 14
        self.conv5 = nn.Sequential(
            nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
        )
        # 全连接层
        self.fc = nn.Sequential(
            nn.Linear(in_features=7 * 7 * 512, out_features=1 * 1 * 4096),
            nn.Dropout(),
            nn.Linear(in_features=4096, out_features=1 * 1 * 4096),
            nn.Dropout(),
            nn.Linear(in_features=4096, out_features=1 * 1 * out_channels),
            nn.Softmax()
        )

    def forward(self, x):
        x1_shape = x.shape
        out = self.conv1(x)
        x2_shape = out.shape
        out = self.conv2(out)
        x3_shape = out.shape
        out = self.conv3(out)
        x4_shape = out.shape
        out = self.conv4(out)
        x5_shape = out.shape
        out = self.conv5(out)
        x6_shape = out.shape

        out = torch.flatten(out, 1)
        out = self.fc(out)

        return out



def test():
    net = VGG(in_channels=3, out_channels=10)
    y = net(torch.randn(4, 3, 224, 224))
    #print(net.out_channels)
    print(net)


test()

# 代码没啥好说的,就是照着网络结构写的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值