CIFAR10數(shù)據(jù)集來(lái)源:torchvision.datasets.CIFAR10()
CIFAR10數(shù)據(jù)集是一個(gè)用于識(shí)別普適物體的小型數(shù)據(jù)集斥滤,一共包含10個(gè)類(lèi)別的RGB彩色圖片奋早,圖片尺寸大小為32x32,如圖:
CIFAR10.png
相較于MNIST數(shù)據(jù)集洛二,MNIST數(shù)據(jù)集是28x28的單通道灰度圖舱沧,而CIFAR10數(shù)據(jù)集是32x32的RGB三通道彩色圖妹沙,CIFAR10數(shù)據(jù)集更接近于真實(shí)世界的圖片。
1. 數(shù)據(jù)集構(gòu)建
每個(gè)像素點(diǎn)即每條數(shù)據(jù)中的值范圍為0-255熟吏,有的數(shù)字過(guò)大不利于訓(xùn)練且難以收斂距糖,故將其歸一化到(0-1)之間
# 數(shù)據(jù)集處理
# transforms.RandomHorizontalFlip(p=0.5)---以0.5的概率對(duì)圖片做水平橫向翻轉(zhuǎn)
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), 每個(gè)像素點(diǎn)從(0-255)映射到(0-1):直接除以255
# transforms.Normalize---先將輸入歸一化到(0,1),像素點(diǎn)通過(guò)"(x-mean)/std",將每個(gè)元素分布到(-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)
2.構(gòu)建DenseNet網(wǎng)絡(luò)模型
1)構(gòu)建 DenseNet-Block單元
class Bottleneck(nn.Module):
def __init__(self, input_channel, growth_rate):
super(Bottleneck, self).__init__()
self.bn1 = nn.BatchNorm2d(input_channel)
self.relu1 = nn.ReLU(inplace=True)
self.conv1 = nn.Conv2d(input_channel, 4 * growth_rate, kernel_size=1)
self.bn2 = nn.BatchNorm2d(4 * growth_rate)
self.relu2 = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(4 * growth_rate, growth_rate, kernel_size=3, padding=1)
def forward(self, x):
out = self.conv1(self.relu1(self.bn1(x)))
out = self.conv2(self.relu2(self.bn2(out)))
out = torch.cat([out, x], 1)
return out
2)Transition模塊---連接兩個(gè)DenseNet_Block
class Transition(nn.Module):
def __init__(self, input_channels, out_channels):
super(Transition, self).__init__()
self.bn = nn.BatchNorm2d(input_channels)
self.relu = nn.ReLU(inplace=True)
self.conv = nn.Conv2d(input_channels, out_channels, kernel_size=1)
def forward(self, x):
out = self.conv(self.relu(self.bn(x)))
out = F.avg_pool2d(out, 2)
return out
3. 構(gòu)建損失函數(shù)和優(yōu)化器
損失函數(shù)采用CrossEntropyLoss
優(yōu)化器采用 SGD 隨機(jī)梯度優(yōu)化算法
# 構(gòu)造損失函數(shù)和優(yōu)化器
criterion = nn.CrossEntropyLoss()
opt = optim.SGD(model.parameters(), lr=0.01, momentum=0.8, weight_decay=5e-4)
# 動(dòng)態(tài)更新學(xué)習(xí)率------每隔step_size : lr = lr * gamma
schedule = optim.lr_scheduler.StepLR(opt, step_size=10, gamma=0.6, last_epoch=-1)
4.完整代碼
import math
import torch.nn.functional as F
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的概率對(duì)圖片做水平橫向翻轉(zhuǎn)
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), 每個(gè)像素點(diǎn)從(0-255)映射到(0-1):直接除以255
# transforms.Normalize---先將輸入歸一化到(0,1),像素點(diǎn)通過(guò)"(x-mean)/std",將每個(gè)元素分布到(-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)
class Bottleneck(nn.Module):
def __init__(self, input_channel, growth_rate):
super(Bottleneck, self).__init__()
self.bn1 = nn.BatchNorm2d(input_channel)
self.relu1 = nn.ReLU(inplace=True)
self.conv1 = nn.Conv2d(input_channel, 4 * growth_rate, kernel_size=1)
self.bn2 = nn.BatchNorm2d(4 * growth_rate)
self.relu2 = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(4 * growth_rate, growth_rate, kernel_size=3, padding=1)
def forward(self, x):
out = self.conv1(self.relu1(self.bn1(x)))
out = self.conv2(self.relu2(self.bn2(out)))
out = torch.cat([out, x], 1)
return out
class Transition(nn.Module):
def __init__(self, input_channels, out_channels):
super(Transition, self).__init__()
self.bn = nn.BatchNorm2d(input_channels)
self.relu = nn.ReLU(inplace=True)
self.conv = nn.Conv2d(input_channels, out_channels, kernel_size=1)
def forward(self, x):
out = self.conv(self.relu(self.bn(x)))
out = F.avg_pool2d(out, 2)
return out
class DenseNet(nn.Module):
def __init__(self, nblocks, growth_rate, reduction, num_classes):
super(DenseNet, self).__init__()
self.growth_rate = growth_rate
num_planes = 2 * growth_rate
self.basic_conv = nn.Sequential(
nn.Conv2d(3, 2 * growth_rate, kernel_size=7, stride=2, padding=3),
nn.BatchNorm2d(2 * growth_rate),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
)
self.dense1 = self._make_dense_layers(num_planes, nblocks[0])
num_planes += nblocks[0] * growth_rate
out_planes = int(math.floor(num_planes * reduction))
self.trans1 = Transition(num_planes, out_planes)
num_planes = out_planes
self.dense2 = self._make_dense_layers(num_planes, nblocks[1])
num_planes += nblocks[1] * growth_rate
out_planes = int(math.floor(num_planes * reduction))
self.trans2 = Transition(num_planes, out_planes)
num_planes = out_planes
self.dense3 = self._make_dense_layers(num_planes, nblocks[2])
num_planes += nblocks[2] * growth_rate
out_planes = int(math.floor(num_planes * reduction))
self.trans3 = Transition(num_planes, out_planes)
num_planes = out_planes
self.dense4 = self._make_dense_layers(num_planes, nblocks[3])
num_planes += nblocks[3] * growth_rate
self.AdaptiveAvgPool2d = nn.AdaptiveAvgPool2d(1)
# 全連接層
self.fc = nn.Sequential(
nn.Linear(num_planes, 256),
nn.ReLU(inplace=True),
# 使一半的神經(jīng)元不起作用玄窝,防止參數(shù)量過(guò)大導(dǎo)致過(guò)擬合
nn.Dropout(0.5),
nn.Linear(256, 128),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(128, 10)
)
def _make_dense_layers(self, in_planes, nblock):
layers = []
for i in range(nblock):
layers.append(Bottleneck(in_planes, self.growth_rate))
in_planes += self.growth_rate
return nn.Sequential(*layers)
def forward(self, x):
out = self.basic_conv(x)
out = self.trans1(self.dense1(out))
out = self.trans2(self.dense2(out))
out = self.trans3(self.dense3(out))
out = self.dense4(out)
out = self.AdaptiveAvgPool2d(out)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
def DenseNet121():
return DenseNet([6, 12, 24, 16], growth_rate=32, reduction=0.5, num_classes=10)
def DenseNet169():
return DenseNet([6, 12, 32, 32], growth_rate=32, reduction=0.5, num_classes=10)
def DenseNet201():
return DenseNet([6, 12, 48, 32], growth_rate=32, reduction=0.5, num_classes=10)
def DenseNet265():
return DenseNet([6, 12, 64, 48], growth_rate=32, reduction=0.5, num_classes=10)
# 初始化模型
model = DenseNet121().to(device)
# 構(gòu)造損失函數(shù)和優(yōu)化器
criterion = nn.CrossEntropyLoss()
opt = optim.SGD(model.parameters(), lr=0.01, momentum=0.8, weight_decay=0.001)
# 動(dòng)態(tài)更新學(xué)習(xí)率------每隔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)
# 將數(shù)據(jù)送入模型訓(xùn)練
outputs = model(inputs)
# 計(jì)算損失
loss = criterion(outputs, labels).to(device)
# 重置梯度
opt.zero_grad()
# 計(jì)算梯度,反向傳播
loss.backward()
# 根據(jù)反向傳播的梯度值優(yōu)化更新參數(shù)
opt.step()
# 100個(gè)batch的 loss 之和
running_loss += loss.item()
loss_list.append(loss.item())
# 每100個(gè) 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
# 每一輪結(jié)束輸出一下當(dāng)前的學(xué)習(xí)率 lr
lr_1 = opt.param_groups[0]['lr']
print("learn_rate:%.15f" % lr_1)
schedule.step()
verify()
end = time.time()
# 計(jì)算并打印輸出你的訓(xùn)練時(shí)間
print("time:{}".format(end - start))
# 訓(xùn)練過(guò)程可視化
plt.plot(loss_list)
plt.ylabel('loss')
plt.xlabel('Epoch')
plt.savefig('./DenseNet_train_img.png')
plt.show()
# Test
def verify():
model.eval()
correct = 0.0
total = 0
# 訓(xùn)練模式不需要反向傳播更新梯度
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:%.2f %%" % (100 * correct / total))
print("==========================================================")
if __name__ == '__main__':
train(100)
verify()
# DenseNet: 所有卷積層全部使用使用3*3的卷積核, 兩個(gè)3*3=一個(gè)5*5 同時(shí)可以減少參數(shù)量, 加深神經(jīng)網(wǎng)絡(luò)的深度
# 使用 DenseNet 神經(jīng)網(wǎng)絡(luò)訓(xùn)練 CIFAR10 數(shù)據(jù)集