Views
No views yet
1from transformers import AutoImageProcessor, LwDetrForObjectDetection
2import torch
3from PIL import Image
4import requests
5
6url = "http://images.cocodataset.org/val2017/000000039769.jpg"
7image = Image.open(requests.get(url, stream=True).raw)
8
9processor = AutoImageProcessor.from_pretrained("AnnaZhang/lwdetr_medium_60e_coco")
10model = LwDetrForObjectDetection.from_pretrained("AnnaZhang/lwdetr_medium_60e_coco")
11
12inputs = processor(images=image, return_tensors="pt")
13outputs = model(**inputs)
14
15# convert outputs (bounding boxes and class logits) to COCO API
16# let's only keep detections with score > 0.7
17target_sizes = torch.tensor([image.size[::-1]])
18results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.7)[0]
19
20for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
21 box = [round(i, 2) for i in box.tolist()]
22 print(
23 f"Detected {model.config.id2label[label.item()]} with confidence "
24 f"{round(score.item(), 3)} at location {box}"
25 )Detected cat with confidence 0.934 at location [7.19, 55.99, 317.32, 472.44]
Detected cat with confidence 0.931 at location [345.73, 24.62, 639.85, 373.62]
Detected remote with confidence 0.916 at location [40.7, 72.94, 175.14, 117.5]
Detected couch with confidence 0.788 at location [0.79, 0.84, 639.25, 474.6]
Detected remote with confidence 0.752 at location [334.23, 77.08, 370.48, 188.87]1@article{chen2024lw,
2 title={LW-DETR: A Transformer Replacement to YOLO for Real-Time Detection},
3 author={Chen, Qiang and Su, Xiangbo and Zhang, Xinyu and Wang, Jian and Chen, Jiahui and Shen, Yunpeng and Han, Chuchu and Chen, Ziliang and Xu, Weixiang and Li, Fanrong and others},
4 journal={arXiv preprint arXiv:2406.03459},
5 year={2024}
6 }