柚子快报激活码778899分享:Mac YOLO V9推理测试

http://yzkb.51969.com/

环境:

Mac M1 (MacOS Sonoma 14.3.1)

Python 3.11+PyTorch 2.1.2

一、准备工作

工程及模型下载:​​​​​​​https://github.com/WongKinYiu/yolov9

git clone https://github.com/WongKinYiu/yolov9.git

克隆后安装相关依赖(没啥依赖好装的)

cd yolov9

pip install -r requirements.txt -q

YOLOv9目前提供了四种模型下载:yolov9-c.pt、yolov9-e.pt、gelan-c.pt、gelan-e.pt(建议手动下载)。

wget -P /Users/zhujiahui/Local/model/yolov9 -q https://github.com/WongKinYiu/yolov9/releases/download/v0.1/yolov9-c.pt

wget -P /Users/zhujiahui/Local/model/yolov9 -q https://github.com/WongKinYiu/yolov9/releases/download/v0.1/yolov9-e.pt

wget -P /Users/zhujiahui/Local/model/yolov9 -q https://github.com/WongKinYiu/yolov9/releases/download/v0.1/gelan-c.pt

wget -P /Users/zhujiahui/Local/model/yolov9 -q https://github.com/WongKinYiu/yolov9/releases/download/v0.1/gelan-e.pt

注:gelan(Generalized ELAN ,广义ELAN),在yolov9中,gelan被作为block用在了backbone中。

将下载好的模型放到指定的位置。

下载示例图片(也可手动下载放置),放到data/images下:

wget -P data/images -q https://media.roboflow.com/notebooks/examples/dog.jpeg

二、推理

基于yolov9-c.pt进行推理

python detect_dual.py --weights /Users/zhujiahui/Local/model/yolov9/yolov9-c.pt --conf 0.1 --source data/images/dog.jpeg --device cpu

基于yolov9-e.pt进行推理

python detect_dual.py --weights /Users/zhujiahui/Local/model/yolov9/yolov9-e.pt --conf 0.1 --source data/images/dog.jpeg --device cpu

注意:

yolo模型的推理要用detect_dual.py

本人是在Mac环境下跑的,因此设置为--device cpu或--device mps。

运行过程中会输出如下信息:

detect_dual: weights=['/Users/zhujiahui/Local/model/yolov9/yolov9-c.pt'], source=data/images/dog.jpeg, data=data/coco128.yaml, imgsz=[640, 640], conf_thres=0.1, iou_thres=0.45, max_det=1000, device=cpu, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False, vid_stride=1

YOLO  v0.1-89-g93f1a28 Python-3.11.4 torch-2.1.2 CPU

Fusing layers... 

Model summary: 604 layers, 50880768 parameters, 0 gradients, 237.6 GFLOPs

image 1/1 /Users/zhujiahui/Local/PycharmProjects/yolov9/data/images/dog.jpeg: 640x384 1 person, 1 car, 1 dog, 1 backpack, 313.4ms

Speed: 0.6ms pre-process, 313.4ms inference, 0.6ms NMS per image at shape (1, 3, 640, 640)

Results saved to runs/detect/exp

最终会在runs/detect/exp下生成相关结果图片。运行多次后依次为exp2、exp3… 

​​​原始yolov9-c.ptyolov9-e.pt

从以上结果可知yolov9-e.pt的效果更好,能够额外识别正确背包和手提包。

基于gelan-c.pt进行推理

python detect.py --weights /Users/zhujiahui/Local/model/yolov9/gelan-c.pt --conf 0.1 --source data/images/dog.jpeg --device cpu

基于gelan-e.pt进行推理

python detect.py --weights /Users/zhujiahui/Local/model/yolov9/gelan-e.pt --conf 0.1 --source data/images/dog.jpeg --device cpu

注意:gelan模型的推理要用detect.py

结果如下:

​​​原始gelan-c.ptgelan-e.pt

效果不如yolov9-e.pt。

三、相关问题

1. Mac下--device cpu和--device mps速度问题

明显cpu更快,不明所以。

2. AttributeError: 'list' object has no attribute 'device'

Traceback (most recent call last):

  File "/Users/zhujiahui/Local/PycharmProjects/yolov9/detect.py", line 231, in

    main(opt)

  File "/Users/zhujiahui/Local/PycharmProjects/yolov9/detect.py", line 226, in main

    run(**vars(opt))

  File "/opt/anaconda3/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context

    return func(*args, **kwargs)

           ^^^^^^^^^^^^^^^^^^^^^

  File "/Users/zhujiahui/Local/PycharmProjects/yolov9/detect.py", line 102, in run

    pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)

           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/Users/zhujiahui/Local/PycharmProjects/yolov9/utils/general.py", line 905, in non_max_suppression

    device = prediction.device

             ^^^^^^^^^^^^^^^^^

AttributeError: 'list' object has no attribute 'device'

原因:对yolov9-c.pt/yolov9-e.pt采用了detect.py去推理,yolov9-c.pt/yolov9-e.pt采用train_dual.py训练得到,应该对应地采用detect_dual.py进行推理。

解决方案:

python detect.py --weights /Users/zhujiahui/Local/model/yolov9/yolov9-c.pt --conf 0.1 --source data/images/dog.jpeg --device cpu

改为

python detect_dual.py --weights /Users/zhujiahui/Local/model/yolov9/yolov9-c.pt --conf 0.1 --source data/images/dog.jpeg --device cpu

3. IndexError: index 1 is out of bounds for dimension 0 with size 1

Traceback (most recent call last):

  File "/Users/zhujiahui/Local/PycharmProjects/yolov9/detect_dual.py", line 232, in

    main(opt)

  File "/Users/zhujiahui/Local/PycharmProjects/yolov9/detect_dual.py", line 227, in main

    run(**vars(opt))

  File "/opt/anaconda3/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context

    return func(*args, **kwargs)

           ^^^^^^^^^^^^^^^^^^^^^

  File "/Users/zhujiahui/Local/PycharmProjects/yolov9/detect_dual.py", line 99, in run

    pred = pred[0][1]

           ~~~~~~~^^^

IndexError: index 1 is out of bounds for dimension 0 with size 1

原因:对gelan-c.pt/gelan-e.pt采用了detect_dual.py去推理,gelan-c.pt/gelan-e.pt需采用detect.py进行推理。

解决方案:

python detect_dual.py --weights /Users/zhujiahui/Local/model/yolov9/gelan-c.pt --conf 0.1 --source data/images/dog.jpeg --device cpu

改为

python detect.py --weights /Users/zhujiahui/Local/model/yolov9/gelan-c.pt --conf 0.1 --source data/images/dog.jpeg --device cpu

柚子快报激活码778899分享:Mac YOLO V9推理测试

http://yzkb.51969.com/

参考文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。