Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
with class remapping for YOLO-NAS
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
from typing import Tuple
|
| 2 |
|
| 3 |
-
import spaces
|
| 4 |
import gradio as gr
|
| 5 |
import numpy as np
|
| 6 |
import supervision as sv
|
|
@@ -11,17 +10,17 @@ MARKDOWN = """
|
|
| 11 |
|
| 12 |
Welcome to YOLO-Arena! This demo showcases the performance of various YOLO models:
|
| 13 |
|
| 14 |
-
- YOLOv8
|
| 15 |
-
- YOLOv9
|
| 16 |
-
- YOLOv10
|
| 17 |
-
- YOLO-NAS
|
| 18 |
|
| 19 |
Powered by Roboflow [Inference](https://github.com/roboflow/inference) and
|
| 20 |
[Supervision](https://github.com/roboflow/supervision).
|
| 21 |
"""
|
| 22 |
|
| 23 |
IMAGE_EXAMPLES = [
|
| 24 |
-
['https://media.roboflow.com/dog.jpeg', 0.
|
| 25 |
]
|
| 26 |
|
| 27 |
YOLO_V8_MODEL = get_model(model_id="yolov8m-640")
|
|
@@ -29,6 +28,17 @@ YOLO_NAS_MODEL = get_model(model_id="coco/15")
|
|
| 29 |
YOLO_V9_MODEL = get_model(model_id="coco/17")
|
| 30 |
YOLO_V10_MODEL = get_model(model_id="coco/22")
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
LABEL_ANNOTATORS = sv.LabelAnnotator(text_color=sv.Color.black())
|
| 33 |
BOUNDING_BOX_ANNOTATORS = sv.BoundingBoxAnnotator()
|
| 34 |
|
|
@@ -37,7 +47,8 @@ def detect_and_annotate(
|
|
| 37 |
model,
|
| 38 |
input_image: np.ndarray,
|
| 39 |
confidence_threshold: float,
|
| 40 |
-
iou_threshold: float
|
|
|
|
| 41 |
) -> np.ndarray:
|
| 42 |
result = model.infer(
|
| 43 |
input_image,
|
|
@@ -45,15 +56,28 @@ def detect_and_annotate(
|
|
| 45 |
iou_threshold=iou_threshold
|
| 46 |
)[0]
|
| 47 |
detections = sv.Detections.from_inference(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
annotated_image = input_image.copy()
|
| 49 |
annotated_image = BOUNDING_BOX_ANNOTATORS.annotate(
|
| 50 |
scene=annotated_image, detections=detections)
|
| 51 |
annotated_image = LABEL_ANNOTATORS.annotate(
|
| 52 |
-
scene=annotated_image, detections=detections)
|
| 53 |
return annotated_image
|
| 54 |
|
| 55 |
|
| 56 |
-
@spaces.GPU(duration=200)
|
| 57 |
def process_image(
|
| 58 |
input_image: np.ndarray,
|
| 59 |
confidence_threshold: float,
|
|
@@ -62,7 +86,11 @@ def process_image(
|
|
| 62 |
yolo_v8_annotated_image = detect_and_annotate(
|
| 63 |
YOLO_V8_MODEL, input_image, confidence_threshold, iou_threshold)
|
| 64 |
yolo_nas_annotated_image = detect_and_annotate(
|
| 65 |
-
YOLO_NAS_MODEL,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
yolo_v9_annotated_image = detect_and_annotate(
|
| 67 |
YOLO_V9_MODEL, input_image, confidence_threshold, iou_threshold)
|
| 68 |
yolo_10_annotated_image = detect_and_annotate(
|
|
|
|
| 1 |
from typing import Tuple
|
| 2 |
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
import supervision as sv
|
|
|
|
| 10 |
|
| 11 |
Welcome to YOLO-Arena! This demo showcases the performance of various YOLO models:
|
| 12 |
|
| 13 |
+
- YOLOv8 [[code](https://github.com/ultralytics/ultralytics)]
|
| 14 |
+
- YOLOv9 [[code](https://github.com/WongKinYiu/yolov9)]
|
| 15 |
+
- YOLOv10 [[code](https://github.com/THU-MIG/yolov10)]
|
| 16 |
+
- YOLO-NAS [[code](https://github.com/Deci-AI/super-gradients/blob/master/YOLONAS.md)]
|
| 17 |
|
| 18 |
Powered by Roboflow [Inference](https://github.com/roboflow/inference) and
|
| 19 |
[Supervision](https://github.com/roboflow/supervision).
|
| 20 |
"""
|
| 21 |
|
| 22 |
IMAGE_EXAMPLES = [
|
| 23 |
+
['https://media.roboflow.com/dog.jpeg', 0.4]
|
| 24 |
]
|
| 25 |
|
| 26 |
YOLO_V8_MODEL = get_model(model_id="yolov8m-640")
|
|
|
|
| 28 |
YOLO_V9_MODEL = get_model(model_id="coco/17")
|
| 29 |
YOLO_V10_MODEL = get_model(model_id="coco/22")
|
| 30 |
|
| 31 |
+
YOLO_NAS_TO_COCO_CLASS_ID_MAPPING = {
|
| 32 |
+
49: 0, 9: 1, 18: 2, 44: 3, 0: 4, 16: 5, 73: 6, 74: 7, 11: 8, 72: 9, 31: 10, 63: 11,
|
| 33 |
+
48: 12, 8: 13, 10: 14, 20: 15, 28: 16, 37: 17, 56: 18, 25: 19, 30: 20, 6: 21,
|
| 34 |
+
79: 22, 34: 23, 2: 24, 76: 25, 36: 26, 68: 27, 64: 28, 33: 29, 59: 30, 60: 31,
|
| 35 |
+
62: 32, 40: 33, 4: 34, 5: 35, 58: 36, 65: 37, 67: 38, 13: 39, 78: 40, 26: 41,
|
| 36 |
+
32: 42, 41: 43, 61: 44, 14: 45, 3: 46, 1: 47, 54: 48, 46: 49, 15: 50, 19: 51,
|
| 37 |
+
38: 52, 50: 53, 29: 54, 17: 55, 22: 56, 24: 57, 51: 58, 7: 59, 27: 60, 70: 61,
|
| 38 |
+
75: 62, 42: 63, 45: 64, 53: 65, 39: 66, 21: 67, 43: 68, 47: 69, 69: 70, 57: 71,
|
| 39 |
+
52: 72, 12: 73, 23: 74, 77: 75, 55: 76, 66: 77, 35: 78, 71: 79
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
LABEL_ANNOTATORS = sv.LabelAnnotator(text_color=sv.Color.black())
|
| 43 |
BOUNDING_BOX_ANNOTATORS = sv.BoundingBoxAnnotator()
|
| 44 |
|
|
|
|
| 47 |
model,
|
| 48 |
input_image: np.ndarray,
|
| 49 |
confidence_threshold: float,
|
| 50 |
+
iou_threshold: float,
|
| 51 |
+
class_id_mapping: dict = None
|
| 52 |
) -> np.ndarray:
|
| 53 |
result = model.infer(
|
| 54 |
input_image,
|
|
|
|
| 56 |
iou_threshold=iou_threshold
|
| 57 |
)[0]
|
| 58 |
detections = sv.Detections.from_inference(result)
|
| 59 |
+
|
| 60 |
+
if class_id_mapping:
|
| 61 |
+
detections.class_id = np.array([
|
| 62 |
+
class_id_mapping[class_id]
|
| 63 |
+
for class_id
|
| 64 |
+
in detections.class_id
|
| 65 |
+
])
|
| 66 |
+
|
| 67 |
+
labels = [
|
| 68 |
+
f"{class_name} ({confidence:.2f})"
|
| 69 |
+
for class_name, confidence
|
| 70 |
+
in zip(detections['class_name'], detections.confidence)
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
annotated_image = input_image.copy()
|
| 74 |
annotated_image = BOUNDING_BOX_ANNOTATORS.annotate(
|
| 75 |
scene=annotated_image, detections=detections)
|
| 76 |
annotated_image = LABEL_ANNOTATORS.annotate(
|
| 77 |
+
scene=annotated_image, detections=detections, labels=labels)
|
| 78 |
return annotated_image
|
| 79 |
|
| 80 |
|
|
|
|
| 81 |
def process_image(
|
| 82 |
input_image: np.ndarray,
|
| 83 |
confidence_threshold: float,
|
|
|
|
| 86 |
yolo_v8_annotated_image = detect_and_annotate(
|
| 87 |
YOLO_V8_MODEL, input_image, confidence_threshold, iou_threshold)
|
| 88 |
yolo_nas_annotated_image = detect_and_annotate(
|
| 89 |
+
YOLO_NAS_MODEL,
|
| 90 |
+
input_image,
|
| 91 |
+
confidence_threshold,
|
| 92 |
+
iou_threshold,
|
| 93 |
+
YOLO_NAS_TO_COCO_CLASS_ID_MAPPING)
|
| 94 |
yolo_v9_annotated_image = detect_and_annotate(
|
| 95 |
YOLO_V9_MODEL, input_image, confidence_threshold, iou_threshold)
|
| 96 |
yolo_10_annotated_image = detect_and_annotate(
|