File size: 1,636 Bytes
499263d
6fede82
e031631
499263d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

---
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:

-best.pt



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("SakshiRathi77/void-150-epoch", filename="best.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='best.pt', imgsz=640, conf_thres=0.1, iou_thres=0.45):
    # Initialize
    device = select_device('0')
    model = DetectMultiBackend(weights='best.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)
```