Skip to content

Commit

Permalink
Update Ultralytics dependencies and enhance model utilities
Browse files Browse the repository at this point in the history
- Updated the `ultralytics` package version in `README.md` and CI workflows to 8.3.50.
- Incremented the version number in `sahi/__init__.py` to 0.11.20.
- Introduced a new utility file `sahi/utils/ultralytics.py` for downloading YOLOv8 and YOLO11 models.
- Removed the deprecated `sahi/utils/yolov8.py` file.
- Updated test files to utilize the new model utilities and ensure compatibility with the latest changes.

These updates improve the integration with the Ultralytics framework and streamline model management.
  • Loading branch information
fcakyon committed Dec 16, 2024
1 parent c8c9684 commit cac2c82
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 180 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ jobs:
run: >
pip install pycocotools==2.0.7
- name: Install ultralytics(8.0.207)
- name: Install ultralytics(8.3.50)
run: >
pip install ultralytics==8.0.207
pip install ultralytics==8.3.50
- name: Unittest for SAHI+YOLOV5/MMDET/Detectron2 on all platforms
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/package_testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ jobs:
run: >
pip install pycocotools==2.0.7
- name: Install ultralytics(8.0.207)
- name: Install ultralytics(8.3.50)
run: >
pip install ultralytics==8.0.207
pip install ultralytics==8.3.50
- name: Install latest SAHI package
run: >
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pip install yolov5==7.0.13
- Install your desired detection framework (ultralytics):

```console
pip install ultralytics==8.0.207
pip install ultralytics==8.3.50
```

- Install your desired detection framework (mmdet):
Expand Down
2 changes: 1 addition & 1 deletion sahi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.11.19"
__version__ = "0.11.20"

from sahi.annotation import BoundingBox, Category, Mask
from sahi.auto_model import AutoDetectionModel
Expand Down
113 changes: 113 additions & 0 deletions sahi/utils/ultralytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import os
from pathlib import Path
from typing import Optional

import requests
from tqdm import tqdm

YOLOV8N_WEIGHTS_URL = "https://github.com/ultralytics/assets/releases/download/v8.3.0/yolov8n.pt"
YOLOV8N_SEG_WEIGHTS_URL = "https://github.com/ultralytics/assets/releases/download/v8.3.0/yolov8n-seg.pt"
YOLO11N_WEIGHTS_URL = "https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11n.pt"
YOLO11N_SEG_WEIGHTS_URL = "https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11n-seg.pt"
YOLO11N_OBB_WEIGHTS_URL = "https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11n-obb.pt"

class UltralyticsTestConstants:
YOLOV8N_MODEL_PATH = "tests/data/models/yolov8n.pt"
YOLOV8N_SEG_MODEL_PATH = "tests/data/models/yolov8n-seg.pt"
YOLO11N_MODEL_PATH = "tests/data/models/yolo11n.pt"
YOLO11N_SEG_MODEL_PATH = "tests/data/models/yolo11n-seg.pt"
YOLO11N_OBB_MODEL_PATH = "tests/data/models/yolo11n-obb.pt"

def download_file(url: str, save_path: str, chunk_size: int = 8192) -> None:
"""
Downloads a file from a given URL to the specified path.
Args:
url: URL to download the file from
save_path: Path where the file will be saved
chunk_size: Size of chunks for downloading
"""
response = requests.get(url, stream=True)
total_size = int(response.headers.get('content-length', 0))

# Ensure directory exists
os.makedirs(os.path.dirname(save_path), exist_ok=True)

with open(save_path, 'wb') as f, tqdm(
desc=os.path.basename(save_path),
total=total_size,
unit='B',
unit_scale=True,
unit_divisor=1024,
) as pbar:
for data in response.iter_content(chunk_size=chunk_size):
size = f.write(data)
pbar.update(size)

def download_yolov8n_model(destination_path: Optional[str] = None) -> str:
"""Downloads YOLOv8n model if not already downloaded."""
if destination_path is None:
destination_path = UltralyticsTestConstants.YOLOV8N_MODEL_PATH

if not os.path.exists(destination_path):
download_file(YOLOV8N_WEIGHTS_URL, destination_path)
return destination_path

def download_yolov8n_seg_model(destination_path: Optional[str] = None) -> str:
"""Downloads YOLOv8n-seg model if not already downloaded."""
if destination_path is None:
destination_path = UltralyticsTestConstants.YOLOV8N_SEG_MODEL_PATH

if not os.path.exists(destination_path):
download_file(YOLOV8N_SEG_WEIGHTS_URL, destination_path)
return destination_path

def download_yolo11n_model(destination_path: Optional[str] = None) -> str:
"""Downloads YOLO11n model if not already downloaded."""
if destination_path is None:
destination_path = UltralyticsTestConstants.YOLO11N_MODEL_PATH

if not os.path.exists(destination_path):
download_file(YOLO11N_WEIGHTS_URL, destination_path)
return destination_path

def download_yolo11n_seg_model(destination_path: Optional[str] = None) -> str:
"""Downloads YOLO11n-seg model if not already downloaded."""
if destination_path is None:
destination_path = UltralyticsTestConstants.YOLO11N_SEG_MODEL_PATH

if not os.path.exists(destination_path):
download_file(YOLO11N_SEG_WEIGHTS_URL, destination_path)
return destination_path

def download_yolo11n_obb_model(destination_path: Optional[str] = None) -> str:
"""Downloads YOLO11n-obb model if not already downloaded."""
if destination_path is None:
destination_path = UltralyticsTestConstants.YOLO11N_OBB_MODEL_PATH

if not os.path.exists(destination_path):
download_file(YOLO11N_OBB_WEIGHTS_URL, destination_path)
return destination_path

def download_model_weights(model_path: str) -> str:
"""
Downloads model weights based on the model path.
Args:
model_path: Path or name of the model
Returns:
Path to the downloaded weights file
"""
model_name = Path(model_path).stem
if model_name == "yolov8n":
return download_yolov8n_model()
elif model_name == "yolov8n-seg":
return download_yolov8n_seg_model()
elif model_name == "yolo11n":
return download_yolo11n_model()
elif model_name == "yolo11n-seg":
return download_yolo11n_seg_model()
elif model_name == "yolo11n-obb":
return download_yolo11n_obb_model()
else:
raise ValueError(f"Unknown model: {model_name}")
166 changes: 0 additions & 166 deletions sahi/utils/yolov8.py

This file was deleted.

2 changes: 1 addition & 1 deletion sahi/utils/yolov8onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import numpy as np

from sahi.utils.yolov8 import download_yolov8n_model
from sahi.utils.ultralytics import download_yolov8n_model


class Yolov8ONNXTestConstants:
Expand Down
Loading

0 comments on commit cac2c82

Please sign in to comment.