YOLOv1代码复现(论文复现)
论文介绍
主要内容
实验部分
卷积网络结构
计算损失
核心代码
class ResNet(nn.Module):
def __init__(self, block, layers):
super(ResNet, self).__init__()
# 通道数64
self.inplanes = 64
# 卷积层和池化层
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
# block块
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
# output_block
self.layer5 = self._make_out_layer(in_channels=2048)
# 将输出变为30个通道数 7*7*30
self.avgpool = nn.AvgPool2d(2) # kernel_size = 2 , stride = 2
self.conv_end = nn.Conv2d(256, 30, kernel_size=3, stride=1, padding=1, bias=False)
self.bn_end = nn.BatchNorm2d(30)
# 参数初始化
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1):
def _make_out_layer(self, in_channels):
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)
x = self.layer5(x)
x = self.avgpool(x)
x = self.conv_end(x)
x = self.bn_end(x)
x = F.sigmoid(x) # sigmoid归一化到0-1
# 改代码只要保证最后是7,7,30就行
x = x.permute(0, 2, 3, 1) # (-1,7,7,30)
return x
缺点