原文来源:
https://blog.youkuaiyun.com/yhdfgvsag/article/details/46554444?hajhd=66545.46115.54440.12
先看YOLO11n模型的识别效果,可见效果不理想,不仅类别错误框也不精确。
接下来实现指鹿为马。
1.使用打标签工具Labelimg对此原图打标注以获得框的4个坐标信息,注意标签信息保存格式不要选YOLO格式生成TXT文件,而是选VOC格式生成XML文件,因为不需要归一化后的坐标信息。
2.改动ultralytics\engine\results.py里的代码。
将
for i, d in enumerate(reversed(pred_boxes)):
c, d_conf, id = int(d.cls), float(d.conf) if conf else None, None if d.id is None else int(d.id.item())
# modified
if new_names is not None:
name = ("" if id is None else f"id:{id} ") + new_names[c]
else:
name = ("" if id is None else f"id:{id} ") + names[c]
label = (f"{name} {d_conf:.2f}" if conf else name) if labels else None
box = d.xyxyxyxy.reshape(-1, 4, 2).squeeze() if is_obb else d.xyxy.squeeze()
annotator.box_label(
box,
label,
color=colors(
c
if color_mode == "class"
else id
if id is not None
else i
if color_mode == "instance"
else None,
True,
),
rotated=is_obb,
)
换为
target_info = [
[0, 1.00, torch.tensor([60, 56, 362, 363])],
]
for c, d_conf, box in target_info:
name = ['horse'][c]
label = (f"{name} {d_conf:.2f}" if conf else name) if labels else None
annotator.box_label(box, label, color=colors(c, True), rotated=is_obb)
其中的target_info = [[0, 1.00, torch.tensor([60, 56, 362, 363])], ]分别为框的类别、置信度和4个坐标信息,0代表name = ['horse'][c]里的第一个类别马horse,置信度为1.00,而坐标信息60, 56, 362, 363则由XML文件里的数据直接复制粘贴即可。
3.运行ultralytics\models\yolo\detect\predict.py对这一张图片进行预测即可,随便输入一个权重文件即可无关紧要。
最后生成效果如下图所示,你看这马的框标得,那是又快又准,绝对保真,好吧!