我做了一个最简单的物体检测任务和一个模型来研究物体检测。
完整代码可在 https://github.com/takbess/MySimplestObjectDetector/blob/main/object_detection.ipynb
获取任务是预测一个人的边界框。每张图像仅包含一个人。 图像示例如下。 我从 MSCOCO 数据集制作了数据集。我选择了仅包含一个人的图像,然后将其大小调整为(32,32)。我有 1k 张图像。
SimpleObjectDetector类制作如下:
class ConvLayer(nn.Module):
def __init__(self,in_channel,out_channel):
super().__init__()
self.conv = nn.Conv2d(in_channel,out_channel,kernel_size=3,stride=1,padding=1)
self.act = nn.ReLU()
self.pool = nn.MaxPool2d(2,2)
def forward(self,x):
x = self.pool(self.act(self.conv(x)))
return x
class SimpleObjectDetector(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = ConvLayer(3,8)
self.conv2 = ConvLayer(8,16)
self.l1 = nn.Linear(8*8*16,64)
self.l2 = nn.Linear(64,4)
self.act = nn.ReLU()
def forward(self,x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size()[0],-1)
x = self.act(self.l1(x))
x = self.l2(x) * 32 + 16
return x # (x,y,w,h) is expected.
x = self.l2(x) * 32 + 16 是为了让输出分布符合图像尺寸(32,32)。
我尝试了损失函数nn.L1Loss()和IoU损失。 在 1~3 epoch 时,损失停止减少。
我进一步研究了这个问题,发现即使在训练之前,所有图像的边界框预测也变得相同! 看起来最终输出的自由度很低。
有什么方法可以让这个模型正确预测边界框吗? 我想尝试在不使用 YOLO 等较大模型的情况下预测边界框。
仅模型结构无法提供整个训练流程的信息。
模型和培训
结果和额外研究
问题