模型简介
模型特点
模型能力
使用案例
🚀 DETR(端到端目标检测)模型,采用ResNet - 101骨干网络
DETR(Detection Transformer)模型在COCO 2017全景数据集(11.8万张标注图像)上进行了端到端训练。该模型由Carion等人在论文End-to-End Object Detection with Transformers中提出,并首次在此仓库发布。
声明:发布DETR的团队未为此模型撰写模型卡片,本模型卡片由Hugging Face团队编写。
🚀 快速开始
你可以使用此原始模型进行全景分割。查看模型中心以查找所有可用的DETR模型。
from transformers import DetrFeatureExtractor, DetrForSegmentation
from PIL import Image
import requests
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)
feature_extractor = DetrFeatureExtractor.from_pretrained('facebook/detr-resnet-101-panoptic')
model = DetrForSegmentation.from_pretrained('facebook/detr-resnet-101-panoptic')
# prepare inputs for the model
inputs = feature_extractor(images=image, return_tensors="pt")
# forward pass
outputs = model(**inputs)
# use the `post_process_panoptic` method of `DetrFeatureExtractor` to convert to COCO format
processed_sizes = torch.as_tensor(inputs["pixel_values"].shape[-2:]).unsqueeze(0)
result = feature_extractor.post_process_panoptic(outputs, processed_sizes)[0]
# the segmentation is stored in a special-format png
panoptic_seg = Image.open(io.BytesIO(result["png_string"]))
panoptic_seg = numpy.array(panoptic_seg, dtype=numpy.uint8)
# retrieve the ids corresponding to each mask
panoptic_seg_id = rgb_to_id(panoptic_seg)
目前,特征提取器和模型均支持PyTorch。
✨ 主要特性
- 端到端检测:DETR模型通过端到端的方式进行目标检测,避免了传统目标检测方法中复杂的后处理步骤。
- 可扩展性:该模型可以自然地扩展到全景分割任务,只需在解码器输出之上添加一个掩码头。
- 基于Transformer架构:采用编码器 - 解码器Transformer架构,结合卷积骨干网络,能够更好地捕捉图像中的全局信息。
📦 安装指南
文档未提供安装步骤,可参考Hugging Face相关库的安装说明进行安装,例如transformers
库可以使用以下命令安装:
pip install transformers
💻 使用示例
基础用法
from transformers import DetrFeatureExtractor, DetrForSegmentation
from PIL import Image
import requests
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)
feature_extractor = DetrFeatureExtractor.from_pretrained('facebook/detr-resnet-101-panoptic')
model = DetrForSegmentation.from_pretrained('facebook/detr-resnet-101-panoptic')
# prepare inputs for the model
inputs = feature_extractor(images=image, return_tensors="pt")
# forward pass
outputs = model(**inputs)
# use the `post_process_panoptic` method of `DetrFeatureExtractor` to convert to COCO format
processed_sizes = torch.as_tensor(inputs["pixel_values"].shape[-2:]).unsqueeze(0)
result = feature_extractor.post_process_panoptic(outputs, processed_sizes)[0]
# the segmentation is stored in a special-format png
panoptic_seg = Image.open(io.BytesIO(result["png_string"]))
panoptic_seg = numpy.array(panoptic_seg, dtype=numpy.uint8)
# retrieve the ids corresponding to each mask
panoptic_seg_id = rgb_to_id(panoptic_seg)
📚 详细文档
模型描述
DETR模型是一个带有卷积骨干网络的编码器 - 解码器Transformer。为了进行目标检测,在解码器输出之上添加了两个头部:一个用于类别标签的线性层和一个用于边界框的MLP(多层感知器)。该模型使用所谓的对象查询来检测图像中的对象,每个对象查询在图像中寻找特定的对象。对于COCO数据集,对象查询的数量设置为100。
模型使用“二分匹配损失”进行训练:将N = 100个对象查询的预测类别和边界框与真实标注进行比较,真实标注会填充到相同的长度N(因此,如果一张图像仅包含4个对象,那么96个标注的类别将为“无对象”,边界框为“无边界框”)。使用匈牙利匹配算法在N个查询和N个标注之间创建最优的一对一映射。接下来,使用标准的交叉熵(用于类别)和L1与广义IoU损失的线性组合(用于边界框)来优化模型的参数。
预期用途和局限性
可以使用此原始模型进行全景分割。
训练数据
DETR模型在COCO 2017全景数据集上进行训练,该数据集分别包含11.8万张和5000张用于训练和验证的标注图像。
训练过程
预处理
训练/验证期间图像预处理的确切细节可以在此处找到。
图像会进行调整大小/缩放,使得最短边至少为800像素,最长边最多为1333像素,并使用ImageNet的均值(0.485, 0.456, 0.406)和标准差(0.229, 0.224, 0.225)在RGB通道上进行归一化。
训练
模型在16个V100 GPU上训练了300个epoch,耗时3天,每个GPU处理4张图像(因此总批量大小为64)。
评估结果
该模型在COCO 2017验证集上取得了以下结果:边界框平均精度(AP)为40.1,分割平均精度(AP)为33,全景质量(PQ)为45.1。
有关评估结果的更多详细信息,请参考原论文中的表5。
🔧 技术细节
- 模型架构:DETR模型由卷积骨干网络、编码器 - 解码器Transformer、类别标签线性层和边界框MLP组成。卷积骨干网络用于提取图像的特征,Transformer用于处理这些特征并生成对象查询,线性层和MLP用于预测对象的类别和边界框。
- 损失函数:使用“二分匹配损失”,结合交叉熵损失和L1与广义IoU损失的线性组合,确保模型能够准确地预测对象的类别和边界框。
- 训练策略:在多个GPU上进行分布式训练,使用特定的图像预处理方法和批量大小,以提高训练效率和模型性能。
📄 许可证
本模型采用Apache - 2.0许可证。
BibTeX引用
@article{DBLP:journals/corr/abs-2005-12872,
author = {Nicolas Carion and
Francisco Massa and
Gabriel Synnaeve and
Nicolas Usunier and
Alexander Kirillov and
Sergey Zagoruyko},
title = {End-to-End Object Detection with Transformers},
journal = {CoRR},
volume = {abs/2005.12872},
year = {2020},
url = {https://arxiv.org/abs/2005.12872},
archivePrefix = {arXiv},
eprint = {2005.12872},
timestamp = {Thu, 28 May 2020 17:38:09 +0200},
biburl = {https://dblp.org/rec/journals/corr/abs-2005-12872.bib},
bibsource = {dblp computer science bibliography, https://dblp.org}
}
📋 信息表格
属性 | 详情 |
---|---|
模型类型 | 端到端目标检测模型,可扩展到全景分割任务 |
训练数据 | COCO 2017全景数据集,包含11.8万张训练图像和5000张验证图像 |
骨干网络 | ResNet - 101 |
训练设备 | 16个V100 GPU |
训练轮数 | 300个epoch |
批量大小 | 64 |
评估指标(COCO 2017验证集) | 边界框AP:40.1;分割AP:33;PQ:45.1 |











