CIFAR10數(shù)據(jù)集來(lái)源:torchvision.datasets.CIFAR10()
CIFAR10數(shù)據(jù)集是一個(gè)用于識(shí)別普適物體的小型數(shù)據(jù)集锰什,一共包含10個(gè)類別的RGB彩色圖片下硕,圖片尺寸大小為32x32,如圖:
相較于MNIST數(shù)據(jù)集汁胆,MNIST數(shù)據(jù)集是28x28的單通道灰度圖梭姓,而CIFAR10數(shù)據(jù)集是32x32的RGB三通道彩色圖,CIFAR10數(shù)據(jù)集更接近于真實(shí)世界的圖片嫩码。
ResNet網(wǎng)絡(luò)模型:
本文采用ResNet18來(lái)構(gòu)建深度網(wǎng)絡(luò)模型:
SeNet:Squeeze-and-Excitation的縮寫(xiě)誉尖,特征壓縮與激發(fā),基于通道注意力
1.Squeeze-and-Excitation(SE) block 并不是一個(gè)完整的網(wǎng)絡(luò)結(jié)構(gòu)谢谦,而是一個(gè)子結(jié)構(gòu)释牺,可以嵌到其他分類或檢測(cè)模型中
2.SENet網(wǎng)絡(luò)的創(chuàng)新點(diǎn)在于關(guān)注 channel 之間的關(guān)系,希望模型可以自動(dòng)學(xué)習(xí)到不同 channel 特征的重要程度
3.本質(zhì)上回挽,SeNet基于注意力機(jī)制給與每個(gè)通道不同的權(quán)重没咙,SE模塊是在 channel 維度上做 attention
具體操作過(guò)程如下:
對(duì)于每一輸出通道,先 global average pool千劈,每個(gè)通道得到 1個(gè)標(biāo)量祭刚,C個(gè)通道得到C個(gè)數(shù),
然后經(jīng)過(guò) FC(in,in/16)-ReLU-FC(in,in/16)-Sigmoid 得到 C個(gè)0~1 之間的標(biāo)量墙牌,作為通道的加權(quán)涡驮,
然后原來(lái)的輸出通道每個(gè)通道用對(duì)應(yīng)的權(quán)重進(jìn)行加權(quán)(對(duì)應(yīng)通道的每個(gè)元素與權(quán)重分別相乘),得到新的加權(quán)后的特征喜滨,作者稱為 feature recalibration
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)
# transforms.RandomCrop(32, padding=4)---填充到40*40后虽风,再隨機(jī)裁剪成32*32
transform_train = transforms.Compose([transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
# 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((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
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=128, shuffle=True, num_workers=2)
test_loader = DataLoader(test_dataset, batch_size=128, shuffle=False, num_workers=2)
2.構(gòu)建SE-ResNet 網(wǎng)絡(luò)模型棒口,最后接Softmax來(lái)處理output
1)構(gòu)建 SE-Block單元
# SE-Block單元--SEblock是一個(gè)子結(jié)構(gòu),幾乎可以嵌入任何一個(gè)神經(jīng)網(wǎng)絡(luò)模型之中
class SE_Block(nn.Module):
def __init__(self, input_channel, reduction=16):
super(SE_Block, self).__init__()
self.adaptive_avg_pool = nn.AdaptiveAvgPool2d(1) # 全局自適應(yīng)池化
self.fc = nn.Sequential(
nn.Linear(input_channel, input_channel // reduction),
nn.ReLU(inplace=True),
nn.Linear(input_channel // reduction, input_channel),
nn.Sigmoid()
)
def forward(self, x):
b, c, h, w = x.size()
# squeeze操作:(b,c,h,w)->(b,c)
y = self.adaptive_avg_pool(x).view(b, c)
# FC獲取通道注意力權(quán)重辜膝,是具有全局信息的
y = self.fc(y).view(b, c, 1, 1)
# 注意力作用每一個(gè)通道上
y = x * y.expand_as(x)
# 殘差連接
return x+y
2)ResNet18_BasicBlock-殘差單元
# 構(gòu)建 VGGNet18 網(wǎng)絡(luò)模型
# 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)維度一致才能進(jìn)行殘差連接
if self.use_conv1_1:
out = self.extra(x) + out
out = self.relu(out)
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.完整代碼
# -*- codeing = utf-8 -*-
# @Time : 2022/6/14 13:09
# @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的概率對(duì)圖片做水平橫向翻轉(zhuǎn)
# transforms.RandomCrop(32, padding=4)---填充到40*40后,再隨機(jī)裁剪成32*32
transform_train = transforms.Compose([transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
# 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((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))])
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=128, shuffle=True, num_workers=2)
test_loader = DataLoader(test_dataset, batch_size=128, shuffle=False, num_workers=2)
# SE-Block單元--SEblock是一個(gè)子結(jié)構(gòu)厂抖,幾乎可以嵌入任何一個(gè)神經(jīng)網(wǎng)絡(luò)模型之中
class SE_Block(nn.Module):
def __init__(self, input_channel, reduction=16):
super(SE_Block, self).__init__()
self.adaptive_avg_pool = nn.AdaptiveAvgPool2d(1) # 全局自適應(yīng)池化
self.fc = nn.Sequential(
nn.Linear(input_channel, input_channel // reduction),
nn.ReLU(inplace=True),
nn.Linear(input_channel // reduction, input_channel),
nn.Sigmoid()
)
def forward(self, x):
b, c, h, w = x.size()
# squeeze操作:(b,c,h,w)->(b,c)
y = self.adaptive_avg_pool(x).view(b, c)
# FC獲取通道注意力權(quán)重茎毁,是具有全局信息的
y = self.fc(y).view(b, c, 1, 1)
# 注意力作用每一個(gè)通道上
y = x * y.expand_as(x)
# 殘差連接
return x+y
# 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)維度一致才能進(jìn)行殘差連接
if self.use_conv1_1:
out = self.extra(x) + out
out = self.relu(out)
return out
# 構(gòu)建 ResNet18 網(wǎng)絡(luò)模型
class ResNet18(nn.Module):
def __init__(self):
super(ResNet18, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(64),
# nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
nn.ReLU(inplace=True)
)
self.block1_1 = ResNet18_BasicBlock(input_channel=64, output_channel=64, stride=1, use_conv1_1=False)
self.se_block64 = SE_Block(64)
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.se_block128 = SE_Block(128)
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.se_block256 = SE_Block(256)
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.se_block512 = SE_Block(512)
self.block4_2 = ResNet18_BasicBlock(input_channel=512, output_channel=512, stride=1, use_conv1_1=False)
# 全連接層
self.FC_layer = nn.Sequential(
nn.Linear(512, 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)
)
self.adaptive_avg_pool2d = nn.AdaptiveAvgPool2d((1, 1))
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.conv1(x)
# ResNet18-網(wǎng)絡(luò)模型
x = self.block1_1(x)
x = self.se_block64(x)
x = self.block1_2(x)
x = self.block2_1(x)
x = self.se_block128(x)
x = self.block2_2(x)
x = self.block3_1(x)
x = self.se_block256(x)
x = self.block3_2(x)
x = self.block4_1(x)
x = self.se_block512(x)
x = self.block4_2(x)
# 平均值池化
x = self.adaptive_avg_pool2d(x)
# 數(shù)據(jù)平坦化處理七蜘,為接下來(lái)的全連接層做準(zhǔn)備
x = x.view(x.size(0), -1)
x = self.FC_layer(x)
return x
# 初始化模型
model = ResNet18().to(device)
# 構(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)
loss_list = []
# train
def train(epoch):
model.train()
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())
# 每一百個(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()
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('./SeResNet_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()
# SeResNet: 采用se_block單元
# 使用 SeResNet 的神經(jīng)網(wǎng)絡(luò)訓(xùn)練 CIFAR10 數(shù)據(jù)集