本文介绍复现VGG11并用于CIFAR10数据集分类(Pytorch)。
1.网络结构

上图给出了所有VGG网络的结构,其中VGG11网络结构为:
- Block1:3*3卷积×1+最大池化×1+relu(输入通道:3,输出通道:64)
- Block2:3*3卷积×1+最大池化×1+relu(输入通道:64,输出通道:128)
- Block3:3*3卷积×2+最大池化×2+relu(输入通道:128,输出通道:256)
- Block4:3*3卷积×2+最大池化×2+relu(输入通道:256,输出通道:512)
- Block5:3*3卷积×2+最大池化×2+relu(输入通道:512,输出通道:512)
- classifier:fc×3+softmax
2.代码实现
原网络中输入图像为3*224*224,经Block5后为512*7*7,fc层输出为1000类,这里使用CIFAR10数据集,输入为3*32*32,输出为10类,fc层神经元数量略有改动。
文件结构:
vgg/__init__.py:
import torch
import torch.nn as nn
import torch.nn.functional as F
class VGG11(nn.Module):
def __init__(self, num_classes=10):
super(VGG11, self).__init__()
self.conv_layer1 = self._make_conv_1(3,64)
self.conv_layer2 = self._make_conv_1(64,128)
self.conv_layer3 = self._make_conv_2(128,256)
self.conv_layer4 = self._make_conv_2(256,512)
self.conv_layer5 = self._make_conv_2(512,512)
self.classifier = nn.Sequential(
nn.Linear(512, 64), # 这里修改一下输入输出维度
nn.ReLU(inplace=True),
nn.Dropout(p=0.5),
nn.Linear(64, 64),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5),
nn.Linear(64, num_classes)
# 使用交叉熵损失函数,pytorch的nn.CrossEntropyLoss()中已经有过一次softmax处理,这里不用再写softmax
)
def _make_conv_1

本文详细介绍如何使用Pytorch复现VGG11网络,并将其应用于CIFAR10数据集进行图像分类。文章涵盖网络结构、代码实现及运行结果分析,实验证明VGG11在CIFAR10上的有效性和潜在改进空间。
最低0.47元/天 解锁文章
931





