pytorch 实现VGG

VGG 全称是Visual Geometry Group,因为是由Oxford的Visual Geometry Group提出的。AlexNet问世之后,很多学者通过改进AlexNet的网络结构来提高自己的准确率,主要有两个方向:小卷积核和多尺度。而VGG的作者们则选择了另外一个方向,即加深网络深度。

卷积网络的输入是224 * 224RGB图像,整个网络的组成是非常格式化的,基本上都用的是3 * 3的卷积核以及 2 * 2max pooling,少部分网络加入了1 * 1的卷积核。因为想要体现出"上下左右中"的概念,3*3的卷积核已经是最小的尺寸了。

python 复制代码
import torch
import torch.nn as nn


# 定义VGG模型
class VGG(nn.Module):
    def __init__(self, num_classes=1000):
        super(VGG, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 64, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(64, 128, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(128, 128, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(128, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(256, 512, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(512, 512, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(512, 512, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            nn.Conv2d(512, 512, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(512, 512, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(512, 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(7 * 7 * 512, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, num_classes)
        )

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


# 创建VGG模型实例
model = VGG()
相关推荐
文心快码 Baidu Comate8 分钟前
Comate Spec模式实测:让AI编程更精准可靠
人工智能·ai编程·文心快码·ai编程助手
疾风sxp8 分钟前
nl2sql技术实现自动sql生成
人工智能·word2vec
阿星AI工作室11 分钟前
让gemini3做的网页拥有支付功能,访客变付费用户!附提示词
人工智能
dhdjjsjs22 分钟前
Day35 PythonStudy
python
LaughingZhu23 分钟前
Product Hunt 每日热榜 | 2025-12-10
人工智能·经验分享·深度学习·神经网络·产品运营
老蒋新思维26 分钟前
创客匠人 2025 万人峰会核心:AI 驱动知识产品变现革新
大数据·人工智能·网络协议·tcp/ip·创始人ip·创客匠人·知识变现
音沐mu.27 分钟前
【34】犬类品种数据集(有v5/v8模型)/YOLO犬类品种检测
人工智能·yolo·目标检测·犬类品种数据集·犬类品种检测
Want59528 分钟前
Vibe Coding实战案例:利用Qoder打造个人知识库AI助手,并上线魔搭创空间
人工智能·aigc
多则惑少则明34 分钟前
AI测试、大模型测试(七)Java主流大模型框架技术
人工智能·ai测试·ai大模型测试
xinyu_Jina35 分钟前
人像精灵 AI 智能相馆:特征解耦与条件生成对抗网络(cGANs)在人像重构中的应用
人工智能·生成对抗网络·重构