我刚学习 transformers,在Windows 上 安装了pytorch 和transformers的4.26.1 版本,
测试 from transformers import DetrImageProcessor, DetrForObjectDetection 成功!
现在我想运行一个 目标检测示例,代码 是从 https://hf-mirror.com/ 找的,如下:
from transformers import pipeline
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("C:\\Users\\hello\\detr_resnet_50", revision="no_timm")
model = DetrForObjectDetection.from_pretrained("C:\\Users\\hello\\detr_resnet_50", revision="no_timm")
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.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}"
)
由于我预先下载了 facebook/detr-resnet-50 模型到本地的 C:\Users\hello\detr_resnet_50 目录下,所以上述代码中的 from_pretrained("C:\Users\hello\detr_resnet_50", revision="no_timm") 表示我自己的 路径,其它地方都是照抄 https://hf-mirror.com,
在python的pycharm 环境下,http://images.cocodataset.org/val2017/000000039769.jpg 的 图片能读取出来,然后长时间没有反应,也没有报错,就是没任何反应,请大家帮助找找原因。