- 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
- 🍖 原作者:K同学啊
- 本文的数据处理和模型训练和测试部分参考之前的博客:CNN进行天气图片识别, 重点在于学习YOLOv5的c3模块,并且换模型跑通数据集。
一. 模型结构
在YOLOv5中,C3
模块是一个关键组件,通常用来增强特征提取和学习过程。它是YOLOv5架构中的一个模块,广泛用于网络的不同层,尤其是在其主干(Backbone)和头部(Head)部分,旨在提升网络的表达能力。
C3
模块的全名是 CSP Bottleneck ,它是基于 CSPNet (Cross-Stage Partial Network) 提出的概念,结合了深度学习中的 Bottleneck 和 CSPNet 的思想,以提高特征提取的效率并降低计算复杂度。
Bottleneck
是一种在卷积神经网络中常用的层类型,通常包括两个卷积层:一个较大的卷积层用来扩展通道数,然后是一个较小的卷积层,用来将通道数压缩回去。这种结构有助于减少计算量,同时保持模型的表达能力。
CSPNet
提出了"跨阶段部分网络"的概念,即在不同的阶段将特征分成两部分,然后在后续的计算中合并。这一设计能够有效减少计算量,并使模型能够学习到更多不同层次的特征。具体来说,C3
模块通过将输入特征分成两部分,分别通过不同的网络路径处理,再在后续融合特征。
c3
示意图如下:
c3模块
中1*1卷积层的作用:
- 调整通道数,做特征压缩或升维。
- 增加网络的非线性变换能力,增强模型的表达能力。
输入模型前打印数据集参数
python
for X, y in train_dl:
X_demo, y_demo = X, y
break
print(X_demo.shape)
print(y_demo.shape)
# torch.Size([32, 3, 224, 224])
# torch.Size([32])
模型代码
python
import torch.nn.functional as F
def autopad(k, p=None): # kernel, padding
# Pad to 'same'
if p is None:
p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
return p
class Conv(nn.Module):
# Standard convolution
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
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):
# Standard bottleneck
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
super().__init__()
c_ = int(c2 * e) # hidden channels
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):
# CSP Bottleneck with 3 convolutions
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
super().__init__()
c_ = int(c2 * e) # hidden channels
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c1, c_, 1, 1)
self.cv3 = Conv(2 * c_, c2, 1) # act=FReLU(c2)
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)
# C3模块1
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
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using {} device".format(device))
model = model_K().to(device)
model(X_demo.to(device)).shape
二. 运行结果
出现了一些过拟合,由于添加了学习率调整策略,模型收敛得更快了
python
epochs = 20
train_loss = []
train_acc = []
test_loss = []
test_acc = []
loss_fn = nn.CrossEntropyLoss() # 创建损失函数
learn_rate = 1e-3 # 学习率
opt = torch.optim.SGD(model.parameters(),lr=learn_rate)
scheduler = torch.optim.lr_scheduler.StepLR(opt, step_size=5, gamma=0.5)
for epoch in range(epochs):
model.train()
epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, opt, scheduler)
model.eval()
epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn, scheduler)
train_acc.append(epoch_train_acc)
train_loss.append(epoch_train_loss)
test_acc.append(epoch_test_acc)
test_loss.append(epoch_test_loss)
template = ('Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%,Test_loss:{:.3f}')
print(template.format(epoch+1, epoch_train_acc*100, epoch_train_loss, epoch_test_acc*100, epoch_test_loss))
print('Done')