W8,W9yolov5 backbone做目标识别
- 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
- 🍖 原作者:K同学啊 | 接辅导、项目定制
- 🚀 文章来源:K同学的学习圈子
本人电脑配置
Python 3.8.18
Pytorch 2.1.0+cu121
torchvision 0.16.0+cu121
Week8 C3模块
1. 设置GPU/CPU
本次是在gpu上对网络进行训练和测试,先识别设备,判断设备类型。
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
2. 导入数据
本次采用的数据集为K同学提供的天气识别数据集。
import os, PIL, random, pathlib
data_dir = '../weather_photos/'
data_dir = pathlib.Path(data_dir)
data_paths = list(data_dir.glob('*'))
data_paths
classNames = [str(path).split("/")[-1] for path in data_paths]
classNames
data_transforms = transforms.Compose([
transforms.Resize([224, 224]),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std = [0.229, 0.224, 0.225])
])
total_data = datasets.ImageFolder("../weather_photos/", transform=data_transforms)
total_data.class_to_idx
train_size = int(0.8*len(total_data))
len(total_data)
test_size = len(total_data) - train_size
train_dataset, test_dataset = torch.utils.data.random_split(total_data, [train_size, test_size])
batch_size = 4
train_dl = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_dl = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=True)
构建网络模型
1.自行搭建模型
采用的是YOLOV5中C3模块中的内容。
import torch.nn.functional as F
def autopad(k, p=None):
if p is None:
p = k//2 if isinstance(k, int) else [x//2 for x in k]
return p
class Conv(nn.Module):
# standard convolution
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):
super().__init__()
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)
self.bn = nn.BatchNorm2d(c2)
self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
def forward(self, x):
return self.act(self.bn(self.conv(x)))
class Bottleneck(nn.Module):
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5):
super().__init__()
c_ = int(c2*e)
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c_, c2, 3, 1, g=g)
self.add = shortcut and c1==c2
def forward(self, x):
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
class C3(nn.Module):
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
super().__init__()
c_ = int(c2*e)
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c1, c_, 1, 1)
self.cv3 = Conv(2*c_, c2, 1)
self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
def forward(self, x):
return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), dim=1))
class model_K(nn.Module):
def __init__(self):
super(model_K, self).__init__()
self.Conv = Conv(3, 32, 3, 2)
self.C3_1 = C3(32, 64, 3, 2)
self.classifier = nn.Sequential(
nn.Linear(in_features=802816, out_features=100),
nn.ReLU(),
nn.Linear(in_features=100, out_features=4)
)
def forward(self, x):
x = self.Conv(x)
x = self.C3_1(x)
x = torch.flatten(x, start_dim=1)
x = self.classifier(x)
return x
model = model_K().to(device)
model
import torchsummary as summary
summary.summary(model, (3, 224, 224))
得到的模型结构如下
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 32, 112, 112] 864
BatchNorm2d-2 [-1, 32, 112, 112] 64
SiLU-3 [-1, 32, 112, 112] 0
Conv-4 [-1, 32, 112, 112] 0
Conv2d-5 [-1, 32, 112, 112] 1,024
BatchNorm2d-6 [-1, 32, 112, 112] 64
SiLU-7 [-1, 32, 112, 112] 0
Conv-8 [-1, 32, 112, 112] 0
Conv2d-9 [-1, 32, 112, 112] 1,024
BatchNorm2d-10 [-1, 32, 112, 112] 64
SiLU-11 [-1, 32, 112, 112] 0
Conv-12 [-1, 32, 112, 112] 0
Conv2d-13 [-1, 32, 112, 112] 9,216
BatchNorm2d-14 [-1, 32, 112, 112] 64
SiLU-15 [-1, 32, 112, 112] 0
Conv-16 [-1, 32, 112, 112] 0
Bottleneck-17 [-1, 32, 112, 112] 0
Conv2d-18 [-1, 32, 112, 112] 1,024
BatchNorm2d-19 [-1, 32, 112, 112] 64
SiLU-20 [-1, 32, 112, 112] 0
Conv-21 [-1, 32, 112, 112] 0
Conv2d-22 [-1, 32, 112, 112] 9,216
...
Forward/backward pass size (MB): 150.06
Params size (MB): 306.40
Estimated Total Size (MB): 457.04
模型参数量展示。
Forward/backward pass size (MB): 150.06
Params size (MB): 306.40
Estimated Total Size (MB): 457.04
2. 编写训练函数和测试函数
def train(dataloader, model, loss_fn, optimizer):
size = len(dataloader.dataset)
num_batches = len(dataloader)
train_loss, train_acc = 0, 0
for X, y in dataloader: # 获取图片及其标签
X, y = X.to(device), y.to(device)
# 计算预测误差
pred = model(X) # 网络输出
loss = loss_fn(pred, y) # 计算网络输出和真实值之间的差距,targets为真实值,计算二者差值即为损失
# 反向传播
optimizer.zero_grad() # grad属性归零
loss.backward() # 反向传播
optimizer.step() # 每一步自动更新
# 记录acc与loss
train_acc += (pred.argmax(1) == y).type(torch.float).sum().item()
train_loss += loss.item()
train_acc /= size
train_loss /= num_batches
return train_acc, train_loss
# Test
def test(dataloader, model, loss_fn):
size= len(dataloader.dataset)
num_batches = len(dataloader)
test_loss, test_acc = 0, 0
with torch.no_grad():
for imgs, target in dataloader:
imgs, target = imgs.to(device), target.to(device)
# calculate loss
target_pred = model(imgs)
loss = loss_fn(target_pred, target)
test_loss += loss.item()
test_acc += (target_pred.argmax(1)==target).type(torch.float).sum().item()
test_acc /= size
test_loss /= num_batches
return test_acc, test_loss
3. 主函数
设置迭代epoch次数,这里设定为20,并记录训练误差、精度,测试误差、精度。在这里采用了学习率自适应的方式,即学习率随着epoch的增加而逐渐减小。
import copy
optimizer = torch.optim.Adam(model.parameters(), lr = 1e-4)
loss_fn = nn.CrossEntropyLoss()
epochs = 20
train_loss = []
train_acc = []
test_loss = []
test_acc = []
best_acc = 0
for epoch in range(epochs):
model.train()
epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, optimizer)
model.eval()
epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn)
if epoch_test_acc > best_acc:
best_acc = epoch_test_acc
best_model = copy.deepcopy(model)
train_acc.append(epoch_train_acc)
train_loss.append(epoch_train_loss)
test_acc.append(epoch_test_acc)
test_loss.append(epoch_test_loss)
lr = optimizer.state_dict()['param_groups'][0]['lr']
template = ('Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%, Test_loss:{:.3f}, Lr:{:.2E}')
print(template.format(epoch+1, epoch_train_acc*100, epoch_train_loss,
epoch_test_acc*100, epoch_test_loss, lr))
PATH = './best_model.pth'
torch.save(model.state_dict(), PATH)
print('Done')
结果总结
采用C3模块得到的训练精度如下。
每个epoch的训练结果如下所示。
Epoch: 1, Train_acc:68.0%, Train_loss:1.677, Test_acc:81.3%, Test_loss:0.422, Lr:1.00E-04
Epoch: 2, Train_acc:83.3%, Train_loss:0.474, Test_acc:88.4%, Test_loss:0.303, Lr:1.00E-04
Epoch: 3, Train_acc:91.7%, Train_loss:0.254, Test_acc:88.4%, Test_loss:0.299, Lr:1.00E-04
Epoch: 4, Train_acc:94.4%, Train_loss:0.180, Test_acc:89.3%, Test_loss:0.330, Lr:1.00E-04
Epoch: 5, Train_acc:95.4%, Train_loss:0.157, Test_acc:91.6%, Test_loss:0.236, Lr:1.00E-04
Epoch: 6, Train_acc:95.1%, Train_loss:0.191, Test_acc:84.4%, Test_loss:0.575, Lr:1.00E-04
Epoch: 7, Train_acc:95.9%, Train_loss:0.155, Test_acc:87.6%, Test_loss:0.497, Lr:1.00E-04
Epoch: 8, Train_acc:97.0%, Train_loss:0.151, Test_acc:89.3%, Test_loss:0.311, Lr:1.00E-04
Epoch: 9, Train_acc:98.3%, Train_loss:0.057, Test_acc:87.6%, Test_loss:0.504, Lr:1.00E-04
Epoch:10, Train_acc:98.9%, Train_loss:0.034, Test_acc:92.4%, Test_loss:0.278, Lr:1.00E-04
Epoch:11, Train_acc:97.1%, Train_loss:0.110, Test_acc:89.3%, Test_loss:0.323, Lr:1.00E-04
Epoch:12, Train_acc:98.6%, Train_loss:0.047, Test_acc:88.9%, Test_loss:0.448, Lr:1.00E-04
Epoch:13, Train_acc:98.0%, Train_loss:0.063, Test_acc:86.2%, Test_loss:0.958, Lr:1.00E-04
Epoch:14, Train_acc:98.9%, Train_loss:0.045, Test_acc:92.0%, Test_loss:0.439, Lr:1.00E-04
Epoch:15, Train_acc:99.7%, Train_loss:0.008, Test_acc:89.3%, Test_loss:0.794, Lr:1.00E-04
Epoch:16, Train_acc:99.7%, Train_loss:0.005, Test_acc:88.4%, Test_loss:0.430, Lr:1.00E-04
Epoch:17, Train_acc:99.8%, Train_loss:0.006, Test_acc:91.1%, Test_loss:0.437, Lr:1.00E-04
Epoch:18, Train_acc:100.0%, Train_loss:0.002, Test_acc:91.6%, Test_loss:0.414, Lr:1.00E-04
Epoch:19, Train_acc:99.9%, Train_loss:0.004, Test_acc:92.4%, Test_loss:0.457, Lr:1.00E-04
Epoch:20, Train_acc:99.6%, Train_loss:0.006, Test_acc:92.0%, Test_loss:0.383, Lr:1.00E-04
Done
Week9 YOLOv5-Backbone模块实现
参数设置
采用的数据集依然为天气数据集,设置与上文中相同。区别在于网络和主函数略有不同。网络模型如下。
import torch.nn.functional as F
def autopad(k, p=None):
if p is None:
p = k//2 if isinstance(k, int) else [x//2 for x in k]
return p
class Conv(nn.Module):
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):
super().__init__()
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k,p), groups=g, bias=False)
self.bn = nn.BatchNorm2d(c2)
self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
def forward(self, x):
return self.act(self.bn(self.conv(x)))
class Bottleneck(nn.Module):
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5):
super().__init__()
c_ = int(c2*e)
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c_, c2, 3, 1, g=g)
self.add = shortcut and c1==c2
def forward(self, x):
return x + self.cv2(self.cv1(x) if self.add else self.cv2(self.cv1(x)))
class C3(nn.Module):
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
super().__init__()
c_ = int(c2*e)
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c1, c_, 1, 1)
self.cv3 = Conv(2*c_, c2, 1)
self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
def forward(self, x):
return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), dim=1))
class SPPF(nn.Module):
# Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOV5 by Glenn Jocher
def __init__(self, c1, c2, k=1):
super().__init__()
c_ = c1//2
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c_*4, c2, 1, 1)
self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k//2)
def forward(self, x):
x = self.cv1(x)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
y1 = self.m(x)
y2 = self.m(y1)
return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1))
class YOLOv5_backbone(nn.Module):
def __init__(self):
super(YOLOv5_backbone, self).__init__()
self.Conv_1 = Conv(3, 64, 3, 2, 2)
self.Conv_2 = Conv(64, 128, 3, 2)
self.C3_3 = C3(128, 128)
self.Conv_4 = Conv(128, 256, 3, 2)
self.C3_5 = C3(256, 256)
self.Conv_6 = Conv(256, 512, 3, 2)
self.C3_7 = C3(512, 512)
self.Conv_8 = Conv(512, 1024, 3, 2)
self.C3_9 = C3(1024, 1024)
self.SPPF = SPPF(1024, 1024, 5)
self.classifer = nn.Sequential(
nn.Linear(in_features=65536, out_features=100),
nn.ReLU(),
nn.Linear(in_features=100, out_features=4)
)
def forward(self, x):
x = self.Conv_1(x)
x = self.Conv_2(x)
x = self.C3_3(x)
x = self.Conv_4(x)
x = self.C3_5(x)
x = self.Conv_6(x)
x = self.C3_7(x)
x = self.Conv_8(x)
x = self.C3_9(x)
x = self.SPPF(x)
x = torch.flatten(x, start_dim=1)
x = self.classifer(x)
return x
模型结构和参数如下。
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 113, 113] 1,728
BatchNorm2d-2 [-1, 64, 113, 113] 128
SiLU-3 [-1, 64, 113, 113] 0
Conv-4 [-1, 64, 113, 113] 0
Conv2d-5 [-1, 128, 57, 57] 73,728
BatchNorm2d-6 [-1, 128, 57, 57] 256
SiLU-7 [-1, 128, 57, 57] 0
Conv-8 [-1, 128, 57, 57] 0
Conv2d-9 [-1, 64, 57, 57] 8,192
BatchNorm2d-10 [-1, 64, 57, 57] 128
SiLU-11 [-1, 64, 57, 57] 0
Conv-12 [-1, 64, 57, 57] 0
Conv2d-13 [-1, 64, 57, 57] 4,096
BatchNorm2d-14 [-1, 64, 57, 57] 128
SiLU-15 [-1, 64, 57, 57] 0
Conv-16 [-1, 64, 57, 57] 0
Conv2d-17 [-1, 64, 57, 57] 36,864
BatchNorm2d-18 [-1, 64, 57, 57] 128
SiLU-19 [-1, 64, 57, 57] 0
Conv-20 [-1, 64, 57, 57] 0
Bottleneck-21 [-1, 64, 57, 57] 0
Conv2d-22 [-1, 64, 57, 57] 8,192
...
Forward/backward pass size (MB): 137.59
Params size (MB): 82.89
Estimated Total Size (MB): 221.06
----------------------------------------------------------------
主函数
import copy
optimizer = torch.optim.Adam(model.parameters(), lr= 1e-4)
loss_fn = nn.CrossEntropyLoss() # 创建损失函数
epochs = 60
train_loss = []
train_acc = []
test_loss = []
test_acc = []
best_acc = 0 # 设置一个最佳准确率,作为最佳模型的判别指标
for epoch in range(epochs):
model.train()
epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, optimizer)
model.eval()
epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn)
# 保存最佳模型到 best_model
if epoch_test_acc > best_acc:
best_acc = epoch_test_acc
best_model = copy.deepcopy(model)
train_acc.append(epoch_train_acc)
train_loss.append(epoch_train_loss)
test_acc.append(epoch_test_acc)
test_loss.append(epoch_test_loss)
# 获取当前的学习率
lr = optimizer.state_dict()['param_groups'][0]['lr']
template = ('Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%, Test_loss:{:.3f}, Lr:{:.2E}')
print(template.format(epoch+1, epoch_train_acc*100, epoch_train_loss,
epoch_test_acc*100, epoch_test_loss, lr))
# 保存最佳模型到文件中
PATH = './best_model.pth' # 保存的参数文件名
torch.save(best_model.state_dict(), PATH)
print('Done')
训练结果如下。
Epoch: 1, Train_acc:51.7%, Train_loss:1.163, Test_acc:71.1%, Test_loss:0.979, Lr:1.00E-04
Epoch: 2, Train_acc:68.9%, Train_loss:0.796, Test_acc:74.2%, Test_loss:0.627, Lr:1.00E-04
Epoch: 3, Train_acc:75.3%, Train_loss:0.627, Test_acc:76.0%, Test_loss:0.649, Lr:1.00E-04
Epoch: 4, Train_acc:80.6%, Train_loss:0.534, Test_acc:84.9%, Test_loss:0.421, Lr:1.00E-04
Epoch: 5, Train_acc:81.2%, Train_loss:0.499, Test_acc:90.2%, Test_loss:0.284, Lr:1.00E-04
Epoch: 6, Train_acc:82.6%, Train_loss:0.433, Test_acc:84.4%, Test_loss:0.542, Lr:1.00E-04
Epoch: 7, Train_acc:85.0%, Train_loss:0.402, Test_acc:84.0%, Test_loss:0.436, Lr:1.00E-04
Epoch: 8, Train_acc:85.6%, Train_loss:0.390, Test_acc:80.4%, Test_loss:0.514, Lr:1.00E-04
Epoch: 9, Train_acc:86.6%, Train_loss:0.355, Test_acc:92.4%, Test_loss:0.264, Lr:1.00E-04
Epoch:10, Train_acc:88.1%, Train_loss:0.336, Test_acc:86.7%, Test_loss:0.333, Lr:1.00E-04
Epoch:11, Train_acc:90.7%, Train_loss:0.248, Test_acc:85.3%, Test_loss:0.336, Lr:1.00E-04
Epoch:12, Train_acc:90.8%, Train_loss:0.265, Test_acc:84.4%, Test_loss:0.424, Lr:1.00E-04
Epoch:13, Train_acc:90.2%, Train_loss:0.258, Test_acc:90.2%, Test_loss:0.291, Lr:1.00E-04
Epoch:14, Train_acc:92.3%, Train_loss:0.227, Test_acc:86.7%, Test_loss:0.312, Lr:1.00E-04
Epoch:15, Train_acc:91.6%, Train_loss:0.232, Test_acc:84.4%, Test_loss:0.398, Lr:1.00E-04
Epoch:16, Train_acc:92.9%, Train_loss:0.195, Test_acc:91.1%, Test_loss:0.262, Lr:1.00E-04
Epoch:17, Train_acc:93.4%, Train_loss:0.187, Test_acc:93.3%, Test_loss:0.230, Lr:1.00E-04
Epoch:18, Train_acc:95.9%, Train_loss:0.119, Test_acc:89.3%, Test_loss:0.405, Lr:1.00E-04
Epoch:19, Train_acc:94.0%, Train_loss:0.175, Test_acc:89.3%, Test_loss:0.316, Lr:1.00E-04
Epoch:20, Train_acc:95.9%, Train_loss:0.114, Test_acc:90.2%, Test_loss:0.331, Lr:1.00E-04
Epoch:21, Train_acc:94.9%, Train_loss:0.149, Test_acc:86.7%, Test_loss:0.364, Lr:1.00E-04
Epoch:22, Train_acc:94.4%, Train_loss:0.138, Test_acc:90.2%, Test_loss:0.298, Lr:1.00E-04
Epoch:23, Train_acc:94.7%, Train_loss:0.145, Test_acc:91.1%, Test_loss:0.266, Lr:1.00E-04
Epoch:24, Train_acc:97.3%, Train_loss:0.058, Test_acc:91.6%, Test_loss:0.270, Lr:1.00E-04
Epoch:25, Train_acc:96.2%, Train_loss:0.115, Test_acc:88.9%, Test_loss:0.471, Lr:1.00E-04
...
Epoch:58, Train_acc:98.6%, Train_loss:0.037, Test_acc:91.1%, Test_loss:0.471, Lr:1.00E-04
Epoch:59, Train_acc:98.3%, Train_loss:0.031, Test_acc:87.6%, Test_loss:0.443, Lr:1.00E-04
Epoch:60, Train_acc:98.9%, Train_loss:0.035, Test_acc:94.7%, Test_loss:0.326, Lr:1.00E-04
Done