(一)深度学习系列之VGG网络介绍以及pytorch代码实现花分类

论文链接1409.1556.pdf (arxiv.org)

VGG是ILSVRC2014分类任务的亚军,可以看成是加深版的AlexNet,都是由Conv layer + FC layer组成。

一. VGG的特点:

​ 多个小尺寸的卷积核代替大尺寸卷积核

​ 主要表现,使用2个3x3的卷积核替代5x5的卷积核

​ 这样做的好处:

​ 1.1 在保证相同的感受野的情况下,多个小的卷积层堆积可以提高网络 的深度,有利于提高特征提取能力。

​ 1.2 参数量更少,一个5x5的卷积核,其参数量为5 x 5 x Channel_in x Channel_out,两个3x3的卷积核,其参数量为2 x 3 x 3 x Channel,很明显,25Channel_in x Channel_out > 18Channel_in x Channel_out。

​ 1.3 3x3卷积核更有利于保持图像性质

二. VGG的网络结构:

在这里插入图片描述

图1:VGG网络结构图

三. 花品种分类代码实现

3.1 vgg模型框架搭建

import torch
import torch.nn as nn

class VGG(nn.Module):
    def __init__(self, feature, num_classes=100, init_weights=False):
        super(VGG, self).__init__()
        self.features = feature
        self.classifier = nn.Sequential(
            nn.Linear(512*7*7, 4096),
            nn.ReLU(True),
            nn.Dropout(p=0.5),
            nn.Linear(4096, 4096),
            nn.ReLU(True),
            nn.Dropout(p=0.5),
            nn.Linear(4096, num_classes)
        )
        if init_weights:
            self._initialize_weights()

    def forward(self, x):
        x = self.features(x)
        x = torch.flatten(x, start_dim=1)
        x = self.classifier(x)
        return x

    def _initialize_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.xavier_uniform_(m.weight)
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.xavier_uniform_(m.weight)
                nn.init.constant_(m.bias, 0)


cfgs = {
   
    'vgg11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
    'vgg13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用PyTorch实现VGGNet的代码: ``` import torch.nn as nn class VGGNet(nn.Module): def __init__(self, num_classes=1000): super(VGGNet, self).__init__() self.features = nn.Sequential( nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2) ) self.avgpool = nn.AdaptiveAvgPool2d((7, 7)) self.classifier = nn.Sequential( nn.Linear(in_features=512*7*7, out_features=4096), nn.ReLU(inplace=True), nn.Dropout(), nn.Linear(in_features=4096, out_features=4096), nn.ReLU(inplace=True), nn.Dropout(), nn.Linear(in_features=4096, out_features=num_classes) ) def forward(self, x): x = self.features(x) x = self.avgpool(x) x = x.view(x.size(0), -1) x = self.classifier(x) return x ``` VGGNet是由多个卷积层和池化层组成的深度卷积神经网络,这里的实现中包含了原始的VGG16和VGG19两种模型。其中,卷积层包括了大量的$3\times3$卷积核,这是为了减少参数数量,同时提高网络的非线性拟合能力。池化层使用了最大池化,可以有效地降低特征图的大小。最后,全连接层输出分类结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值