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_small_30e_objects365")
10model = LwDetrForObjectDetection.from_pretrained("AnnaZhang/lwdetr_small_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.915 at location [7.31, 54.31, 318.17, 474.92]
Detected Refrigerator with confidence 0.86 at location [41.68, 73.28, 175.56, 117.57]
Detected Jug with confidence 0.831 at location [342.32, 24.23, 639.49, 366.43]
Detected Refrigerator with confidence 0.733 at location [334.53, 77.02, 371.51, 187.5]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 }