🚀 DETR(端到端目标检测)模型
本项目是一个基于ResNet - 50骨干网络的DETR(端到端目标检测)模型,在SKU110K数据集上进行训练,设置了400个查询数(num_queries)。该模型能够有效解决目标检测问题,为相关领域的应用提供了强大的支持。
🚀 快速开始
DETR(Detection Transformer)模型在SKU110K目标检测数据集(包含8000张标注图像)上进行了端到端的训练。与原始模型相比,主要区别在于设置了400个查询数(num_queries),并且在SKU110K数据集上进行了预训练。
模型使用方法
以下是使用该模型的示例代码:
from transformers import DetrImageProcessor, DetrForObjectDetection
import torch
from PIL import Image, ImageOps
import requests
url = "https://github.com/Isalia20/DETR-finetune/blob/main/IMG_3507.jpg?raw=true"
image = Image.open(requests.get(url, stream=True).raw)
image = ImageOps.exif_transpose(image)
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
model = DetrForObjectDetection.from_pretrained("isalia99/detr-resnet-50-sku110k")
model = model.eval()
inputs = processor(images=image, return_tensors="pt")
outputs = model(**inputs)
target_sizes = torch.tensor([image.size[::-1]])
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.8)[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 LABEL_1 with confidence 0.983 at location [665.49, 480.05, 708.15, 650.11]
Detected LABEL_1 with confidence 0.938 at location [204.99, 1405.9, 239.9, 1546.5]
...
Detected LABEL_1 with confidence 0.998 at location [772.85, 169.49, 829.67, 372.18]
Detected LABEL_1 with confidence 0.999 at location [828.28, 1475.16, 874.37, 1593.43]
目前,特征提取器和模型均支持PyTorch。
📚 详细文档
训练数据
DETR模型在SKU110K数据集上进行训练,该数据集分别包含8219/588/2936张标注图像用于训练/验证/测试。
训练过程
训练
模型在1块RTX 4060 Ti GPU上进行训练,前140个epoch仅微调解码器,批量大小为8;后70个epoch微调整个网络,批量大小为3,并进行3步梯度累积。
评估结果
该模型在SKU110k验证集上实现了58.9的平均精度均值(mAP)。结果使用torchmetrics的MeanAveragePrecision类进行计算。
训练代码
训练代码已发布在本仓库 仓库链接。不过,代码尚未最终确定和充分测试,但主要功能已包含在内。
📄 许可证
本项目采用Apache - 2.0许可证。