前言
?ResNet 網(wǎng)絡是在 2015年 由微軟實驗室提出,斬獲當年ImageNet競賽中分類任務第一名,目標檢測第一名。獲得COCO數(shù)據(jù)集中目標檢測第一名,圖像分割第一名。
原論文地址:Deep Residual Learning for Image Recognition(作者是CV大佬何凱明團隊)
ResNet創(chuàng)新點介紹
在ResNet網(wǎng)絡中創(chuàng)新點:
- 提出 Residual 結構(殘差結構),并搭建超深的網(wǎng)絡結構(可突破1000層)
- 使用 Batch Normalization 加速訓練(丟棄dropout)
1、什么是殘差網(wǎng)絡?
殘差網(wǎng)絡讓非線形層滿足
F
(
x
)
F(x)
F(x) ,然后從輸入直接引入一個短連接到非線形層的輸出上,使得整個映射變?yōu)?
y
=
F
(
x
)
+
x
y= F(x) + x
y=F(x)+x.
一個殘差模塊的定義如下圖:
殘差塊分為兩種:BasicBlock結構和Bottleneck結構;
2、殘差塊中 shortcut 的介紹
? 有些殘差塊的 short cut 是實線的,而有些則是虛線的;這些虛線的 short cut 上通過1×1的卷積核對輸入?yún)?shù)進行了維度處理,來保證主分支與 short cut 的輸出維度相同;
-
BasicBlock殘差結構的shotcut
-
Bottleneck殘差結構的shotcut
3、傳統(tǒng)神經(jīng)網(wǎng)絡相對于殘差網(wǎng)絡的問題:
?在根據(jù)損失函數(shù)計算的誤差通過梯度反向傳播的方式對深度網(wǎng)絡權值進行更新時,得到的梯度值接近0或特別大,也就是梯度消失或爆炸
- 梯度消失或梯度爆炸問題
- 梯度消失:若每一層的誤差梯度小于1,反向傳播時,網(wǎng)絡越深,梯度越趨近于0;
- 梯度爆炸:若每一層的誤差梯度大于1,反向傳播時,網(wǎng)路越深,梯度越來越大;
解決方案:在網(wǎng)絡中使用 BN(Batch Normalization)層來解決;
- 深層中的網(wǎng)絡退化問題
解決方案:引入殘差網(wǎng)絡。可以人為地讓神經(jīng)網(wǎng)絡某些層跳過下一層神經(jīng)元的連接,隔層相連,弱化每層之間的強聯(lián)系。
BasicBlock結構
1、BasicBlock結構適用于淺層網(wǎng)絡;
上圖BasicBlock殘差結構所需的參數(shù)為:
3 × 3 × 64 × 64 + 3 × 3 × 64× 64 = 73728
Bottleneck結構
1、Bottleneck結構適用于深層網(wǎng)絡;其中1×1的卷積核起到降維和升維的作用,同時可以大大減少網(wǎng)絡參數(shù)。
上圖Bottleneck殘差結構所需的參數(shù)為:
1 × 1 × 256× 64 + 3 × 3 × 64× 64 + 1×1 ×64×256= 69632
Batch Normalization介紹
BN計算過程
BN作用
- 抑制梯度消失和梯度爆炸
- 加快收斂速度
- 防止過擬合
ResNet介紹
?下圖是原論文給出的不同深度的ResNet網(wǎng)絡結構配置,注意表中的殘差結構給出了主分支上卷積核的大小與卷積核個數(shù),表中 殘差塊×N 表示將該殘差結構重復N次。
程序的介紹
? 以下程序中model.py為為ResNet模型;train.py為訓練腳本;train_tool.py為一個類,訓練時用到的函數(shù),供train.py調(diào)用;為預測腳本,應用訓練出的模型進行預測。
model.py
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchinfo import summary
class BasicBlock(nn.Module):
expansion = 1 #殘差網(wǎng)絡中,主分支的卷積核個數(shù)是否發(fā)生變化,不變?yōu)?
def __init__(self, in_channels, out_channels, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3,
stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU()
self.conv2 = nn.Conv2d(in_channels=out_channels, out_channels=out_channels,kernel_size=3,
stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(out_channels)
self.downsample = downsample
def forward(self, x):
identity = x
if self.downsample is not None: #虛線殘差結構,需要下采樣
identity = self.downsample(x)
x = self.bn1(self.conv1(x))
x = self.relu(x)
x = self.relu(self.conv2(x))
x = x + identity
x = self.relu(x)
return x
class Bottleneck(nn.Module):
expansion = 4 #殘差結構中第三層卷積核個數(shù)是第一、二層卷積核個數(shù)的四倍
def __init__(self, in_channels, out_channels,stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1,
stride=1, bias=False)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3,
stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(out_channels)
self.conv3 = nn.Conv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=1,
stride=1, bias=False)
self.bn3 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU()
self.downsample = downsample
def forward(self, x):
identity = x
if self.downsample is not None:
identity = self.downsample(x)
x = self.bn1(self.conv1(x))
x = self.relu(x)
x = self.bn2(self.conv2(x))
x = self.relu(x)
x = self.bn2(self.conv3(x))
x = x + identity
x = self.relu(x)
return x
class ResNet(nn.Module):
def __init__(self, block, blocks_num, num_classes=5, include_top=True):
super(ResNet, self).__init__()
self.include_top = include_top
self.in_channel = 64
self.conv1 = nn.Conv2d(in_channels=3, out_channels=self.in_channel, kernel_size=7, stride=2,
padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(self.in_channel)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, blocks_num[0])
self.layer2 = self._make_layer(block, 128, blocks_num[1], stride=2)
self.layer3 = self._make_layer(block, 256, blocks_num[2], stride=2)
self.layer4 = self._make_layer(block, 512, blocks_num[3], stride=2)
if self.include_top:
self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) #output size = (1, 1)
self.fc = nn.Linear(512 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
if self.include_top:
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.fc(x)
return x
# channel為殘差結構中第一層卷積核個數(shù)
def _make_layer(self, block, channel, block_num, stride=1):
"""
:param block: BasicBlock或者Bottleneck
:param channel:
:param block_num:
:param stride:
:return:
"""
downsample = None
if stride != 1 or self.in_channel != channel * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.in_channel, channel * block.expansion, kernel_size=1,
stride=stride, bias=False),
nn.BatchNorm2d(channel * block.expansion)
)
layers = []
layers.append(block(self.in_channel, channel, downsample=downsample, stride=stride))
self.in_channel = channel * block.expansion
for _ in range(1, block_num):
layers.append(block(self.in_channel, channel))
return nn.Sequential(*layers)
#定義34層殘差網(wǎng)絡
def resnet34(num_classes=1000, include_top=True):
return ResNet(BasicBlock, [3, 4, 6, 3], num_classes=num_classes, include_top=include_top)
#定義101層殘差網(wǎng)絡
def resnet101(num_classes=1000, include_top=True):
return ResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes, include_top=include_top)
train.py
把數(shù)據(jù)類別和對應索引寫到.json文件中
#--------------------------------------------------------#
# 把類別數(shù)據(jù)寫到.json文件中
#--------------------------------------------------------#
# 字典,類別:索引 {'daisy':0, 'dandelion':1, 'roses':2, 'sunflower':3, 'tulips':4}
flower_list = train_dataset.class_to_idx
# 將 flower_list 中的 key 和 val 調(diào)換位置
cla_dict = dict((val, key) for key, val in flower_list.items())
# 將 cla_dict 寫入 json 文件中
json_str = json.dumps(cla_dict, indent=4)
with open('class_indices.json', 'w') as json_file:
json_file.write(json_str)
train.py
import torch
import torch.nn as nn
from torchvision import transforms, datasets
import json
import os
from model import ResNet, BasicBlock, Bottleneck, resnet34
import torch.optim as optim
from train_tool import TrainTool
import torchvision.models.resnet
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#--------------------------------------------------------#
# 數(shù)據(jù)預處理
#--------------------------------------------------------#
transform = {
"train": transforms.Compose([transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])]),
"test": transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])])
}
#--------------------------------------------------------#
# 獲取數(shù)據(jù)的路徑
#--------------------------------------------------------#
data_root = os.getcwd()
image_path = data_root + "/data/"
print(image_path)
# 導入訓練集并進行處理
train_dataset = datasets.ImageFolder(root=image_path + "/train",
transform=transform["train"])
#加載訓練集
train_loader = torch.utils.data.DataLoader(train_dataset, #導入的訓練集
batch_size=16, #每批訓練樣本個數(shù)
shuffle=True, #打亂訓練集
num_workers=0) #使用線程數(shù)
#導入測試集并進行處理
test_dataset = datasets.ImageFolder(root=image_path + "/test",
transform=transform["test"])
test_num = len(test_dataset)
#加載測試集
test_loader = torch.utils.data.DataLoader(test_dataset, #導入的測試集
batch_size=16, #每批測試樣本個數(shù)
shuffle=True, #打亂測試集
num_workers=0) #使用線程數(shù)
#--------------------------------------------------------#
# 網(wǎng)絡模型實例化
#--------------------------------------------------------#
resnet = resnet34()
# load pretrain weights
model_weight_path = "./pre_model/resnet34-b627a593.pth" #記載預訓練模型(比從頭開始訓練要快)
missing_keys, unexpected_keys = resnet.load_state_dict(torch.load(model_weight_path), strict=False)
inchannel = resnet.fc.in_features
resnet.fc = nn.Linear(inchannel, 5) #預訓練模型的輸出類別為1000,改為5
resnet.to(device)
#--------------------------------------------------------#
# 設置損失函數(shù)和優(yōu)化器
#--------------------------------------------------------#
loss_function = nn.CrossEntropyLoss()
optimizer = optim.Adam(resnet.parameters(), lr=0.0001)
#--------------------------------------------------------#
# 開始訓練
#--------------------------------------------------------#
#正式訓練
train_acc = []
train_loss = []
test_acc = []
test_loss = []
epoch = 0
#for epoch in range(epochs):
while True:
epoch = epoch + 1;
resnet.train()
epoch_train_acc, epoch_train_loss = TrainTool.train(train_loader, resnet, optimizer, loss_function, device)
resnet.eval()
epoch_test_acc, epoch_test_loss = TrainTool.test(test_loader,resnet, loss_function,device)
if epoch_train_acc < 0.93 and epoch_test_acc < 0.94:
template = ('Epoch:{:2d}, train_acc:{:.1f}%, train_loss:{:.2f}, test_acc:{:.1f}%, test_loss:{:.2f}')
print(template.format(epoch, epoch_train_acc * 100, epoch_train_loss, epoch_test_acc * 100, epoch_test_loss))
continue
else:
torch.save(resnet.state_dict(),'./model/resnet34_params.pth')
print('Done')
break
train_tool.py
import torch
import matplotlib.pyplot as plt
class TrainTool:
def image_show(images):
print(images.shape)
images = images.numpy() #將圖片有tensor轉換成array
print(images.shape)
images = images.transpose((1, 2, 0)) # 將【3,224,256】-->【224,256,3】
# std = [0.5, 0.5, 0.5]
# mean = [0.5, 0.5, 0.5]
# images = images * std + mean
print(images.shape)
plt.imshow(images)
plt.show()
def train(train_loader, model, optimizer, loss_function, device):
train_size = len(train_loader.dataset) # 訓練集的大小
num_batches = len(train_loader) # 批次數(shù)目
train_acc, train_loss = 0, 0 # 初始化正確率和損失
# 獲取圖片及標簽
for images, labels in train_loader:
images, labels = images.to(device), labels.to(device)
# 計算預測誤差
pred_labels = model(images)
loss = loss_function(pred_labels, labels)
# 反向傳播
optimizer.zero_grad() # grad屬性歸零
loss.backward() # 反向傳播
optimizer.step() # 每一步自動更新
# 記錄acc和loss
train_acc += (pred_labels.argmax(1) == labels).type(torch.float).sum().item()
train_loss += loss.item()
train_acc /= train_size
train_loss /= num_batches
return train_acc, train_loss
def test(test_loader, model, loss_function, device):
test_size = len(test_loader.dataset) # 測試集的大小,一共10000張圖片
num_batches = len(test_loader) # 批次數(shù)目,313(10000/32=312.5,向上取整)
test_loss, test_acc = 0, 0
# 當不進行訓練時,停止梯度更新,節(jié)省計算內(nèi)存消耗
with torch.no_grad():
for images, labels in test_loader:
images, labels = images.to(device), labels.to(device)
# 計算loss
pred_labels = model(images)
loss = loss_function(pred_labels, labels)
test_acc += (pred_labels.argmax(1) == labels).type(torch.float).sum().item()
test_loss += loss.item()
test_acc /= test_size
test_loss /= num_batches
return test_acc, test_loss
predict.py預測腳本參照:圖像分類:Pytorch圖像分類之–VGG模型文章中預測腳本文章來源:http://www.zghlxwxcb.cn/news/detail-410360.html
如有錯誤歡迎指正,如果幫到您請點贊加關注哦!文章來源地址http://www.zghlxwxcb.cn/news/detail-410360.html
到了這里,關于圖像分類:Pytorch圖像分類之--ResNet模型的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!