henry000 commited on
Commit
43b7a51
Β·
1 Parent(s): b6b57c7

πŸš€ [New] Huggingface examples!

Browse files
Files changed (3) hide show
  1. README.md +3 -1
  2. demo/hf_demo.py +76 -0
  3. yolo/__init__.py +2 -1
README.md CHANGED
@@ -1,6 +1,8 @@
1
  # YOLO: Official Implementation of YOLOv9, YOLOv7
2
 
 
3
  ![WIP](https://img.shields.io/badge/status-WIP-orange)
 
4
  > [!IMPORTANT]
5
  > This project is currently a Work In Progress and may undergo significant changes. It is not recommended for use in production environments until further notice. Please check back regularly for updates.
6
  >
@@ -35,7 +37,7 @@ pip install -r requirements.txt
35
 
36
  | Tools | pip 🐍 | HuggingFace πŸ€— | Docker 🐳 |
37
  | -------------------- | :----: | :--------------: | :-------: |
38
- | Compatibility | βœ… | ❔ | πŸ§ͺ |
39
 
40
  | Phase | Training | Validation | Inference |
41
  | ------------------- | :------: | :---------: | :-------: |
 
1
  # YOLO: Official Implementation of YOLOv9, YOLOv7
2
 
3
+ ![GitHub License](https://img.shields.io/github/license/WongKinYiu/YOLO)
4
  ![WIP](https://img.shields.io/badge/status-WIP-orange)
5
+ [![Hugging Face Spaces](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/henry000/YOLO)
6
  > [!IMPORTANT]
7
  > This project is currently a Work In Progress and may undergo significant changes. It is not recommended for use in production environments until further notice. Please check back regularly for updates.
8
  >
 
37
 
38
  | Tools | pip 🐍 | HuggingFace πŸ€— | Docker 🐳 |
39
  | -------------------- | :----: | :--------------: | :-------: |
40
+ | Compatibility | βœ… | βœ… | πŸ§ͺ |
41
 
42
  | Phase | Training | Validation | Inference |
43
  | ------------------- | :------: | :---------: | :-------: |
demo/hf_demo.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ from pathlib import Path
3
+
4
+ import gradio
5
+ import torch
6
+ from omegaconf import OmegaConf
7
+
8
+ sys.path.append(str(Path(__file__).resolve().parent.parent))
9
+
10
+ from yolo import (
11
+ AugmentationComposer,
12
+ NMSConfig,
13
+ Vec2Box,
14
+ bbox_nms,
15
+ create_model,
16
+ draw_bboxes,
17
+ )
18
+
19
+ DEFAULT_MODEL = "v9-c"
20
+ IMAGE_SIZE = (640, 640)
21
+
22
+
23
+ def load_model(model_name, device):
24
+ model_cfg = OmegaConf.load(f"yolo/config/model/{model_name}.yaml")
25
+ model_cfg.model.auxiliary = {}
26
+ model = create_model(model_cfg, True)
27
+ model.to(device).eval()
28
+ return model
29
+
30
+
31
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
32
+ model = load_model(DEFAULT_MODEL, device)
33
+ v2b = Vec2Box(model, IMAGE_SIZE, device)
34
+ class_list = OmegaConf.load("yolo/config/general.yaml").class_list
35
+
36
+ transform = AugmentationComposer([])
37
+
38
+
39
+ def predict(model_name, image, nms_confidence, nms_iou):
40
+ global DEFAULT_MODEL, model, device, v2b, class_list
41
+ if model_name != DEFAULT_MODEL:
42
+ model = load_model(model_name, device)
43
+ v2b = Vec2Box(model, IMAGE_SIZE, device)
44
+ DEFAULT_MODEL = model_name
45
+
46
+ image_tensor, _, rev_tensor = transform(image)
47
+
48
+ image_tensor = image_tensor.to(device)[None]
49
+ rev_tensor = rev_tensor.to(device)
50
+
51
+ with torch.no_grad():
52
+ predict = model(image_tensor)
53
+ pred_class, _, pred_bbox = v2b(predict["Main"])
54
+
55
+ nms_config = NMSConfig(nms_confidence, nms_iou)
56
+
57
+ pred_bbox = pred_bbox / rev_tensor[0] - rev_tensor[None, None, 1:]
58
+ pred_bbox = bbox_nms(pred_class, pred_bbox, nms_config)
59
+ result_image = draw_bboxes(image, pred_bbox, idx2label=class_list)
60
+
61
+ return result_image
62
+
63
+
64
+ interface = gradio.Interface(
65
+ fn=predict,
66
+ inputs=[
67
+ gradio.components.Dropdown(choices=["v9-c", "v9-m", "v9-s"], value="v9-c", label="Model Name"),
68
+ gradio.components.Image(type="pil", label="Input Image"),
69
+ gradio.components.Slider(0, 1, step=0.01, value=0.5, label="NMS Confidence Threshold"),
70
+ gradio.components.Slider(0, 1, step=0.01, value=0.5, label="NMS IoU Threshold"),
71
+ ],
72
+ outputs=gradio.components.Image(type="pil", label="Output Image"),
73
+ )
74
+
75
+ if __name__ == "__main__":
76
+ interface.launch()
yolo/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- from yolo.config.config import Config
2
  from yolo.model.yolo import create_model
3
  from yolo.tools.data_loader import AugmentationComposer, create_dataloader
4
  from yolo.tools.drawer import draw_bboxes
@@ -10,6 +10,7 @@ from yolo.utils.logging_utils import custom_logger
10
  all = [
11
  "create_model",
12
  "Config",
 
13
  "custom_logger",
14
  "validate_log_directory",
15
  "draw_bboxes",
 
1
+ from yolo.config.config import Config, NMSConfig
2
  from yolo.model.yolo import create_model
3
  from yolo.tools.data_loader import AugmentationComposer, create_dataloader
4
  from yolo.tools.drawer import draw_bboxes
 
10
  all = [
11
  "create_model",
12
  "Config",
13
+ "NMSConfig",
14
  "custom_logger",
15
  "validate_log_directory",
16
  "draw_bboxes",