File size: 2,216 Bytes
8cc8f3d
a196ffa
8cc8f3d
a196ffa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d501eec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
---
pipeline_tag: object-detection
---
# YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information

This is the model repository for YOLOv9, containing the following checkpoints:

- GELAN-C (a newer, lighter architecture)
- GELAN-E
- YOLO9-C
- YOLO9-E

### How to Use

Clone YOLOv9 repository.

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

Download the weights using `hf_hub_download` and use the loading function in helpers of YOLOv9.

```python
from huggingface_hub import hf_hub_download 
hf_hub_download("merve/yolov9", filename="yolov9-c.pt", local_dir="./")
```

Load the model.

```python
# make sure you have the following dependencies
import torch
import numpy as np
from models.common import DetectMultiBackend
from utils.general import non_max_suppression, scale_boxes
from utils.torch_utils import select_device, smart_inference_mode
from utils.augmentations import letterbox
import PIL.Image

@smart_inference_mode()
def predict(image_path, weights='yolov9-c.pt', imgsz=640, conf_thres=0.1, iou_thres=0.45):
    # Initialize
    device = select_device('0')
    model = DetectMultiBackend(weights='yolov9-c.pt', device="0", fp16=False, data='data/coco.yaml')
    stride, names, pt = model.stride, model.names, model.pt

    # Load image
    image = np.array(PIL.Image.open(image_path))
    img = letterbox(img0, imgsz, stride=stride, auto=True)[0]
    img = img[:, :, ::-1].transpose(2, 0, 1)
    img = np.ascontiguousarray(img)
    img = torch.from_numpy(img).to(device).float()
    img /= 255.0
    if img.ndimension() == 3:
        img = img.unsqueeze(0)

    # Inference
    pred = model(img, augment=False, visualize=False)

    # Apply NMS
    pred = non_max_suppression(pred[0][0], conf_thres, iou_thres, classes=None, max_det=1000)
```

### Citation

```
@article{wang2024yolov9,
  title={{YOLOv9}: Learning What You Want to Learn Using Programmable Gradient Information},
  author={Wang, Chien-Yao  and Liao, Hong-Yuan Mark},
  booktitle={arXiv preprint arXiv:2402.13616},
  year={2024}
}
```

The Colab notebook can be found [here](https://colab.research.google.com/drive/1U3rbOmAZOwPUekcvpQS4GGVJQYR7VaQX?usp=sharing#scrollTo=k-JxtpQ_2e0F). 🧡