个人总结的,不详细,建议大家多看论文。
DETR
End-to-End Object Detection with Transformers
Given a fixed small set of learned object queries, DETR reasons about the relations of the objects and the global image context to directly output the final set of predictions in parallel
DETR的思路是将backbone输出特征图的像素展开成一维后当成了序列长度,而batch和channel的定义不变。故而DETR可以计算特征图的每一个像素相对于其他所有像素的相关性,这一点在CNN中是依靠感受野来实现的,可以看出Transformer能够捕获到比CNN更大的感受范围
DETR基于transformer框架,合并了set-based 匈牙利算法,通过二分图匹配,强制每一个gt都有唯一的预测结果
模型流程
模型架构
encoder
分离了实例,这可能简化了解码器的对象提取和定位。
encoder最后一层的可视化结果
decoder
在编码器通过全局注意分离出实例后,解码器只需要注意到末端就可以提取出类和对象的边界。
object queries
损失计算
Our loss produces an optimal bipartite matching between predicted and ground truth objects, and
then optimize object-specific (bounding box) losses.
损失计算包括类别和检测框
To find a bipartite matching between these two sets we search for a permutation of N
elements [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xsr8ojmu-1666781110544)(C:\SelfFile\笔记\论文\detr\dertr.assets\image-20221022095711257.png)] with the lowest cost:
is a pair-wise matching cost between ground truth
y
i
y_{i}
yi and a prediction with index σ(i). 使用Hungarian algorithm进行一对一匹配。
损失函数
对比实验
特点
- 大物体表现更好,小物体性能较差
- 和寻常的transformer模型一样,收敛速度慢
论文中的思路代码
import torch
from torch import nn
from torchvision.models import resnet50
class DETR(nn.Module):
def __init__(self, num_classes, hidden_dim, nheads,
num_encoder_layers, num_decoder_layers):
super().__init__()
# We take only convolutional layers from ResNet-50 model
self.backbone = nn.Sequential(*list(resnet50(pretrained=True).children())[:-2])
self.conv = nn.Conv2d(2048, hidden_dim, 1)
self.transformer = nn.Transformer(hidden_dim, nheads,
num_encoder_layers, num_decoder_layers)
self.linear_class = nn.Linear(hidden_dim, num_classes + 1)
self.linear_bbox = nn.Linear(hidden_dim, 4)
self.query_pos = nn.Parameter(torch.rand(100, hidden_dim))
self.row_embed = nn.Parameter(torch.rand(50, hidden_dim // 2))
self.col_embed = nn.Parameter(torch.rand(50, hidden_dim // 2))
def forward(self, inputs):
x = self.backbone(inputs)
h = self.conv(x)
H, W = h.shape[-2:]
pos = torch.cat([
self.col_embed[:W].unsqueeze(0).repeat(H, 1, 1),
self.row_embed[:H].unsqueeze(1).repeat(1, W, 1),
], dim=-1).flatten(0, 1).unsqueeze(1)
h = self.transformer(pos + h.flatten(2).permute(2, 0, 1),
self.query_pos.unsqueeze(1))
return self.linear_class(h), self.linear_bbox(h).sigmoid()
detr = DETR(num_classes=91, hidden_dim=256, nheads=8, num_encoder_layers=6, num_decoder_layers=6)
detr.eval()
inputs = torch.randn(1, 3, 800, 1200)
logits, bboxes = detr(inputs)
print(logits)
DDETR
DEFORMABLE DETR: DEFORMABLE TRANSFORMERS FOR END-TO-END OBJECT DETECTION
By replacing Transformer attention modules with deformable attention modules in DETR, we establish an efficient and fast converging detection system, dubbed as Deformable DETR.
贡献
DEFORMABLE DETR是一种端到端目标检测器,高效、快速收敛。它使我们能够探索更有趣和实用的端到端对象探测器的变体。可变形DETR的核心是(多尺度)可变形注意模块,这是处理图像特征图的一种有效的注意机制。
引入ddetr的原因
-
detr训练时间极长
-
DETR在小物体检测上性能较差
- 现存的检测器通常带有多尺度的特征,小物体目标通常在高分辨率特征图上检测,而DETR没有采用多尺度特征来检测,主要是高分辨率的特征图会对DETR增加不可接受的计算复杂度
ddetr解决方式
- 缓解DETR的缓慢收敛和高复杂性的问题。
- 只关注参考周围的一小部分关键采样点
- 提出的多尺度可变形注意模块可以通过注意机制自然地聚合多尺度特征图,而无需借助特征金字塔网络
ddetr设计灵感
It combines the best of the sparse spatial sampling of deformable convolution, and the relation modeling capability of Transformers
- 结合了可变性卷积和transformers
- 可变性卷积的稀疏空间采样
- 可变性卷积的稀疏空间采样
- transformer的关系建模能力
Deformable Attention Module
对每一个query,之前关注所有的空间位置(所有位置作为key),现在只关注更有意义的、网络认为更包含局部信息的位置(少且固定数量位置作为key),缓解特征图大带来大运算量的问题。
实施过程中, z q z_{q} zq(特征图) 输入给一个线性映射,输出3MK个通道,前2MK个通道编码采样的offset,决定每一个query应该找哪些key,最后MK个通道,输出keys的贡献(不再用k×q计算,直接输入q回归),且只对找到的keys的贡献进行归一化
Multi-scale Deformable Attention Module
将deformable attention module扩展为多尺度feature map,主要解小目标问题
相当于单层版本的扩展,对于一个query,每一层采集K个点作为keys,转换成,对一个query,所有层均采K个点,融合了不同层的特征,故不需要FPN
模块设计
Deformable Transformer Encoder
多尺度可变形注意模块
multi-scale feature maps(多尺度特征图)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pLtZgiWS-1666781110545)(https://cdn.jsdelivr.net/gh/cxy-sky/jat-blog-img/image-20221022105207254.png)]
Deformable Transformer Decoder.
Since our proposed deformable attention module is designed for processing convolutional feature maps as key elements, we only replace each cross-attention module to be the multi-scale deformable attention module, while leaving the self-attention modules unchanged.