Skip to content

Commit

Permalink
fix some hf util bugs (#33)
Browse files Browse the repository at this point in the history
* fix some hf util bugs

* update version

* update test

* fix env variable

* fix workflows

* auto download font when not present

* fix font selection

* improve tests

* update tests

* updates

* updates

* updates

* updates

* update
  • Loading branch information
fcakyon authored Jan 21, 2023
1 parent 74ed05b commit 04219a0
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ jobs:
pip list
- name: Unittest
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
run: |
pytest
pytest -s
4 changes: 3 additions & 1 deletion .github/workflows/package_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@ jobs:
pip list
- name: Unittest
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
run: |
pytest
pytest -s
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import subprocess
from pathlib import Path

from ultralytics.yolo.utils import ROOT, SETTINGS
from ultralytics.yolo.utils import SETTINGS

MODEL = Path(SETTINGS['weights_dir']) / 'yolov8n'
CFG = 'yolov8n'
Expand Down
36 changes: 36 additions & 0 deletions tests/test_hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
download_from_hub,
postprocess_classify_output,
render_result,
push_to_hfhub,
)
import subprocess
import os

hub_id = "ultralyticsplus/yolov8s"

Expand Down Expand Up @@ -56,3 +59,36 @@ def test_classification_inference():
results = model(image, imgsz=640)
name_to_probs = postprocess_classify_output(model=model, result=results[0])
name_to_probs


def run(cmd):
# Run a subprocess command with check=True
subprocess.run(cmd.split(), check=True)


def test_detection_upload():
import platform
from packaging.version import Version

# run following lines if linux and python major == 3 and python minor == 10 (python micor can be anything)
print(f'platform.system(): {platform.system()}')
print(f'platform.python_version(): {platform.python_version()}')
print(f'os.getenv("RUNNER_OS"): {os.getenv("RUNNER_OS")}')
print(f'Version(platform.python_version()) >= Version("3.10"): {Version(platform.python_version()) >= Version("3.10")}')
if platform.system() == 'Linux' and Version(platform.python_version()) >= Version("3.10"):
print('training started')
run('yolo train detect model=yolov8n.pt data=coco8.yaml imgsz=32 epochs=1')
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')
push_to_hfhub(
hf_model_id="fcakyon/yolov8n-test",
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'
)
print('push to hub ended')
2 changes: 1 addition & 1 deletion ultralyticsplus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .hf_utils import download_from_hub, push_to_hfhub
from .ultralytics_utils import YOLO, postprocess_classify_output, render_result

__version__ = "0.0.16"
__version__ = "0.0.17"
10 changes: 5 additions & 5 deletions ultralyticsplus/hf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def generate_thumbnail(
text=thumbnail_text,
pil_image=read_image_as_pil(image_path_or_url),
brightness=0.60,
text_font=65,
text_font_size=65,
crop_margin=None,
)

Expand Down Expand Up @@ -508,14 +508,14 @@ def push_to_hfhub(
score_top5_acc = None
if task in ["object-detection", "instance-segmentation"]:
# read the largest value in metrics/mAP50(B) column from csv file named results.csv
score_map50 = results_df["metrics/mAP50(B)"].max()
score_map50 = results_df["metrics/mAP50(B)"].max().item()
if task == "instance-segmentation":
# read the largest value in metrics/mAP50(B) metrics/mAP50(M) columns from csv file named results.csv
score_map50_mask = results_df["metrics/mAP50(M)"].max()
score_map50_mask = results_df["metrics/mAP50(M)"].max().item()
if task == "image-classification":
# read the largest value in metrics/accuracy_top1 metrics/accuracy_top5 columns from csv file named results.csv
score_top1_acc = results_df["metrics/accuracy_top1"].max()
score_top5_acc = results_df["metrics/accuracy_top5"].max()
score_top1_acc = results_df["metrics/accuracy_top1"].max().item()
score_top5_acc = results_df["metrics/accuracy_top5"].max().item()

_push_to_hfhub(
hf_model_id=hf_model_id,
Expand Down
31 changes: 26 additions & 5 deletions ultralyticsplus/other_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import os
from PIL.ImageFont import FreeTypeFont

FONT_URL = "https://github.com/fcakyon/ultralyticsplus/releases/download/0.0.16/OpenSans-Bold.ttf"


def get_font(size: int) -> FreeTypeFont:
from PIL import ImageFont

try:
font = ImageFont.truetype("OpenSans-Bold.ttf", size)
except OSError:
from sahi.utils.file import download_from_url

package_dir = os.path.dirname(os.path.abspath(__file__))
font_path = os.path.join(package_dir, "OpenSans-Bold.ttf")
download_from_url(from_url=FONT_URL, to_path=font_path)
font = ImageFont.truetype(font_path, size)
return font


def add_text_to_image(
text,
pil_image,
brightness: float = 0.75,
text_font: int = 50,
text_font_size: int = 50,
crop_margin: int = None,
):
from PIL import ImageDraw, ImageFont, ImageEnhance
from PIL import ImageDraw, ImageEnhance

RESIZE_WIDTH_TO = 1280
MAX_TEXT_WIDTH = 860
Expand All @@ -31,7 +52,7 @@ def add_text_to_image(
draw = ImageDraw.Draw(pil_image)

# Define the font and text to be written
font = ImageFont.truetype("OpenSans-Bold.ttf", text_font)
font = get_font(size=text_font_size)

# Get the bounding box of the text
bbox = draw.textbbox((0, 0), text, font=font)
Expand All @@ -42,8 +63,8 @@ def add_text_to_image(

# update font size
if text_width > MAX_TEXT_WIDTH:
text_font = int(MAX_TEXT_WIDTH / text_width * text_font)
font = ImageFont.truetype("OpenSans-Bold.ttf", text_font)
text_font_size = int(MAX_TEXT_WIDTH / text_width * text_font_size)
font = get_font(size=text_font_size)
bbox = draw.textbbox((0, 0), text, font=font)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
Expand Down

0 comments on commit 04219a0

Please sign in to comment.