模型:
facebook/detr-resnet-50
DEtection TRansformer(DETR)模型是在COCO 2017目标检测(118k个标注图像)数据集上端到端训练的。该模型由Carion等人在论文 End-to-End Object Detection with Transformers 中提出,并首次在 this repository 中发布。
免责声明:发布DETR模型的团队未为该模型编写模型卡片,因此本模型卡片由Hugging Face团队编写。
DETR模型是一个具有卷积骨干网络的编码器-解码器变换器。在解码器输出的顶部添加了两个头部,用于执行目标检测:一个用于类标签的线性层,一个用于边界框的多层感知器(MLP)。该模型使用所谓的对象查询来检测图像中的对象。每个对象查询在图像中寻找特定的对象。对于COCO数据集,对象查询的数量设置为100。
模型使用“二部图匹配损失”进行训练:将每个N = 100个对象查询的预测类别+边界框与地面实况注释进行比较,将其填充到相同长度N(因此,如果图像仅包含4个对象,则96个注释将只有“无对象”作为类别和“无边界框”作为边界框)。使用匈牙利匹配算法在每个N个查询和每个N个注释之间创建最佳一对一映射。接下来,使用标准的交叉熵(用于类别)和L1和广义IoU损失的线性组合(用于边界框)优化模型的参数。
链接您可以使用原始模型进行目标检测。请参阅链接 model hub 以查找所有可用的DETR模型。
以下是使用此模型的方法:
链接from transformers import DetrImageProcessor, DetrForObjectDetection import torch from PIL import Image import requests url = "http://images.cocodataset.org/val2017/000000039769.jpg" image = Image.open(requests.get(url, stream=True).raw) processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50") inputs = processor(images=image, return_tensors="pt") outputs = model(**inputs) # convert outputs (bounding boxes and class logits) to COCO API # let's only keep detections with score > 0.9 target_sizes = torch.tensor([image.size[::-1]]) results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0] for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): box = [round(i, 2) for i in box.tolist()] print( f"Detected {model.config.id2label[label.item()]} with confidence " f"{round(score.item(), 3)} at location {box}" )
这应该会输出:
链接Detected remote with confidence 0.998 at location [40.16, 70.81, 175.55, 117.98] Detected remote with confidence 0.996 at location [333.24, 72.55, 368.33, 187.66] Detected couch with confidence 0.995 at location [-0.02, 1.15, 639.73, 473.76] Detected cat with confidence 0.999 at location [13.24, 52.05, 314.02, 470.93] Detected cat with confidence 0.999 at location [345.4, 23.85, 640.37, 368.72]
目前,特征提取器和模型都支持PyTorch。
DETR模型在链接 COCO 2017 object detection 上进行了训练,该数据集包含了118k/5k个用于训练/验证的标注图像。
在训练/验证期间对图像进行预处理的确切细节可以在链接 here 中找到。
图像被调整大小/重新缩放,以使最短边至少为800像素,最大边至多为1333像素,并在RGB通道上使用ImageNet的均值(0.485,0.456,0.406)和标准差(0.229,0.224,0.225)进行归一化。
该模型在16个V100 GPU上训练了300个epochs。每个GPU上有4个图像(因此总批量大小为64),训练时间为3天。
该模型在COCO 2017验证集上实现了42.0的AP(平均精度)。有关评估结果的更多详细信息,请参阅原始论文的表1。
链接@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} }