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_large_30e_objects365")
10model = LwDetrForObjectDetection.from_pretrained("AnnaZhang/lwdetr_large_30e_objects365")
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 Jug with confidence 0.978 at location [5.03, 54.69, 315.82, 473.99]
Detected Jug with confidence 0.959 at location [346.48, 23.7, 640.46, 372.56]
Detected Refrigerator with confidence 0.881 at location [40.41, 72.95, 175.7, 116.89]
Detected Refrigerator with confidence 0.72 at location [333.39, 76.8, 370.59, 187.61]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 }