CIFAR10数据集来源:torchvision.datasets.CIFAR10()
CIFAR10数据集是一个用于识别普适物体的小型数据集,一共包含10个类别的RGB彩色图片,图片尺寸大小为32x32,如图:
相较于MNIST数据集,MNIST数据集是28x28的单通道灰度图,而CIFAR10数据集是32x32的RGB三通道彩色图,CIFAR10数据集更接近于真实世界的图片。
全连接的缺点有:
全连接参数过多,会导致训练量过大
全连接把图像展开成一个向量,丢失了图像原本的位置信息
全连接限制图像的尺寸,而卷积则不关心图像尺寸大小,只需要接受输入的通道数,输出的通道数和卷积核大小即可确定图像尺寸的变换过程,即
padding:对输入图片进行填充,一般用0填充,padding=1,代表填充一圈,保证卷积前后的图像尺寸大小一致,padding计算公式如下:
stride步长:指的是卷积核每次滑动的距离大小
ResNet网络模型:
本文采用ResNet18来构建深度网络模型:
1. 数据集构建
每个像素点即每条数据中的值范围为0-255,有的数字过大不利于训练且难以收敛,故将其归一化到(0-1)之间
# 数据集处理
# transforms.RandomHorizontalFlip(p=0.5)---以0.5的概率对图片做水平横向翻转
transform_train = transforms.Compose([transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))])
# transforms.ToTensor()---shape从(H,W,C)->(C,H,W), 每个像素点从(0-255)映射到(0-1):直接除以255
# transforms.Normalize---先将输入归一化到(0,1),像素点通过"(x-mean)/std",将每个元素分布到(-1,1)
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(std=(0.485, 0.456, 0.406), mean=(0.226, 0.224, 0.225))])
train_dataset = datasets.CIFAR10(root="../DataSet/cifar10", train=True, transform=transform_train,
download=True)
test_dataset = datasets.CIFAR10(root="../DataSet/cifar10", train=False, transform=transform,
download=True)
# dataset:数据集 batch_size:mini-batch的大小 shuffle:是否打乱数据集顺序
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
2.构建 VGGNet18 网络模型,最后接Softmax来处理output
1)ResNet18_BasicBlock-残差单元
# 构建 VGGNet18 网络模型
# 1.ResNet18_BasicBlock-残差单元
class ResNet18_BasicBlock(nn.Module):
def __init__(self, input_channel, output_channel, stride, use_conv1_1):
super(ResNet18_BasicBlock, self).__init__()
# 第一层卷积
self.conv1 = nn.Conv2d(input_channel, output_channel, kernel_size=3, stride=stride, padding=1)
# 第二层卷积
self.conv2 = nn.Conv2d(output_channel, output_channel, kernel_size=3, stride=1, padding=1)
# 1*1卷积核,在不改变图片尺寸的情况下给通道升维
self.extra = nn.Sequential(
nn.Conv2d(input_channel, output_channel, kernel_size=1, stride=stride, padding=0),
nn.BatchNorm2d(output_channel)
)
self.use_conv1_1 = use_conv1_1
self.bn = nn.BatchNorm2d(output_channel)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
out = self.bn(self.conv1(x))
out = self.relu(out)
out = self.bn(self.conv2(out))
# 残差连接-(B,C,H,W)维度一致才能进行残差连接
if self.use_conv1_1:
out = self.extra(x) + out
out = self.relu(out)
return out
2)构建 ResNet18 网络模型
# 2.构建 ResNet18 网络模型
class ResNet18(nn.Module):
def __init__(self):
super(ResNet18, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=3, padding=1),
nn.BatchNorm2d(64)
)
self.block1_1 = ResNet18_BasicBlock(input_channel=64, output_channel=64, stride=1, use_conv1_1=False)
self.block1_2 = ResNet18_BasicBlock(input_channel=64, output_channel=64, stride=1, use_conv1_1=False)
self.block2_1 = ResNet18_BasicBlock(input_channel=64, output_channel=128, stride=2, use_conv1_1=True)
self.block2_2 = ResNet18_BasicBlock(input_channel=128, output_channel=128, stride=1, use_conv1_1=False)
self.block3_1 = ResNet18_BasicBlock(input_channel=128, output_channel=256, stride=2, use_conv1_1=True)
self.block3_2 = ResNet18_BasicBlock(input_channel=256, output_channel=256, stride=1, use_conv1_1=False)
self.block4_1 = ResNet18_BasicBlock(input_channel=256, output_channel=512, stride=2, use_conv1_1=True)
self.block4_2 = ResNet18_BasicBlock(input_channel=512, output_channel=512, stride=1, use_conv1_1=False)
self.FC_layer = nn.Linear(512 * 1 * 1, 10)
self.adaptive_avg_pool2d = nn.AdaptiveAvgPool2d((1,1))
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.relu(self.conv1(x))
# ResNet18-网络模型
x = self.block1_1(x)
x = self.block1_2(x)
x = self.block2_1(x)
x = self.block2_2(x)
x = self.block3_1(x)
x = self.block3_2(x)
x = self.block4_1(x)
x = self.block4_2(x)
# 平均值池化
x = self.adaptive_avg_pool2d(x)
# 数据平坦化处理,为接下来的全连接层做准备
x = x.view(x.size(0), -1)
x = self.FC_layer(x)
return x
3. 构建损失函数和优化器
损失函数采用CrossEntropyLoss
优化器采用 SGD 随机梯度优化算法
# 构造损失函数和优化器
criterion = nn.CrossEntropyLoss()
opt = optim.SGD(model.parameters(), lr=0.01, momentum=0.8, weight_decay=0.001)
# 动态更新学习率------每隔step_size : lr = lr * gamma
schedule = optim.lr_scheduler.StepLR(opt, step_size=10, gamma=0.6, last_epoch=-1)
4.完整代码
# -*- codeing = utf-8 -*-
# @Software : PyCharm
import torch
from torch import nn, optim
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from matplotlib import pyplot as plt
import time
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# transforms.RandomHorizontalFlip(p=0.5)---以0.5的概率对图片做水平横向翻转
transform_train = transforms.Compose([transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))])
# transforms.ToTensor()---shape从(H,W,C)->(C,H,W), 每个像素点从(0-255)映射到(0-1):直接除以255
# transforms.Normalize---先将输入归一化到(0,1),像素点通过"(x-mean)/std",将每个元素分布到(-1,1)
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(std=(0.485, 0.456, 0.406), mean=(0.226, 0.224, 0.225))])
train_dataset = datasets.CIFAR10(root="../DataSet/cifar10", train=True, transform=transform_train,
download=True)
test_dataset = datasets.CIFAR10(root="../DataSet/cifar10", train=False, transform=transform,
download=True)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
# ResNet18_BasicBlock-残差单元
class ResNet18_BasicBlock(nn.Module):
def __init__(self, input_channel, output_channel, stride, use_conv1_1):
super(ResNet18_BasicBlock, self).__init__()
# 第一层卷积
self.conv1 = nn.Conv2d(input_channel, output_channel, kernel_size=3, stride=stride, padding=1)
# 第二层卷积
self.conv2 = nn.Conv2d(output_channel, output_channel, kernel_size=3, stride=1, padding=1)
# 1*1卷积核,在不改变图片尺寸的情况下给通道升维
self.extra = nn.Sequential(
nn.Conv2d(input_channel, output_channel, kernel_size=1, stride=stride, padding=0),
nn.BatchNorm2d(output_channel)
)
self.use_conv1_1 = use_conv1_1
self.bn = nn.BatchNorm2d(output_channel)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
out = self.bn(self.conv1(x))
out = self.relu(out)
out = self.bn(self.conv2(out))
# 残差连接-(B,C,H,W)维度一致才能进行残差连接
if self.use_conv1_1:
out = self.extra(x) + out
out = self.relu(out)
return out
# 构建 ResNet18 网络模型
class ResNet18(nn.Module):
def __init__(self):
super(ResNet18, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=3, padding=1),
nn.BatchNorm2d(64)
)
self.block1_1 = ResNet18_BasicBlock(input_channel=64, output_channel=64, stride=1, use_conv1_1=False)
self.block1_2 = ResNet18_BasicBlock(input_channel=64, output_channel=64, stride=1, use_conv1_1=False)
self.block2_1 = ResNet18_BasicBlock(input_channel=64, output_channel=128, stride=2, use_conv1_1=True)
self.block2_2 = ResNet18_BasicBlock(input_channel=128, output_channel=128, stride=1, use_conv1_1=False)
self.block3_1 = ResNet18_BasicBlock(input_channel=128, output_channel=256, stride=2, use_conv1_1=True)
self.block3_2 = ResNet18_BasicBlock(input_channel=256, output_channel=256, stride=1, use_conv1_1=False)
self.block4_1 = ResNet18_BasicBlock(input_channel=256, output_channel=512, stride=2, use_conv1_1=True)
self.block4_2 = ResNet18_BasicBlock(input_channel=512, output_channel=512, stride=1, use_conv1_1=False)
self.FC_layer = nn.Linear(512 * 1 * 1, 10)
self.adaptive_avg_pool2d = nn.AdaptiveAvgPool2d((1,1))
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.relu(self.conv1(x))
# ResNet18-网络模型
x = self.block1_1(x)
x = self.block1_2(x)
x = self.block2_1(x)
x = self.block2_2(x)
x = self.block3_1(x)
x = self.block3_2(x)
x = self.block4_1(x)
x = self.block4_2(x)
# 平均值池化
x = self.adaptive_avg_pool2d(x)
# 数据平坦化处理,为接下来的全连接层做准备
x = x.view(x.size(0), -1)
x = self.FC_layer(x)
return x
# 初始化模型
model = ResNet18().to(device)
# 构造损失函数和优化器
criterion = nn.CrossEntropyLoss()
opt = optim.SGD(model.parameters(), lr=0.01, momentum=0.8, weight_decay=0.001)
# 动态更新学习率------每隔step_size : lr = lr * gamma
schedule = optim.lr_scheduler.StepLR(opt, step_size=10, gamma=0.6, last_epoch=-1)
loss_list = []
# train
def train(epoch):
start = time.time()
for epoch in range(epoch):
running_loss = 0.0
for i, (inputs, labels) in enumerate(train_loader, 0):
inputs, labels = inputs.to(device), labels.to(device)
# 将数据送入模型训练
outputs = model(inputs)
# 计算损失
loss = criterion(outputs, labels).to(device)
# 重置梯度
opt.zero_grad()
# 计算梯度,反向传播
loss.backward()
# 根据反向传播的梯度值优化更新参数
opt.step()
# 100个batch的 loss 之和
running_loss += loss.item()
loss_list.append(loss.item())
# 每一百个 batch 查看一下 平均loss
if (i + 1) % 100 == 0:
print('epoch = %d , batch = %d , loss = %.6f' % (epoch + 1, i + 1, running_loss / 100))
running_loss = 0.0
# 每一轮结束输出一下当前的学习率 lr
lr_1 = opt.param_groups[0]['lr']
print("learn_rate:%.15f" % lr_1)
schedule.step()
end = time.time()
# 计算并打印输出你的训练时间
print("time:{}".format(end - start))
# 训练过程可视化
plt.plot(loss_list)
plt.ylabel('loss')
plt.xlabel('Epoch')
plt.show()
# Test
def verify():
model.eval()
correct = 0.0
total = 0
# 训练模式不需要反向传播更新梯度
with torch.no_grad():
print("=======================test=======================")
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
pred = outputs.argmax(dim=1) # 返回每一行中最大值元素索引
total += inputs.size(0)
correct += torch.eq(pred, labels).sum().item()
print("Accuracy of the network on the 10000 test images:%.5f %%" % (100 * correct / total))
print("===============================================")
if __name__ == '__main__':
train(100)
verify()
# ResNet18: 所有卷积层全部使用使用3*3的卷积核, 两个3*3=一个5*5 同时可以减少参数量, 加深神经网络的深度
# 使用 ResNet-18 的神经网络训练 CIFAR10 数据集的准确率在 90% 左右